Skip to content

Commit

Permalink
added honggfuzz custom mutator :)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanhauser-thc committed Jul 23, 2020
1 parent 2ba88dc commit 72b46a0
Show file tree
Hide file tree
Showing 14 changed files with 1,832 additions and 0 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Roadmap 2.67+

- -i - + foreign fuzzer sync support: scandir with time sort
- pre_save custom module example to save away test cases
- expand on AFL_LLVM_INSTRUMENT_FILE to also support sancov allowlist format
- allow to sync against honggfuzz and libfuzzer
Expand Down
15 changes: 15 additions & 0 deletions custom_mutators/honggfuzz/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

CFLAGS = -O3 -funroll-loops -fPIC -Wl,-Bsymbolic

all: honggfuzz.so

honggfuzz.so: honggfuzz.c input.h mangle.c ../../src/afl-performance.c
$(CC) $(CFLAGS) -I../../include -I. -shared -o honggfuzz.so honggfuzz.c mangle.c ../../src/afl-performance.c

update:
wget --unlink https://github.com/google/honggfuzz/raw/master/mangle.c
wget --unlink https://github.com/google/honggfuzz/raw/master/mangle.h
wget --unlink https://github.com/google/honggfuzz/raw/master/honggfuzz.h

clean:
rm -f *.o *~ *.so core
12 changes: 12 additions & 0 deletions custom_mutators/honggfuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# custum mutator: honggfuzz mangle

this is the very good honggfuzz mutator in mangle.c as a custom mutator
module for afl++. It is the original mangle.c, mangle.h and honggfuzz.h
with a lot of mocking around it :-)

just type `make` to build

```AFL_CUSTOM_MUTATOR_LIBRARY=custom_mutators/honggfuzz/honggfuzz.so afl-fuzz ...```

> Original repository: https://github.com/google/honggfuzz
> Source commit: d0fbcb0373c32436b8fb922e6937da93b17291f5
Empty file.
22 changes: 22 additions & 0 deletions custom_mutators/honggfuzz/custom_mutator_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CUSTOM_MUTATOR_HELPERS
#define CUSTOM_MUTATOR_HELPERS

#include "config.h"
#include "types.h"
#include "afl-fuzz.h"
#include <stdlib.h>

#define INITIAL_GROWTH_SIZE (64)

/* Use in a struct: creates a name_buf and a name_size variable. */
#define BUF_VAR(type, name) \
type * name##_buf; \
size_t name##_size;
/* this filles in `&structptr->something_buf, &structptr->something_size`. */
#define BUF_PARAMS(struct, name) \
(void **)&struct->name##_buf, &struct->name##_size

#undef INITIAL_GROWTH_SIZE

#endif

141 changes: 141 additions & 0 deletions custom_mutators/honggfuzz/honggfuzz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "custom_mutator_helpers.h"
#include "mangle.h"

#define NUMBER_OF_MUTATIONS 5

uint8_t * queue_input;
size_t queue_input_size;
afl_state_t * afl_struct;
run_t run;
honggfuzz_t global;
struct _dynfile_t dynfile;

typedef struct my_mutator {

afl_state_t *afl;
run_t * run;
u8 * mutator_buf;
unsigned int seed;
unsigned int extras_cnt, a_extras_cnt;

} my_mutator_t;

my_mutator_t *afl_custom_init(afl_state_t *afl, unsigned int seed) {

my_mutator_t *data = calloc(1, sizeof(my_mutator_t));
if (!data) {

perror("afl_custom_init alloc");
return NULL;

}

if ((data->mutator_buf = malloc(MAX_FILE)) == NULL) {

perror("mutator_buf alloc");
return NULL;

}

run.dynfile = &dynfile;
run.global = &global;
data->afl = afl;
data->seed = seed;
data->run = &run;
afl_struct = afl;

run.global->mutate.maxInputSz = MAX_FILE;
run.global->mutate.mutationsPerRun = NUMBER_OF_MUTATIONS;
run.mutationsPerRun = NUMBER_OF_MUTATIONS;
run.global->timing.lastCovUpdate = 6;

// global->feedback.cmpFeedback
// global->feedback.cmpFeedbackMap

return data;

}

/* When a new queue entry is added we check if there are new dictionary
entries to add to honggfuzz structure */

void afl_custom_queue_new_entry(my_mutator_t * data,
const uint8_t *filename_new_queue,
const uint8_t *filename_orig_queue) {

while (data->extras_cnt < data->afl->extras_cnt &&
run.global->mutate.dictionaryCnt < 1024) {

memcpy(run.global->mutate.dictionary[run.global->mutate.dictionaryCnt].val,
data->afl->extras[data->extras_cnt].data,
data->afl->extras[data->extras_cnt].len);
run.global->mutate.dictionary[run.global->mutate.dictionaryCnt].len =
data->afl->extras[data->extras_cnt].len;
run.global->mutate.dictionaryCnt++;
data->extras_cnt++;

}

while (data->extras_cnt < data->afl->a_extras_cnt &&
run.global->mutate.dictionaryCnt < 1024) {

memcpy(run.global->mutate.dictionary[run.global->mutate.dictionaryCnt].val,
data->afl->a_extras[data->a_extras_cnt].data,
data->afl->a_extras[data->a_extras_cnt].len);
run.global->mutate.dictionary[run.global->mutate.dictionaryCnt].len =
data->afl->a_extras[data->a_extras_cnt].len;
run.global->mutate.dictionaryCnt++;
data->a_extras_cnt++;

}

}

/* we could set only_printable if is_ascii is set ... let's see
uint8_t afl_custom_queue_get(void *data, const uint8_t *filename) {
//run.global->cfg.only_printable = ...
}
*/

/* here we run the honggfuzz mutator, which is really good */

size_t afl_custom_fuzz(my_mutator_t *data, uint8_t *buf, size_t buf_size,
u8 **out_buf, uint8_t *add_buf, size_t add_buf_size,
size_t max_size) {

/* set everything up, costly ... :( */
memcpy(data->mutator_buf, buf, buf_size);
queue_input = data->mutator_buf;
run.dynfile->data = data->mutator_buf;
queue_input_size = buf_size;
run.dynfile->size = buf_size;
*out_buf = data->mutator_buf;

/* the mutation */
mangle_mangleContent(&run, NUMBER_OF_MUTATIONS);

/* return size of mutated data */
return run.dynfile->size;

}

/**
* Deinitialize everything
*
* @param data The data ptr from afl_custom_init
*/
void afl_custom_deinit(my_mutator_t *data) {

free(data->mutator_buf);
free(data);

}

Loading

0 comments on commit 72b46a0

Please sign in to comment.