CSAPP(Computer Systems A Programmer's Perspective),中译名为深入理解计算机系统,是一本优秀的计算机教材。该书配套了若干个课后实验,可供读者检验所学知识。其中bomb lab(从该页面下载实验材料)检验的是读者的汇编知识。这个实验笔者久闻大名但一直没有做。最近花时间研究了一番发现果然有趣。
该实验需在linux系统下进行,分六关,每关实验者需要输入符合条件的字符串才能进入下一关。通过六关视为拆弹成功。实验提供了一个可执行文件bomb和bomb.c以及相应的实验说明。
第一关
终端运行bomb
$ ./bomb
终端显示
Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
实验者需要输入符合条件的字符串才能进入下一关。
打开bomb.c,main函数代码如下
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 , the bomb reads from
* until EOF, and then switches to standard input. Thus, as you
* defuse each phase, you can add its defusing string to 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 []\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;
}
由代码可知函数phase_1
~phase_6
是关键函数,如果能够知道这些函数的实现,我们就能推导出符合条件的字符串,拆除炸弹。然而实验材料里并不包含phase.c。这时需要逆向工程反推出这些函数的运行机制。
终端输入
$ objdump -d bomb > bomb.S
将反汇编bomb得到的汇编代码存储到bomb.S文件中
phase_1的汇编代码如下所示
0000000000400ee0 :
400ee0: 48 83 ec 08 sub $0x8,%rsp
400ee4: be 00 24 40 00 mov $0x402400,%esi
400ee9: e8 4a 04 00 00 callq 401338
400eee: 85 c0 test %eax,%eax
400ef0: 74 05 je 400ef7
400ef2: e8 43 05 00 00 callq 40143a
400ef7: 48 83 c4 08 add $0x8,%rsp
400efb: c3 retq
从汇编代码可知,phase_1会将用户输入的字符串与内存中的某一特定字符串s做比较,如果相同过关,不同则炸弹爆炸。
如何知道字符串s的内容?借助gdb。命令x/s addr
会显示起始地址为addr的字符串
如何知道字符串s的地址?提示:X86-64体系通过寄存器传递函数参数, %rdi存储第一个参数,%rsi存储第二个参数。
第二关
phase_2汇编代码以及注释如下
0000000000400efc :
400efc: 55 push %rbp
400efd: 53 push %rbx
400efe: 48 83 ec 28 sub $0x28,%rsp
400f02: 48 89 e6 mov %rsp,%rsi
400f05: e8 52 05 00 00 callq 40145c
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp)
400f0e: 74 20 je 400f30 //first等于1跳至400f30
400f10: e8 25 05 00 00 callq 40143a // 不等于1直接爆炸
400f15: eb 19 jmp 400f30
400f17: 8b 43 fc mov -0x4(%rbx),%eax //%eax = M[%rbx-4]
400f1a: 01 c0 add %eax,%eax //%eax = 2 * %eax
400f1c: 39 03 cmp %eax,(%rbx) //%eax == M[%rbx]
400f1e: 74 05 je 400f25 //等于跳至400f25
400f20: e8 15 05 00 00 callq 40143a //否则爆炸
400f25: 48 83 c3 04 add $0x4,%rbx //%rbx = %rbx + 4
400f29: 48 39 eb cmp %rbp,%rbx //%rbx == %rbp
400f2c: 75 e9 jne 400f17 //不等跳至400f17
400f2e: eb 0c jmp 400f3c //等于phase_2拆弹成功
400f30: 48 8d 5c 24 04 lea 0x4(%rsp),%rbx //%rbx = second_num addr
400f35: 48 8d 6c 24 18 lea 0x18(%rsp),%rbp //%rbp = %rsp + 24
400f3a: eb db jmp 400f17 //跳至400f17
400f3c: 48 83 c4 28 add $0x28,%rsp
400f40: 5b pop %rbx
400f41: 5d pop %rbp
400f42: c3 retq
phase_2调用了函数 read_six_numbers
。此函数接收两个参数,在被phase_2
调用时read_six_numbers
第一个参数是phase_2的第一个参数即实验者输入的字符串地址,第二个参数是%rsp的值。
下面是read_six_numbers
的汇编代码
000000000040145c :
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) //M[%rsp+8]=%rax
401470: 48 8d 46 10 lea 0x10(%rsi),%rax //%rax=%rsi+16
401474: 48 89 04 24 mov %rax,(%rsp) //M[%rsp]=%rax
401478: 4c 8d 4e 0c lea 0xc(%rsi),%r9 //%r9=%rsi+12
40147c: 4c 8d 46 08 lea 0x8(%rsi),%r8 //%r8=%rsi+8
401480: be c3 25 40 00 mov $0x4025c3,%esi //%esi=char *format
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
401494: e8 a1 ff ff ff callq 40143a
401499: 48 83 c4 18 add $0x18,%rsp
40149d: c3 retq
由代码可知read_six_numbers
调用了库函数sscanf
从给定字符串中读取数字。那么这6个数字具体类型是什么呢?
$ man sscanf
可知sscanf
的函数签名为int sscanf(const char *str, const char *format, ...);
第二个参数指定读取数据格式。在调用sscanf
前有如下指令
mov $0x4025c3,%esi
。可见第二个参数const char *format的地址为0x4025c3。借助gdb得到格式字符串的内容为"%d %d %d %d %d %d"。所以这六个数字的类型为int
。
由于此处的sscanf
要读取六个整型数据,所以一共接收8个参数,前两个参数为const char *str
和const char *format
,后六个参数为六个int *
指针。这八个参数中前六个分别由%rdi
,%rsi
, %rdx
, %rcx
, %r8
, %r9
这六个寄存器传递。后两个参数由内存传递。六个int*
指针的值分别为%rsi
,%rsi+4
, %rsi+8
, %rsi+12
, %rsi+16
, %rsi+20
。考虑到read_six_numbers
被phase_2
调用时,%rsi的值指向phase_2
栈帧的顶部。所以这六个数字是以数组形式存储在phase_2的栈帧上,起始地址为phase_2
栈帧顶部。
回到phase_2
的汇编代码,在调用read_six_numbers
后有如下指令
400f0a: 83 3c 24 01 cmpl $0x1,(%rsp)
400f0e: 74 20 je 400f30 //first等于1跳至400f30
400f10: e8 25 05 00 00 callq 40143a
可知第一个数字必须为1。
阅读接下来的代码,易知此数组相邻数字间满足固定的比例关系。至此答案已呼之欲出。
第三关
0000000000400f43 : //sscanf(str, "%d, %d", &a, &b);
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
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 //返回值>1,jump 0x400f6a
400f65: e8 d0 04 00 00 callq 40143a //否则爆炸
400f6a: 83 7c 24 08 07 cmpl $0x7,0x8(%rsp) //a == 7
400f6f: 77 3c ja 400fad // a>7 爆炸
400f71: 8b 44 24 08 mov 0x8(%rsp),%eax //%eax = a
400f75: ff 24 c5 70 24 40 00 jmpq *0x402470(,%rax,8) //jump M[0x402470+8*%rax]
400f7c: b8 cf 00 00 00 mov $0xcf,%eax //%eax = 207
400f81: eb 3b jmp 400fbe //jump 0x400fbe
400f83: b8 c3 02 00 00 mov $0x2c3,%eax //%eax = 707
400f88: eb 34 jmp 400fbe //jump 0x400fbe
400f8a: b8 00 01 00 00 mov $0x100,%eax //%eax = 256
400f8f: eb 2d jmp 400fbe
400f91: b8 85 01 00 00 mov $0x185,%eax
400f96: eb 26 jmp 400fbe
400f98: b8 ce 00 00 00 mov $0xce,%eax
400f9d: eb 1f jmp 400fbe
400f9f: b8 aa 02 00 00 mov $0x2aa,%eax
400fa4: eb 18 jmp 400fbe
400fa6: b8 47 01 00 00 mov $0x147,%eax
400fab: eb 11 jmp 400fbe
400fad: e8 88 04 00 00 callq 40143a
400fb2: b8 00 00 00 00 mov $0x0,%eax
400fb7: eb 05 jmp 400fbe
400fb9: b8 37 01 00 00 mov $0x137,%eax
400fbe: 3b 44 24 0c cmp 0xc(%rsp),%eax
400fc2: 74 05 je 400fc9 //if b==%eax defused
400fc4: e8 71 04 00 00 callq 40143a //否则爆炸
400fc9: 48 83 c4 18 add $0x18,%rsp
400fcd: c3 retq
阅读0x400f43
-0x400f6f
的汇编代码易知phase_3
通过sscanf
扫描用户输入的字符串从中提取出两个无符号整数设为a, b,其中a小等于7。
关键代码为如下两行
400f71: 8b 44 24 08 mov 0x8(%rsp),%eax //%eax = a
400f75: ff 24 c5 70 24 40 00 jmpq *0x402470(,%rax,8) //jump M[0x402470+8*%rax]
其中jmpq *0x402470(,%rax,8)
为indirect jump。该指令没有直接给出跳转地址,而是指定一个内存地址,此内存地址对应的内存区域存储着跳转地址。CPU先访问内存获取跳转地址再进行跳转。由代码可知跳转地址的内存地址为0x402470+8*%rax
,其中%rax=a
。可见该跳转指令的跳转地址由用户输入的第一个数字决定。
在看接下来的代码,发现他们都具有同一特征:给%eax
赋值,然后跳转至0x400fbe
。
400fbe: 3b 44 24 0c cmp 0xc(%rsp),%eax
400fc2: 74 05 je 400fc9 //if b==%eax defused
400fc4: e8 71 04 00 00 callq 40143a //否则爆炸
阅读这三行代码可知,b
必需等于%eax
。问题是如何知道%eax
的值,借助gdb输入合法的a
,自然就能知道对应的%eax
的值,从而得到对应的b
。至此第三关解析完毕。