实验简介
实验提供二进制可执行文件bomb
和bomb
对应的不完整的bomb.c
源文件.
C
代码告诉我们需要依次输入6
次, 如果输入错误💣爆炸, 实验失败.
实验工具
objdump
- 反汇编bomb
vim
- 查看反汇编代码
gdb
- 单步调试/查看内存和寄存器信息
实验内容
main.c
int main(int argc, char *argv[])
{
char *input;
/* Note to self: remember to port this bomb to Windows and put a
* fantastic GUI on it. */
/* When run with no arguments, the bomb reads its input lines
* from standard input. */
if (argc == 1) {
infile = stdin;
}
/* When run with one argument <file>, the bomb reads from <file>
* until EOF, and then switches to standard input. Thus, as you
* defuse each phase, you can add its defusing string to <file> and
* avoid having to retype it. */
else if (argc == 2) {
if (!(infile = fopen(argv[1], "r"))) {
printf("%s: Error: Couldn't open %s\n", argv[0], argv[1]);
exit(8);
}
}
/* You can't call the bomb with more than 1 command line argument. */
else {
printf("Usage: %s [<input_file>]\n", argv[0]);
exit(8);
}
/* Do all sorts of secret stuff that makes the bomb harder to defuse. */
initialize_bomb();
printf("Welcome to my fiendish little bomb. You have 6 phases with\n");
printf("which to blow yourself up. Have a nice day!\n");
/* Hmm... Six phases must be more secure than one phase! */
input = read_line(); /* Get input */
phase_1(input); /* Run the phase */
phase_defused(); /* Drat! They figured it out!
* Let me know how they did it. */
printf("Phase 1 defused. How about the next one?\n");
/* The second phase is harder. No one will ever figure out
* how to defuse this... */
input = read_line();
phase_2(input);
phase_defused();
printf("That's number 2. Keep going!\n");
/* I guess this is too easy so far. Some more complex code will
* confuse people. */
input = read_line();
phase_3(input);
phase_defused();
printf("Halfway there!\n");
/* Oh yeah? Well, how good is your math? Try on this saucy problem! */
input = read_line();
phase_4(input);
phase_defused();
printf("So you got that one. Try this one.\n");
/* Round and 'round in memory we go, where we stop, the bomb blows! */
input = read_line();
phase_5(input);
phase_defused();
printf("Good work! On to the next...\n");
/* This phase will never be used, since no one will get past the
* earlier ones. But just in case, make this one extra hard. */
input = read_line();
phase_6(input);
phase_defused();
/* Wow, they got it! But isn't something... missing? Perhaps
* something they overlooked? Mua ha ha ha ha! */
return 0;
}
如果bomb
运行时无参数, 则从键盘接受输入; 若有1
个参数, 则将参数对应文件读取作为输入; 无法接受
超过1
个参数.
在经过未知的initialize_bomb()
后, main
每次接受一行输入, 判断是否正确.
解决方案
我们可以用gdb
工具调试bomb
可执行文件, 对于参加csapp
的学生来说, bomb
实验需要在服务器执行, 且
每次💣爆炸都会扣分, 所以需要用gdb
工具在爆炸函数前添加断点. 但我对gdb
还不熟悉, 所以下面
解法是将bomb
反汇编重定向至文件, 用vim
观察汇编代码, 只在某些地方使用gdb
工具.
汇编代码
objdump -d bomb > bomb.s
phase_1
首先来分析phase_1
对应的汇编代码. 可以用vim
的查找命令/Word
查找phase_1
.
下面是phase_1
汇编代码及解释.
0000000000400ee0 <phase_1>:
400ee0: 48 83 ec 08 sub $0x8,%rsp #栈顶指针-1(8字节)
400ee4: be 00 24 40 00 mov $0x402400,%esi #%esi = 地址?
400ee9: e8 4a 04 00 00 callq 401338 <strings_not_equal>#调用函数strings_not_equal
400eee: 85 c0 test %eax,%eax #判断返回值是否为0
400ef0: 74 05 je 400ef7 <phase_1+0x17> #如果返回值为0 跳过爆炸
400ef2: e8 43 05 00 00 callq 40143a <explode_bomb> #返回值不为0 boom!!
400ef7: 48 83 c4 08 add $0x8,%rsp #栈顶指针+1
400efb: c3 retq #退出函数phase_1
现在的问题是strings_not_equal
的过程, 同样的方法查找对应汇编代码. 下面是部分汇编代码.
0000000000401338 <strings_not_equal>:
40133f: 48 89 f5 mov %rsi,%rbp
401363: 3a 45 00 cmp 0x0(%rbp),%al
401366: 74 0a je 401372 <strings_not_equal+0x3a>
401368: eb 25 jmp 40138f <strings_not_equal+0x57>
通过函数名可以猜测函数的作用是比较字符串是否相等, 这里截取关键部分 : 用%al
与phase_1
赋值
给%esi
的地址处比较, 后面的条件跳转和无条件跳转实现C
的if-else
, 相等时函数返回0
.
所以我们可以猜测输入对应地址的字符串则strings_not_equal
返回0
, 函数不boom
, 可以用gdb
查看.
(gdb) x/s 0x402400
0x402400: "Border relations with Canada have never been better."
输入上字符串解决phase_1
.
phase_2
phase_2
汇编代码 :
0000000000400efc <phase_2>:
400efc: 55 push %rbp #保存寄存器内容
400efd: 53 push %rbx
400efe: 48 83 ec 28 sub $0x28,%rsp #栈顶
400f02: 48 89 e6 mov %rsp,%rsi #rsi保存栈顶
400f05: e8 52 05 00 00 callq 40145c <read_six_numbers> #调用函数read_six_numbers
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp) #rsp内容与1比较
400f0e: 74 20 je 400f30 <phase_2+0x34> #相等则跳过explode
400f10: e8 25 05 00 00 callq 40143a <explode_bomb>
400f15: eb 19 jmp 400f30 <phase_2+0x34> #无条件跳转
400f17: 8b 43 fc mov -0x4(%rbx),%eax #rbx-4地址对应内存内容->eax
400f1a: 01 c0 add %eax,%eax #eax *= 2
400f1c: 39 03 cmp %eax,(%rbx) #比较
400f1e: 74 05 je 400f25 <phase_2+0x29> #相等跳过explode
400f20: e8 15 05 00 00 callq 40143a <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>
400f2e: eb 0c jmp 400f3c <phase_2+0x40>
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>
400f3c: 48 83 c4 28 add $0x28,%rsp
400f40: 5b pop %rbx
400f41: 5d pop %rbp
400f42: c3 retq
首先我们看到400f02
: mov %rsp,%rsi
, %rsi
作为第二个函数参数保存了栈顶内容. 接着调用了函数
read_six_numbers
, 通过名字判断函数功能是将读入字符串转为6
个数字, 而%rsp
做为参数可能是
用栈保存6
个数字数值. 这里用gdb
工具测试判断是否正确.
将断点打在read_six_numbers
函数返回处0x400f0a
.
(gdb) break *0x400f0a
在输入第一个字符串后输入1, 1, 2, 2, 3, 3
. 接着查看rsp附近内容
(gdb) x/32xb $rsp
0x7fffffffdcf0: 0x01 0x00 0x00 0x00 0x01 0x00 0x00 0x00
0x7fffffffdcf8: 0x02 0x00 0x00 0x00 0x02 0x00 0x00 0x00
0x7fffffffdd00: 0x03 0x00 0x00 0x00 0x03 0x00 0x00 0x00
0x7fffffffdd08: 0x31 0x14 0x40 0x00 0x00 0x00 0x00 0x00
可以看到每4
字节依次存数我们输入的6
个数字.
read_six_numbers
返回后首先判断栈顶内容是否为1
, 相等则跳过boom
, 所以输入的第一个数字为1
.
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp) #rsp内容与1比较
400f0e: 74 20 je 400f30 <phase_2+0x34> #相等则跳过explode
400f10: e8 25 05 00 00 callq 40143a <explode_bomb>
400f15: eb 19 jmp 400f30 <phase_2+0x34> #无条件跳转
接着代码跳转至400f30
处
400f30: 48 8d 5c 24 04 lea 0x4(%rsp),%rbx #rbx = rsp + 4
400f35: 48 8d 6c 24 18 lea 0x18(%rsp),%rbp #rbp = rsp + 24
400f3a: eb db jmp 400f17 <phase_2+0x1b> #无条件跳转
代码跳转至400f17
处
400f17: 8b 43 fc mov -0x4(%rbx),%eax # eax = (rsp + 4 - 4)
400f1a: 01 c0 add %eax,%eax # eax = eax + eax
400f1c: 39 03 cmp %eax,(%rbx)
400f1e: 74 05 je 400f25 <phase_2+0x29> #eax==*rbx则跳转, 否则爆炸
400f20: e8 15 05 00 00 callq 40143a <explode_bomb>
也就是(rsp + 4
)处的数值为( rsp
)处的两倍, 也就是第二个输入数字为2
.
接着函数跳转至400f25
.
400f25: 48 83 c3 04 add $0x4,%rbx #rbx ++ 4字节一单位
400f29: 48 39 eb cmp %rbp,%rbx #比较 rbx <-> rsp + 24, rbp作为边界
400f2c: 75 e9 jne 400f17 <phase_2+0x1b>
400f2e: eb 0c jmp 400f3c <phase_2+0x40>
若结果不相等, 返回400f17
处, 可以看出这里是循环
结构, 每次比较相邻两个数字大小, 并让rbx++
, 直到
比较完6
个数字为止(rsp + 20
是最后一个数字), 且数字比前一个大2
倍.
所以第二个输入是 1 2 4 8 16 32
.
phase_3
0000000000400f43 <phase_3>:
400f43: 48 83 ec 18 sub $0x18,%rsp #rsp -= 0x18
400f47: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx #rcx = rsp + 12
400f4c: 48 8d 54 24 08 lea 0x8(%rsp),%rdx #rdx = rsp + 8
400f51: be cf 25 40 00 mov $0x4025cf,%esi #esi =
400f56: b8 00 00 00 00 mov $0x0,%eax #eax = 0
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> #rax > 1 跳转, 否则爆炸
400f65: e8 d0 04 00 00 callq 40143a <explode_bomb>
400f6a: 83 7c 24 08 07 cmpl $0x7,0x8(%rsp)
400f6f: 77 3c ja 400fad <phase_3+0x6a>
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>
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
首先观察前几句汇编代码
400f43: 48 83 ec 18 sub $0x18,%rsp
400f47: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx #rcx = rsp + 12
400f4c: 48 8d 54 24 08 lea 0x8(%rsp),%rdx #rdx = rsp + 8
400f51: be cf 25 40 00 mov $0x4025cf,%esi #esi = 可能作为地址
400f56: b8 00 00 00 00 mov $0x0,%eax #eax = 0
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> #rax > 1 跳转, 否则爆炸
400f65: e8 d0 04 00 00 callq 40143a <explode_bomb>
函数调用了__isoc99_sscanf@plt
, 在read_six_numbers
中也出现过, 观察read_six_numbers
汇编代码
000000000040145c <read_six_numbers>:
40145c: 48 83 ec 18 sub $0x18,%rsp
401460: 48 89 f2 mov %rsi,%rdx #rdx = rsi 保存存储数值的地址
401463: 48 8d 4e 04 lea 0x4(%rsi),%rcx #rcx = rsi + 4
401467: 48 8d 46 14 lea 0x14(%rsi),%rax #rax = (rsi) + 20
40146b: 48 89 44 24 08 mov %rax,0x8(%rsp) #
401470: 48 8d 46 10 lea 0x10(%rsi),%rax #rax = rsi + 16
401474: 48 89 04 24 mov %rax,(%rsp) #(rsp) =
401478: 4c 8d 4e 0c lea 0xc(%rsi),%r9
40147c: 4c 8d 46 08 lea 0x8(%rsi),%r8 #各个寄存器保存rsi附近的值
401480: be c3 25 40 00 mov $0x4025c3,%esi #esi作为参数 可能保存地址
401485: b8 00 00 00 00 mov $0x0,%eax
40148a: e8 61 f7 ff ff callq 400bf0 <__isoc99_sscanf@plt> ###
40148f: 83 f8 05 cmp $0x5,%eax
401492: 7f 05 jg 401499 <read_six_numbers+0x3d>
401494: e8 a1 ff ff ff callq 40143a <explode_bomb>
401499: 48 83 c4 18 add $0x18,%rsp
40149d: c3
推测read_six_numbers
函数首先让参数寄存器保存数值的地址, __isoc99_sscanf@plt
以esi内容
作为首地址读取字符串, 并返回以空格隔开的数字的个数. 返回个数大于1
, 所以首先输入要大于1
个.
用gdb
判断输入结果是否保存在rsp
附近.
(gdb) break *0x400f60
输入前两个字符串后, 输入16, 32, 48, 64
.
0x7fffffffdc50: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x7fffffffdc58: 0x10 0x00 0x00 0x00 0x20 0x00 0x00 0x00
0x7fffffffdc60: 0x90 0x0c 0x40 0x00 0x00 0x00 0x00 0x00
0x7fffffffdc68: 0x77 0x0e 0x40 0x00 0x00 0x00 0x00 0x00
输入其他数据测试, 发现在(rsp + 8
)处存入输入的前两个数值.
代码跳转至400f6a
, 假设输入值为x
, y
400f6a: 83 7c 24 08 07 cmpl $0x7,0x8(%rsp) #与第一个输入x比较
400f6f: 77 3c ja 400fad <phase_3+0x6a> #x > 7, 跳转至爆炸处!
400f71: 8b 44 24 08 mov 0x8(%rsp),%eax #eax = x
400f75: ff 24 c5 70 24 40 00 jmpq *0x402470(,%rax,8) #无条件跳转 对应地址内容 + 8x
400fad: e8 88 04 00 00 callq 40143a <explode_bomb>
用gdb
查看对应地址内容.
(gdb) x/8xb 0x402470
0x402470: 0x7c 0x0f 0x40 0x00 0x00 0x00 0x00 0x00
也就是跳转至 0x400f7c + 8x
处, 0 <= x <= 7
. 所以我们可以跳转至 0x400f7c
- 0x400f84
- 0x400f8c
- 0x400f94
-0x400f549c
- 0x400f54a4
- 0x400f54ac
- 0x400f54b4
上述地址只有第一个作为指令的有效起始地址, 所以x
为0
, 代码跳转至0x400f547c
.
400f7c: b8 cf 00 00 00 mov $0xcf,%eax #eax = 0xcf
400f81: eb 3b jmp 400fbe <phase_3+0x7b>
代码跳转至400fbe
#eax = 0xcf
400fbe: 3b 44 24 0c cmp 0xc(%rsp),%eax #eax 与 (rsp + 12)也就是第二个输入比较
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
也就是第二个输入值为0xcf
= 207
.
所以第三个字符串为0 207
.
phase_4
000000000040100c <phase_4>:
40100c: 48 83 ec 18 sub $0x18,%rsp
401010: 48 8d 4c 24 0c lea 0xc(%rsp),%rcx #rcx = rsp + 12 y
401015: 48 8d 54 24 08 lea 0x8(%rsp),%rdx #rdx = rsp + 8 x
40101a: be cf 25 40 00 mov $0x4025cf,%esi #esi = sscanf输入地址?
40101f: b8 00 00 00 00 mov $0x0,%eax #eax = 0
401024: e8 c7 fb ff ff callq 400bf0 <__isoc99_sscanf@plt> #熟悉的接收输入
401029: 83 f8 02 cmp $0x2,%eax #eax == 2 ?
40102c: 75 07 jne 401035 <phase_4+0x29> #返回值不为2 跳转至爆炸
40102e: 83 7c 24 08 0e cmpl $0xe,0x8(%rsp) #x <= 12(unsigned)
401033: 76 05 jbe 40103a <phase_4+0x2e> #x <= 12 跳过爆炸!
401035: e8 00 04 00 00 callq 40143a <explode_bomb>
40103a: ba 0e 00 00 00 mov $0xe,%edx #edx = 14
40103f: be 00 00 00 00 mov $0x0,%esi #esi = 0
401044: 8b 7c 24 08 mov 0x8(%rsp),%edi #edi = x
401048: e8 81 ff ff ff callq 400fce <func4> #调用func4?
40104d: 85 c0 test %eax,%eax #返回值是否为0 ?
40104f: 75 07 jne 401058 <phase_4+0x4c> #eax不为0, 跳转至爆炸!
401051: 83 7c 24 0c 00 cmpl $0x0,0xc(%rsp) #y == 0
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
又遇到熟悉的输入函数__isoc99_sscanf@plt
, 代码理解难度降低了很多.
首先要输入两个数值; 第一个数 0 <= x <= 12
.
调用了新的函数func4
, 返回值需要为0
, 观察fun4
的汇编代码.
0000000000400fce <func4>:
400fce: 48 83 ec 08 sub $0x8,%rsp
400fd2: 89 d0 mov %edx,%eax #eax = edx = 14
400fd4: 29 f0 sub %esi,%eax #eax = edx - esi = 14 - 0
400fd6: 89 c1 mov %eax,%ecx #ecx = eax = 14
400fd8: c1 e9 1f shr $0x1f,%ecx #ecx >>= 31 = 0(符号位)
400fdb: 01 c8 add %ecx,%eax #eax += ecx = 14
400fdd: d1 f8 sar %eax #默认右移一位, eax /= 2
400fdf: 8d 0c 30 lea (%rax,%rsi,1),%ecx #eax = rax + 0
400fe2: 39 f9 cmp %edi,%ecx #ecx <= x ?
400fe4: 7e 0c jle 400ff2 <func4+0x24> #eax <= x, 跳转
400fe6: 8d 51 ff lea -0x1(%rcx),%edx #edx = rcx - 1 = -1
400fe9: e8 e0 ff ff ff callq 400fce <func4> #递归调用
400fee: 01 c0 add %eax,%eax #返回值eax *= 2
400ff0: eb 15 jmp 401007 <func4+0x39> #跳转至程序结束
400ff2: b8 00 00 00 00 mov $0x0,%eax #eax = 0
400ff7: 39 f9 cmp %edi,%ecx #ecx >= x ?
400ff9: 7d 0c jge 401007 <func4+0x39> #ecx >= x, 跳转至程序结束
400ffb: 8d 71 01 lea 0x1(%rcx),%esi #esi = 1 + rcx =
400ffe: e8 cb ff ff ff callq 400fce <func4> #递归调用
401003: 8d 44 00 01 lea 0x1(%rax,%rax,1),%eax #eax = rax * 2 + 1
401007: 48 83 c4 08 add $0x8,%rsp
40100b: c3 retq
观察到程序存在递归结构, 尝试逆向构造对应C
程序.
//初始值 x = 第一个输入值; y = 0; z = 14
int func4(int x, int y, int z)
{//x in edi, y in esi, z in edx , ret in eax, a in ecx
int ret = z - y; //sub esi,edx
int a = ret >> 31; // shr 0x1f,ecx
ret = ( ret + a ) / 2; //add ecx, eax; sar eax
a = ret + y; //lea (rax,rsi,1),ecx
if( a <= x )
{
ret = 0;
if( a >= x ) // a == x
{
return ret;
}
else
{
y = a + 1;
ret = func4(x, y, z);
ret = 2 * ret + 1;
}
}
else
{
z = a - 1;
ret = func4(x, y, z);
ret *= 2;
return ret;
}
}
初始第一次判断a
与x
的关系时a = 7
, 所以第一个输入值x = 7
时程序返回0
.
而根据phase_4
的后续汇编代码可知第二个参数为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 #rbx = rdi
40106a: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax #rax = fs * 16 + 0x28 ??
401071: 00 00
401073: 48 89 44 24 18 mov %rax,0x18(%rsp) #(rsp + 24) = rax
401078: 31 c0 xor %eax,%eax #eax ^ eax -> eax = 0
40107a: e8 9c 02 00 00 callq 40131b <string_length> #调用字符串长度
40107f: 83 f8 06 cmp $0x6,%eax
401082: 74 4e je 4010d2 <phase_5+0x70> #eax == 6 ? 不等于boom!
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 #(rbx + rax)的1B内容->ecx
40108f: 88 0c 24 mov %cl,(%rsp) #(rsp) = cl, (rbx + rax)的1B内容
401092: 48 8b 14 24 mov (%rsp),%rdx #rdx = (rsp) =
401096: 83 e2 0f and $0xf,%edx #edx &= 1111
401099: 0f b6 92 b0 24 40 00 movzbl 0x4024b0(%rdx),%edx #(rdx +...)的1B内容->edx
4010a0: 88 54 04 10 mov %dl,0x10(%rsp,%rax,1)#(rdx +...)的1B内容->(...)
4010a4: 48 83 c0 01 add $0x1,%rax #rax += 1
4010a8: 48 83 f8 06 cmp $0x6,%rax
4010ac: 75 dd jne 40108b <phase_5+0x29>#rax != 6, 跳回前面, 循环结构
4010ae: c6 44 24 16 00 movb $0x0,0x16(%rsp) #(rsp + 22) = 0
4010b3: be 5e 24 40 00 mov $0x40245e,%esi #esi = 保存地址
4010b8: 48 8d 7c 24 10 lea 0x10(%rsp),%rdi #rdi = (rsp + 16)
4010bd: e8 76 02 00 00 callq 401338 <strings_not_equal> #phase_1中出现的函数
4010c2: 85 c0 test %eax,%eax
4010c4: 74 13 je 4010d9 <phase_5+0x77> #函数返回值rax == 0 跳过boom
4010c6: e8 6f 03 00 00 callq 40143a <explode_bomb>
4010cb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) #do nothing?什么都不做?
4010d0: eb 07 jmp 4010d9 <phase_5+0x77>
4010d2: b8 00 00 00 00 mov $0x0,%eax #eax = 0
4010d7: eb b2 jmp 40108b <phase_5+0x29> #跳转至之前代码 循环
4010d9: 48 8b 44 24 18 mov 0x18(%rsp),%rax #rax =
4010de: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
4010e5: 00 00
4010e7: 74 05 je 4010ee <phase_5+0x8c> #0x18(%rsp) == %fs:0x28
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
分步观察汇编代码.
401062: 53 push %rbx
401063: 48 83 ec 20 sub $0x20,%rsp
401067: 48 89 fb mov %rdi,%rbx #rbx = rdi 第一个输入
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 ^ eax -> eax = 0
40107a: e8 9c 02 00 00 callq 40131b <string_length> #调用字符串长度
40107f: 83 f8 06 cmp $0x6,%eax
401082: 74 4e je 4010d2 <phase_5+0x70> #eax == 6 ? 不等于boom!
401084: e8 b1 03 00 00 callq 40143a <explode_bomb>
首先将首个输入保存在rbx
中, [mov %fs:0x28,%rax ; mov %rax,0x18(%rsp)]
作用暂时未知;
将eax
置0
后调用字符串函数, 判断输入的字符串长度是否为6
, 为6
则跳过爆炸.
来到跳转位置4010d2
4010d2: b8 00 00 00 00 mov $0x0,%eax
4010d7: eb b2 jmp 40108b <phase_5+0x29>
将eax
置0
后无条件跳转至40108b
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>
首先将rbx
(输入)作为起始地址加上rax
(初始为0
)处1B
内容, 实际上就是输入字符串的第rax
个字符.
(输入字符串rbx
保存字符串首地址). 用掩码方式使得edx
保存其低4
位,再将(其自身值
+0x4024b0
)作为
地址内容的1B
交给自身edx
;将rdx
低8
位赋值给(rsp + rax + 16
); rax ++
; 如果rax != 6
, 继续到起始的40108b
.
也就是rax
= 0
~ 5
循环6
次执行.
循环过程 : 每次依次取输入字符, 保留字符二进制表示的低4
位 赋值到 rdx
将rdx
获得的内容再次作为偏移量, 将对应地址位置1B
内容依次存入(rsp + rax + 16
), rax
= 0
~ 5
.
循环6
次后继续向下执行
4010ae: c6 44 24 16 00 movb $0x0,0x16(%rsp) #rsp:16~21保存字符, 最后0相当于'\0'结束符
4010b3: be 5e 24 40 00 mov $0x40245e,%esi #存放字节地址
4010b8: 48 8d 7c 24 10 lea 0x10(%rsp),%rdi #(rsp + 16)作为第一个参数rsp:16 ~ 21
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> #返回0 跳过爆炸
4010c6: e8 6f 03 00 00 callq 40143a <explode_bomb>
也就是将上述6
次循环获得的内容与0x40245e
处内容比较, 我们先用gdb
查看0x40245e
处内容.
(gdb) x/s 0x40245e
0x40245e: "flyers"
这就是我们需要匹配的内容, 同样查看0x4024b0
为基址的内容.
(gdb) x/s 0x4024b0
0x4024b0 <array.3449>: "maduiersnfotvbylSo you think you can stop the bomb with ctrl-c, do you?"
也就是我们依次输入字符值的低4
位的数值作为索引值, 组成字符串”flyers
”.
索引值可以是9 15 14 5 6 7
, 低4
位是索引值的对应字符可以是IONEFG
.(用64
作为偏移量得到)
所以输入IONEFG
解决phase_5
.
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 #caller-saved ?
4010fc: 48 83 ec 50 sub $0x50,%rsp
401100: 49 89 e5 mov %rsp,%r13 #r13 = rsp
401103: 48 89 e6 mov %rsp,%rsi #rsi = rsp
401106: e8 51 03 00 00 callq 40145c <read_six_numbers> #将6个数字保存在rsp
40110b: 49 89 e6 mov %rsp,%r14 #r14 = rsp
40110e: 41 bc 00 00 00 00 mov $0x0,%r12d r12d = 0 r12的低32位 相当于r12 = 0
401114: 4c 89 ed mov %r13,%rbp #rbp = r13 = rsp
401117: 41 8b 45 00 mov 0x0(%r13),%eax #eax = (rsp) = 第一个数字
40111b: 83 e8 01 sub $0x1,%eax #eax--
40111e: 83 f8 05 cmp $0x5,%eax
401121: 76 05 jbe 401128 <phase_6+0x34> #eax <= 5 跳过boom!
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
401158: 4c 89 f0 mov %r14,%rax
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)
401166: 48 83 c0 04 add $0x4,%rax
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>
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
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
4011bd: 48 8b 10 mov (%rax),%rdx
4011c0: 48 89 51 08 mov %rdx,0x8(%rcx)
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
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
按步分析
4010f4: 41 56 push %r14
4010f6: 41 55 push %r13
4010f8: 41 54 push %r12
4010fa: 55 push %rbp
4010fb: 53 push %rbx #caller-saved
4010fc: 48 83 ec 50 sub $0x50,%rsp
401100: 49 89 e5 mov %rsp,%r13 #r13 = rsp
401103: 48 89 e6 mov %rsp,%rsi #rsi = rsp
401106: e8 51 03 00 00 callq 40145c <read_six_numbers> #将6个数字保存在rsp
40110b: 49 89 e6 mov %rsp,%r14 #r14 = rsp
40110e: 41 bc 00 00 00 00 mov $0x0,%r12d #r12d = 0 (r12d是r12的低32位 相当于r12 = 0)
401114: 4c 89 ed mov %r13,%rbp #rbp = r13 = rsp
401117: 41 8b 45 00 mov 0x0(%r13),%eax #eax = (rsp) = 第一个数字
40111b: 83 e8 01 sub $0x1,%eax #eax--
40111e: 83 f8 05 cmp $0x5,%eax
401121: 76 05 jbe 401128 <phase_6+0x34> #eax <= 5(unsigned) 跳过boom!
401123: e8 12 03 00 00 callq 40143a <explode_bomb>
read_six_numbers
函数将以空格间隔的数字字符串按4B
依次保存以rsp
作为起始地址处.
判断第一个数字num1--
后是否小于等于5
, 也就是第一个数字num1<=6
, 跳过爆炸来到401128
401128: 41 83 c4 01 add $0x1,%r12d #r12d ++ (初始r12d=0)
40112c: 41 83 fc 06 cmp $0x6,%r12d
401130: 74 21 je 401153 <phase_6+0x5f>#r12d == 6 时跳转
401132: 44 89 e3 mov %r12d,%ebx #ebx = r12d
401135: 48 63 c3 movslq %ebx,%rax #rax = ebx(有符号) 作为索引值
401138: 8b 04 84 mov (%rsp,%rax,4),%eax #rsp + 4*rax -> eax
40113b: 39 45 00 cmp %eax,0x0(%rbp) #rax与rbp(初始为rsp)
40113e: 75 05 jne 401145 <phase_6+0x51> #若不相等 跳过爆炸
401140: e8 f5 02 00 00 callq 40143a <explode_bomb>
跳转至401145
401145: 83 c3 01 add $0x1,%ebx #ebx++, 初始为1
401148: 83 fb 05 cmp $0x5,%ebx
40114b: 7e e8 jle 401135 <phase_6+0x41> #ebx <= 5, 跳转
40114d: 49 83 c5 04 add $0x4,%r13 #r13 = rsp + ...
401151: eb c1 jmp 401114 <phase_6+0x20>
跳转至上面的401135
, 构成循环. 且为了不爆炸退出循环的条件只能是 ebx <= 5
不成立,
也就是循环会执行 ebx
= 1
~ 5
$->$ eax
= 1
~ 5
, 对应前5
个输入不能为1 2 3 4 5
.
循环至ebx
= 6
后, 汇编代码跳转至最开始的401114
, 变化的是r13
指向的是第二个输入.
401114: 4c 89 ed mov %r13,%rbp #rbp = r13 = rsp + ...
401117: 41 8b 45 00 mov 0x0(%r13),%eax #eax = (rsp) = 第x个数字
40111b: 83 e8 01 sub $0x1,%eax #eax--
40111e: 83 f8 05 cmp $0x5,%eax
401121: 76 05 jbe 401128 <phase_6+0x34> #eax <= 5 跳过boom!
401123: e8 12 03 00 00 callq 40143a <explode_bomb>
根据上述分析将代码转换为C
代码格式
//假设输入保存在arr[]数组中, 这里rsp = arr
//输入爆炸不会执行explode_bomb!!!
r13 = rsi = r14 = arr; //rsp ~ rsp + 5 * 4 存储输入6个数字[rsp,rsp+23]
r12d = 0;
while( true )//401114
{
rbp = r13;
eax = *r13;
if( --eax > 5u ) explode_bomb!!!
//401128
r12d ++;
if( r12d == 6 ) break;//执行中r12d = 1 ~ 5 jmp 401153
ebx = r12d; //ebx = 1 ~ 5
while( true )//401135
{
rax = eax;
eax = arr[eax];
if( eax == *rbp ) explode_bomb!!!
else
{
//401145
ebx ++ ; //ebx = i ~ 5
if( ebx > 5 )
{
r13 ++; //+4相当于int型arr指针向前加1 r13 ++ 在执行过程中 r13 = arr ~ arr + 4
break; //跳出内层while循环
}
}
}
}
也就是外层循环5
次 : r12d
= 1
~ 5
; rbp
= r13
= arr
~ arr + 4
.
eax
= arr[0]
, … , arr[4]
.
所以0 <= arr[0 ~ 4] <= 6
.
内层循环的作用实际上是判断arr[i]
和arr[i+1 ~ 5]
是否相等, 相等则爆炸. 所以输入6
个数字各不相等
.
跳出循环(唯一安全的跳转)401153
401153: 48 8d 74 24 18 lea 0x18(%rsp),%rsi #rsi = rsp + 24 //arr[6]
401158: 4c 89 f0 mov %r14,%rax #rax = r14 = rsp //arr
40115b: b9 07 00 00 00 mov $0x7,%ecx #ecx = 7
401160: 89 ca mov %ecx,%edx #edx = ecx = 7
401162: 2b 10 sub (%rax),%edx #edx -= *rax 7 - arr[i]
401164: 89 10 mov %edx,(%rax) #*rax = edx
401166: 48 83 c0 04 add $0x4,%rax #rax += 4 //arr + ...
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>
理解过上面折磨的二重循环后再看这段代码就简单了许多. rsi
= arr + 6
作为循环的终止条件.
每次将输入内容arr[i]
进行arr[i] = 7 - arr[i]
的操作. 可以转化为C
格式
rsi = arr + 6; rax = arr; ecx = 7;
do
{
edx = ecx; //7
*rax = 7 - *rax;
rax ++;
}while( rax != rsi )
esi = 0; jmp 401197
程序来到401197
401197: 8b 0c 34 mov (%rsp,%rsi,1),%ecx #ecx = *(arr + rsi) rsi初值为0
40119a: 83 f9 01 cmp $0x1,%ecx
40119d: 7e e4 jle 401183 <phase_6+0x8f>#ecx <= 1
40119f: b8 01 00 00 00 mov $0x1,%eax #rax = 1
4011a4: ba d0 32 60 00 mov $0x6032d0,%edx #edx = 地址?
4011a9: eb cb jmp 401176 <phase_6+0x82>#ecx > 1
这里没有需要避免爆炸的情况, 分别分析两个跳转地址的内容.
因为两个跳转位置都有地址0x6032d0
, 首先用gdb
查看对应地址内容.
(gdb) x/12xg 0x6032d0
0x6032d0 <node1>: 0x000000010000014c 0x00000000006032e0
0x6032e0 <node2>: 0x00000002000000a8 0x00000000006032f0
0x6032f0 <node3>: 0x000000030000039c 0x0000000000603300
0x603300 <node4>: 0x00000004000002b3 0x0000000000603310
0x603310 <node5>: 0x00000005000001dd 0x0000000000603320
0x603320 <node6>: 0x00000006000001bb 0x0000000000000000
看出地址内容为链表结构, 每个结点保存一个8
字节内容和下一个结点的地址, 共有6
个结点, 结点数值递增.
401183
401183: ba d0 32 60 00 mov $0x6032d0,%edx #edx = 结点首地址
401188: 48 89 54 74 20 mov %rdx,0x20(%rsp,%rsi,2) #*(arr + 2rsi + 32) = rdx(node内容)
40118d: 48 83 c6 04 add $0x4,%rsi #rsi += 4 (一个int)
401191: 48 83 fe 18 cmp $0x18,%rsi
401195: 74 14 je 4011ab <phase_6+0xb7> #rsi == 24 跳转 即遍历arr[]
401197: 8b 0c 34 mov (%rsp,%rsi,1),%ecx #ecx = *(arr + rsi) rsi初值为0
40119a: 83 f9 01 cmp $0x1,%ecx
40119d: 7e e4 jle 401183 <phase_6+0x8f>#若ecx <= 1 形成循环结构
40119f: b8 01 00 00 00 mov $0x1,%eax #eax = 1
4011a4: ba d0 32 60 00 mov $0x6032d0,%edx #edx = node首地址
4011a9: eb cb jmp 401176 <phase_6+0x82>#回到第二个分支
401176
初始%edx
= 0x6032d0
node
的首地址; eax = 1
401176: 48 8b 52 08 mov 0x8(%rdx),%rdx #rdx = *(rdx + 8) 初始:rdx = node.next
40117a: 83 c0 01 add $0x1,%eax #eax ++
40117d: 39 c8 cmp %ecx,%eax #ecx为第一个输入, 且ecx>1
40117f: 75 f5 jne 401176 <phase_6+0x82> #eax != ecx 循环
401181: eb 05 jmp 401188 <phase_6+0x94> #eax == ecx 跳转
经过上述分析我们知道初始 0 <= arr[i] <= 6
, 经过arr[i] = 7 - arr[i]
后,
1 <= arr[i] <= 7
, 这里ecx
为arr[0]
且arr[0] > 1
.
如果arr[0] = 2
, 则进入上述的401188
, 否则edx
会每次+1
直到与arr[0]
匹配, 代码执行次数为arr[0] - 1
.
设x = arr[0] - 1
, 执行jmp
指令时rdx = node + 8x
; eax = arr[0]
. 我们再次来到401188
综合这两个分支 : 若arr[0] == 1
(也就是输入第一个数为6
), 程序直接来到第一个分支, edx = node首地址
;
否则进入第二个分支后回到第一个分支, edx = node + 8x(x = arr[0] - 1)
==> edx = node + 8(arr[0] - 1)
. 此时arr[0]
是7-第一个输入数值
.
所以我们需要继续分析401188
段 初始rsi = 0
; rdx = node + 8x(x = arr[0] - 1)
;
401183: ba d0 32 60 00 mov $0x6032d0,%edx #edx = 结点首地址
401188: 48 89 54 74 20 mov %rdx,0x20(%rsp,%rsi,2) #*(arr + 32 + 2*rsi ) = rdx
40118d: 48 83 c6 04 add $0x4,%rsi #rsi += 4 (一个int)
401191: 48 83 fe 18 cmp $0x18,%rsi
401195: 74 14 je 4011ab <phase_6+0xb7> #rsi == 24 跳转
401197: 8b 0c 34 mov (%rsp,%rsi,1),%ecx #ecx = *(arr + rsi)
40119a: 83 f9 01 cmp $0x1,%ecx
40119d: 7e e4 jle 401183 <phase_6+0x8f>#若ecx <= 1 形成循环结构
40119f: b8 01 00 00 00 mov $0x1,%eax #eax = 1
4011a4: ba d0 32 60 00 mov $0x6032d0,%edx #edx = node首地址
4011a9: eb cb jmp 401176 <phase_6+0x82>#回到第二个分支
4011a9
的jmp
保证如果ecx = arr[i] == 1
, 则继续循环, 否则jmp
至第二个分支再回到第一个分支.
所以程序执行为 : rsi
= 0
~ 4*5
, 每次rdx = node + 8x(x = arr[i] - 1)
, 并将rdx
内容存储
至以rsp/arr + 32
为基址的位置到arr + 72
.
直到rsi == 24,
程序跳转至4011ab
, 下面为方便将arr + 32 = brr
, brr
以8
字节为单位存储.
4011ab: 48 8b 5c 24 20 mov 0x20(%rsp),%rbx #rbx = arr + 32 = brr(上面rdx存储内容首地址)
4011b0: 48 8d 44 24 28 lea 0x28(%rsp),%rax #rax = brr + 1
4011b5: 48 8d 74 24 50 lea 0x50(%rsp),%rsi #rsi = brr + 6
4011ba: 48 89 d9 mov %rbx,%rcx #rcx = rbx
//4011bd
4011bd: 48 8b 10 mov (%rax),%rdx #rdx = brr[1]
4011c0: 48 89 51 08 mov %rdx,0x8(%rcx) #
4011c4: 48 83 c0 08 add $0x8,%rax #rax += 8 -> rax = brr + 2
4011c8: 48 39 f0 cmp %rsi,%rax
4011cb: 74 05 je 4011d2 <phase_6+0xde>#rax == rsi, brr + 2 == brr + 6
4011cd: 48 89 d1 mov %rdx,%rcx #rcx = brr[1]
4011d0: eb eb jmp 4011bd <phase_6+0xc9>
//je 4011d2
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
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>
//jmp 4011bd
4011bd: 48 8b 10 mov (%rax),%rdx #rdx = brr[]
4011c0: 48 89 51 08 mov %rdx,0x8(%rcx) #rdx =
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> #循环结构
这里再次遇到循环结构, 尝试用C
格式表示
//上一步存储的6个8字节内容存储在brr = rsp/arr + 32地址中
rbx = brr; rax = brr + 1; rsi = brr + 6; rcx = rbx = brr;
while( true ) //4011bd
{
*(rcx + 1) = rdx = *rax; //rax = brr + 1/3/5 对应node->next(node 1,2,3)
rax ++ ; // rax = brr + 2/4/6 作为node 2/3/4的内容
if( rax == rsi )
{
//4011d2
.Lable //跳跃语句标签
*(rdx + 1) = 0;
ebp = 5;
eax = *(rbx + 1);
if( *rbx >= eax )
explode_bomb!!!
rbx = *(rbx + 1);
ebp --;
if( *rbx != eax ) break; //jmp 4011df;
}
else
{ //rax = brr + 2/4
rcx = rdx;
//4011bd
*(rcx + 1) = rdx = *rax;
rax ++;
if( rax == rsi ) goto .Lable;
rcx = rdx;
}
}
程序循环至rax == rsi
时终于来到了有explode_bomb
函数的地方, 这指明了方向 : 不能爆炸, 那么只能
跳转至另一个地址.
这里一步一步推算难度较大, 先看看4011df
地址做了什么, 以此反推之前代码的行为.
在进入4011df
前对rbx
的赋值只有rbx = *(rbx + 1)
#初始 rbx = *node'1->next ebp = 4
4011df: 48 8b 43 08 mov 0x8(%rbx),%rax #rax = ->next
4011e3: 8b 00 mov (%rax),%eax #eax = *(next)
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 #rbx = *nodei=>next = node(i+1)
4011f2: 83 ed 01 sub $0x1,%ebp #ebp--
4011f5: 75 e8 jne 4011df <phase_6+0xeb>#ebp != 0, 继续循环
4011f7: 48 83 c4 50 add $0x50,%rsp #退出循环
4011fb: 5b pop %rbx #restore
4011fc: 5d pop %rbp
4011fd: 41 5c pop %r12
4011ff: 41 5d pop %r13
401201: 41 5e pop %r14
401203: c3 retq
brr
保存的是node1~node6
的内容, 具体内容为node + 8x(x = arr[i] - 1)
. 可以看出程序大致
框架为循环4
次(ebp = 4 ~ 1
), 每次都有类似p = p->next
的结构. 这里比较的是低4
位数值,
低4位从大到小排列为 : 3 4 5 6 1 2
, 这里因为有x = 7 - x,
所以我试着输入4 3 2 1 6 5
,
程序通过了 - - . (这一题暂时没完全清楚)
secret_phase
至此从bomb.c
中明确给出的6
个语句全部解决, 但注意最后的注释
/* Wow, they got it! But isn't something... missing? Perhaps
* something they overlooked? Mua ha ha ha ha! */
观察bomb.s
, 在phase_6
的下面的fun7
下还有一个secret_phase
, 具体我还没解决Mua ha ha ha ha!
链接博客大佬的本人吗QwQ,厉害的
不是的, 是其他大佬的 = 。=
牛逼o( ̄▽ ̄)d