...takes no responsibility for damage, frustration, insanity, bug-eyes, carpal-tunnel syndrome, loss of sleep, or other harm to the VICTIM... ...对受害者造成的任何损害、挫败感、精神失常、眼球突出、腕管综合征、失眠或其他伤害,加害者概不负责...
/* 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");
browniecake@MSI:~/csapp-lab/bomb$ gdb bomb
GNU gdb (Ubuntu 15.1-1ubuntu1~24.04.1) 15.1
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bomb...
(gdb)
好的,目前我们需要的是做反汇编。在 GDB 中输入 layout asm,映入眼帘的是这个画面,一串 x86-64 Linux ABI 的指令:
disassembly
通过移动方向键可以一览整个炸弹的函数接口名称,我们往下翻就能翻到 <phase_1>,于是我们在那里利用 break phase_1 打个断点,接着 run 运行程序:
Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
好的,进入了这个令人心惊胆战的二进制炸弹了。接着输入 continue 在我们打的断点那里停下来:
phase_1
可以看到确实停在了 phase_1 的入口,我们接下来就可以仔细研究这段反汇编程序了:
sub$0x8,%rsp
mov$0x402400,%esi
callq 0x401338 <strings_not_equal>
test %eax,%eax
je0x400ef7 <phase_1+23>
callq 0x40143a <explode_bomb>
add$0x8,%rsp
ret
栈指针
sub $0x8,%rsp 就是把栈指针(Register Stack Pointer,寄存器 rsp)减 8,给局部变量开辟栈空间,并且保持 16 字节对齐——因为 main 在调用 phase_1 的时候,已经提前把 phase_1 的返回地址(占用 8 字节)压入栈中,使得栈指针本身就偏离了 8 个字节。
《C 语言程序设计》似乎在之前的某篇文章里辣评了一下,这里不说了。但不得不承认的是,这门课发挥了它应有的价值:虽然学校自己的教授方法落后,作业考试大部分只是 intellectual masturbation,用的 IDE 是 Dev C++ 这种。为了让自己写得舒服点,我迫使自己掌握了 VS Code / VS + Git 的工作流,并且顺便熟悉了 Linux 操作系统等等。
所以,如果没有这么一套课程,我没有那么强烈的动力去做这些事情——这些事情其实就是 MIT 那套大名鼎鼎的 The Missing Semester of Your CS Education 课程会讲授的内容,但是我一直都没机会(或者说不敢/不想)上手做一遍。