Reverse Engineering Portable Executable files
Understanding basics of RE
Beginning to analyze PE files
N Ananthakrishnan Potti
InfoSec Enthusiast
About Me
What is Reverse Engineering
Why should you learn it?
What is Reverse Engineering?
Reverse Engineering is the process of extracting the knowledge or design blue-prints from anything man-made.
To take some product and break it down in order to understand how it was produced
In Restaurant: tasting a delicious dish and trying to recreate it at home
Reverse Engineering is challenging
Real life software might be
ASM overview
Now begins the fun!
What is assembly?
Basically,
if(x == 4){
func1();
}else{
return;
}
mov RAX, x
cmp RAX, 4
jne 5 ; Line 5 (ret)
call func1
ret
Compilation and Disassembly
A malware author writes code in any high-level language, say C.
The C code is compiled into machine code – a series of bytes that the computer understands.
The researcher usually has no access to the C code file, only to the bytes of the machine code.
To make life easier, a disassembler translates those bytes into an easier to read and textual representaion
Registers
Let's talk about General Purpose Registers (GPR).
You can think of these as variables because that's essentially what they are.
Your CPU has it's own data storage that is extremely fast. This is helpful, however, space in the CPU is extremely limited.
Any data the CPU can't store on its own is stored in memory. Memory (RAM) is much slower for the CPU to use.
Because of the slow speed, the CPU tries to put data in registers instead of memory if it can.
General Purpose register
Instructions
The ability to read and comprehend Assembly code is vital to reverse engineering. There are roughly 1,500 instructions, however, a majority of the instructions are not commonly used or they're just variations (such as MOV and MOVS).
There are three different terms you should know:
What do you think these assembly code do?
They do these…
Memory
There are different segments/sections in which data or code is stored. They are laid out in the following order:
Stack - Holds non-static local variables.
Heap - Contains dynamically allocated data that can be uninitialized at first.
.data - Contains global and static data initialized to a non-zero value.
.bss - Contains global and static data that is uninitialized or initialized to zero.
.text - Contains the code of the program (don't blame me for the name, I didn't make it).
Let us take a look at some tools
Portable Executable format
What is inside a PE file?
PE?
PE stands for Portable Executable, it’s a file format for executables used in Windows operating systems, it’s based on the COFF file format (Common Object File Format).
Not only .exe files are PE files, dynamic link libraries (.dll), Kernel modules (.srv), Control panel applications (.cpl) and many others are also PE files.
A PE file is a data structure that holds information necessary for the OS loader to be able to load that executable into memory and execute it.
Contents of PE file
1. DOS Header: Every PE file starts with a 64-bytes-long structure called the DOS header, it’s what makes the PE file an MS-DOS executable.
2. DOS stub: small MS-DOS 2.0 compatible executable that just prints an error message saying “This program cannot be run in DOS mode” when the program is run in DOS mode.
Contents of PE file contd.
3. NT Headers: contains 3 parts
- PE signature: A 4-byte signature that identifies the file as a PE file.
- File Header: A standard COFF File Header. It holds some information about the PE file.
- Optional Header: The most important header of the NT Headers, its name is the Optional Header because some files like object files don’t have it, however it’s required for image files (files like .exe files). This header provides important information to the OS loader.
4. Section table: The section table follows the Optional Header immediately, it is an array of Image Section Headers, there’s a section header for every section in the PE file.
Each header contains information about the section it refers to.
Contents of PE file contd..
5. Sections: it is where the actual contents of the file are stored, these include things like data and resources that the program uses, and also the actual code of the program, there are several sections each one with its own purpose.
Let’s see how it is done!
THANK YOU
References