官网

http://csapp.cs.cmu.edu/3e/labs.html

Students implement simple logical, two’s complement, and floating point functions, but using a highly restricted subset of C. For example, they might be asked to compute the absolute value of a number using only bit-level operations and straightline code. This lab helps students understand the bit-level representations of C data types and the bit-level behavior of the operations on data.

参考

《深入理解计算机系统》(CSAPP)实验二 —— Bomb Lab

csapp(3e)的bomblab的phase_6详解(没有详细到逐行解析的程度)

准备工作

bomb.c是本实验的源码,一共有phase_1 ~ phase_6 6个炸弹,从命令行输入的内容必须要和phase函数中的一致,否则就会爆炸退出程序。因为phase函数并没有给出源码,所以无法得知其期望的字符串是什么。

bomb文件是编译完成的文件,反汇编下这个文件,从反汇编推算下其内容是什么。

objdump -d bomb > bomb.asm

从main函数开始分析。

0000000000400da0 <main>:
400da0: 53 push %rbx # saved register
400da1: 83 ff 01 cmp $0x1,%edi # if(argc==1)
400da4: 75 10 jne 400db6 <main+0x16> # 不相等 跳到400db6
400da6: 48 8b 05 9b 29 20 00 mov 0x20299b(%rip),%rax # 603748 <stdin@@GLIBC_2.2.5>
400dad: 48 89 05 b4 29 20 00 mov %rax,0x2029b4(%rip) # 603768 <infile>
400db4: eb 63 jmp 400e19 <main+0x79> # 跳到initialize_bomb
400db6: 48 89 f3 mov %rsi,%rbx
400db9: 83 ff 02 cmp $0x2,%edi # if(argc==2)
400dbc: 75 3a jne 400df8 <main+0x58> # 不相等 跳到400df8
400dbe: 48 8b 7e 08 mov 0x8(%rsi),%rdi
400dc2: be b4 22 40 00 mov $0x4022b4,%esi
400dc7: e8 44 fe ff ff callq 400c10 <fopen@plt>
400dcc: 48 89 05 95 29 20 00 mov %rax,0x202995(%rip) # 603768 <infile>
400dd3: 48 85 c0 test %rax,%rax
400dd6: 75 41 jne 400e19 <main+0x79> # 跳到initialize_bomb
400dd8: 48 8b 4b 08 mov 0x8(%rbx),%rcx
400ddc: 48 8b 13 mov (%rbx),%rdx
400ddf: be b6 22 40 00 mov $0x4022b6,%esi
400de4: bf 01 00 00 00 mov $0x1,%edi
400de9: e8 12 fe ff ff callq 400c00 <__printf_chk@plt>
400dee: bf 08 00 00 00 mov $0x8,%edi
400df3: e8 28 fe ff ff callq 400c20 <exit@plt>
400df8: 48 8b 16 mov (%rsi),%rdx
400dfb: be d3 22 40 00 mov $0x4022d3,%esi
400e00: bf 01 00 00 00 mov $0x1,%edi
400e05: b8 00 00 00 00 mov $0x0,%eax
400e0a: e8 f1 fd ff ff callq 400c00 <__printf_chk@plt>
400e0f: bf 08 00 00 00 mov $0x8,%edi
400e14: e8 07 fe ff ff callq 400c20 <exit@plt>
400e19: e8 84 05 00 00 callq 4013a2 <initialize_bomb>
400e1e: bf 38 23 40 00 mov $0x402338,%edi
400e23: e8 e8 fc ff ff callq 400b10 <puts@plt>
400e28: bf 78 23 40 00 mov $0x402378,%edi
400e2d: e8 de fc ff ff callq 400b10 <puts@plt>
400e32: e8 67 06 00 00 callq 40149e <read_line> # 读取输入
400e37: 48 89 c7 mov %rax,%rdi
400e3a: e8 a1 00 00 00 callq 400ee0 <phase_1> # 调用phase_1
400e3f: e8 80 07 00 00 callq 4015c4 <phase_defused> # 接触phase_1
400e44: bf a8 23 40 00 mov $0x4023a8,%edi
400e49: e8 c2 fc ff ff callq 400b10 <puts@plt>
400e4e: e8 4b 06 00 00 callq 40149e <read_line>
400e53: 48 89 c7 mov %rax,%rdi
400e56: e8 a1 00 00 00 callq 400efc <phase_2>
400e5b: e8 64 07 00 00 callq 4015c4 <phase_defused>
400e60: bf ed 22 40 00 mov $0x4022ed,%edi
400e65: e8 a6 fc ff ff callq 400b10 <puts@plt>
400e6a: e8 2f 06 00 00 callq 40149e <read_line>
400e6f: 48 89 c7 mov %rax,%rdi
400e72: e8 cc 00 00 00 callq 400f43 <phase_3>
400e77: e8 48 07 00 00 callq 4015c4 <phase_defused>
400e7c: bf 0b 23 40 00 mov $0x40230b,%edi
400e81: e8 8a fc ff ff callq 400b10 <puts@plt>
400e86: e8 13 06 00 00 callq 40149e <read_line>
400e8b: 48 89 c7 mov %rax,%rdi
400e8e: e8 79 01 00 00 callq 40100c <phase_4>
400e93: e8 2c 07 00 00 callq 4015c4 <phase_defused>
400e98: bf d8 23 40 00 mov $0x4023d8,%edi
400e9d: e8 6e fc ff ff callq 400b10 <puts@plt>
400ea2: e8 f7 05 00 00 callq 40149e <read_line>
400ea7: 48 89 c7 mov %rax,%rdi
400eaa: e8 b3 01 00 00 callq 401062 <phase_5>
400eaf: e8 10 07 00 00 callq 4015c4 <phase_defused>
400eb4: bf 1a 23 40 00 mov $0x40231a,%edi
400eb9: e8 52 fc ff ff callq 400b10 <puts@plt>
400ebe: e8 db 05 00 00 callq 40149e <read_line>
400ec3: 48 89 c7 mov %rax,%rdi
400ec6: e8 29 02 00 00 callq 4010f4 <phase_6>
400ecb: e8 f4 06 00 00 callq 4015c4 <phase_defused>
400ed0: b8 00 00 00 00 mov $0x0,%eax
400ed5: 5b pop %rbx
400ed6: c3 retq

说明主要还是看phase_1~phase_6函数。

phase_1

0000000000400ee0 <phase_1>:
400ee0: 48 83 ec 08 sub $0x8,%rsp # 栈指针 0x8
400ee4: be 00 24 40 00 mov $0x402400,%esi # 传参
400ee9: e8 4a 04 00 00 callq 401338 <strings_not_equal> # 判断字符串是否相等
400eee: 85 c0 test %eax,%eax # 测试返回结果
400ef0: 74 05 je 400ef7 <phase_1+0x17>
400ef2: e8 43 05 00 00 callq 40143a <explode_bomb> # 如果结果不对,调用炸弹爆炸
400ef7: 48 83 c4 08 add $0x8,%rsp # 释放空间
400efb: c3 retq

在内存为0x402400的地方存储的就是程序期望我们输入的字符串,那么利用GDB工具调试下代码,打印0x402400处的值看下。得到正解。

Border relations with Canada have never been better.

phase_2

0000000000400efc <phase_2>:
400efc: 55 push %rbp
400efd: 53 push %rbx
400efe: 48 83 ec 28 sub $0x28,%rsp # 栈指针 0x28
400f02: 48 89 e6 mov %rsp,%rsi
400f05: e8 52 05 00 00 callq 40145c <read_six_numbers> # 调用 读入六个数字
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp) # 比较1和(%rsp)的值
400f0e: 74 20 je 400f30 <phase_2+0x34> # 相等就继续400f30
400f10: e8 25 05 00 00 callq 40143a <explode_bomb> # explode_bomb
400f15: eb 19 jmp 400f30 <phase_2+0x34> # 跳到400f30
400f17: 8b 43 fc mov -0x4(%rbx),%eax
400f1a: 01 c0 add %eax,%eax
400f1c: 39 03 cmp %eax,(%rbx)
400f1e: 74 05 je 400f25 <phase_2+0x29> # 相等就继续400f25
400f20: e8 15 05 00 00 callq 40143a <explode_bomb> # explode_bomb
400f25: 48 83 c3 04 add $0x4,%rbx
400f29: 48 39 eb cmp %rbp,%rbx
400f2c: 75 e9 jne 400f17 <phase_2+0x1b> # 不相等就跳到400f17
400f2e: eb 0c jmp 400f3c <phase_2+0x40> # 跳到400f3c,结束phase_2
400f30: 48 8d 5c 24 04 lea 0x4(%rsp),%rbx
400f35: 48 8d 6c 24 18 lea 0x18(%rsp),%rbp
400f3a: eb db jmp 400f17 <phase_2+0x1b> # 跳到400f17
400f3c: 48 83 c4 28 add $0x28,%rsp # 释放空间
400f40: 5b pop %rbx
400f41: 5d pop %rbp
400f42: c3 retq

伪代码如下:

phase_2:
if((%rsp)==1) //第一个数一定是1
{
goto 400f30;
400f17:
%eax = (%rbx - 4); //%eax被赋予(%rbx - 4)地址的数据,最初为1
%eax *= 2; //%eax翻倍
if((%rbx) == %eax)
%rbx += 0x4; //+4 下一地址
if(%rbx == %rbp) return; //地址到达%rsp + 0x18
else goto 400f17;
}
else explode_bomb;
}
else explode_bomb;


400f30:
%rbx = %rsp + 0x4;
%rbp = %rsp + 0x18; #结束条件存放在%rsp + 0x18
goto 400f17;

程序执行顺序大概是:

设%rsp = 0x400000, 0x400000的值是1
则%rbx = 0x400004, %rbp = 0x400018
%eax = 0x400000的值 = 0x1
%eax = 0x2
0x400004的数据 == 0x2
%rbx = 0x400008
0x400008 != 0x400018 继续
%eax = 0x400004的值 = 0x2
%eax = 0x4
0x400008的数据 == 0x4
%rbx = 0x40000c
......
继续
(第六次)0x400018 == 0x400018 return

所以一共输入六个数,第一个数为1,后面的数依次为前一个数的两倍。

1 2 4 8 16 32

phase_3

0000000000400f43 <phase_3>:
400f43: 48 83 ec 18 sub $0x18,%rsp # 栈指针 0x18
400f47: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx
400f4c: 48 8d 54 24 08 lea 0x8(%rsp),%rdx
400f51: be cf 25 40 00 mov $0x4025cf,%esi
400f56: b8 00 00 00 00 mov $0x0,%eax
400f5b: e8 90 fc ff ff callq 400bf0 <__isoc99_sscanf@plt>
400f60: 83 f8 01 cmp $0x1,%eax
400f63: 7f 05 jg 400f6a <phase_3+0x27> # 大于 跳到400f6a
400f65: e8 d0 04 00 00 callq 40143a <explode_bomb>
400f6a: 83 7c 24 08 07 cmpl $0x7,0x8(%rsp) # 比较7和(%rdx)
400f6f: 77 3c ja 400fad <phase_3+0x6a> # explode_bomb
400f71: 8b 44 24 08 mov 0x8(%rsp),%eax
400f75: ff 24 c5 70 24 40 00 jmpq *0x402470(,%rax,8) # 间接跳转
400f7c: b8 cf 00 00 00 mov $0xcf,%eax
400f81: eb 3b jmp 400fbe <phase_3+0x7b> # 跳到400fbe
400f83: b8 c3 02 00 00 mov $0x2c3,%eax
400f88: eb 34 jmp 400fbe <phase_3+0x7b>
400f8a: b8 00 01 00 00 mov $0x100,%eax
400f8f: eb 2d jmp 400fbe <phase_3+0x7b>
400f91: b8 85 01 00 00 mov $0x185,%eax
400f96: eb 26 jmp 400fbe <phase_3+0x7b>
400f98: b8 ce 00 00 00 mov $0xce,%eax
400f9d: eb 1f jmp 400fbe <phase_3+0x7b>
400f9f: b8 aa 02 00 00 mov $0x2aa,%eax
400fa4: eb 18 jmp 400fbe <phase_3+0x7b>
400fa6: b8 47 01 00 00 mov $0x147,%eax
400fab: eb 11 jmp 400fbe <phase_3+0x7b>
400fad: e8 88 04 00 00 callq 40143a <explode_bomb>
400fb2: b8 00 00 00 00 mov $0x0,%eax
400fb7: eb 05 jmp 400fbe <phase_3+0x7b>
400fb9: b8 37 01 00 00 mov $0x137,%eax
400fbe: 3b 44 24 0c cmp 0xc(%rsp),%eax
400fc2: 74 05 je 400fc9 <phase_3+0x86>
400fc4: e8 71 04 00 00 callq 40143a <explode_bomb>
400fc9: 48 83 c4 18 add $0x18,%rsp
400fcd: c3 retq

0000000000400fce <func4>:
400fce: 48 83 ec 08 sub $0x8,%rsp
400fd2: 89 d0 mov %edx,%eax
400fd4: 29 f0 sub %esi,%eax
400fd6: 89 c1 mov %eax,%ecx
400fd8: c1 e9 1f shr $0x1f,%ecx
400fdb: 01 c8 add %ecx,%eax
400fdd: d1 f8 sar %eax
400fdf: 8d 0c 30 lea (%rax,%rsi,1),%ecx
400fe2: 39 f9 cmp %edi,%ecx
400fe4: 7e 0c jle 400ff2 <func4+0x24>
400fe6: 8d 51 ff lea -0x1(%rcx),%edx
400fe9: e8 e0 ff ff ff callq 400fce <func4>
400fee: 01 c0 add %eax,%eax
400ff0: eb 15 jmp 401007 <func4+0x39> # 结束
400ff2: b8 00 00 00 00 mov $0x0,%eax
400ff7: 39 f9 cmp %edi,%ecx
400ff9: 7d 0c jge 401007 <func4+0x39> # 结束
400ffb: 8d 71 01 lea 0x1(%rcx),%esi
400ffe: e8 cb ff ff ff callq 400fce <func4>
401003: 8d 44 00 01 lea 0x1(%rax,%rax,1),%eax
401007: 48 83 c4 08 add $0x8,%rsp
40100b: c3 retq

伪代码如下:

phase_3:
%rcx = (%rsp) + 0xc
%rdx = (%rsp) + 0x8
if(%eax > 1)
{
if((%rdx) <= 7) //无符号数比较
{
%eax = (%rdx);
goto 0x402470 + %rax * 8所指向的地址;
%eax = 某个值;
if(%eax == (%rcx)) return;
else explode_bomb;
}
else explode_bomb;
}
else explode_bomb;
(gdb) x/6a 0x402470
0x402470: 0x400f7c <phase_3+57> 0x400fb9 <phase_3+118>
0x402480: 0x400f83 <phase_3+64> 0x400f8a <phase_3+71>
0x402490: 0x400f91 <phase_3+78> 0x400f98 <phase_3+85>

使用gdb检查 0x402470处的值为0x400f7c

说明第一个参数为0时,程序跳转到0x400f7c,%eax = 0xcf = 207

因此,0 207为一组解。

0 207(非唯一解,还有1 311, 2 707, 3 256, 4 389, 5 206, 6 682, 7 327)

phase_4

000000000040100c <phase_4>:
40100c: 48 83 ec 18 sub $0x18,%rsp
401010: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx # 第二个参数
401015: 48 8d 54 24 08 lea 0x8(%rsp),%rdx # 第一个参数
40101a: be cf 25 40 00 mov $0x4025cf,%esi
40101f: b8 00 00 00 00 mov $0x0,%eax
401024: e8 c7 fb ff ff callq 400bf0 <__isoc99_sscanf@plt>
401029: 83 f8 02 cmp $0x2,%eax
40102c: 75 07 jne 401035 <phase_4+0x29> # %eax!=2则explode_bomb
40102e: 83 7c 24 08 0e cmpl $0xe,0x8(%rsp)
401033: 76 05 jbe 40103a <phase_4+0x2e>
401035: e8 00 04 00 00 callq 40143a <explode_bomb>
40103a: ba 0e 00 00 00 mov $0xe,%edx
40103f: be 00 00 00 00 mov $0x0,%esi
401044: 8b 7c 24 08 mov 0x8(%rsp),%edi
401048: e8 81 ff ff ff callq 400fce <func4>
40104d: 85 c0 test %eax,%eax
40104f: 75 07 jne 401058 <phase_4+0x4c>
401051: 83 7c 24 0c 00 cmpl $0x0,0xc(%rsp)
401056: 74 05 je 40105d <phase_4+0x51>
401058: e8 dd 03 00 00 callq 40143a <explode_bomb>
40105d: 48 83 c4 18 add $0x18,%rsp
401061: c3 retq

伪代码:

phase_4:
%rcx = (%rsp) + 0xc #第二个参数
%rdx = (%rsp) + 0x8 #第一个参数
if(%eax == 0x2)
{
if((%rdx) <= 0xe) //第一个参数要<14
{
%edx = 0xe; //传参
%esi = 0x0;
%edi = (%rdx);
goto func4; //三个参数,(%rdx),0,14
if(%eax == 0) //func4的返回值为0
{
if((%rcx)==0x0) return; //第二个参数为0
else explode_bomb;
}
else explode_bomb;
}
else explode_bomb;
}
else explode_bomb;

//x in %rdi,y in %rsi,z in %rdx,t in %rax,k in %ecx
//x未知 y=0 z=14,需要确保func4执行完后t=0
func4:
int t = z - y = 14;
int k = t >> 31;
t = (t + k) >> 1;
k = t + y;
if(k <= x)
{
t = 0;
if(k >= x) return;
y = k + 1;
goto func4;
t = 2*t + 1;
return;
}
else
{
z = k - 1;
goto func4;
t *= 2;
}

//x=k=7即可,不用进递归。

如上分析,答案为7 0。

7 0

phase_5

0000000000401062 <phase_5>:
401062: 53 push %rbx
401063: 48 83 ec 20 sub $0x20,%rsp
401067: 48 89 fb mov %rdi,%rbx
40106a: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
401071: 00 00
401073: 48 89 44 24 18 mov %rax,0x18(%rsp)
401078: 31 c0 xor %eax,%eax # %eax=0
40107a: e8 9c 02 00 00 callq 40131b <string_length>
40107f: 83 f8 06 cmp $0x6,%eax # 字符串长度为6
401082: 74 4e je 4010d2 <phase_5+0x70>
401084: e8 b1 03 00 00 callq 40143a <explode_bomb>
401089: eb 47 jmp 4010d2 <phase_5+0x70>
40108b: 0f b6 0c 03 movzbl (%rbx,%rax,1),%ecx
40108f: 88 0c 24 mov %cl,(%rsp)
401092: 48 8b 14 24 mov (%rsp),%rdx
401096: 83 e2 0f and $0xf,%edx
401099: 0f b6 92 b0 24 40 00 movzbl 0x4024b0(%rdx),%edx
4010a0: 88 54 04 10 mov %dl,0x10(%rsp,%rax,1)
4010a4: 48 83 c0 01 add $0x1,%rax
4010a8: 48 83 f8 06 cmp $0x6,%rax
4010ac: 75 dd jne 40108b <phase_5+0x29>
4010ae: c6 44 24 16 00 movb $0x0,0x16(%rsp)
4010b3: be 5e 24 40 00 mov $0x40245e,%esi
4010b8: 48 8d 7c 24 10 lea 0x10(%rsp),%rdi
4010bd: e8 76 02 00 00 callq 401338 <strings_not_equal>
4010c2: 85 c0 test %eax,%eax
4010c4: 74 13 je 4010d9 <phase_5+0x77>
4010c6: e8 6f 03 00 00 callq 40143a <explode_bomb>
4010cb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
4010d0: eb 07 jmp 4010d9 <phase_5+0x77>
4010d2: b8 00 00 00 00 mov $0x0,%eax
4010d7: eb b2 jmp 40108b <phase_5+0x29>
4010d9: 48 8b 44 24 18 mov 0x18(%rsp),%rax
4010de: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
4010e5: 00 00
4010e7: 74 05 je 4010ee <phase_5+0x8c> # return
4010e9: e8 42 fa ff ff callq 400b30 <__stack_chk_fail@plt>
4010ee: 48 83 c4 20 add $0x20,%rsp
4010f2: 5b pop %rbx
4010f3: c3 retq

伪代码:

if(strlen(str)==6)
{
%eax = 0;
do{
%ecx = (%rbx + %rax); //取第%rax个字符
(%rsp) = %cl; //取低八位到%rdx中
%rdx = (%rsp);
%edx ^= 0xf; //只保留低四位
%edx = (%rdx + 0x4024b0); //偏移为%rdx,基地址为0x4024b0,取32位值
(%rsp + %rax + 0x10) = %dl; //储存所取值的低八位
%rax += 1; //循环次数加一
} while(%rax != 6); //必须循环6次
(%rsp+0x16) = 0x0; //储存'\0'
%esi = 0x40245e; //%esi指向储存的flyers字符串
%rdi = (%rsp + 0x10); //%rdi指向%rsp + 0x10保存的
if(strings_not_equal) explode_bomb;
else{
%rax = (%rsp + 18);
%rax ^fs:0x28;
if(0) return;
}
}
else explode_bomb;

字符串六个字符保存在%rsp + 0x10 ~ %rsp + 0x15。

(gdb) x/32xb 0x4024b0
0x4024b0 <array.3449>: 0x6d 0x61 0x64 0x75 0x69 0x65 0x72 0x73
0x4024b8 <array.3449+8>: 0x6e 0x66 0x6f 0x74 0x76 0x62 0x79 0x6c

# 0x40245e处是flyers字符串
(gdb) x/7c 0x40245e
0x40245e: 102 'f' 108 'l' 121 'y' 101 'e' 114 'r' 115 's' 0 '\000'

(gdb) x/7xb 0x40245e
0x40245e: 0x66 0x6c 0x79 0x65 0x72 0x73 0x00
# flyers对应的ascii值 0x66 0x6c 0x79 0x65 0x72 0x73

基于0x4024b0的对应偏移量为0x9 0xF 0xE 0x5 0x6 0x7。

即六个字符的低4bit分别为0x9 0xF 0xE 0x5 0x6 0x7。

若输入为大写字母,将低4bit的值加上0x40,获得输入字符串IONEFG。

若输入为小写字母,将低4bit的值加上0x60,获得输入字符串ionefg。

IONEFG(或ionefg)

phase_6

00000000004010f4 <phase_6>:
4010f4: 41 56 push %r14
4010f6: 41 55 push %r13
4010f8: 41 54 push %r12
4010fa: 55 push %rbp
4010fb: 53 push %rbx
4010fc: 48 83 ec 50 sub $0x50,%rsp # 开辟空间
401100: 49 89 e5 mov %rsp,%r13
401103: 48 89 e6 mov %rsp,%rsi
401106: e8 51 03 00 00 callq 40145c <read_six_numbers> # 读入6个值,%rdi %rsi %rdx %rcx %r8 %r9
#################################################################
40110b: 49 89 e6 mov %rsp,%r14
40110e: 41 bc 00 00 00 00 mov $0x0,%r12d
401114: 4c 89 ed mov %r13,%rbp
401117: 41 8b 45 00 mov 0x0(%r13),%eax
40111b: 83 e8 01 sub $0x1,%eax
40111e: 83 f8 05 cmp $0x5,%eax # 数字<=6
401121: 76 05 jbe 401128 <phase_6+0x34>
401123: e8 12 03 00 00 callq 40143a <explode_bomb>
401128: 41 83 c4 01 add $0x1,%r12d
40112c: 41 83 fc 06 cmp $0x6,%r12d
401130: 74 21 je 401153 <phase_6+0x5f> # 程序出口
401132: 44 89 e3 mov %r12d,%ebx
401135: 48 63 c3 movslq %ebx,%rax
401138: 8b 04 84 mov (%rsp,%rax,4),%eax
40113b: 39 45 00 cmp %eax,0x0(%rbp)
40113e: 75 05 jne 401145 <phase_6+0x51>
401140: e8 f5 02 00 00 callq 40143a <explode_bomb>
401145: 83 c3 01 add $0x1,%ebx
401148: 83 fb 05 cmp $0x5,%ebx
40114b: 7e e8 jle 401135 <phase_6+0x41>
40114d: 49 83 c5 04 add $0x4,%r13
401151: eb c1 jmp 401114 <phase_6+0x20>
#################################################################
401153: 48 8d 74 24 18 lea 0x18(%rsp),%rsi # %rsi = (%rsp+0x18)的地址 第六个数的地址
401158: 4c 89 f0 mov %r14,%rax # %rax = %r14;
40115b: b9 07 00 00 00 mov $0x7,%ecx #
401160: 89 ca mov %ecx,%edx #
401162: 2b 10 sub (%rax),%edx #
401164: 89 10 mov %edx,(%rax) # (%rax) = 7-(%rax);
401166: 48 83 c0 04 add $0x4,%rax # %rax += 4;
40116a: 48 39 f0 cmp %rsi,%rax
40116d: 75 f1 jne 401160 <phase_6+0x6c>
#################################################################
40116f: be 00 00 00 00 mov $0x0,%esi
401174: eb 21 jmp 401197 <phase_6+0xa3>
401176: 48 8b 52 08 mov 0x8(%rdx),%rdx # 指向链表的下一个节点的首地址
40117a: 83 c0 01 add $0x1,%eax
40117d: 39 c8 cmp %ecx,%eax
40117f: 75 f5 jne 401176 <phase_6+0x82> # 不相等,继续遍历链表,最终 %rdx 指向链表的第 %ecx 个节点
401181: eb 05 jmp 401188 <phase_6+0x94>
401183: ba d0 32 60 00 mov $0x6032d0,%edx
401188: 48 89 54 74 20 mov %rdx,0x20(%rsp,%rsi,2)
40118d: 48 83 c6 04 add $0x4,%rsi
401191: 48 83 fe 18 cmp $0x18,%rsi
401195: 74 14 je 4011ab <phase_6+0xb7>
401197: 8b 0c 34 mov (%rsp,%rsi,1),%ecx
40119a: 83 f9 01 cmp $0x1,%ecx # %ecx是从第1个数到第6个数(7-之后)
40119d: 7e e4 jle 401183 <phase_6+0x8f>
40119f: b8 01 00 00 00 mov $0x1,%eax
4011a4: ba d0 32 60 00 mov $0x6032d0,%edx # 进入链表
4011a9: eb cb jmp 401176 <phase_6+0x82>
#################################################################
4011ab: 48 8b 5c 24 20 mov 0x20(%rsp),%rbx # 第一个节点的地址
4011b0: 48 8d 44 24 28 lea 0x28(%rsp),%rax # 第二个节点的地址
4011b5: 48 8d 74 24 50 lea 0x50(%rsp),%rsi # 结束循环的地址
4011ba: 48 89 d9 mov %rbx,%rcx # %rcx = 前一个节点的首地址
4011bd: 48 8b 10 mov (%rax),%rdx # %rdx = 后一个节点的首地址
4011c0: 48 89 51 08 mov %rdx,0x8(%rcx) # pNext重新链接
4011c4: 48 83 c0 08 add $0x8,%rax
4011c8: 48 39 f0 cmp %rsi,%rax
4011cb: 74 05 je 4011d2 <phase_6+0xde> # 出口
4011cd: 48 89 d1 mov %rdx,%rcx # 继续循环
4011d0: eb eb jmp 4011bd <phase_6+0xc9>
#################################################################
4011d2: 48 c7 42 08 00 00 00 movq $0x0,0x8(%rdx)
4011d9: 00
4011da: bd 05 00 00 00 mov $0x5,%ebp # 固定五次循环
4011df: 48 8b 43 08 mov 0x8(%rbx),%rax # pNext
4011e3: 8b 00 mov (%rax),%eax # 第二个节点的值
4011e5: 39 03 cmp %eax,(%rbx) # 比较两个节点中第一个字段值的大小
4011e7: 7d 05 jge 4011ee <phase_6+0xfa>
4011e9: e8 4c 02 00 00 callq 40143a <explode_bomb> # <就爆炸
4011ee: 48 8b 5b 08 mov 0x8(%rbx),%rbx # >=
4011f2: 83 ed 01 sub $0x1,%ebp
4011f5: 75 e8 jne 4011df <phase_6+0xeb>
#################################################################
4011f7: 48 83 c4 50 add $0x50,%rsp
4011fb: 5b pop %rbx
4011fc: 5d pop %rbp
4011fd: 41 5c pop %r12
4011ff: 41 5d pop %r13
401201: 41 5e pop %r14
401203: c3 retq

Tips

movl是以寄存器运算结果为游标,访问内存获得该地址的值,然后给目标寄存器赋值。

leal是获得寄存器的运算结果,很多时候会遇到用来完成类似于mips或者arm上三元运算的工作,一步完成加法。

代码分段

0x4010f4 -> 0x401106

这段代码是把输入的6个数字读到栈上,存在rsp到rsp+0x18(6个数字共占了24个字节)。

0x40110b -> 0x401151

这段代码是循环检查6个数字的初值,两个条件:1. 每个数字都小于6,2. 每个数字都不相等。

0x401153 -> 0x40116d

这段代码是将每个数字都用7减一遍,6个数由a[i]变成了7-a[i],例如:

a[i]: 4, 1, 2, 5, 6, 3
7-a[i]: 3, 6, 5, 2, 1, 4

0x40116f -> 0x4011a9

这段代码十分复杂,可以通过代入特定值的方式来了解其行为,其功能是根据7-a[i]的值,将链表的node的首地址在栈中排序,例如:

7-a[i]: 3, 6, 5, 2, 1, 4

node的排序: node3, node6, node5, node2, node1, node4,也就是赋值给了0x20+%rsp开始的一片区域。

这里值得注意的地方是0x20(%rsp,%rsi,2), 输入6个数是4字节, 现在每个数对应1个8字节的. 如果是1, 存的内容是$0x6032d0.

0x4011ab -> 0x4011d2

基于上一步在栈中的node顺序,将链表中的各个node重新链接起来,通过更改node.pNext来完成,最后一个node的pNext置为0;

0x4011d9 -> 结束

检查在上一步中完成排序的链表,是否是按照值从大到小排列的,如果不是的话,bomb就炸了。

(gdb) x/24xw 0x6032d0
0x6032d0 <node1>: 0x0000014c 0x00000001 0x006032e0 0x00000000
0x6032e0 <node2>: 0x000000a8 0x00000002 0x006032f0 0x00000000
0x6032f0 <node3>: 0x0000039c 0x00000003 0x00603300 0x00000000
0x603300 <node4>: 0x000002b3 0x00000004 0x00603310 0x00000000
0x603310 <node5>: 0x000001dd 0x00000005 0x00603320 0x00000000
0x603320 <node6>: 0x000001bb 0x00000006 0x00000000 0x00000000
# 节点值 下个节点地址值

排序是 3 4 5 6 1 2,7-之前是 4 3 2 1 6 5。

4 3 2 1 6 5

测试结果