English
Plan
- Three chapters of The IDA Pro Book
- Ten chapters of Reverse Engineering for Beginners
- C++ classes when I have time
Reverse Engineering for Beginners
Part One: Explaining Instructions
Chapter 1: Introduction to the CPU
This chapter didn't cover anything too important; it introduced CPU architectures and so on, and then explained which architectures the book focuses on.
Important
Three kinds of arm instruction sets: the arm instruction set, the thumb mode instruction set, and the arm64 instruction set
Although these instruction sets are inextricably linked, it must be emphasized that different instruction sets belong to different instruction set architectures; an instruction set is by no means a variant of another instruction set.
Chapter 2: The Simplest Function
A function that returns a predetermined constant already counts as the simplest kind of function.
c
int f()
{
return 123;
}2.1 x86
assembly
f:
mov eax, 123
ret
#这个函数仅由两条指令构成:第一条指令把数值123存放在EAX寄存器里;根据函数调用约定①,后面一条指令会把EAX的值当作返回值传递给调用者函数,而调用者函数(caller)会从EAX寄存器里取值,把它当作返回结果2.2 ARM
assembly
f PROC # PROC过程标识符,作用就是声明
MOV r0,#0x7b ; 123
BX lr
ENDP #表示结束
# proc是定义子程序的伪指令,位置在子程序的开始处,它和endp分别表示子程序定义的开始和结束两者必须成对出现。2.3 MIPS
Important
Why are the positions of the assignment instruction LI and the jump instructions J/JR reversed? This is one of the characteristics of the RISC instruction set — the branch delay slot phenomenon. Put simply, whether or not the branch (jump) is taken, the instruction located after the branch instruction (the instruction in the delay slot) is always committed before the branch instruction. This is a special case of the RISC instruction set, and we don't need to dig into it here. In short, the assignment instruction after the jump instruction actually runs before the jump instruction.
In MIPS instructions, registers have two naming schemes. One is numeric naming ($0~
assembly
j $31
li $2,123 # 0x7bTip
jis the jump instruction (Jump) in MIPS assembly language, used to unconditionally jump to a specified address to execute code.$31is a special register in the MIPS register set, namedra(Return Address), usually used to store the return address when a subroutine returns.liis short for the load immediate (Load Immediate) instruction in MIPS assembly language, used to load an immediate value (that is, a number written directly in the instruction) into a specified register.$2is a general-purpose register in the MIPS register set.
jr $ra
li $v0, 0x7BTip
$v0is a general-purpose register in the MIPS register set, but in the MIPS ABI (Application Binary Interface) it is often used as the return-value register for system calls. However, in ordinary MIPS assembly code it can also be used like any other general-purpose register.- The
jinstruction jumps directly to a specified address or label, while thejrinstruction jumps to an address stored in a register. Thejinstruction is more suitable for unconditionally jumping to any position in a program, while thejrinstruction is usually used to return from a function call, by jumping back to the return address saved in the$raregister.
Chapter 3: Hello, world!
Here is the most famous program
c
#include <stdio.h>
int main(){
printf("hello, world\n");
return 0;
}3.1 x86
3.1.1 MSVC
assembly
CONST SEGMENT
$SG3830 DB 'hello, world', 0AH, 00H ; 字符串 'hello, world' 加上换行符和字符串结束符
CONST ENDS
PUBLIC _main
EXTRN _printf:PROC ; 声明外部过程 _printf
_TEXT SEGMENT
_main PROC
push ebp ; 保存旧的 ebp
mov ebp, esp ; 设置新的栈帧
push OFFSET $SG3830 ; 将字符串的地址压入栈中
call _printf ; 调用 printf 函数
add esp, 4 ; 清理栈(这里假设 _printf 消耗了 4 字节的栈空间)
xor eax, eax ; 设置 eax 为 0,表示程序正常退出
pop ebp ; 恢复旧的 ebp
ret ; 返回
_main ENDP
_TEXT ENDS
END ; 表示文件结束Tip
OFFSETis an operator used to obtain the offset address in memory of a label, variable name, or constant.We can see that the compiler appended a hexadecimal 0, i.e. 00h, to the end of the string constant. According to the standard specification for C/C++ strings, the compiler must add a terminator (a single byte with the value zero) to this string constant.
The source code of the above program is equivalent to:
c
#include <stdio.h>
// 声明一个指向 const char 的指针,指向字符串 "hello, world\n"
const char *SG3830 = "hello, world\n";
int main() {
// 使用 printf 函数打印字符串
printf("%s", SG3830);
// 或者更简单地,因为 SG3830 已经是一个指向字符串的指针
// printf(SG3830);
return 0;
}3.1.2 GCC
Assembly instructions observed in IDA
assembly
Main PROC NEAR
var_10 = dword ptr -10h
push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 10h
mov eax, offset aHelloWorld ; "hello, world\n"
mov [esp+10h+var_10], eax ; 注意:这行代码的意图可能不清晰或错误
call _printf
mov eax, 0
leave
retn
Main ENDP
; 假设aHelloWorld是在其他地方定义的字符串常量
; aHelloWorld DB 'hello, world\n', 0 ; 字符串定义通常包括末尾的0作为结束符Tip
and esp, 0FFFFFFF0hthis instruction is used to perform a bitwise AND on the stack pointer registerESP, with the goal of rounding the value ofESPdown to the nearest 16 bytes; put plainly, it clears the low four bits in binary. If the address is not aligned, the CPU may need to access memory twice to obtain the data on the stack. Although alignment on an 8-byte boundary is enough to satisfy 32-bit x86 CPUs and 64-bit x64 CPUs, the compilation rules of mainstream compilers stipulate that "addresses accessed by a program must be aligned to 16 bytes (divisible by 16)". People still laid down this compilation rule specifically to improve instruction execution efficiency.- The LEAVE instruction is equivalent to the two instructions " MOV ESP, EBP " and " POP EBP ". It can be seen that this instruction adjusts the data stack pointer ESP and restores the value of EBP to its initial state before this function was called.
3.1.3 GCC: AT&T Syntax
The AT&T syntax is likewise a display style of assembly language. This syntax is fairly common in UNIX.
The program obtained after removing a large number of assembly macros
assembly
.LC0: .string "hello, world\n"
main:
pushl %ebp ; 保存旧的基指针
movl %esp, %ebp ; 将栈指针的值复制到基指针,设置新的栈帧
andl $-16, %esp ; 将栈指针向下调整到16字节的边界
subl $16, %esp ; 为局部变量和可能的函数调用预留空间
movl $.LC0, (%esp) ; 将字符串的地址压入栈中,作为printf的参数
call printf ; 调用printf函数打印字符串
movl $0, %eax ; 设置返回值0,表示程序成功执行
leave ; 恢复旧的栈帧(mov %ebp, %esp; pop %ebp)
ret ; 返回调用者Important
The operands are written in the opposite order.
Intel format:
<instruction><destination><source>AT&T format:
<instruction><source><destination>In AT&T syntax, a percent sign (%) is used before register names, and a dollar sign ($) before immediate values. AT&T syntax uses parentheses, while Intel syntax uses square brackets.
In AT&T syntax, every operation requires declaring the type of the data being operated on:
q - quad (64-bit)
l refers to 32-bit long type data.
w refers to 16-bit word type data.
b refers to 8-bit byte type data.
