Part 39 Buffer Overflow
Extracting and Analyzing CFG with angr
So far, we have learned how to examine the program execution paths with Symbolic Execution and find the appropriate input to reach a specific path with angr
But before we go into analyzing the paths, it is better to understand the overall structure of the program
This is where CFG or Control Flow Graph comes into play
CFG
is a graphical representation of the program execution flow and shows us how the different instructions and blocks are connected
Using CFG, we can better understand the structure of conditions, loops, jumps and the relationship between functions
CFG
is made up of what parts?
In a CFG, we usually deal with two main concepts
Basic Block
A Basic Block is a set of instructions whose execution starts from the beginning and continues sequentially
Usually, a Basic Block has only one entry point and its execution flow is transferred to another section through the final instruction
Edge
Edge
shows the relationship between two Basic Blocks
For example, when there is a condition in the program, the program execution may be divided into two different paths
Basic Block A
Check the condition
True
Basic Block B
First path
False
Basic Block C
Second path
In fact, Nodes are the Basic Blocks and Edges determine the relationship between them
Creating a CFG with angr
To start, we consider a simple executable file
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
if (argv[1][0] == 'A') {
puts("Path A");
} else {
puts("Path B");
}
puts("Finished");
return 0;
}
Compile the program
gcc -O0 -fno-pie -no-pie sample.c -o sample
Now we create a Python script called
cfg.py
import angr
project = angr.Project(
"./sample",
auto_load_libs=False
cfg = project.analyses.CFGFast()
print("Number of nodes:", len(cfg.graph.nodes))
print("Number of edges:", len(cfg.graph.edges))
Here we used CFGFast
This analysis tries to extract the CFG of the program by examining the structure of the executable file, instructions and information about jumps
Its main advantage is high speed and is also useful for initial examination of large programs
Examination of Basic Blocks
After creating the CFG, we can examine the blocks in the program
for node in cfg.graph.nodes():
print(
"Address:", hex(node.addr),
"Size:", node.size,
"Function:", node.function_address
)
The information we get includes
addr Basic Block starting address
size Block size in bytes
function_address Function address to which the block belongs
Of course, some blocks may not be assigned to a specific function or their function information may not be complete
Checking the connection between blocks
Now we want to see what blocks each block is connected to
for source, destination in cfg.graph.edges():
print(
hex(source.addr),
"->",
hex(destination.addr)
)
The output shows the connection between the Nodes
For example, if the output includes this connection
0x401150 -> 0x401160
0x401150 -> 0x401180
0x401160 -> 0x401190
0x401180 -> 0x401190
We can see that block 0x401150 has two output paths and both paths eventually reach block 0x401190
This structure usually corresponds to a condition and two execution branches
Checking the CFG of a specific function
Sometimes we don't need to analyze the entire program and we just want to check the structure of a specific function
For this we can use the function address
main_function = cfg.kb.functions["main"]
print("Main address:", hex(main_function.addr))
print("Blocks:", len(main_function.blocks))
for block in main_function.blocks:
print(
hex(block.addr),
block.size
)
This code finds the main function from the information extracted by angr and displays the Basic Blocks related to it
This method is useful when Let's focus on a specific part of the program