標籤:csapp
CSAPP && lab3
level2 & leve3未完,待更新! : )
In this lab, you will gain firsthand experience with one of the methods commonly used to exploit security weaknesses in operating systems and network servers. Our purpose is to help you learn about the runtime operation of programs and to understand the nature of this form of security weakness so that you can avoid it when you write system code. We do not condone the use of these or any other form of attack to gain unauthorized access to any system resources. There are criminal statutes governing such activities.
實驗指導說明:
http://download.csdn.net/detail/u011368821/7920313
實驗材料:
http://download.csdn.net/detail/u011368821/7920335
The Exploits
There are three functions that you must exploit for this lab. The exploits increase in difficulty. For those of you looking for a challenge, there is a fourth function you can exploit for extra credit.
Level 0: Candle
When getbuf() executes its return statement, the program ordinarily resumes execution within function test(). Within the file bufbomb, there is a function smoke():
void smoke(){entry_check(0); /* Make sure entered this function properly */printf("Smoke!: You called smoke()\n");validate(0);exit(0);}
Your task is to get bufbomb to execute the code for smoke() when getbuf() executes its return statement, rather than returning to test().You can do this by supplying an exploit string that overwrites the stored return pointer in the stack frame for getbuf() with the address of the first instruction in smoke. Note that your exploit string may also corrupt other parts of the stack state, but this will not cause a problem, because smoke() causes the program to exit directly.
Advice:
- All the information you need to devise your exploit string for this level can be determined by examining a disassembled version of bufbomb.
- Be careful about byte ordering.
- You might want to use gdb to step the program through the last few instructions of getbuf() to make sure it is doing the right thing.
- The placement of buf within the stack frame for getbuf() depends on which version of gcc was used to compile bufbomb. You will need to pad the beginning of your exploit string with the proper number of bytes to overwrite the return pointer. The values of these bytes can be arbitrary.
- Check the line endings if your smoke.txt with hexdump -C smoke.txt.
我了個大X,調了幾天才搞明白怎麼用“溢出緩衝區攻擊"
利用先構建一個輸入文本,記錄要資料的字元(溢出用)
我這裡是
exploit.txt
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 02 03 04 01 02 03 04 05 06 07 08 c0 10 40 00 00 00 00 00
這裡一定不要有分行符號號(如果有也沒關係,去產生的檔案裡,用vim開啟編輯了之後去掉就是了)
這裡一共0x30byte+8byte+8byte == 0x40byte
第一個0x30byte 是由於64bits的機器位元組對齊導致的36byte的數組被擴充到0x30byte!
而後的8byte是之前 push %rbp 寄存器的value
最後的8byte是return address,也就是我們要”攻擊“的地方!
考慮到機器的大小端.
smoke的地址是
00 00 00 00 00 40 10 c0
寫入的時候應該是
c0 10 40 00 00 00 00 00
這裡把產生的資料記錄到exploit.byte裡面
接著把該檔案作為輸入就是啦~ : )
Level 1: Sparkler
問題解決基礎:
/* $begin fizz-c */void fizz(int arg1, char arg2, long arg3, char* arg4, short arg5, short arg6, unsigned long long val){ entry_check(1); /* Make sure entered this function properly */ if (val == cookie) {printf("Fizz!: You called fizz(0x%llx)\n", val);validate(1); } else {printf("Misfire: You called fizz(0x%llx)\n", val); } exit(0);}/* $end fizz-c */
Similar to Level 0, your task is to get bufbomb to execute the code for fizz() rather than returning to test.In this case, however, you must make it appear to fizz as if you have passed your cookie as its argument. You can do this by encoding your cookie in the appropriate place within your exploit string.
Advice:
Note that in x86--64, the first six arguments are passed into registers and additional arguments are passed through the stack. Your exploit code needs to write to the appropriate place within the stack.
You can use gdb to get the information you need to construct your exploit string. Set a breakpoint within getbuf() and run to this breakpoint. Determine parameters such as the address of global_value and the location of the buffer.
在破壞原來test()調用getbuf push的return address之後,跳轉到fizz,此時看到題目要求是要把cookie作為fizz的參數輸入,根據反組譯碼得到的資訊,我們可以看見這裡
cmp 0x201296(%rip) %rsi
如果相等就會跳轉到0x40109f接著順利退出.
由於之前rsp寄存器的值沒有被破壞,於是我們反推,如果”攻擊rsp“的值,使得0x10(%rsp)指向 cookie
那麼就可以順利退出了!
由於最後leaveq的時候會把儲存好的%rbp重新賦值給%rsp,於是
我們這裡可以轉而攻擊儲存好的%rbp,來實現間接改變%rsp
cmp 0x201296(%rip) %rsi
死死的扣住這句不放,這裡吧0x201296(%rip) 地址處的值和%rsi的值比較
0x201296(%rip)
就是cookie的地址,然後這裡%rsi是參數val的地址
cmp把cookie地址處的值!注意是cookie地址處的值,和%rsi比較
如果想等才能正確返回否則你看到的永遠是missfire!
這裡給出我的解
首先查看cookie地址處的值然後輸入
exploit_for_level1.txt
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 02 03 04 20 23 60 00 00 00 00 00 70 10 40 00 00 00 00 00 00 00 00 00 00 00 00 00 c2 46 c1 21 48 ca 33 39
細心的人會發現,我這裡在return address之上還多填了8byte的空間,接著才填充我們的答案.
這是因為return addres之上還有什麼我這裡確實不清楚,不過我這裡這麼做是根據反組譯碼的結果測試並計算出來的.
有心人可以一起討論這裡跳轉到fizz之後之前寄存器的變化 : ) [email protected]
然後把exploit_for_level1.byte作為輸入即可
Level 2: Firecracker
A much more sophisticated form of buffer attack involves supplying a string that encodes actual machine instructions. The exploit string then overwrites the return pointer with the starting address of these instructions. When the calling function (in this case getbuf) executes its ret instruction, the program will start executing the instructions on the stack rather than returning. With this form of attack, you can get the program to do almost anything. The code you place on the stack is called the exploit code. This style of attack is tricky, though, because you must get machine code onto the stack and set the return pointer to the start of this code.
For level 2, you will need to run your exploit within gdb for it to succeed. (attu has special memory protection that prevents execution of memory locations in the stack. Since gdb works a little differently, it will allow the exploit to succeed.)
Similar to Levels 0 and 1, your task is to get bufbomb to execute the code for bang() rather than returning to test(). Before this, however, you must set global variable global_value to your cookie. Your exploit code should set global_value, push the address of bang() on the stack, and then execute a retq instruction to cause a jump to the code for bang().
Advice:
Determining the byte encoding of instruction sequences by hand is tedious and prone to errors. You can let tools do all of the work by writing an assembly code file containing the instructions and data you want to put on the stack. Assemble this file with gcc and disassemble it with objdump. You should be able to get the exact byte sequence that you will type at the prompt. (A brief example of how to do this is included in the Generating Byte Codes section above.)
Keep in mind that your exploit string depends on your machine, your compiler, and even your cookie. Make sure your exploit string works on attu or your VM, and make sure you include your UWNetID on the command line to bufbomb.
Watch your use of address modes when writing assembly code. Note that movq $0x4, %rax moves the value
0x0000000000000004 into register %rax; whereas movq 0x4, %rax moves the value at memory location
0x0000000000000004 into %rax. Because that memory location is usually undefined, the second instruction will
cause a segmentation fault!
Do not attempt to use either a jmp or a call instruction to jump to the code for bang(). These instructions use PC- relative addressing, which is very tricky to set up correctly. Instead, push an address on the stack and use the retq instruction.
這裡我跳轉到buffer地址的時候有問題...segment fault
我的嘗試:
首先觀察發現這裡cmp 相等就會跳轉並安全結束.
比較的兩個對象是0x602320指向的value,而%rsi是global_value.
cookie的值在我們輸入的時候就定了. 這裡能改的就是global_value處(即0x602308處)的值了。
普通的做法是搞不定的.
這裡必須自己寫一段攻擊代碼——彙編形式的
然後在這段代碼裡面把cookie的值賦值給global_value,就成功了一半了,
接著把這段代碼gcc -c 編譯成obj。然後objdump去看相應的機器碼
mov 0x602330,%rax 就對應的機器碼就是
48 8b 04 25 20 23 60
不解釋了
值得一提的是這裡有
pushq $0x401020
和retq操作(稍後解釋用途)
然後把這些機器碼寫到buffer裡面去!
試想一下這裡如果我覆蓋的reture address是只想buffer頭的,那麼在buffer處的這些指令就得以執行.
接著當這些指令執行到最後的時候
pushq $0x401020
retq
這步就可以跳轉到0x401020(bang函數頭)。執行 bang函數,我們已經在攻擊代碼裡面修改了global_value,那麼cmp的結構就是0,je跳轉,程式安全結束.
這僅僅是我的理論分析,我遇到的問題就是這裡跳轉到buffer頭的時候會出現segment fault...
路過高手可以指教一下 : )
CSAPP 六個重要實驗 lab3