【软件安全】Linux GDB在软件安全中的概念和应用
Linux & GDB 基础笔记(中英文对照 + 通俗解释 + 例题)
结合你上传的文档内容(如《Tutorial-1》和《Tutorial-2》),下面是一份完整的论坛笔记风格总结,帮助大家系统理解 Linux 命令与 GDB 调试。
,。 一、Linux 基础概念 / Linux Basics
English:
Linux is an open-source operating system used in servers, security research, and development because it gives users full control of files, memory, and processes.
中文:
Linux 是一个开源操作系统,广泛应用于服务器、安全研究和软件开发中,因为它允许用户完全控制文件、内存和进程。
💡 比喻理解 / Metaphor
Linux 就像一辆手动挡跑车,你要自己换挡、控制油门。虽然复杂,但能精确掌控每一步。
Linux is like a manual sports car — more control, but you need to know what each lever does.
常用命令 / Essential Commands
| 命令 | 功能 | 英文解释 |
|---|---|---|
ls | 列出目录内容 | List directory contents |
cd | 切换目录 | Change current directory |
mkdir | 创建目录 | Make a new directory |
cp, mv, rm | 拷贝 / 移动 / 删除文件 | Copy / move / remove files |
man <cmd> | 查看命令手册 | Display manual for a command |
chmod +x | 赋予执行权限 | Give execution permission |
sudo apt install | 安装软件 | Install packages |
二、GDB 概念与作用 / GDB (GNU Debugger)
English:
GDB is the GNU Debugger, a powerful tool to see what’s happening “inside” your program while it runs — like an X-ray for your code.
中文:
GDB 是 GNU 调试器,它能让你像照 X 光一样观察程序在运行中的内部状态,如内存、寄存器和变量的变化。
比喻 / Metaphor
GDB 就像游戏里的**“暂停 + 回放”功能**:
- 你可以暂停在某个点(breakpoint)
- 逐步播放程序(step)
- 查看角色状态(registers、stack)
🧾 常用命令 / Common GDB Commands
| 命令 | 含义(中文) | 含义(英文) |
|---|---|---|
gdb program | 启动调试程序 | Start debugging |
break main | 在 main 设置断点 | Set a breakpoint |
run | 运行程序直到断点 | Run until breakpoint |
next / nexti | 执行下一行 / 下一条指令 | Step over / step instruction |
info registers | 查看寄存器状态 | Show registers |
x/40x $esp | 查看堆栈内容 | Examine 40 hex words on stack |
disassemble main | 反汇编函数 | Show assembly code |
quit | 退出 GDB | Quit debugger |
常见寄存器解释 / Registers Simplified
| 寄存器 | 含义 | 类比 |
|---|---|---|
| EAX | 存储计算结果 | 计算器的“结果屏幕” |
| EBP | 栈基指针(函数起点) | 书签标记每个函数的开始 |
| ESP | 栈顶指针(函数结束处) | 当前任务堆叠的最顶端 |
| EIP | 当前执行的指令位置 | 播放器中的“当前帧” |
Stack/Memory View
English:
In GDB, you can check what’s inside memory at a given address. For example:
x/40x $esp — view 40 words from the current stack pointer.
中文:
在 GDB 中可查看指定内存内容。例如:
x/40x $esp 表示从栈顶位置开始,查看 40 个十六进制单元。
三、选择题(Multiple Choice)×5
Q1:
Which of the following best describes GDB?
以下哪项最能描述 GDB?
A. A compiler
B. A text editor
C. A debugger ✅
D. A disassembler
✅ Correct: C — GDB is used to debug running programs.
❌ Wrong options:
A 编译器是 GCC;B 编辑器如 Vim;D 反汇编器功能仅为 GDB 的一部分。
Q2:
Command break main in GDB means:
GDB 中 break main 的含义是:
A. Stop program immediately
B. Set a breakpoint at main ✅
C. Print main function
D. Exit GDB
✅ Correct: B
❌ Wrong: A 不会立刻停止,C 是 list main,D 是 quit。
Q3:
What does info registers show?
info registers 显示什么?
A. CPU memory
B. Stack layout
C. Register values ✅
D. Variables in C
✅ Correct: C — It lists all CPU registers and their values.
❌ A 是内存,B 是 info stack,D 是 print var。
Q4:
What is the use of nexti in GDB?
nexti 命令的作用是?
A. Run program
B. Execute next machine instruction ✅
C. Step over next function
D. Show stack
✅ Correct: B
❌ A 应为 run,C 是 next,D 是 info stack。
Q5:
How do you view assembly of a function in GDB?
在 GDB 中查看函数汇编代码的命令是?
A. view main
B. dump main
C. disassemble main ✅
D. asm main
✅ Correct: C
❌ 其他命令无效。
🧠 四、简答题(Short Answer)×5
Q1:
What is GDB used for?
GDB 的用途是什么?
Answer: To debug programs — view variables, memory, and step through code.
答案: 用于调试程序,查看变量、内存、逐步执行代码。
Q2:
What’s the difference between next and nexti?
next 与 nexti 的区别?
Answer: next runs one source line; nexti runs one assembly instruction.
答案: next 执行一行源码;nexti 执行一条机器指令。
Q3:
How do you check stack contents?
如何查看堆栈内容?
Answer: Use x/40x $esp to display 40 hexadecimal words from the stack top.
答案: 用 x/40x $esp 查看从栈顶开始的 40 个十六进制单元。
Q4:
Why use the -g flag in GCC?
为什么在 GCC 编译时加 -g?
Answer: It includes debugging info so GDB can map machine code back to source lines.
答案: 加入调试信息,方便 GDB 将机器代码对应到源码。
Q5:
Explain what happens when you type run in GDB.
解释在 GDB 输入 run 会发生什么。
Answer: The program executes until it hits a breakpoint or finishes.
答案: 程序开始运行,直到遇到断点或执行完毕。
五、总结 / Summary
English:
Linux provides a stable environment for security testing, and GDB is your microscope to look inside the code. Together, they help understand memory, stack, and vulnerabilities deeply.
中文:
Linux 是安全研究的稳定平台,GDB 则是查看程序内部的显微镜,两者结合能让你深度理解内存、堆栈和漏洞原理。
