0Pricing
Reverse Engineering & Binary Analysis Basics · 课时

内存与寄存器检查

练习检查内存区域、查看寄存器值,并在程序执行期间修改程序状态。

内存与寄存器检查 是 CoddyKit 上的免费 Reverse Engineering & Binary Analysis Basics 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Reverse Engineering & Binary Analysis Basics 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Reverse Engineering & Binary Analysis Basics 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Debugging's Core: Memory & Registers

When analyzing programs, especially during dynamic analysis, understanding what's happening inside the CPU is key. This means looking at registers and memory.

These are the CPU's direct workspaces, holding data and instructions that are actively being processed.

CPU's Scratchpad: Registers

Registers are tiny, super-fast storage locations directly within the CPU itself. Think of them as the CPU's "scratchpad" where it keeps data it needs immediately.

  • They hold temporary values, addresses, and control information.
  • Accessing data in registers is much faster than accessing RAM.
  • Different architectures (like x86, ARM) have different sets of registers.

Common x86/x64 Registers

While there are many registers, some are crucial for reverse engineering:

  • General-Purpose: RAX/EAX, RBX/EBX, RCX/ECX, RDX/EDX (used for data, function arguments, return values).
  • Stack Pointer: RSP/ESP (points to the top of the stack).
  • Base Pointer: RBP/EBP (points to the base of the current stack frame).
  • Instruction Pointer: RIP/EIP (points to the next instruction to execute).

Viewing Registers in GDB

Let's see how to inspect registers using a debugger like GDB. We'll use a simple C program.

First, compile with debug info (-g): gcc -g -o myprog myprog.c

After compiling and starting GDB (e.g., gdb -q ./myprog), you can set a breakpoint (break main), run (run), and then use info registers.

    #include <stdio.h>

    int main() {
        int a = 10;
        int b = 20;
        int sum = a + b;
        printf("Sum: %d\n", sum);
        return 0;
    }

Program's Workspace: Memory

Memory (RAM) is where your program stores larger amounts of data that aren't actively being processed by the CPU. This includes variables, program code, and other resources.

Every byte in memory has a unique address. When a program runs, it gets its own dedicated "virtual" memory space.

Simplified Memory Layout

A program's memory is typically divided into sections:

  • Text/Code Segment: Contains the executable instructions.
  • Data Segment: Stores global and static variables.
  • Heap: Used for dynamically allocated memory (e.g., with malloc).
  • Stack: Used for local variables, function arguments, and return addresses.

Viewing Memory in GDB

To inspect memory in GDB, we use the x command (examine memory). It has a flexible syntax:

  • x /NFS ADDRESS
  • N: Number of units to display (optional).
  • F: Format (e.g., x for hex, d for decimal, s for string, i for instruction).
  • S: Size (e.g., b for byte, h for halfword (2 bytes), w for word (4 bytes), g for giant (8 bytes)).

Example: Viewing a Stack Variable

Let's use our previous program. Compile it and set a breakpoint before printf. Then, we can find the address of sum and examine its content.

Run this code, then attach GDB (gdb -q ./myprog), set a breakpoint at line 7 (break main.c:7), and run (run).

In GDB: p &sum to get its address. Finally, x /w ADDRESS_OF_SUM to view its 4-byte value.

    #include <stdio.h>

    int main() {
        int a = 10;
        int b = 20;
        int sum = a + b; // Breakpoint here
        printf("Sum: %d\n", sum);
        return 0;
    }

Changing Register Values

A powerful debugging technique is to modify register values on the fly. This can change how a program behaves without altering its code.

In GDB, you can use the set command:

  • set $rax = 0x1234
  • set $rip = *0x400500 (jump to a new address)

This is useful for bypassing checks or redirecting execution flow.

Altering Memory Content

Just like registers, you can also modify memory content while debugging. This allows you to change variable values, strings, or even instructions in memory.

Using GDB's set command:

  • set var_name = new_value (if the variable is in scope)
  • set {int}0x400000 = 123 (change 4 bytes at address 0x400000 to 123)

Be careful, incorrect modifications can crash the program!

Debugger Challenge

You're debugging a program. You want to see the value of a 4-byte integer variable named counter located at memory address 0x7fffffff0000. What GDB command would you use?

Recap: Debugging's Core

Today, we explored how to examine and modify the core components of a running program: registers and memory.

  • Registers are CPU's fast storage, viewed with info registers.
  • Memory holds larger data, viewed with x /NFS ADDRESS.
  • Both can be modified with set to alter program state dynamically.

These skills are fundamental for understanding program execution and reverse engineering!

常见问题解答

「内存与寄存器检查」课时是免费的吗?

是的 — 「内存与寄存器检查」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Reverse Engineering & Binary Analysis Basics 课程的其余内容,请升级到 CoddyKit PRO。 Reverse Engineering & Binary Analysis Basics 课程共包含 4 节课。

「内存与寄存器检查」这节课中我会学到什么?

练习检查内存区域、查看寄存器值,并在程序执行期间修改程序状态。 你通过在浏览器中直接运行的动手代码来练习 Reverse Engineering & Binary Analysis Basics,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Reverse Engineering & Binary Analysis Basics 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Reverse Engineering & Binary Analysis Basics 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「内存与寄存器检查」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Reverse Engineering & Binary Analysis Basics 课中编写并运行代码吗?

能。每节 Reverse Engineering & Binary Analysis Basics 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 调试器基础(GDB、WinDbg)
  2. 设置断点与单步执行
  3. 内存与寄存器检查
  4. 运行时跟踪 API 与系统调用
← 返回 Reverse Engineering & Binary Analysis Basics