┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
The Fuzzy Notebook
~ CuB3y0nd
# Prologue
 `Pwn Squad`  `Lost Squad`


:::note
 fuzzer

:::

# Concept

 [Frequently asked questions (FAQ)](https://aflplus.plus/docs/faq/
)

-  ** (Function)**
-  ** (Basic Block)** 
-  ** (Entry Point)**
线


 **A****B****C****D****E** 

```plaintext showLineNumbers=false
function() {
  A:
    some
    code
  B:
    if (x) goto C; else goto D;
  C:
    some code
    goto E
  D:
    some code
    goto B
  E:
    return
}
```

** (Edge)** 

```plaintext showLineNumbers=false
              Block A
                |
                v
              Block B  <------+
            /               |
            v          v      |
        Block C    Block D --+
            
              v
              Block E
```

# Demo

 Fuzz 

```c
#include <stdio.h>
#include <stdlib.h>

int isBigPrime(int n) {
  if (n <= 5)
    return 0;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}

int main(void) {
  char s[35];
  scanf("%s", s);

  char cnt[300] = {0};

  for (int i = 0; s[i]; i++) {
    cnt[s[i]]++;
    if (s[i] < 'x' || s[i] > 'z') {
      puts("unacceptable");
      return 0;
    }
  }

  if (isBigPrime(cnt['x']) && isBigPrime(cnt['y']) && isBigPrime(cnt['z']))
    abort();

  puts("Nice string");

  return 0;
}
```



- **** `x``y``z` 
 unacceptable 退
- ****使 `cnt`  `x``y``z` 
- ****
  - `isBigPrime`  5  (e.g. 7, 11, 13, 17 etc.)
  -  `x``y`  `z`  5  `abort`
- ** Bug**
  - `scanf` 

 [Selecting the best AFL++ compiler for instrumenting the target](https://gi
thub.com/AFLplusplus/AFLplusplus/blob/stable/docs/fuzzing_in_depth.md#a-selectin
g-the-best-afl-compiler-for-instrumenting-the-target)  `afl-clan
g-lto`  ** (Instrumentation)** 

使

```shellsession
afl-clang-lto ./test.c -o test
```

 `inputs` 

```shellsession
λ ~/Projects/Fuzz/ cat inputs/text/*
aaaabaaacaaadaaaeaaa
helloworld
Hello world!
ahfoer
```

 AFL++ 


 Arch  fuzzer 便
使

```shellsession
sudo afl-system-config
```

使

```shellsession
afl-fuzz -i inputs -o out/ -- ./test
```

 6  crash 1min  abort  c
rash 
 `out/default/crashes`  10 

`check` 

```bash
#!/usr/bin/env bash

for f in out/default/crashes/id:*; do
  echo "==== $f ===="
  # hexdump -C "$f" | head
  ./test <"$f"
done
```

# Fuzzing-Module

[Fuzzing-Module](https://github.com/alex-maleno/Fuzzing-Module)  AFL++ [
](https://github.com/AFLplusplus/AFLplusplus?tab=readme-ov-file#tutorials)
 3  exercisesspeedrun 

## Exercise 1



```cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main() {

  string str;

  cout << "enter input string: ";
  getline(cin, str);
  cout << str << endl << str[0] << endl;

  if (str[0] == 0 || str[str.length() - 1] == 0) {
    abort();
  } else {
    int count = 0;
    char prev_num = 'x';
    while (count != str.length() - 1) {
      char c = str[count];
      if (c >= 48 && c <= 57) {
        if (c == prev_num + 1) {
          abort();
        }
        prev_num = c;
      }
      count++;
    }
  }

  return 0;
}
```

使 `CC=afl-clang-lto CXX=afl-clang-lto++ cmake -S . -B build` 
 `cmake --build build` 

 crash  fuzz 

1. `str[0] == x00`
2. `str[str.length() -1] == x00`
3. 
4. `n``EOF`
 Exercise 1 使 5  seeds

```bash
#!/usr/bin/env bash

mkdir seeds
for i in {0..4}; do
  dd if=/dev/urandom of=seeds/seed_"$i" bs=64 count=10
done
```

 `afl-fuzz -i seeds -o out/ -m 0 -- ./build/simple_crash`
 crash 
 Case 2 Case 1 Case 3

:::important
 Case  crash  Undefined Behaviour AFL+
+  flaky UB  /  / 
 crash 

 crash 使

```c showLineNumbers=false
if (str.length() == 0)
  abort();
```

 Sanitizer fuzzer 


```bash showLineNumbers=false
#!/usr/bin/env bash

cmake -S . -B build 
  -DCMAKE_C_COMPILER=afl-clang-lto 
  -DCMAKE_CXX_COMPILER=afl-clang-lto++ 
  -DCMAKE_C_FLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" 
  -DCMAKE_CXX_FLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined
"
```

 **AddressSanitizer (ASan)**  **UndefinedBehav
iorSanitizer (UBSan)** fuzz 

```shellsession
export ASAN_OPTIONS=abort_on_error=1:symbolize=0:detect_leaks=0
export UBSAN_OPTIONS=abort_on_error=1
```

debug 使

```shellsession
export ASAN_OPTIONS=abort_on_error=1:symbolize=1:detect_leaks=0
export UBSAN_OPTIONS=abort_on_error=1:print_stacktrace=1
```

:::

## Exercise 2

 abort,  `ffl` 

```cpp
} else if (input[i] == 'l') {
    if (crew.num == 0) {
        abort();
    }
    land();
}
```

使 input  200  De Bruijn Sequence沿 Exercise 1
  seeds 
 crashes,  `ffl`  `f`  `h` 

## Exercise 3

 abort points:

- `choose_color`: 
- `min_alt`:  0
- `min_airspeed`:  0
- `fuel_cap`:  0
- `check_alt`:  `alt`  0
- `check_fuel`:  `fuel`  0
- `check_speed`:  `speed`  0

 `cmake`  `-DCMAKE_EXPORT_CO
MPILE_COMMANDS=1`  `compile_commands.json`使 [
Sourcetrail](https://github.com/CoatiSoftware/Sourcetrail) 
 nvim ……


 template

```cpp
/*
 * This file isolates the Specs class and tests out the
 * choose_color function specifically.
 */

#include "specs.h"

int main(int argc, char **argv) {
  // In order to call any functions in the Specs class, a Specs
  // object is necessary. This is using one of the constructors
  // found in the Specs class.
  Specs spec(505, 110, 50);
// By looking at all the code in our project, this is all the
// necessary setup required. Most projects will have much more
// that is needed to be done in order to properly setup objects.

// This section should be in your code that you write after all the
// necessary setup is done. It allows AFL++ to start from here in
// your main() to save time and just throw new input at the target.
#ifdef __AFL_HAVE_MANUAL_CONTROL
  __AFL_INIT();
#endif

  spec.choose_color();
  // spec.min_alt();

  return 0;
}
```

 `__AFL_HAVE_MANUAL_CONTROL`  fuzz 

 `choose_color`
 `x20`

```cpp
std::cin >> color;
if (isNumber(color))
  abort();

bool Specs::isNumber(std::string str) {
  for (int i = 0; i < str.length(); i++) {
    if (isdigit(str[i]) == 0)
      return false;
  }
  return true;
}
```

`choose_color`  `cin >>`  `>>`  spaces
 space  `x20` 
 `color = ""`for  true 

 slice fuzz `min_airspeed`  `exec speed: 55.54
/sec` o_O

 fuzzer  exec 


```cpp
void Specs::min_airspeed() {
  bool out_of_bounds = true;
  std::cout << "enter aircraft minimum airspeed: ";
  std::cin >> speed;
  do {
    out_of_bounds = false;
    if (speed < 0)
      abort();
    if (speed < 100) {
      std::cout << "too low. please re-enter: ";
      std::cin >> speed;
      out_of_bounds = true;
    } else if (speed > 200) {
      std::cout << "too high. please re-enter: ";
      std::cin >> speed;
      out_of_bounds = true;
    }
  } while (out_of_bounds);
}
```

 crash 
 `>>`  `1-3113x09` 
 `1`  `-3113` 
 `speed < 100`  ab
ort 

 check 


# Fuzzing101

 [Fuzzing101](https://github.com/antonio-morales/Fuzzing101) Exerc
ises 

- [Exercise 1 - Xpdf](/posts/fuzz/xpdf-cve-2019-13288/)
- [Exercise 2 - libexif](/posts/fuzz/libexif/)
- [Exercise 3 - TCPdump](/posts/fuzz/tcpdump-cve-2017-13028/)
- [Exercise 4 - LibTIFF](/posts/fuzz/libtiff-cve-2016-9297/)
- [Exercise 5 - libxml2](/posts/fuzz/libxml2-cve-2017-9048/)