1 730
Part 27 Buffer OverflowlibFuzzer and Coverage Guided Fuzzing So far, we have understood that Fuzzing means giving a lot of different inputs to the program and waiting for it to mess up somewhere😁 But new Fuzzers don't just generate random inputs. Some of them check what paths each input has taken in the program, which makes it gradually generate more interesting inputs What does Coverage Guided mean Suppose a program looks like this
Input ↓ Check 1 ↓ Check 2 ↓ Hidden FunctionIf the first input only reaches Check 1 It tries to change the input path so that a new Fuzzer opens For example, it reaches Check 2 Then it uses the same input again and makes more changes The goal is to run more parts of the program because apparently we decided to go everywhere in the program to find the bug 😅 libFuzzer What does libFuzzer do A Fuzzing Engine for C Programs And it's C++ that works with LLVM and Clang. We give it a specific function. Then it runs that function over and over again with different inputs. Each input that leads to a new path becomes more valuable. The main Fuzzing function Usually we have something like this: C
#include <stdint.h> #include <stddef.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { return 0; }Explanation of the code below This function is the target of the Fuzzer Every time libFuzzer generates a new input, the content of the input is placed in data and its size is placed in size A simple example: C
#include <stdint.h> #include <stddef.h> #include <string.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { if (size >= 5) { if (memcmp(data, "HELLO", 5) == 0) { volatile int x = 1; (void)x; } } return 0; }What happens here The Fuzzer trys different inputs For example
AAAAthen
HELAAthen maybe
HELLOWhen the input reaches HELLO a new path is executed from the program Coverage Guided Fuzzing detects this new path and stores that input to use for finding subsequent paths Compile with Clang: shell
clang -g -fsanitize=fuzzer,address file23_fuzz.c -o file23_fuzzHere two things are enabled together
libFuzzer + AddressSanitizerlibFuzzer generates input ASan watches for memory errors This combination is very powerful for finding memory bugs Run Fuzzer shell
./file23_fuzzThen the program starts generating and modifying inputs If the input causes a crash, usually the same input is saved so that we can check it again later Let's do Why is it important to us? Suppose you have a complex program But you don't know exactly what input will cause a critical function to be reached Fuzzer can find new paths by trying different inputs Then you can examine those paths in Ghidra or IDA That is:
Fuzzing ↓ New Code Path ↓ Crash or Behavior ↓ Ghidra / IDA ↓ Assembly AnalysisCoverage Guided Fuzzing It doesn't just look for crashes It also looks for new paths Each new path is a new part of the program that is worth examining and when we combine libFuzzer with ASan we can both generate smarter inputs and detect memory bugs faster Exercise: Change the above function a little and add a new condition for a specific input then think about how the Fuzzer should change the input step by step to reach that new path @reverseengine
25 8月, 18:27

