[关闭]
@SovietPower 2021-04-17T10:01:38.000000Z 字数 24622 阅读 1412

CSAPP Lab2

CSAPP



参考:
https://zhuanlan.zhihu.com/p/339461318
https://zhuanlan.zhihu.com/p/339575162

结果


phase_3的其它一种答案:

phase_4的其它两种答案:

phase_5的其它两种答案:

Hints

(其实也没什么用)
* 运行前先可在phase_1...等函数处设断点:break phase_1,以便查看值。也可以break explode_bomb以便在bomb前kill掉。
* layout asm 调出汇编代码窗口;layout reg 调出寄存器窗口。
* 对于一些函数,可根据函数名猜测作用,而不需看它的具体实现(如果想彻底弄懂要看)。

Phase 1

汇编中调用了strings_not_equal函数,可猜出它比较两个字符串是否相等,相等则返回
testje可知,strings_not_equal返回时不会explode_bomb,也就是输入的字符串和要比较的字符串相等时可行。
而要比较的字符串即参数中的串,也就是位置的串。
通过(gdb) x/s 0x402400(gdb) print (char*) 0x402400查看该串,然后(gdb) run输入即可。

答案

Border relations with Canada have never been better.

汇编

  1. 0000000000400ee0 <phase_1>:
  2. 400ee0: 48 83 ec 08 sub $0x8,%rsp ;为调用函数的参数分配8的空间
  3. 400ee4: be 00 24 40 00 mov $0x402400,%esi ;%esi=0x402400
  4. 400ee9: e8 4a 04 00 00 callq 401338 <strings_not_equal> ;判断%esi处的字符串是否与读入串相等
  5. 400eee: 85 c0 test %eax,%eax
  6. 400ef0: 74 05 je 400ef7 <phase_1+0x17>
  7. 400ef2: e8 43 05 00 00 callq 40143a <explode_bomb>
  8. 400ef7: 48 83 c4 08 add $0x8,%rsp
  9. 400efb: c3 retq

Phase 2

先看调用的read_six_numbers函数,可以发现主要部分调用了sscanf,并且判断返回值,即读入的数据是个数否,不足则bomb;然后sscanf的第二个参数%esi为地址0x4025c3处的值,x/s 0x4025c3可知sscanf的参数为"%d %d %d %d %d %d",所以是输入恰好int
sscanf是库函数,可知读入的int分别存在sscanf的第个参数中,且这些参数分别是通过%rdx,%rcx,%r8,%r9和栈帧传递也就是保存。and它是直接修改地址处的值而不是寄存器的值(废话?)。
%rdx,%rcx,%r8,%r9观察read_six_numbers中的赋值可知(注意调用read_six_numbers前有%rsi=%rsp),分别表示地址%rsp,%rsp+4,%rsp+8,%rsp+12(这里的%rsp是调用前的栈顶),而第个参数在%rsp,%rsp+8处(这里的%rsp是现在的栈顶)也就是%rsp+16,%rsp+20处(之前的栈顶即%rsi)。
所以可知,读入的个数存在了phase_2中从栈顶向下的个地址中。
如下面代码中的注释所示,栈中自顶向下分别为读入的(六个)元素,读入的第一个元素需为,其余的元素需要分别是上一个元素的两倍。

答案

1 2 4 8 16 32

汇编

  1. 0000000000400efc <phase_2>:
  2. 400efc: 55 push %rbp
  3. 400efd: 53 push %rbx
  4. 400efe: 48 83 ec 28 sub $0x28,%rsp
  5. 400f02: 48 89 e6 mov %rsp,%rsi
  6. 400f05: e8 52 05 00 00 callq 40145c <read_six_numbers>
  7. 400f0a: 83 3c 24 01 cmpl $0x1,(%rsp) ;cmp (%rsp):1
  8. 400f0e: 74 20 je 400f30 <phase_2+0x34> ;to L2 if((%rsp)==1)->cond. 栈顶元素需为1
  9. 400f10: e8 25 05 00 00 callq 40143a <explode_bomb>
  10. 400f15: eb 19 jmp 400f30 <phase_2+0x34> ;to L2
  11. 400f17: 8b 43 fc mov -0x4(%rbx),%eax ;.L0: %eax=(%rbx-4) %eax为%rbx上面4的位置的值
  12. 400f1a: 01 c0 add %eax,%eax ;%eax*=2 %eax数值乘2
  13. 400f1c: 39 03 cmp %eax,(%rbx) ;cmp (%rbx):%eax
  14. 400f1e: 74 05 je 400f25 <phase_2+0x29> ;to L1 if((%rsp+4)==%eax)->cond. 需满足(%rbx)=2(%rbx-4)
  15. 400f20: e8 15 05 00 00 callq 40143a <explode_bomb>
  16. 400f25: 48 83 c3 04 add $0x4,%rbx ;.L1: %rbx+=4 %rbx向下4
  17. 400f29: 48 39 eb cmp %rbp,%rbx ;cmp %rbx:%rbq 若%rbx没到达%rbp则继续L0
  18. 400f2c: 75 e9 jne 400f17 <phase_2+0x1b> ;to L0 if(%rbx!=%rbp)
  19. 400f2e: eb 0c jmp 400f3c <phase_2+0x40> ;to L3 ->OK
  20. 400f30: 48 8d 5c 24 04 lea 0x4(%rsp),%rbx ;.L2: %rbx=%rsp+4 %rbx为栈顶以下4的位置
  21. 400f35: 48 8d 6c 24 18 lea 0x18(%rsp),%rbp ;%rbp=%rsp+24 %rbp为栈顶以下24的位置
  22. 400f3a: eb db jmp 400f17 <phase_2+0x1b> ;to L0
  23. 400f3c: 48 83 c4 28 add $0x28,%rsp ;.L3:
  24. 400f40: 5b pop %rbx
  25. 400f41: 5d pop %rbp
  26. 400f42: c3 retq

Phase 3

x/s 0x4025cf查看sscanf的格式为"%d %d",即输入两个整数。然后判断了读入个数是否为不是则bomb。
注意sscanf的参数为:sscanf(%rdi,%rsi,%rdx,%rcx),前两个为输入的字符串及格式,后两个为字符串转为的读入的数。由phase_3%rdx=%rsp+8,%rcx=%rsp+12可得读入的两个数的位置。
然后是ja,可知读入的第一个数为非负且小于等于
然后jmp到了M[8(%rsp+8)+0x402470]位置。(这是一个switch的跳转表)
x/8xg 0x402470查看从0x402470开始的字节地址:

  1. 0x402470: 0x0000000000400f7c 0x0000000000400fb9
  2. 0x402480: 0x0000000000400f83 0x0000000000400f8a
  3. 0x402490: 0x0000000000400f91 0x0000000000400f98
  4. 0x4024a0: 0x0000000000400f9f 0x0000000000400fa6

可知,若读入的第一个数为,则jmp400f7c处,判断读入的第二个数是否为0xcf;若读入的第一个数为,则jmp400fb9处,判断读入的第二个数是否为0x137...

答案

所以答案有,前两种为0 2071 311
(gdb) print 0xcf可查看0xcf的十进制表示,(gdb) print \x 207可查看207的十六进制表示)

汇编

  1. 0000000000400f43 <phase_3>:
  2. 400f43: 48 83 ec 18 sub $0x18,%rsp
  3. 400f47: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx
  4. 400f4c: 48 8d 54 24 08 lea 0x8(%rsp),%rdx
  5. 400f51: be cf 25 40 00 mov $0x4025cf,%esi
  6. 400f56: b8 00 00 00 00 mov $0x0,%eax
  7. 400f5b: e8 90 fc ff ff callq 400bf0 <__isoc99_sscanf@plt>
  8. 400f60: 83 f8 01 cmp $0x1,%eax
  9. 400f63: 7f 05 jg 400f6a <phase_3+0x27> ;%rax>1 ->cond.
  10. 400f65: e8 d0 04 00 00 callq 40143a <explode_bomb>
  11. 400f6a: 83 7c 24 08 07 cmpl $0x7,0x8(%rsp) ;(%rsp+8):7
  12. 400f6f: 77 3c ja 400fad <phase_3+0x6a> ;(%rsp+8)>7 (unsigned) (%rsp+8)<=7 ->cond.
  13. 400f71: 8b 44 24 08 mov 0x8(%rsp),%eax ;%rax=(%rsp+8)
  14. 400f75: ff 24 c5 70 24 40 00 jmpq *0x402470(,%rax,8) ;jmp (8%rax+0x402470)
  15. 400f7c: b8 cf 00 00 00 mov $0xcf,%eax ;%rax=0xcf
  16. 400f81: eb 3b jmp 400fbe <phase_3+0x7b>
  17. 400f83: b8 c3 02 00 00 mov $0x2c3,%eax
  18. 400f88: eb 34 jmp 400fbe <phase_3+0x7b>
  19. 400f8a: b8 00 01 00 00 mov $0x100,%eax
  20. 400f8f: eb 2d jmp 400fbe <phase_3+0x7b>
  21. 400f91: b8 85 01 00 00 mov $0x185,%eax
  22. 400f96: eb 26 jmp 400fbe <phase_3+0x7b>
  23. 400f98: b8 ce 00 00 00 mov $0xce,%eax
  24. 400f9d: eb 1f jmp 400fbe <phase_3+0x7b>
  25. 400f9f: b8 aa 02 00 00 mov $0x2aa,%eax
  26. 400fa4: eb 18 jmp 400fbe <phase_3+0x7b>
  27. 400fa6: b8 47 01 00 00 mov $0x147,%eax
  28. 400fab: eb 11 jmp 400fbe <phase_3+0x7b>
  29. 400fad: e8 88 04 00 00 callq 40143a <explode_bomb>
  30. 400fb2: b8 00 00 00 00 mov $0x0,%eax
  31. 400fb7: eb 05 jmp 400fbe <phase_3+0x7b>
  32. 400fb9: b8 37 01 00 00 mov $0x137,%eax
  33. 400fbe: 3b 44 24 0c cmp 0xc(%rsp),%eax ;0xcf:(%rsp+12)
  34. 400fc2: 74 05 je 400fc9 <phase_3+0x86> ;0xcf==(%rsp+12) ->cond.
  35. 400fc4: e8 71 04 00 00 callq 40143a <explode_bomb>
  36. 400fc9: 48 83 c4 18 add $0x18,%rsp
  37. 400fcd: c3 retq

Phase 4

同上查看sscanf的格式即0x4025cf处的字符串,为"%d %d",所以读入两个int,存在了%rsp+8,%rsp+12位置。
首先第一个数要。因为是jbe比较可知读入的数应非负。
然后进入func4,三个参数为%rdi=(%rsp+8) %rsi=0 %rdx=14
先看phase_4的后面,要满足func4的返回值为且读入的第二个数为
再看func4,大体写一下注释可得原始C代码:

  1. int func4(int x,int y,int z)//(input_1,0,14)
  2. {
  3. int t=(z-y)/2+y;
  4. if(t<=x)
  5. {
  6. if(t>=x) return 0;//t==x
  7. return func4(x,t+1,z);
  8. }
  9. else
  10. return func4(x,y,t-1)*2;
  11. }

答案

模拟一下可知,要使返回值为0,合法的输入,即答案有四种7 03 01 00 0,分别对应t的四种可能取值。

汇编

func4的注释:

  1. 0000000000400fce <func4>:
  2. 400fce: 48 83 ec 08 sub $0x8,%rsp ;Augment:(%rdi,%rsi,%rdx) (input_1,0,14)(at the beginning)
  3. 400fd2: 89 d0 mov %edx,%eax
  4. 400fd4: 29 f0 sub %esi,%eax ;%rax=%rdx-%rsi =14(at the beginning)
  5. 400fd6: 89 c1 mov %eax,%ecx
  6. 400fd8: c1 e9 1f shr $0x1f,%ecx
  7. 400fdb: 01 c8 add %ecx,%eax ;%rax+=%rax>>31(逻辑右移) -> %rax+=0
  8. 400fdd: d1 f8 sar %eax ;%rax>>=1 =7(at the beginning)
  9. 400fdf: 8d 0c 30 lea (%rax,%rsi,1),%ecx ;%rcx=%rax+%rsi =7(at the beginning)
  10. 400fe2: 39 f9 cmp %edi,%ecx ;%rcx:%rdi 7:input_1(at the beginning)
  11. 400fe4: 7e 0c jle 400ff2 <func4+0x24> ;if(%rcx<=%rdi) to L0
  12. 400fe6: 8d 51 ff lea -0x1(%rcx),%edx ;%rdx=%rcx-1 =6(at the beginning)
  13. 400fe9: e8 e0 ff ff ff callq 400fce <func4>
  14. 400fee: 01 c0 add %eax,%eax ;%rax*=2
  15. 400ff0: eb 15 jmp 401007 <func4+0x39> ;return
  16. 400ff2: b8 00 00 00 00 mov $0x0,%eax ;.L0: %rax=0
  17. 400ff7: 39 f9 cmp %edi,%ecx ;%rcx:%rdi
  18. 400ff9: 7d 0c jge 401007 <func4+0x39> ;if(%rcx>=%rdi) return
  19. 400ffb: 8d 71 01 lea 0x1(%rcx),%esi ;%rsi=%rcx+1
  20. 400ffe: e8 cb ff ff ff callq 400fce <func4>
  21. 401003: 8d 44 00 01 lea 0x1(%rax,%rax,1),%eax ;%rax=2%rax+1
  22. 401007: 48 83 c4 08 add $0x8,%rsp
  23. 40100b: c3 retq

phase_4

  1. 000000000040100c <phase_4>:
  2. 40100c: 48 83 ec 18 sub $0x18,%rsp
  3. 401010: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx
  4. 401015: 48 8d 54 24 08 lea 0x8(%rsp),%rdx
  5. 40101a: be cf 25 40 00 mov $0x4025cf,%esi
  6. 40101f: b8 00 00 00 00 mov $0x0,%eax
  7. 401024: e8 c7 fb ff ff callq 400bf0 <__isoc99_sscanf@plt> ;sscanf(%rdi,%rsi,%rdx,%rcx)
  8. 401029: 83 f8 02 cmp $0x2,%eax
  9. 40102c: 75 07 jne 401035 <phase_4+0x29>
  10. 40102e: 83 7c 24 08 0e cmpl $0xe,0x8(%rsp) ;cmp (%rsp+8):0xe
  11. 401033: 76 05 jbe 40103a <phase_4+0x2e> ;(%rsp+8)<=0xe ->cond.
  12. 401035: e8 00 04 00 00 callq 40143a <explode_bomb>
  13. 40103a: ba 0e 00 00 00 mov $0xe,%edx
  14. 40103f: be 00 00 00 00 mov $0x0,%esi
  15. 401044: 8b 7c 24 08 mov 0x8(%rsp),%edi ;%rdi=(%rsp+8) %rsi=0 %rdx=0xe
  16. 401048: e8 81 ff ff ff callq 400fce <func4>
  17. 40104d: 85 c0 test %eax,%eax ;cmp %eax:0
  18. 40104f: 75 07 jne 401058 <phase_4+0x4c> ;%eax!=0 -> %eax=0 ->cond.
  19. 401051: 83 7c 24 0c 00 cmpl $0x0,0xc(%rsp) ;(%rsp+12):0
  20. 401056: 74 05 je 40105d <phase_4+0x51> ;(%rsp+12)=0 ->cond.
  21. 401058: e8 dd 03 00 00 callq 40143a <explode_bomb>
  22. 40105d: 48 83 c4 18 add $0x18,%rsp
  23. 401061: c3 retq

Phase 5

首先有一个fs[0x28],这是金丝雀数,这里用M[%rsp+18]来保存,避免phase_5中出现溢出。

金丝雀:
现代操作系统有了防止缓冲区溢出的一系列保护措施,包括 不可执行的栈 和 栈内金丝雀(stack canary)。
金丝雀是通过汇编来实现的。例如,由于GCC的栈保护器选项的原因,金丝雀能被用于任何可能有漏洞的函数上。在初始化一个栈帧时,在栈底设置一个随机的canary值,并确保这个值完好无损。栈帧销毁前测试该值是否“死掉”,即是否被改变。如果这个值被改变,则说明发生了缓冲区溢出(或者bug),程序通过__stack_chk_fail 被终止运行,以免漏洞利用成功。由于金丝雀处于栈的关键位置上,它使得栈缓冲区溢出的漏洞挖掘变得非常困难。
有三种基本的绕过canary方法:
1. 泄露canary
2. 多进程程序的canary爆破
3. SSP Leak(利用__stack_chk_fail函数泄露信息)。

首先调用了string_length,可知输入的字符串长度需为
然后可以看出40108b4010ac为一个do while循环,%rax循环到(设为)。
循环中的%rbx即第一个参数%rbi也就是读入的串,每次取出%rbx+i也就是放到%rdx中,然后在%rsp+i+10中存入。
要分析一下这段:

  1. 40108b: 0f b6 0c 03 movzbl (%rbx,%rax,1),%ecx ;.L1: %rcx=(%rbx+%rax)
  2. 40108f: 88 0c 24 mov %cl,(%rsp) ;(%rsp)=%rcx
  3. 401092: 48 8b 14 24 mov (%rsp),%rdx ;%rdx=%rcx
  4. 401096: 83 e2 0f and $0xf,%edx ;%rdx&=0xf (取低4位)
  5. 401099: 0f b6 92 b0 24 40 00 movzbl 0x4024b0(%rdx),%edx ;%rdx=(%rdx+0x4024b0)
  6. 4010a0: 88 54 04 10 mov %dl,0x10(%rsp,%rax,1) ;(%rsp+%rax+10)=%rdx
  7. 4010a4: 48 83 c0 01 add $0x1,%rax ;%rax+=1
  8. 4010a8: 48 83 f8 06 cmp $0x6,%rax ;%rax:6
  9. 4010ac: 75 dd jne 40108b <phase_5+0x29> ;if(%rax!=6) to L1

拿C写一下会更清楚:

  1. {
  2. long rcx=(long)s[i];
  3. char cl=rcx; //只取低8位,高位截断
  4. cl&=0xf; //只取低4位,也就是0~16
  5. long edx=(long)M[cl+0x4024b0];
  6. M[%rsp+i+10]=edx&0xff; //只取低8位
  7. if(++i==6) break;
  8. }

中间做了将s[i]扩充又截断的无用操作,在%rsp+i+10中存入M[(s[i]&0xf)+0x4024b0]
然后下面将%rsi=0x40245e处的字符串和%rsp+10处的字符串进行比较,需满足两个串相同。
所以我们看一下0x40245e0x4024b0的两个串是什么:

  1. (gdb) x/s 0x40245e
  2. 0x40245e: "flyers"
  3. (gdb) x/s 0x4024b0
  4. 0x4024b0 <array.3449>: "maduiersnfotvbylSo you think you can stop the bomb with ctrl-c, do you?"

可知的取值应为:。但是ASCII码在以内的为非打印字符不太好输入(不过还是可以^I^O^N^E^F^G,注意^i确实是可见的,就是打一个tab)。注意到只取了低位,所以可以加上一个,变成)/.%&';或者加上一个2^6=64,变成IONEFG。同理还可以加等。当然最初的也不是唯一解。

答案

其中三种答案为:^I^O^N^E^F^G)/.%&'IONEFG

汇编

  1. 000000000401062 <phase_5>:
  2. 401062: 53 push %rbx
  3. 401063: 48 83 ec 20 sub $0x20,%rsp
  4. 401067: 48 89 fb mov %rdi,%rbx ;%rbx=%rdi
  5. 40106a: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax ;%rax=fs[0x28]
  6. 401071: 00 00
  7. 401073: 48 89 44 24 18 mov %rax,0x18(%rsp) ;(%rsp+18)=fs[0x28]
  8. 401078: 31 c0 xor %eax,%eax ;%rax=0
  9. 40107a: e8 9c 02 00 00 callq 40131b <string_length>
  10. 40107f: 83 f8 06 cmp $0x6,%eax ;%rax:6
  11. 401082: 74 4e je 4010d2 <phase_5+0x70> ;if(%rax==6) to L2 ->cond.
  12. 401084: e8 b1 03 00 00 callq 40143a <explode_bomb>
  13. 401089: eb 47 jmp 4010d2 <phase_5+0x70> ;to L2
  14. 40108b: 0f b6 0c 03 movzbl (%rbx,%rax,1),%ecx ;.L1: %rcx=(%rbx+%rax)
  15. 40108f: 88 0c 24 mov %cl,(%rsp) ;(%rsp)=%rcx
  16. 401092: 48 8b 14 24 mov (%rsp),%rdx ;%rdx=%rcx
  17. 401096: 83 e2 0f and $0xf,%edx ;%rdx&=0xf (取低4位)
  18. 401099: 0f b6 92 b0 24 40 00 movzbl 0x4024b0(%rdx),%edx ;%rdx=(%rdx+0x4024b0)
  19. 4010a0: 88 54 04 10 mov %dl,0x10(%rsp,%rax,1) ;(%rsp+%rax+10)=%rdx
  20. 4010a4: 48 83 c0 01 add $0x1,%rax ;%rax+=1
  21. 4010a8: 48 83 f8 06 cmp $0x6,%rax ;%rax:6
  22. 4010ac: 75 dd jne 40108b <phase_5+0x29> ;if(%rax!=6) to L1
  23. 4010ae: c6 44 24 16 00 movb $0x0,0x16(%rsp) ;(%rsp+16)=0
  24. 4010b3: be 5e 24 40 00 mov $0x40245e,%esi ;%rsi=0x40245e
  25. 4010b8: 48 8d 7c 24 10 lea 0x10(%rsp),%rdi ;%rdi=%rsp+10
  26. 4010bd: e8 76 02 00 00 callq 401338 <strings_not_equal>
  27. 4010c2: 85 c0 test %eax,%eax ;%rax:0
  28. 4010c4: 74 13 je 4010d9 <phase_5+0x77> ;if(%rax==0) to L3 ->cond.(strings equal)
  29. 4010c6: e8 6f 03 00 00 callq 40143a <explode_bomb>
  30. 4010cb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
  31. 4010d0: eb 07 jmp 4010d9 <phase_5+0x77> ;to L3
  32. 4010d2: b8 00 00 00 00 mov $0x0,%eax ;.L2: %rax=0
  33. 4010d7: eb b2 jmp 40108b <phase_5+0x29>
  34. 4010d9: 48 8b 44 24 18 mov 0x18(%rsp),%rax ;.L3: (return)
  35. 4010de: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
  36. 4010e5: 00 00
  37. 4010e7: 74 05 je 4010ee <phase_5+0x8c>
  38. 4010e9: e8 42 fa ff ff callq 400b30 <__stack_chk_fail@plt>
  39. 4010ee: 48 83 c4 20 add $0x20,%rsp
  40. 4010f2: 5b pop %rbx
  41. 4010f3: c3 retq

Phase 6

phase_2中对read_six_numbers的分析可知,读入的个数存在了phase_6中从栈顶向下的个地址中,且%rsi,%r13都是栈顶。注意M[rsp+4i]A[i]
对汇编写一下注释,然后转化为C代码。
下面C代码中Part 1,2要求读入的个数非负且不大于,且互不相等。
Part 3简单,就是
Part 4比较复杂,但如果C代码写对并且精简成循环,可以看出,对于每个(被减过后),若,则;否则rdx=0x6032d0,rdx=*(rdx)会执行次,然后(若下标超过则表示后面的栈帧空间,因为有)。
这个形式我们可以猜到是链表,0x6032d0为表头。然后汇编中直接出现的*(0x6032d0)可以查看一下,以及**(0x6032d0)也看一下:

  1. (gdb) x 0x6032d0
  2. 0x6032d0 <node1>: 0x0000014c
  3. (gdb) x 0x6032d8
  4. 0x6032d8 <node1+8>: 0x006032e0
  5. (gdb) x 0x6032e0
  6. 0x6032e0 <node2>: 0x000000a8

<node1><node1+8>我们可以猜到是一个链表结构体的初始地址和next<node2>则到了链表的下一个节点。它们存储是连续的,可知一个结构体大小为0x6032e0-0x6032d0=16。可以x/24xw 0x6032d0查看下面的个节点(并且可知node6为链表尾部)。其中第三列为next

  1. (gdb) x/24xw 0x6032d0
  2. 0x6032d0 <node1>: 0x0000014c 0x00000001 0x006032e0 0x00000000
  3. 0x6032e0 <node2>: 0x000000a8 0x00000002 0x006032f0 0x00000000
  4. 0x6032f0 <node3>: 0x0000039c 0x00000003 0x00603300 0x00000000
  5. 0x603300 <node4>: 0x000002b3 0x00000004 0x00603310 0x00000000
  6. 0x603310 <node5>: 0x000001dd 0x00000005 0x00603320 0x00000000
  7. 0x603320 <node6>: 0x000001bb 0x00000006 0x00000000 0x00000000

所以,对每个rdx=0x6032d0,会有rdx=rdx->next,然后(%rsp+8i+32)=A[2i+8]=rdx。因为互不相同,所以(%rsp+8i+32)会分别得到个节点的初始地址,分别是node[A[i]]的初始地址(若则就是node1)。
Part 5,可看出是在从(%rsp+32)开始重新调整链表的next,且就是按照在(%rsp+8i+32)中的顺序,即(*(%rsp+8i+32))->next=*(%rsp+8(i+1)+32)。所以新的链表顺序为:node[A[0]],node[A[1]],...,node[A[5]]
Part 6容易些,对链表中的每个节点x,应有x->val >= x->next->val,而链表中的顺序是新的,也就是说重排序后的链表是按val(第一个元素)递减的。
而由链表中的值可知,node大小顺序为:3 4 5 6 1 2,被减后的为:3 4 5 6 1/0 2,所以原始的为:4 3 2 1 6 5(因为不能为所以少了一种答案)。

等效的C++代码:

  1. void phase_6(input)
  2. {
  3. for(int i=0; i<6; ++i) read(A[i]);
  4. int rsp=*stack;//=*A
  5. //以下A也可以看作栈帧
  6. int r12=0;
  7. int *rsi=A,*r13=A,r14=A,*rbp=A;
  8. int rax=A[0],rcx,rdx;
  9. //Part 1: Check every A[i]<=6
  10. while(1)
  11. {
  12. rbp=r13, rax=*r13;
  13. --rax;
  14. if(!(rax<=5)) bomb();//->cond.
  15. ++r12;
  16. if(r12==6) goto L3;
  17. rbx=r12;//i
  18. //Part 2: Check every A[i]!=A[j] (j>i)
  19. do
  20. {
  21. rax=A[rbx];
  22. if(*rbp!=rax) ;//->cond.
  23. else bomb();
  24. rbx++;
  25. }
  26. while(rbx<=5);
  27. r13+=4;
  28. }
  29. //Part 3: A[i]=7-A[i]
  30. L3:
  31. rsi=A[6];
  32. rax=A;
  33. rcx=7;
  34. do
  35. {
  36. rdx=7-*rax;
  37. *rax=rdx;
  38. rax+=4;
  39. }
  40. while(rax!=rsi);
  41. //Part 4:
  42. rsi=0;
  43. while(1)
  44. {
  45. //L8
  46. if(A[rsi/4]<=1) rdx=0x6032d0;
  47. else
  48. {
  49. rax=1;
  50. rdx=0x6032d0;
  51. //L5
  52. do
  53. {
  54. rdx=*(rdx+8);//*0x6032d8
  55. ++rax;
  56. }while(rax!=A[rsi/4]);
  57. }
  58. //L7
  59. for(; A[rsi/4]<=1; rdx=0x6032d0/*L6*/)
  60. {
  61. A[rsi/2+8]=rdx;//rsi/2+8=8,10,12,14,16,18
  62. rsi+=4;
  63. if(rsi==24) goto L9;
  64. }
  65. }
  66. //Part 5
  67. L9:
  68. rbx=*(rsp+32);
  69. rax=rsp+40;
  70. rsi=rsp+80;
  71. rcx=rbx;
  72. while(1)
  73. {
  74. rdx=*rax;
  75. *(rcx+8)=rdx;
  76. rax+=8;
  77. if(rax==rsi) break;
  78. rcx=rdx;
  79. }
  80. *(rdx+8)=0;//end->next=0
  81. //Part 6
  82. rbp=5;
  83. do
  84. {
  85. rax=*(rbx+8);
  86. rax=*rax;//rax=rbx->next->val
  87. if(*(rbx)>=rax) ;//->cond.
  88. else bomb();
  89. rbx=*(rbx+8);
  90. rbp--;
  91. }while(rbp!=0);
  92. //return
  93. rsp+=80;
  94. }

答案

4 3 2 1 6 5

汇编

  1. 00000000004010f4 <phase_6>:
  2. 4010f4: 41 56 push %r14
  3. 4010f6: 41 55 push %r13
  4. 4010f8: 41 54 push %r12
  5. 4010fa: 55 push %rbp
  6. 4010fb: 53 push %rbx
  7. 4010fc: 48 83 ec 50 sub $0x50,%rsp
  8. 401100: 49 89 e5 mov %rsp,%r13
  9. 401103: 48 89 e6 mov %rsp,%rsi ;%rsi=%r13=%rsp
  10. 401106: e8 51 03 00 00 callq 40145c <read_six_numbers>
  11. 40110b: 49 89 e6 mov %rsp,%r14 ;%r14=%rsp
  12. 40110e: 41 bc 00 00 00 00 mov $0x0,%r12d ;%r12=0
  13. 401114: 4c 89 ed mov %r13,%rbp ;.L0: %rbp=%r13
  14. 401117: 41 8b 45 00 mov 0x0(%r13),%eax ;%rax=(%r13)
  15. 40111b: 83 e8 01 sub $0x1,%eax ;%rax-=1
  16. 40111e: 83 f8 05 cmp $0x5,%eax ;cmp %rax:5
  17. 401121: 76 05 jbe 401128 <phase_6+0x34> ;if(%rax<=5) to L1 ->cond. (unsigned)
  18. 401123: e8 12 03 00 00 callq 40143a <explode_bomb>
  19. 401128: 41 83 c4 01 add $0x1,%r12d ;.L1: %r12+=1
  20. 40112c: 41 83 fc 06 cmp $0x6,%r12d ;cmp %r12:6
  21. 401130: 74 21 je 401153 <phase_6+0x5f> ;if(%r12==6) to L3
  22. 401132: 44 89 e3 mov %r12d,%ebx ;%rbx=%r12
  23. 401135: 48 63 c3 movslq %ebx,%rax ;.Lp: %rax=(long)%ebx (signed extend)
  24. 401138: 8b 04 84 mov (%rsp,%rax,4),%eax ;%rax=(%rsp+4%rax)
  25. 40113b: 39 45 00 cmp %eax,0x0(%rbp) ;cmp (%rbp):%rax
  26. 40113e: 75 05 jne 401145 <phase_6+0x51> ;if((%rbp)!=%rax) to L2 ->cond.
  27. 401140: e8 f5 02 00 00 callq 40143a <explode_bomb>
  28. 401145: 83 c3 01 add $0x1,%ebx ;.L2: %rbx+=1
  29. 401148: 83 fb 05 cmp $0x5,%ebx ;cmp %rbx:5
  30. 40114b: 7e e8 jle 401135 <phase_6+0x41> ;if(%rbx<=5) to Lp
  31. 40114d: 49 83 c5 04 add $0x4,%r13 ;%r13+=4
  32. 401151: eb c1 jmp 401114 <phase_6+0x20> ;to L0
  33. 401153: 48 8d 74 24 18 lea 0x18(%rsp),%rsi ;.L3: %rsi=%rsp+24
  34. 401158: 4c 89 f0 mov %r14,%rax ;%rax=%r14
  35. 40115b: b9 07 00 00 00 mov $0x7,%ecx ;%rcx=7
  36. 401160: 89 ca mov %ecx,%edx ;.L4: %rdx=7
  37. 401162: 2b 10 sub (%rax),%edx ;%rdx-=(%rax)
  38. 401164: 89 10 mov %edx,(%rax) ;(%rax)=%rdx
  39. 401166: 48 83 c0 04 add $0x4,%rax ;%rax+=4
  40. 40116a: 48 39 f0 cmp %rsi,%rax ;cmp %rax:%rsi
  41. 40116d: 75 f1 jne 401160 <phase_6+0x6c> ;if(%rax!=%rsi) to L4
  42. 40116f: be 00 00 00 00 mov $0x0,%esi ;%rsi=0
  43. 401174: eb 21 jmp 401197 <phase_6+0xa3> ;to L8
  44. 401176: 48 8b 52 08 mov 0x8(%rdx),%rdx ;.L5: rdx=*(rdx+8);
  45. 40117a: 83 c0 01 add $0x1,%eax ;%rax+=1
  46. 40117d: 39 c8 cmp %ecx,%eax ;cmp %rax:%rcx
  47. 40117f: 75 f5 jne 401176 <phase_6+0x82> ;if(%rax!=%rcx) to L5
  48. 401181: eb 05 jmp 401188 <phase_6+0x94> ;to L7
  49. 401183: ba d0 32 60 00 mov $0x6032d0,%edx ;.L6: %rdx=0x6032d0
  50. 401188: 48 89 54 74 20 mov %rdx,0x20(%rsp,%rsi,2) ;.L7: (%rsp+2%rsi+32)=%rdx
  51. 40118d: 48 83 c6 04 add $0x4,%rsi ;%rsi+=4
  52. 401191: 48 83 fe 18 cmp $0x18,%rsi ;cmp %rsi:24
  53. 401195: 74 14 je 4011ab <phase_6+0xb7> ;if(%rsi==24) to L9
  54. 401197: 8b 0c 34 mov (%rsp,%rsi,1),%ecx ;.L8: %rcx=(%rsp+%rsi)
  55. 40119a: 83 f9 01 cmp $0x1,%ecx ;cmp %rcx:1
  56. 40119d: 7e e4 jle 401183 <phase_6+0x8f> ;if(%rcx<=1) to L6
  57. 40119f: b8 01 00 00 00 mov $0x1,%eax ;%rax=1
  58. 4011a4: ba d0 32 60 00 mov $0x6032d0,%edx ;%rdx=0x6032d0
  59. 4011a9: eb cb jmp 401176 <phase_6+0x82> ;to L5
  60. 4011ab: 48 8b 5c 24 20 mov 0x20(%rsp),%rbx ;.L9: %rbx=(%rsp+32)
  61. 4011b0: 48 8d 44 24 28 lea 0x28(%rsp),%rax ;%rax=%rsp+40
  62. 4011b5: 48 8d 74 24 50 lea 0x50(%rsp),%rsi ;%rsi=%rsp+80
  63. 4011ba: 48 89 d9 mov %rbx,%rcx ;%rcx=%rbx
  64. 4011bd: 48 8b 10 mov (%rax),%rdx ;.L10: %rdx=(%rax)
  65. 4011c0: 48 89 51 08 mov %rdx,0x8(%rcx) ;(%rcx+8)=%rdx
  66. 4011c4: 48 83 c0 08 add $0x8,%rax ;%rax+=8
  67. 4011c8: 48 39 f0 cmp %rsi,%rax ;cmp %rax:%rsi
  68. 4011cb: 74 05 je 4011d2 <phase_6+0xde> ;if(%rax==%rsi) to L11
  69. 4011cd: 48 89 d1 mov %rdx,%rcx ;%rcx=%rdx
  70. 4011d0: eb eb jmp 4011bd <phase_6+0xc9> ;to L10
  71. 4011d2: 48 c7 42 08 00 00 00 movq $0x0,0x8(%rdx) ;.L11: (%rdx+8)=0
  72. 4011d9: 00
  73. 4011da: bd 05 00 00 00 mov $0x5,%ebp ;%rbp=5
  74. 4011df: 48 8b 43 08 mov 0x8(%rbx),%rax ;.L12: %rax=(%rbx+8)
  75. 4011e3: 8b 00 mov (%rax),%eax ;%rax=(%rax)
  76. 4011e5: 39 03 cmp %eax,(%rbx) ;cmp (%rbx):%rax
  77. 4011e7: 7d 05 jge 4011ee <phase_6+0xfa> ;if((%rbx)>=%rax) to L13 ->cond.
  78. 4011e9: e8 4c 02 00 00 callq 40143a <explode_bomb>
  79. 4011ee: 48 8b 5b 08 mov 0x8(%rbx),%rbx ;.L13: %rbx=(%rbx+8)
  80. 4011f2: 83 ed 01 sub $0x1,%ebp ;%rbp-=1
  81. 4011f5: 75 e8 jne 4011df <phase_6+0xeb> ;if(%rbp!=0) to L12
  82. 4011f7: 48 83 c4 50 add $0x50,%rsp ;%rsp+=80
  83. 4011fb: 5b pop %rbx
  84. 4011fc: 5d pop %rbp
  85. 4011fd: 41 5c pop %r12
  86. 4011ff: 41 5d pop %r13
  87. 401201: 41 5e pop %r14
  88. 401203: c3 retq

Secret Phase

可以看到phase_6下面还有一个secret_phase
开始先read_line,应该为读入一行字符串,然后调用库函数int strtol(const char *nptr,char **endptr,int base)。由参数的初始化可知,将读入的串转为了10进制数,存到%rax,%rbx中。
然后有,,即转成的的应数小于等于
然后调用fun7(0x6030f0,x)。由下可知,fun7的返回值需为,才不会bomb并输出0x402438处的字符串Wow! You've defused the secret stage!
再看fun7,写一下汇编的注释可得等效C代码:

  1. int fun7(int *x,int y)//(rdi,rsi)=(0x6030f0,x)
  2. {
  3. if(!x) return -1;
  4. if(*x<=y)
  5. {
  6. if(*x==y) return 0;
  7. return 2*fun7(*(x+10),y)+1;//*x<y
  8. }
  9. else return 2*fun7(*(x+8),y);//*x>y
  10. }

因为时返回若返回一个奇数,可知若要返回值为,则第一次调用fun7,第二次调用fun7,第三次调用fun7并结束递归。
重新记,记为输入的数,有:
查看一下这个地方的值:

  1. (gdb) x/160 0x6030f0
  2. 0x6030f0 <n1>: 0x00000024 0x00000000 0x00603110 0x00000000
  3. 0x603100 <n1+16>: 0x00603130 0x00000000 0x00000000 0x00000000
  4. 0x603110 <n21>: 0x00000008 0x00000000 0x00603190 0x00000000
  5. 0x603120 <n21+16>: 0x00603150 0x00000000 0x00000000 0x00000000
  6. 0x603130 <n22>: 0x00000032 0x00000000 0x00603170 0x00000000
  7. 0x603140 <n22+16>: 0x006031b0 0x00000000 0x00000000 0x00000000
  8. 0x603150 <n32>: 0x00000016 0x00000000 0x00603270 0x00000000
  9. 0x603160 <n32+16>: 0x00603230 0x00000000 0x00000000 0x00000000
  10. 0x603170 <n33>: 0x0000002d 0x00000000 0x006031d0 0x00000000
  11. 0x603180 <n33+16>: 0x00603290 0x00000000 0x00000000 0x00000000
  12. 0x603190 <n31>: 0x00000006 0x00000000 0x006031f0 0x00000000
  13. 0x6031a0 <n31+16>: 0x00603250 0x00000000 0x00000000 0x00000000
  14. 0x6031b0 <n34>: 0x0000006b 0x00000000 0x00603210 0x00000000
  15. 0x6031c0 <n34+16>: 0x006032b0 0x00000000 0x00000000 0x00000000
  16. 0x6031d0 <n45>: 0x00000028 0x00000000 0x00000000 0x00000000
  17. 0x6031e0 <n45+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  18. 0x6031f0 <n41>: 0x00000001 0x00000000 0x00000000 0x00000000
  19. 0x603200 <n41+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  20. 0x603210 <n47>: 0x00000063 0x00000000 0x00000000 0x00000000
  21. 0x603220 <n47+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  22. 0x603230 <n44>: 0x00000023 0x00000000 0x00000000 0x00000000
  23. 0x603240 <n44+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  24. 0x603250 <n42>: 0x00000007 0x00000000 0x00000000 0x00000000
  25. 0x603260 <n42+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  26. 0x603270 <n43>: 0x00000014 0x00000000 0x00000000 0x00000000
  27. 0x603280 <n43+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  28. 0x603290 <n46>: 0x0000002f 0x00000000 0x00000000 0x00000000
  29. 0x6032a0 <n46+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  30. 0x6032b0 <n48>: 0x000003e9 0x00000000 0x00000000 0x00000000
  31. 0x6032c0 <n48+16>: 0x00000000 0x00000000 0x00000000 0x00000000
  32. 0x6032d0 <node1>: 0x0000014c 0x00000001 0x006032e0 0x00000000
  33. 0x6032e0 <node2>: 0x000000a8 0x00000002 0x006032f0 0x00000000
  34. 0x6032f0 <node3>: 0x0000039c 0x00000003 0x00603300 0x00000000
  35. 0x603300 <node4>: 0x000002b3 0x00000004 0x00603310 0x00000000
  36. 0x603310 <node5>: 0x000001dd 0x00000005 0x00603320 0x00000000
  37. 0x603320 <node6>: 0x000001bb 0x00000006 0x00000000 0x00000000
  38. 0x603330: 0x00000000 0x00000000 0x00000000 0x00000000
  39. 0x603340 <host_table>: 0x00402629 0x00000000 0x00402643 0x00000000
  40. 0x603350 <host_table+16>: 0x0040265d 0x00000000 0x00000000 0x00000000
  41. 0x603360 <host_table+32>: 0x00000000 0x00000000 0x00000000 0x00000000

可知这个位置存的就是行的node,每个node个值,并且第、第个值指向其它节点的地址。不难猜出是一棵二叉树,的值分别为左右儿子。我们可以画出这棵树,如果标上节点的值(第一个)就能看出还是一棵二叉搜索树(值就不标了):

存的值,的左儿子,的右儿子。
所以由,得。所以n32的值
所以要输入的字符串为

还有一个问题是,如何进入secret_phase/输入这个答案。
搜索调用secret_phase的函数,可以发现在phase_defused中调用了这个函数。
break secret_phase,在输入phase_1的答案后调试这个函数。

分析phase_defused怎样进入secret_phase。对于sscanf依旧查看一下两个参数:

  1. (gdb) x/s 0x603870
  2. 0x603870 <input_strings+240>: ""
  3. (gdb) x/s 0x402619
  4. 0x402619: "%d %d %s"

后面判断了返回值为,所以要输入两个int和一个字符串(且分别存在rsp+8,rsp+12,rsp+16位置)。
然后是strings_not_equal,要满足rsp+16位置的串,也就是输入串,和0x402622位置的串,也就是DrEvil相等。然后就可以puts Curses, you've found the secret phase!But finding it and solving it are quite different...,并进入secret_phase了。
(再之前还有个if(*(rip+0x202181)!=6) goto ...,即程序执行到某个位置才可能向下,先不管)

那么什么时候输入呢?前边sscanf的第一个参数是固定输入源,而此时是空字符串,所以肯定是之后改过。而汇编中并没有出现过0x603870的位置。
可以直接在0x4015fa处调用sscanf这里break,然后再看0x603870的值(这样还能解决什么时候有*(rip+0x202181)==6的判断)。

  1. (gdb) break *0x4015fa
  2. Breakpoint 4 at 0x4015fa
  3. (gdb) info break
  4. Num Type Disp Enb Address What
  5. 4 breakpoint keep y 0x00000000004015fa <phase_defused+54>
  6. (gdb) r
  7. Starting program: /home/gxb/桌面/Lab2/bomb/bomb
  8. Welcome to my fiendish little bomb. You have 6 phases with
  9. which to blow yourself up. Have a nice day!
  10. Border relations with Canada have never been better.
  11. Phase 1 defused. How about the next one?
  12. 1 2 4 8 16 32
  13. That's number 2. Keep going!
  14. 1 311
  15. Halfway there!
  16. 0 0
  17. So you got that one. Try this one.
  18. IONEFG
  19. Good work! On to the next...
  20. 4 3 2 1 6 5
  21. Breakpoint 4, 0x00000000004015fa in phase_defused ()
  22. (gdb) x/s 0x603870
  23. 0x603870 <input_strings+240>: "0 0"

这样我们可以看到,0x603870的串即输入源,就算phase_4中输入的答案(可以输入phase_4的其它答案确认)。
因为都是用sscanf格式化读入,所以输入phase_4的答案时,输入0 0 DrEvil(或7 0 DrEvil...)不影响答案,且在phase_6结束时可进入secret_phase
进入后输入上面的答案就好了。

答案

phase_4中输入phase_4的答案加字符串DrEvil,可在最后进入secret_phase
secret_phase的答案为

secret_phase汇编

  1. 0000000000401242 <secret_phase>:
  2. 401242: 53 push %rbx
  3. 401243: e8 56 02 00 00 callq 40149e <read_line>
  4. 401248: ba 0a 00 00 00 mov $0xa,%edx ;rdx=10
  5. 40124d: be 00 00 00 00 mov $0x0,%esi ;rsi=0
  6. 401252: 48 89 c7 mov %rax,%rdi ;rdi=rax
  7. 401255: e8 76 f9 ff ff callq 400bd0 <strtol@plt>
  8. 40125a: 48 89 c3 mov %rax,%rbx ;rbx=rax
  9. 40125d: 8d 40 ff lea -0x1(%rax),%eax ;rax--
  10. 401260: 3d e8 03 00 00 cmp $0x3e8,%eax ;cmp rax:0x3e8(1000)
  11. 401265: 76 05 jbe 40126c <secret_phase+0x2a> ;if(rax<=0x3e8) to L1 ->cond.
  12. 401267: e8 ce 01 00 00 callq 40143a <explode_bomb>
  13. 40126c: 89 de mov %ebx,%esi ;.L1: rsi=rbx
  14. 40126e: bf f0 30 60 00 mov $0x6030f0,%edi ;rdi=0x6030f0
  15. 401273: e8 8c ff ff ff callq 401204 <fun7>
  16. 401278: 83 f8 02 cmp $0x2,%eax ;cmp rax:2
  17. 40127b: 74 05 je 401282 <secret_phase+0x40> ;if(rax==2) to L2 ->cond.
  18. 40127d: e8 b8 01 00 00 callq 40143a <explode_bomb>
  19. 401282: bf 38 24 40 00 mov $0x402438,%edi ;.L2: rdi=0x402438
  20. 401287: e8 84 f8 ff ff callq 400b10 <puts@plt>
  21. 40128c: e8 33 03 00 00 callq 4015c4 <phase_defused>
  22. 401291: 5b pop %rbx
  23. 401292: c3 retq
  24. 401293: 90 nop
  25. 401294: 90 nop
  26. 401295: 90 nop
  27. 401296: 90 nop
  28. 401297: 90 nop
  29. 401298: 90 nop
  30. 401299: 90 nop
  31. 40129a: 90 nop
  32. 40129b: 90 nop
  33. 40129c: 90 nop
  34. 40129d: 90 nop
  35. 40129e: 90 nop
  36. 40129f: 90 nop

fun7汇编

  1. 0000000000401204 <fun7>:
  2. 401204: 48 83 ec 08 sub $0x8,%rsp ;rsp-=8
  3. 401208: 48 85 ff test %rdi,%rdi ;cmp rdi:0
  4. 40120b: 74 2b je 401238 <fun7+0x34> ;if(rdi==0) to L2
  5. 40120d: 8b 17 mov (%rdi),%edx ;rdx=(rdi)
  6. 40120f: 39 f2 cmp %esi,%edx ;cmp rdx:rsi
  7. 401211: 7e 0d jle 401220 <fun7+0x1c> ;if(rdx<=rsi) to L1
  8. 401213: 48 8b 7f 08 mov 0x8(%rdi),%rdi ;rdi=(rdi+8)
  9. 401217: e8 e8 ff ff ff callq 401204 <fun7> ;fun7(rdi,rsi)
  10. 40121c: 01 c0 add %eax,%eax ;rax*=2
  11. 40121e: eb 1d jmp 40123d <fun7+0x39> ;to L3
  12. 401220: b8 00 00 00 00 mov $0x0,%eax ;.L1: rax=0
  13. 401225: 39 f2 cmp %esi,%edx ;cmp rdx:rsi
  14. 401227: 74 14 je 40123d <fun7+0x39> ;if(rdx==rsi) to L3
  15. 401229: 48 8b 7f 10 mov 0x10(%rdi),%rdi ;rdi=(rdi+16)
  16. 40122d: e8 d2 ff ff ff callq 401204 <fun7> ;fun7(rdi,rsi)
  17. 401232: 8d 44 00 01 lea 0x1(%rax,%rax,1),%eax ;rax=2*rax+1
  18. 401236: eb 05 jmp 40123d <fun7+0x39> ;to L3
  19. 401238: b8 ff ff ff ff mov $0xffffffff,%eax ;.L2: rax=-1
  20. 40123d: 48 83 c4 08 add $0x8,%rsp ;.L3: rsp+=8
  21. 401241: c3 retq

phase_defused汇编

  1. 00000000004015c4 <phase_defused>:
  2. 4015c4: 48 83 ec 78 sub $0x78,%rsp
  3. 4015c8: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
  4. 4015cf: 00 00
  5. 4015d1: 48 89 44 24 68 mov %rax,0x68(%rsp)
  6. 4015d6: 31 c0 xor %eax,%eax
  7. 4015d8: 83 3d 81 21 20 00 06 cmpl $0x6,0x202181(%rip) # 603760 <num_input_strings>
  8. 4015df: 75 5e jne 40163f <phase_defused+0x7b> ;if((rip+0x202181)!=6) to L2
  9. 4015e1: 4c 8d 44 24 10 lea 0x10(%rsp),%r8 ;r8=rsp+16
  10. 4015e6: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx ;rcx=rsp+12
  11. 4015eb: 48 8d 54 24 08 lea 0x8(%rsp),%rdx ;rdx=rsp+8
  12. 4015f0: be 19 26 40 00 mov $0x402619,%esi ;rsi=0x402619
  13. 4015f5: bf 70 38 60 00 mov $0x603870,%edi ;rdi=0x603870
  14. 4015fa: e8 f1 f5 ff ff callq 400bf0 <__isoc99_sscanf@plt> ;sscanf(rdi,rsi,rdx,rcx,r8,...)
  15. 4015ff: 83 f8 03 cmp $0x3,%eax ;cmp rax:3
  16. 401602: 75 31 jne 401635 <phase_defused+0x71> ;if(rax!=3) to L1 rax==3->cond.
  17. 401604: be 22 26 40 00 mov $0x402622,%esi ;rsi=0x402622
  18. 401609: 48 8d 7c 24 10 lea 0x10(%rsp),%rdi ;rdi=rsp+16
  19. 40160e: e8 25 fd ff ff callq 401338 <strings_not_equal>
  20. 401613: 85 c0 test %eax,%eax ;cmp rax:0
  21. 401615: 75 1e jne 401635 <phase_defused+0x71> ;if(rax!=0) to L1 rax==0(strings equal)->cond.
  22. 401617: bf f8 24 40 00 mov $0x4024f8,%edi
  23. 40161c: e8 ef f4 ff ff callq 400b10 <puts@plt>
  24. 401621: bf 20 25 40 00 mov $0x402520,%edi
  25. 401626: e8 e5 f4 ff ff callq 400b10 <puts@plt>
  26. 40162b: b8 00 00 00 00 mov $0x0,%eax
  27. 401630: e8 0d fc ff ff callq 401242 <secret_phase>
  28. 401635: bf 58 25 40 00 mov $0x402558,%edi ;.L1:
  29. 40163a: e8 d1 f4 ff ff callq 400b10 <puts@plt>
  30. 40163f: 48 8b 44 24 68 mov 0x68(%rsp),%rax ;.L2:
  31. 401644: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
  32. 40164b: 00 00
  33. 40164d: 74 05 je 401654 <phase_defused+0x90>
  34. 40164f: e8 dc f4 ff ff callq 400b30 <__stack_chk_fail@plt>
  35. 401654: 48 83 c4 78 add $0x78,%rsp
  36. 401658: c3 retq
  37. 401659: 90 nop
  38. 40165a: 90 nop
  39. 40165b: 90 nop
  40. 40165c: 90 nop
  41. 40165d: 90 nop
  42. 40165e: 90 nop
  43. 40165f: 90 nop

总结

刚开始做phase_2的时候感觉特别难(主要是read_six_numbers中的参数问题和gdb),但花了两个多小时看懂之后,再整理一下gdb命令(主要是(gdb) x/<n/f/u> <address>),后面感觉就简单了,就是很绕/花时间。

基本做法就,先对每条汇编简单写一下表达式,再一步步转成goto的C代码,再转成for/while/do while循环或是递归,再理解意思。
这样做phase_3,phase_4,phase_5都比较简单。
phase_6的汇编很长要注意别写错寄存器名以及是不是指针,C代码要写对。
phase_6secret_phase要用(gdb) x查看对应位置的若干个值,并判断出相应结构(链表或树...)。知道结构后再模拟C代码就ok。

做完phase才算真会了教材第三章。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注