日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

堆栈图解CSAPP Bomb Lab实验解析

發布時間:2023/12/2 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 堆栈图解CSAPP Bomb Lab实验解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CSAPP Bomb Lab 實驗解析

Bomblab是csapp的第二個配套實驗,該實驗提供了一個bomb二進制文件和一個bomb.c源文件,我們的目標是運行bomb并按照提示一步步輸入字符串,直到完成整個拆炸彈的流程。但是源文件中只提供了部分代碼,所以我們需要通過反匯編工具 objDump 來分析bomb的匯編代碼,推導出所有能夠拆解炸彈的字符串。

調試前在explode_bomb的地址上設置斷點,break *0x804986d。

Phase_1

反匯編代碼:

Dump of assembler code for function phase_1:0x08049045 <+0>: sub $0x14,%esp0x08049048 <+3>: push $0x8049dac0x0804904d <+8>: pushl 0x1c(%esp)0x08049051 <+12>: call 0x8049474 <strings_not_equal>0x08049056 <+17>: add $0x10,%esp0x08049059 <+20>: test %eax,%eax0x0804905b <+22>: je 0x804906a <phase_1+37>0x0804905d <+24>: sub $0xc,%esp0x08049060 <+27>: push $0x10x08049062 <+29>: call 0x804986d <explode_bomb>0x08049067 <+34>: add $0x10,%esp0x0804906a <+37>: add $0xc,%esp0x0804906d <+40>: ret

分析

從函數名strings_not_equal可以看出這是比較兩個字符串的函數,字符串相同則返回1,不同則返回0。接下來對返回值進行test,并判斷test結果是否為0,為零(字符串相等)跳過explode_bomb,否則(字符串不等)執行explode_bomb引爆。

從代碼中可以看到strings_not_equal函數的參數是%esp+0x1c和0x8049dac地址中的內容,而%esp+0x1c就是“input”的內容,因此只要保證輸入內容等于0x8049dac地址中的內容,便能不引爆炸彈。

查找0x8049dac中的內容:

(gdb) x/sb 0x8049dac
0x8049dac: “Public speaking is very easy.”

即得到拆除第1個雷所需的第3個字符串,輸入后運行如下:
The bomb phase 1 is defused. Congratulations!

Phase_2

反匯編代碼:

Dump of assembler code for function phase_2:0x0804906e <+0>: push %esi0x0804906f <+1>: push %ebx0x08049070 <+2>: sub $0x2c,%esp0x08049073 <+5>: lea 0x10(%esp),%ebx0x08049077 <+9>: push %ebx0x08049078 <+10>: pushl 0x3c(%esp)0x0804907c <+14>: call 0x8049892 <read_six_numbers>0x08049081 <+19>: lea 0x2c(%esp),%esi0x08049085 <+23>: add $0x10,%esp0x08049088 <+26>: mov (%ebx),%eax0x0804908a <+28>: add $0x5,%eax0x0804908d <+31>: cmp %eax,0x4(%ebx)0x08049090 <+34>: je 0x804909f <phase_2+49>0x08049092 <+36>: sub $0xc,%esp0x08049095 <+39>: push $0x20x08049097 <+41>: call 0x804986d <explode_bomb>0x0804909c <+46>: add $0x10,%esp0x0804909f <+49>: add $0x4,%ebx0x080490a2 <+52>: cmp %esi,%ebx0x080490a4 <+54>: jne 0x8049088 <phase_2+26>0x080490a6 <+56>: add $0x24,%esp0x080490a9 <+59>: pop %ebx0x080490aa <+60>: pop %esi0x080490ab <+61>: ret

分析

由call了子程序read_six_numbers,可猜測第2個雷的字符串是6個數字,調用堆棧如下圖

子程序返回后讓esi指向圖中esp-8的位置(-0x34+0x2c=-0x8),而通過查看子程序read_six_numbers可知ebx的值并沒有發生改變,因此,ebx還是指向圖中esp-0x1c的位置,即ebx+0x14=esi,而0x14=20=4×5;接下來執行操作A(即0x08049088,不引爆bomb的情況),將ebx加4后與esi比較,如果不相等則重新執行操作A,相等則功能結束,可以看出這個過程就是一個循環,將操作A執行5次。

對于操作A,則是比較[ebx-4]+5與[ebx]的大小,相等則將ebx加4,繼續循環;不相等則執行explode_bomb引爆。這個過程將被執行5次,一共參與的元素是相鄰的6個??梢圆聹y這6個元素是輸入的6個數字,且后一個數字比前一個數字大于5(或小于5,此處是大于,具體需要看子程序存放的順序),隨便試一組數字就可以了,如果依次加5報錯說明就是依次減5。

輸入如下:0 5 10 15 20 25或1 6 11 16 21 26測試通過:
The bomb phase 1 is defused. Congratulations!
The bomb phase 2 is defused. Congratulations!

Phase_3

反匯編代碼:

Dump of assembler code for function phase_3:0x080490ac <+0>: sub $0x1c,%esp0x080490af <+3>: lea 0x8(%esp),%eax0x080490b3 <+7>: push %eax0x080490b4 <+8>: lea 0x10(%esp),%eax0x080490b8 <+12>: push %eax0x080490b9 <+13>: push $0x804a0cf0x080490be <+18>: pushl 0x2c(%esp)0x080490c2 <+22>: call 0x8048950 <__isoc99_sscanf@plt>0x080490c7 <+27>: add $0x10,%esp0x080490ca <+30>: cmp $0x1,%eax0x080490cd <+33>: jg 0x80490dc <phase_3+48>0x080490cf <+35>: sub $0xc,%esp0x080490d2 <+38>: push $0x30x080490d4 <+40>: call 0x804986d <explode_bomb>0x080490d9 <+45>: add $0x10,%esp0x080490dc <+48>: cmpl $0x7,0xc(%esp)0x080490e1 <+53>: ja 0x8049147 <phase_3+155>0x080490e3 <+55>: mov 0xc(%esp),%eax0x080490e7 <+59>: jmp *0x8049de0(,%eax,4)0x080490ee <+66>: mov $0x2f2,%eax0x080490f3 <+71>: jmp 0x80490fa <phase_3+78>0x080490f5 <+73>: mov $0x0,%eax0x080490fa <+78>: sub $0 *0x80490x1ce,%eax0x080490ff <+83>: jmp 0x8049106 <phase_3+90>0x08049101 <+85>: mov $0x0,%eax0x08049106 <+90>: add $0x156,%eax0x0804910b <+95>: jmp 0x8049112 <phase_3+102>0x0804910d <+97>: mov $0x0,%eax0x08049112 <+102>: sub $0x32c,%eax0x08049117 <+107>: jmp 0x804911e <phase_3+114>0x08049119 <+109>: mov $0x0,%eax0x0804911e <+114>: add $0x238,%eax0x08049123 <+119>: jmp 0x804912a <phase_3+126>0x08049125 <+121>: mov $0x0,%eax0x0804912a <+126>: sub $0x39b,%eax0x0804912f <+131>: jmp 0x8049136 <phase_3+138>0x08049131 <+133>: mov $0x0,%eax0x08049136 <+138>: add $0x39b,%eax0x0804913b <+143>: jmp 0x8049142 <phase_3+150>0x0804913d <+145>: mov $0x0,%eax0x08049142 <+150>: sub $0x73,%eax0x08049145 <+153>: jmp 0x8049159 <phase_3+173>0x08049147 <+155>: sub $0xc,%esp0x0804914a <+158>: push $0x30x0804914c <+160>: call 0x804986d <explode_bomb>0x08049151 <+165>: add $0x10,%esp0x08049154 <+168>: mov $0x0,%eax0x08049159 <+173>: cmpl $0x5,0xc(%esp)0x0804915e <+178>: jg 0x8049166 <phase_3+186>0x08049160 <+180>: cmp 0x8(%esp),%eax0x08049164 <+184>: je 0x8049173 <phase_3+199>0x08049166 <+186>: sub $0xc,%esp0x08049169 <+189>: push $0x30x0804916b <+191>: call 0x804986d <explode_bomb>0x08049170 <+196>: add $0x10,%esp0x08049173 <+199>: add $0x1c,%esp0x08049176 <+202>: ret

分析

從代碼可以看出,主程序又調用了一個叫做“sscanf”的子程序,從函數的名字就能看出是讀入輸入的參數,但是要輸入幾個參數現在還不知道??聪旅嬉粭l指令“cmp?$0x1,%eax”,將輸入參數個數eax與1進行比較,大于1則繼續執行,否則引爆,說明至少輸入兩個數據。

隨便輸入兩個測試數據,break *0x804986d下斷點,運行后程序斷下,查看sccanf函數第二個參數內容如下

(gdb) x/sb 0x804a0cf
0x804a0cf: “%d %d”

因此,可以判斷這個字符串是兩個數字,且Sccanf函數調用可以還原為:sccanf(input,”%d %d”,&A,&B);調用堆棧還原如下

圖中A、B就是存放讀取的兩個數字,繼續查看代碼,若A大于7,則引爆炸彈;后面若A大于5同樣引爆炸彈。說明A取值為0~5之間,之后,由A值取值的不同跳轉到不同的地址,跳轉到地址0x8049de0+4×A。

查看0x8049de0地址處的內容,如下

(gdb) x/8xw 0x8049de0
0x8049de0: 0x080490ee 0x080490f5 0x08049101 0x0804910d
0x8049df0: 0x08049119 0x08049125 0x08049131 0x0804913d

其實這些地址都是phase3中的不同語句地址??梢钥闯銎鋵嵤菆绦幸粋€c語言中的case語句,根據輸入的不同,所執行的命令也不同。?接著,就是對eax進行一些數值的運算,然后與B作比較。當A=0時,eax=0x2f2-0x1ce+0x156-0x32c+0x238-0x39b+0x39b-0x73=0x113(275),因此,此題答案不唯一,其中一組字符串為:0 275,輸入后,運行如下

The bomb phase 1 is defused. Congratulations!
The bomb phase 2 is defused. Congratulations!
The bomb phase 3 is defused. Congratulations!

當A=1~5時,可以用同樣的方法計算出相應的結果,在此不做贅述。

Phase_4

反匯編代碼:

Dump of assembler code for function phase_4:0x080491a0 <+0>: sub $0x20,%esp0x080491a3 <+3>: lea 0x10(%esp),%eax0x080491a7 <+7>: push %eax0x080491a8 <+8>: push $0x804a0d20x080491ad <+13>: pushl 0x2c(%esp)0x080491b1 <+17>: call 0x8048950 <__isoc99_sscanf@plt>0x080491b6 <+22>: add $0x10,%esp0x080491b9 <+25>: cmp $0x1,%eax0x080491bc <+28>: jne 0x80491c5 <phase_4+37>0x080491be <+30>: cmpl $0x0,0xc(%esp)0x080491c3 <+35>: jg 0x80491d2 <phase_4+50>0x080491c5 <+37>: sub $0xc,%esp0x080491c8 <+40>: push $0x40x080491ca <+42>: call 0x804986d <explode_bomb>0x080491cf <+47>: add $0x10,%esp0x080491d2 <+50>: sub $0xc,%esp0x080491d5 <+53>: pushl 0x18(%esp)0x080491d9 <+57>: call 0x8049177 <func4>0x080491de <+62>: add $0x10,%esp0x080491e1 <+65>: cmp $0x58980,%eax0x080491e6 <+70>: je 0x80491f5 <phase_4+85>0x080491e8 <+72>: sub $0xc,%esp0x080491eb <+75>: push $0x40x080491ed <+77>: call 0x804986d <explode_bomb>0x080491f2 <+82>: add $0x10,%esp0x080491f5 <+85>: add $0x1c,%esp0x080491f8 <+88>: retDump of assembler code for function func4:0x08049177 <+0>: push %ebx0x08049178 <+1>: sub $0x8,%esp0x0804917b <+4>: mov 0x10(%esp),%ebx0x0804917f <+8>: mov $0x1,%eax0x08049184 <+13>: cmp $0x1,%ebx0x08049187 <+16>: jle 0x804919b <func4+36>0x08049189 <+18>: sub $0xc,%esp0x0804918c <+21>: lea -0x1(%ebx),%eax0x0804918f <+24>: push %eax0x08049190 <+25>: call 0x8049177 <func4>0x08049195 <+30>: add $0x10,%esp0x08049198 <+33>: imul %ebx,%eax0x0804919b <+36>: add $0x8,%esp0x0804919e <+39>: pop %ebx0x0804919f <+40>: ret

分析

由之前的經驗看出,這個字符串為一個非0整數。程序中,將該整數傳入func4函數中進行運算,若返回結果等于0x58980(362880),則拆除成功,否則引爆。分析func4的代碼,用C語言表示為:

int func4(int n) {int eax=1;if (n<1) return eax;eax=func4(n-1);eax=eax*n;return eax; }

仔細想一下,其實這是個遞歸函數,其功能就是計算n的階乘,而362880正好等于9的階乘,所以這個整數應為9,輸入后運行,提示如下,成功。

Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
The bomb phase 1 is defused. Congratulations!
The bomb phase 2 is defused. Congratulations!
The bomb phase 3 is defused. Congratulations!
The bomb phase 4 is defused. Congratulations!

Phase_5

反匯編代碼:

Dump of assembler code for function phase_5:0x080491f9 <+0>: sub $0x1c,%esp0x080491fc <+3>: lea 0x8(%esp),%eax0x08049200 <+7>: push %eax0x08049201 <+8>: lea 0x10(%esp),%eax0x08049205 <+12>: push %eax0x08049206 <+13>: push $0x804a0cf0x0804920b <+18>: pushl 0x2c(%esp)0x0804920f <+22>: call 0x8048950 <__isoc99_sscanf@plt>0x08049214 <+27>: add $0x10,%esp0x08049217 <+30>: cmp $0x1,%eax0x0804921a <+33>: jg 0x8049229 <phase_5+48>0x0804921c <+35>: sub $0xc,%esp0x0804921f <+38>: push $0x50x08049221 <+40>: call 0x804986d <explode_bomb>0x08049226 <+45>: add $0x10,%esp0x08049229 <+48>: mov 0xc(%esp),%eax0x0804922d <+52>: and $0xf,%eax0x08049230 <+55>: mov %eax,0xc(%esp)0x08049234 <+59>: cmp $0xf,%eax0x08049237 <+62>: je 0x8049267 <phase_5+110>0x08049239 <+64>: mov $0x0,%ecx0x0804923e <+69>: mov $0x0,%edx0x08049243 <+74>: add $0x1,%edx0x08049246 <+77>: mov 0x8049e00(,%eax,4),%eax0x0804924d <+84>: add %eax,%ecx0x0804924f <+86>: cmp $0xf,%eax0x08049252 <+89>: jne 0x8049243 <phase_5+74>0x08049254 <+91>: movl $0xf,0xc(%esp)0x0804925c <+99>: cmp $0x8,%edx0x0804925f <+102>: jne 0x8049267 <phase_5+110>0x08049261 <+104>: cmp 0x8(%esp),%ecx0x08049265 <+108>: je 0x8049274 <phase_5+123>0x08049267 <+110>: sub $0xc,%esp0x0804926a <+113>: push $0x50x0804926c <+115>: call 0x804986d <explode_bomb>0x08049271 <+120>: add $0x10,%esp0x08049274 <+123>: add $0x1c,%esp0x08049277 <+126>: ret

分析

與前面的分析類似,第5個字符串也是兩個數字,可還原為

sccanf(input, ”%d %d, &A, &B);

分析得到運算流程為:A=A&0xf;且運算后A不能為0xf,之后就是執行一個循環,將地址0x8049e00+eax×4中的內容賦值給eax,然后ecx=ecx+eax。該循環一共執行8次,且要求8次后eax的值為0xf,ecx的值為B。

接下來就好辦了,首先查看內存地址0x8049e00處的值如下

(gdb) x/16xw 0x8049e00
0x8049e00 <array.2934>: 0x0000000a 0x00000002 0x0000000e 0x00000007
0x8049e10 <array.2934+16>: 0x00000008 0x0000000c 0x0000000f 0x0000000b
0x8049e20 <array.2934+32>: 0x00000000 0x00000004 0x00000001 0x0000000d
0x8049e30 <array.2934+48>: 0x00000003 0x00000009 0x00000006 0x00000005

已知最后一次eax=0xf,倒推8次便知道第一次的eax值了,從而得到輸入的第一個數字A的十六進制最低位的值,而輸入的第二個數字B的值為第第2到第9個的eax的和。

edx= 8 7 6 5 4 3 2 1 0
eax= 15 6 14 2 1 10 0 8 4

所以第一個數字A=4+k×16(k為自然數),第二個數字B=15+6+14+2+1+10+0+8=56。
取k=1,則A=20,輸入20 56,運行,成功!

Phase_6

反匯編代碼:

Dump of assembler code for function phase_6:0x080492cf <+0>: sub $0x10,%esp0x080492d2 <+3>: push $0xa0x080492d4 <+5>: push $0x00x080492d6 <+7>: pushl 0x1c(%esp)0x080492da <+11>: call 0x80489b0 <strtol@plt>0x080492df <+16>: mov %eax,0x804c1740x080492e4 <+21>: movl $0x804c174,(%esp)0x080492eb <+28>: call 0x8049278 <fun6>0x080492f0 <+33>: add $0x10,%esp0x080492f3 <+36>: mov $0x8,%edx0x080492f8 <+41>: mov 0x8(%eax),%eax0x080492fb <+44>: sub $0x1,%edx0x080492fe <+47>: jne 0x80492f8 <phase_6+41>0x08049300 <+49>: mov 0x804c174,%ecx0x08049306 <+55>: cmp %ecx,(%eax)0x08049308 <+57>: je 0x8049317 <phase_6+72>0x0804930a <+59>: sub $0xc,%esp0x0804930d <+62>: push $0x60x0804930f <+64>: call 0x804986d <explode_bomb>0x08049314 <+69>: add $0x10,%esp0x08049317 <+72>: add $0xc,%esp0x0804931a <+75>: ret

分析

通過分析程序,第6個雷是將字符串用strtol轉換為10進制數字。通過對堆棧調用的分析,調用完strtol后,將返會的十進制數字存入0x804c174地址中,緊接著直接將0x804c174(134529396)作為一個數字壓入棧中,作為參數傳入fun6函數,因此可知fun6子程序是對這個與輸入無關的量進行操作,也就是對這個整數134529396進行操作,因此可以判斷其返回值eax為定值,通過break *0x080492cf對phase_6下斷,單步執行可知,返回值eax的值仍然為0x804c174,即eax=0x804c174。

接下來執行的操作是進行一個循環,把eax+8地址中的值賦給eax,循環8次,然后與ecx的值(其實是輸入的字符串轉換成的10進制數)進行比較,若相等則成功拆解!通過在cmp %ecx,(%eax)處設置斷點,查看最終eax所指向地址中的內容,為0x5e(94),因此此題字符串前兩位為數字94,第3位是非數字字符即可。例如,輸入:94abc!23423423,運行如下

Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
The bomb phase 1 is defused. Congratulations!
The bomb phase 2 is defused. Congratulations!
The bomb phase 3 is defused. Congratulations!
The bomb phase 4 is defused. Congratulations!
The bomb phase 5 is defused. Congratulations!
Congratulations! You’ve defused the bomb!
The bomb phase 6 is defused. Congratulations!

最終PhaseID中的內容為(有的題答案不唯一):

Public speaking is very easy.
0 5 10 15 20 25
0 275
9
4 56
94abc!23423423

獲取更多內容請關注:

逆向and通信愛好者coding

總結

以上是生活随笔為你收集整理的堆栈图解CSAPP Bomb Lab实验解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。