顯示具有 wargame 標籤的文章。 顯示所有文章
顯示具有 wargame 標籤的文章。 顯示所有文章

2013年6月18日 星期二

Defcon 21 競賽解題思路 - linked


題目給了一個 C 的 struct
typedef struct _llist {
   struct _llist *next;
   uint32_t tag;
   char data[100];
 } llist;

並且給了一小段 C 的程式碼

register char *answer;
char *(*func)();
llist *head;
...
func = (char *(*)(llist *))userBuf;
answer = (char *)(*func)(head);
send_string(answer);
exit(0);

此關卡程式會亂數配置若干 linked-list。要解開此關,則要送一段 shellcode 給 Server 執行,即 answer = (char *)(*func)(head); 這行呼叫。

Write me shellcode that traverses the randomly generated linked list, looking for a node with a tag 0x41414100, and returns a pointer to the data associated with that tag, such that the call to send_string will output the answer.

看來只要寫個 shellcode,走訪每個 linked-list,檢查 tag 是否為 0x41414100,若是則將此 linked-list 的位址傳回,應該就能讓 Server 吐出答案。
用 C 來表示大概像這樣:

char *shellcode(llist *head)
{
while(1) {
if (head->tag == 0x41414100)
return head->data;
head = head->next;
}
}

看來很簡單吧!
不過實際轉換成 shellcode 送出才發現,Server 回應 shellcode 只吃 16 bytes,也就是 shellcode 長度不能超過 16 bytes。這個長度限制馬上讓這題變成一個難解之題,不管怎麼縮小,改變 asm 指令等,都相當有難度。
此時,就想到如果不照題目所給的流程走,讓 shellcode 直接吐出走訪 linked-list 的所有資料,那 shellcode 能否控制在 16 bytes 之內呢?

用 C 來表示大概像這樣:

char *shellcode(llist *head)
{
dump:
                write(4, head, 100);
head = head->next;
                goto dump:
}

若是照正常的系統呼叫設定各暫存器,那鐵定超出長度,因此有些暫存器要故意忽略不設,例如呼叫 write() 的長度,C 這邊我寫 100,實際上完全不確定會 write 多少字元。

最後寫出的 shellcode 長得這樣:



沒幾行,分別解說如下:

7. pop   edi        
8. pop   ecx          

此兩行主要是因為  (char *)(*func)(head); ,也就是 call 之前會將參數 head 先放入堆疊(Stack) 再執行 call 時 x86 又會將返回位址放到堆疊,因此 pop edi 則會將返回位址取到 edi,而 pop ecx 則會將 head 存至 ecx。此時 ecx = head

10. mov ecx,[ecx]

此行則為 head = head->next,會放在一開始則完全是 shellcode 長度限制考量。

11. xor eax,eax
12. add eax,4

設定 eax = 4,因為 write 系統呼叫編號為 4。

14. push eax
15. pop ebx

恰好打算要輸出的 socket = 4,所以透過 push/pop 把 eax 複製到 ebx,一樣是長度限制考量。

16. ;mov edx,100
17. int 0x80
18. jmp _run

本來想設定 edx,代表要輸出的長度,不過受到長度限制考量,就完全不理長度,看當時執行此 shellcode 時 edx 暫存器值為何,就輸出多少長度,所以把這行註解掉了。
最後就是系統呼叫 int 0x80 把記憶體內容吐出來,然後 jmp _run 則不斷地走訪 linked-list 不斷地吐資料。
寫個小程式 dump,把 shellcode 輸出到 stdout,再透過 nc 送出給 Server。


大概收集個100M ~ 900M,完全看運氣,然後再從 dump 出的記憶體中找尋關鍵字 "The key"。
答案出來!


2013年6月17日 星期一

Defcon 21 競賽解題思路 - incest


好久沒來寫點東西啦!前兩天又是一年一度的 Defcon Quals,照例 CHROOT 集合多位朋友一塊解題闖關。今年我沒能花太多時間在解題,只看了較有把握的兩題也順利解出,先來分享 "\xff\xe4\xcc" 的第一題 - incest。

此題目給了兩隻 Linux x64 binary,maw 和 sis 。當然二話不說,先來 strings。

$ strings maw
eth0
Could not open key.
/home/services/sis
$ objdump -D sis > sis.disasm


從 sis.disasm 反組譯的內容中,可以看到此程式有 fork() 產生的子行程,會配置一塊記憶體,再將 recv() 收到的內容放到此記憶體空間,最後直接呼叫 callq *rdx,跳躍到該記憶體區域執行。而父行程也配了一塊記憶體,從某檔案中讀取內容後,就不斷呼叫 sched_yield()。
如果轉寫成 C 程式碼,大概長得像這樣:

很明顯 maw 是一個網路 daemon,accept() 連線後再執行 sis,執行時會先 open() key file,然後將 key file 的 fd 和 socket fd 一塊傳給 sis。根據 strace/ltrace 可以發現,key_fd = 3,sock_fd = 4,而且 sis 的子行程會直接跳到遠端傳送的資料裡執行。注意到,呼叫 mmap() 配置 sock_buffer 時,是用 PROT_READ|PROT_WRITE|PROT_EXEC,可讀可寫可執行,就可以塞 shellcode 執行啦。

但是,要怎麼取得 key file 的資料呢?

原來的想法是透過打造 open("key", O_RDONLY),然後 read(key_fd) 再 write(sock_fd),但測試結果發現會 Permission denied。而根據題目 incest 原意,搭配 sis.c,可以猜想應該是要由子行程透過 ptrace() 來讀取父行程裡的資料,也就是父行程已 read(key_fd, key_buffer),只要能讀取父行程的 key_buffer 內容,再 write(sock_fd) 就能遠端取得 key_buffer 內容啦。

打造 x64_86 的 shellcode,我是參考此文 64-bit-linux-shellcode,然後用 nasm 寫了一小段程式碼。流程大概如下:
shellcode()
{
int ppid;
int key_buffer_address;
int key_buffer;
int i;
char buffer[40];
user_regs_struct regs;
ppid = getppid();

ptrace (PTRACE_ATTACH, ppid, NULL, NULL);
wait(NULL);
ptrace (PTRACE_GETREGS,ppid, NULL, &regs);
key_buffer_address = regs.rbp-0x18;
ptrace (PTRACE_PEEKDATA, ppid, key_buffer_address, &key_buffer);
for(i = 0; i < 10; i++)
ptrace (PTRACE_PEEKDATA, ppid, key_buffer+i*8, buffer+i*8);
write(4, buffer, 40);
}

根據前面所提的 sis.disasm
  1. 400949:       e8 82 fd ff ff          callq  4006d0
  2. 40094e:       48 89 45 e8             mov    %rax,-0x18(%rbp)

可以發現,取得父行程 rbp 暫存器值後去 -0x18,即為存放 calloc() 配置的記憶體位址。所以透過
key_buffer_address = regs.rbp-0x18;
ptrace (PTRACE_PEEKDATA, ppid, key_buffer_address, &key_buffer);

則會得到父行程的 key_buffer 位址啦,接著用一個 for-loop 把父行程的 key_buffer 存的內容讀出來,再透過 write() 輸出到 sock_fd = 4,就能看到解答。
此題  shellcode 的 nasm 如下:

使用 nasm 編譯後,寫了一個小程式 dump,把 shellcode 輸出 stdout。


這樣就過關啦!

2009年6月10日 星期三

Defcon 17 Wargame 競賽解題心得

上週末剛好是美國知名駭客大會 Defcon 17 的 Wargame 外圍競賽期間。和往年一樣,這一次大夥又一塊聚在一起解題目,各展長才,不論是卡關卡的很痛苦、解謎解的快抓狂,或是終於解出答案的喜悅,總之我喜歡這樣的感覺。

經過兩天的努力,成績差強人意,雖然還是未能打進前十名,但應該也有二十多名,在全球兩三百個團隊競賽下,也不算太差吧!這兩日我共解了兩題,一題 100 分,另一題 400 分,我打算把解法寫出來,和大家分享交流。

這題是屬於 Binary_L33tness 的 100 分題目,題目很簡單,給一個檔案然後 "Find the key",如圖:


下載完檔案,第一步當然用 Linux 的 file 指令來判斷檔案型態囉!

[timhsu@localhost 100]$ file 100.file
100.file: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, stripped

此 100.file 檔為 Linux 的靜態執行檔,而且被 strip 過。不要怕,直接執行看看。

[timhsu@localhost 100]$ ./100.file
What is the password? 123
I smack you down. Step off bitch.

看來是要猜密碼耶。可以猜想一下,這個題目應該是密碼字串就在 Binary 執行檔裡,只要把檔案用 strings 指令來找這個字串,應該就是答案了。

有這麼容易嗎? 哦!若是直接用 strings 指令,大概找不出什麼像答案的字串吧!來用 hexdump 把檔頭資料印出來瞧瞧。

[timhsu@localhost 100]$ hexdump -C 100.file -n 256
00000000 7f 45 4c 46 01 01 01 03 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 03 00 01 00 00 00 e8 83 c0 00 34 00 00 00 |............4...|
00000020 00 00 00 00 00 00 00 00 34 00 20 00 02 00 28 00 |........4. ...(.|
00000030 00 00 00 00 01 00 00 00 00 00 00 00 00 10 c0 00 |................|
00000040 00 10 c0 00 d0 7b 00 00 d0 7b 00 00 05 00 00 00 |.....{...{......|
00000050 00 10 00 00 01 00 00 00 1c 0e 00 00 1c 0e 05 08 |................|
00000060 1c 0e 05 08 00 00 00 00 00 00 00 00 06 00 00 00 |................|
00000070 00 10 00 00 01 99 69 2e 55 50 58 21 e6 07 0d 0c |......i.UPX!....|
00000080 00 00 00 00 a6 03 01 00 a6 03 01 00 14 01 00 00 |................|
00000090 9a 00 00 00 02 00 00 00 63 3f 64 f9 7f 45 46 46 |........c?d..EFF|
000000a0 01 00 02 00 03 00 0d 92 ff b7 d9 ee 04 08 34 06 |..............4.|
000000b0 2c e5 17 0b 20 00 07 00 28 00 26 00 23 00 cf 3d |,... ...(.&.#..=|
000000c0 77 b1 06 0f 03 80 04 08 e0 0b 03 5a b3 69 9a 05 |w..........Z.i..|
000000d0 04 03 14 01 81 1b 05 1b ac fb 03 13 0f 13 1b 01 |................|
000000e0 03 ef b5 9f 8b 00 3b 03 a4 5b 0a 03 3f cf b9 b1 |......;..[..?...|
000000f0 d9 00 10 1f 13 eb 1b 03 50 d7 75 5f ec 78 22 00 |........P.u_.x".|

檔頭裡發現關鍵字串 "UPX",為一加殼程式,也就是說這個 Linux 執行檔被 UPX 加殼過,真正的檔案內容要在執行期間才會解開執行。直接用 upx 工具來作解殼動作,還原檔案內容。

[timhsu@localhost 100]$ upx -vvv -d 100.file
Ultimate Packer for eXecutables
Copyright (C) 1996 - 2008
UPX 3.03 Markus Oberhumer, Laszlo Molnar & John Reiser Apr 27th 2008

File size Ratio Format Name
-------------------- ------ ----------- -----------
upx: 100.file: Exception: checksum error

Unpacked 1 file: 0 ok, 1 error.

失敗!upx 工具的解殼動作出現 checksum error,所以無法解開。
好吧,換個思維想想,既然加殼程式在執行期間會在記憶體還原原檔案內容,所以我們就可以試著在檔案執行時,把記憶體內容傾印出來。

試著建立 CoreDump 檔。

[timhsu@localhost 100]$ ulimit -c unlimited
[timhsu@localhost 100]$ ./100.file &
What is the password? [1] 22488

[1]+ Stopped ./100.file
[timhsu@localhost 100]$ kill -11 22488
[timhsu@localhost 100]$ ls
100.file*
[timhsu@localhost 100]$ ps
PID TTY TIME CMD
22408 pts/4 00:00:00 bash
22488 pts/4 00:00:00 100.file
22490 pts/4 00:00:00 ps
[timhsu@localhost 100]$ bg
[1]+ ./100.file &
[1]+ Segmentation fault (core dumped) ./100.file

CoreDump 建立成功!馬上用 strings 指令瞧瞧檔案內容。

[timhsu@localhost 100]$ strings core.22488 | more
...<略>...
,[^_]
[^_]
What is the password?
visilooksgoodinhotpants
, %d byte packets
CWR ECN
...<略>...
/lib/i686
libc.so.6
What is the password?
rotection after relocation
apic
mtrr
...<略>...
[timhsu@localhost 100]$

有兩組可能的密碼,試一下就知道。

[timhsu@localhost 100]$ ./100.file
What is the password? visilooksgoodinhotpants
You're my daddy! See you at the hacker swinger's club next week.

成功!這樣看來很簡單是吧?
事實上,當時我是用幾年前自己寫的小工具 dump2bin 來傾印執行檔,後來才想到不必寫程式的 DumpCore 方法,也許有其它方法,大家可以動動腦筋。

下回我將分享 Crypto_Badness 400 分的解題心得。