Kickstart your coding journey with our Python Code Assistant. An AI-powered assistant that's always ready to help. Don't miss out!
Disclaimer: This guide is intended for educational purposes only. Readers are advised not to implement the techniques described on systems they do not own or without proper authorization. The author assumes no responsibility for any misuse of the information provided.
In this guide, you'll learn how to make malware persistent using Python. Like the actual meaning of persistence, in the context of malware, persistence refers to the ability of malicious software to maintain a presence on a system beyond its initial execution. When malware achieves persistence, it ensures that it remains active and operational on the infected system, even after reboots or attempts to remove it.
Say, for example, you built malware that is supposed to give you access to a target's computer (a reverse shell), similar to what we built in this tutorial. If you successfully deliver the malware to the target and they execute it, you'll definitely gain access to their system. But the moment they finish working on their computer and shut it down, you lose access. This is the setback this guide aims to address. By making your malware persistent, even if the target shuts their computer down, the moment they turn it back on, because our malware is persistent, it'll get executed again and begin operation on the infected computer.
You may be wondering how this is possible. Well, this is possible because, on most operating systems, files are automatically started (executed) when the system comes on. Luckily for us, these files that are started automatically when the system boots are easily accessible. We can easily add our malicious files to the startup files, and every time the computer boots, our malware will be executed.
If you want to remove persistent malware, then we got you covered. Check this tutorial.
Table of contents:
report_to_file()
Methodsetup_persistence()
Starting with Windows, On your Windows search bar, simply type in regedit
. You can also use the Windows key + R, and then type regedit
. Whichever you prefer. The regedit
command will open up the Windows Registry editor. You should see:
From here, open up HKEY_CURRENT_USER > SOFTWARE > Microsoft > Windows > CurrentVersion > Run
.
You should see a screen similar to:
These are the files that are automatically executed when this system comes on. So, all we need to do is add our malware here. Doing that is also simple. We can achieve that from our command prompt.
We'll try to add a new executable to the above location in our registry. Open up your command prompt (cmd) and enter the following command.
This command basically uses the reg
utility to manipulate the Windows Registry. Here's a breakdown of each part:
reg add
: This part initiates a registry modification action.HKCU
: This specifies the registry hive to which the modification will be applied. HKCU
stands for HKEY_CURRENT_USER
, which is a section of the registry where user-specific settings are stored (as we have seen).\Software\Microsoft\Windows\CurrentVersion\Run
: This is the registry key (or path) where the modification will occur. Specifically, it's the location where Windows stores programs that should be launched automatically when the current user logs into their account./v Index
: This specifies the name of the registry value being modified. In this case, it's creating or modifying a value named Index
within the Run
key./t REG_SZ
: This specifies the data type of the registry value being added or modified. In this case, it's setting the type to a REG_SZ
, which means it's a string value./d C:\Users\test
: This provides the data for the registry value. Here, it's setting the data to C:\Users\test,
which means it's configuring Windows to launch the program located at C:\Users\test
automatically when the user logs in.You may have already guessed that the parameter we'll need to change is the \d
. We will copy our malware to a different location on the targets' system - One that is not very interacted with, so they don't suspect it. A good one is AppData
.
After copying our malware to AppData
, we pass its path to the /d
parameter. So essentially, we will tell the registry to run our malware in the AppData
upon every system startup.
This is what the registry will look like after running the above command:
You can see we now have the index
included. But this will not do anything because the /d
parameter I passed was a dummy one. It does not exist. You can simply right-click Index
and delete it as we implement the actual thing soon.
On Linux, there are multiple ways to make a program persist, such as Systemd services, Cron jobs, .bashrc
and more.
In this tutorial, we'll make our malware persistent by adding it to the cron table (crontab
) of the computer. In Linux, crontab
is a utility that allows you to schedule commands or scripts to run automatically at specified times or intervals. It is part of the cron service, a time-based job scheduler in Unix-like operating systems.
Essentially, we are going to put our malware in a hidden location, similar to what we did on Windows, and add the location to our crontab
, specifying that it should be executed at every startup or reboot.
To access our cron table, open up your Linux terminal and type in:
You should see a screen similar to:
Yours will most likely be blank or include some comments. I just included an application there to explain some concepts. From the entry above, the @reboot
specifies that the given executable or application should be executed whenever the system gets started.
Now that we understand persistence, let's implement it in Python. For this demonstration, I'll be using the keylogger that we built in this tutorial. We'll also make it cross-platform to work on Windows and Linux.
For the keylogger, I'll use the same code explained in the said tutorial with some additions to make the program persistent and cross-platform. Don't worry, I'll explain the additions. So open up a new Python file, name it meaningfully like keyboard_persistent.py
or keyboard_persistence.py
Essentially, here's the full working code:
I assume you're already familiar with the keylogger tutorial. If not, simply add the above setup_persistence()
function (along with its required modules) to any Python program you want to be persistent and call it. Again, we're just using the keylogger tutorial for demonstration purposes.
The next sections outline the changes done to the original keylogger code.
report_to_file()
MethodWe have extended it to first check the OS the program is executed on. For Windows, instead of creating the KeyloggerLogs
folder in the same directory as was implemented in the original keylogger, we are creating it in the Documents
folder. This is because we are hiding our keylogger in AppData
. Usually, the AppData
is not always writable. So we may not see our keystrokes. Also, if the keylogger is being executed on a Linux machine, it creates the KeyloggerLogs
folder in the /root/Documents
directory.
setup_persistence()
This method is the main functionality of this tutorial. The
setup_persistence()
function is designed to achieve persistence on Windows and Linux operating systems by copying the current executable file to a specific location and creating a registry entry or cron job to run the executable on system startup.
For Windows, it copies the executable to the \AppData\
location and creates a registry entry under HKCU\Software\Microsoft\Windows\CurrentVersion\Run
to run the executable on startup.
For Linux, it creates a directory ~/.config/KaliStartup
and copies the current Python executable (sys.executable)
to that location with the name KaliStartup
. It then creates a cron job entry using the crontab
command to run the KaliStartup
executable on system reboot (@reboot
).
I used the names MicrosoftEdgeLauncher
on Windows and KaliStartup
on Linux so that they appear as harmless programs and prevent suspicion. You can name yours whatever flavor (distribution) of Linux you're using. I am using Kali.
Notice the setup_persistence()
function is called right after defining it.
We have a full tutorial on how to convert Python files to executables. Check it out here.
Now that we have written code to make our keylogger persistent, let's package it on both OSs using PyInstaller. We also need to install the keyboard
module for logging the keystrokes. All are covered in the keylogger tutorial.
Install them using the following command:
It's generally a good practice to package a program on the particular OS it will be executed on. Since our program is cross-platform, we'll package and test it on both Windows and Linux.
On Windows, open up your cmd, navigate to your working directory (where your code is) and run:
This command, using PyInstaller
, converts the Python script keylogger_persistent.py
into a standalone executable file (--onefile
), configured to run without displaying a console window when executed (--noconsole
).
Give it a minute or two to package your Python file to an executable.
Afterwards, you should see a dist
folder in your working directory:
Inside the dist
folder, you should see the packaged keylogger
:
So just double-click this, and your program should start running. Go to your registry and you should see our keylogger
being referenced for execution at startup:
In my AppData
folder:
Please ignore the fact that vlc
is being highlighted. Our MicrosoftEdge.exe
(keylogger
) is successfully placed in our AppData
.
Now let's check our keylogs in KeyloggerLogs
folder:
Pretty cool right? Restart your computer as many times as possible and you'll see the program will keep running (logging).
Packaging on Linux is quite similar to Windows. On Linux, usually Pyinstaller
is installed by default. But if for some reason you don't have it, you can install it and keyboard
by running:
Then:
Please note that the keyboard
library on Linux can only be used with admin privileges. So for everything will do regarding this program, please do it as an admin.
But remember, that's just for the keyboard
module. It has nothing to do with persistence. The restriction is just for the keyboard
library. Even without persistence, running a program using the keyboard
library requires admin access. So yes, our program is pretty effective.
If you use another variant of malware, such as a reverse shell or spyware, it'll work seamlessly on all users. But this is a demo. You'll see the results anyway.
Moving on, using your terminal, navigate to the directory where your keylogger_persistent.py
folder is and run:
Again, this command, using PyInstaller
, converts the Python script keylogger_persistence.py
into a standalone executable file (--onefile
), configured to run without displaying a console window when executed (--noconsole
).
Afterwards, you should see a dist
folder:
Inside the dist
folder, you should see the packaged executable:
Now that you have seen the exe
, you can simply double-click it. I prefer to run my programs from the terminal because I can see if there are any errors. On the terminal, you can navigate to your working directory and run it like this:
After running the keylogger, open up your crontab
by running:
You should see:
We can now see that our keylogger has been added to our cron table. Every time the computer is restarted, our keylogger will be executed.
We can also see the directory where the KaliStartup
(keylogger) is saved. /root/.config/KaliStartup/
. Let's browse there to confirm:
Finally, let's check our keylogger files from /root/Documents/Keyloggerlogs
:
You can use the cat
command to read the contents of the files. There you have it. We have successfully made our keylogger
persistent on Windows and Linux!
Please know that for testing purposes, we used the txt
format to record the keystrokes
. In a real-world scenario, you would want to use email reporting to receive the keystrokes. You can simply run the program with report_method="email"
instead.
Also, while running this program, my Anti-Virus was on and the program executed successfully. But while testing with other variants of malware (not this keylogger), I noticed that the AV was preventing the program from running. I believe that was because that particular variant was executing system commands.
In a nutshell, if this program gets caught by an AV, just turn it off for testing purposes. But please don't forget to turn it back on.
Also, check this tutorial if you want to remove persistent malware in Python.
If you want to build other variants of malware, check out these tutorials:
We also have a comprehensive tutorial on how to convert Python files to executables. Check it out here.
Finally, if you want to level up from being a script kiddie to a pro hacker, check out our Ethical Hacking with Python EBook where we build malware along with awesome 35+ ethical hacking tools!
You can always check the complete code of this tutorial here.
Happy hacking ♥
Save time and energy with our Python Code Generator. Why start from scratch when you can generate? Give it a try!
View Full Code Switch My Framework
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!