aboutsummaryrefslogtreecommitdiff
path: root/examples/post_library
diff options
context:
space:
mode:
authorDominik Maier <domenukk@gmail.com>2020-03-28 04:57:44 +0100
committerDominik Maier <domenukk@gmail.com>2020-03-28 04:57:44 +0100
commit53fd8fe6ea6aa07247444dedc33e3b2491ec2334 (patch)
tree7aef4f2717407f0fd198c7c82b29e09fe44a9c5d /examples/post_library
parent0059d167310911976a2ee5e89204d3d9f0fc5409 (diff)
downloadafl++-53fd8fe6ea6aa07247444dedc33e3b2491ec2334.tar.gz
more custom mutator remodelling
Diffstat (limited to 'examples/post_library')
-rw-r--r--examples/post_library/post_library.so.c84
-rw-r--r--examples/post_library/post_library_png.so.c71
2 files changed, 117 insertions, 38 deletions
diff --git a/examples/post_library/post_library.so.c b/examples/post_library/post_library.so.c
index 5d2685cd..735aae9b 100644
--- a/examples/post_library/post_library.so.c
+++ b/examples/post_library/post_library.so.c
@@ -3,6 +3,7 @@
--------------------------------------------------
Originally written by Michal Zalewski
+ Edited by Dominik Maier, 2020
Copyright 2015 Google Inc. All rights reserved.
@@ -41,22 +42,23 @@
AFL will call the afl_postprocess() function for every mutated output buffer.
From there, you have three choices:
- 1) If you don't want to modify the test case, simply return the original
- buffer pointer ('in_buf').
+ 1) If you don't want to modify the test case, simply set `*out_buf = in_buf`
+ and return the original `len`.
2) If you want to skip this test case altogether and have AFL generate a
- new one, return NULL. Use this sparingly - it's faster than running
- the target program with patently useless inputs, but still wastes CPU
- time.
+ new one, return 0 or set `*out_buf = NULL`.
+ Use this sparingly - it's faster than running the target program
+ with patently useless inputs, but still wastes CPU time.
3) If you want to modify the test case, allocate an appropriately-sized
buffer, move the data into that buffer, make the necessary changes, and
- then return the new pointer. You can update *len if necessary, too.
+ then return the new pointer as out_buf. Return an appropriate len
+ afterwards.
Note that the buffer will *not* be freed for you. To avoid memory leaks,
you need to free it or reuse it on subsequent calls (as shown below).
- *** DO NOT MODIFY THE ORIGINAL 'in_buf' BUFFER. ***
+ *** Feel free to reuse the original 'in_buf' BUFFER and return it. ***
Aight. The example below shows a simple postprocessor that tries to make
sure that all input files start with "GIF89a".
@@ -74,47 +76,83 @@
#define HEADER "GIF89a"
-/* The actual postprocessor routine called by afl-fuzz: */
+typedef struct post_state {
+
+ unsigned char *buf;
+ size_t size;
+
+} post_state_t;
+
+void *afl_postprocess_init(void *afl) {
+
+ post_state_t *state = malloc(sizeof(post_state_t));
+ if (!state) {
-const unsigned char *afl_postprocess(const unsigned char *in_buf,
- unsigned int * len) {
+ perror("malloc");
+ return NULL;
- static unsigned char *saved_buf;
- unsigned char * new_buf;
+ }
+
+ state->buf = calloc(sizeof(unsigned char), 4096);
+ if (!state->buf) { return NULL; }
+
+ return state;
+
+}
+
+/* The actual postprocessor routine called by afl-fuzz: */
+
+size_t afl_postprocess(post_state_t *data, unsigned char *in_buf,
+ unsigned int len, unsigned char **out_buf) {
/* Skip execution altogether for buffers shorter than 6 bytes (just to
- show how it's done). We can trust *len to be sane. */
+ show how it's done). We can trust len to be sane. */
- if (*len < strlen(HEADER)) return NULL;
+ if (len < strlen(HEADER)) return 0;
/* Do nothing for buffers that already start with the expected header. */
- if (!memcmp(in_buf, HEADER, strlen(HEADER))) return in_buf;
+ if (!memcmp(in_buf, HEADER, strlen(HEADER))) {
+
+ *out_buf = in_buf;
+ return len;
+
+ }
/* Allocate memory for new buffer, reusing previous allocation if
possible. */
- new_buf = realloc(saved_buf, *len);
+ *out_buf = realloc(data->buf, len);
/* If we're out of memory, the most graceful thing to do is to return the
original buffer and give up on modifying it. Let AFL handle OOM on its
own later on. */
- if (!new_buf) return in_buf;
- saved_buf = new_buf;
+ if (!*out_buf) {
+
+ *out_buf = in_buf;
+ return len;
+
+ }
/* Copy the original data to the new location. */
- memcpy(new_buf, in_buf, *len);
+ memcpy(*out_buf, in_buf, len);
/* Insert the new header. */
- memcpy(new_buf, HEADER, strlen(HEADER));
+ memcpy(*out_buf, HEADER, strlen(HEADER));
- /* Return modified buffer. No need to update *len in this particular case,
- as we're not changing it. */
+ /* Return the new len. It hasn't changed, so it's just len. */
- return new_buf;
+ return len;
}
+/* Gets called afterwards */
+void afl_postprocess_deinit(post_state_t *data) {
+
+ free(data->buf);
+ free(data);
+
+}
diff --git a/examples/post_library/post_library_png.so.c b/examples/post_library/post_library_png.so.c
index 60ab318f..8597c88c 100644
--- a/examples/post_library/post_library_png.so.c
+++ b/examples/post_library/post_library_png.so.c
@@ -5,6 +5,7 @@
Originally written by Michal Zalewski
Copyright 2015 Google Inc. All rights reserved.
+ Adapted to the new API, 2020 by Dominik Maier
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -35,11 +36,32 @@
#define UP4K(_i) ((((_i) >> 12) + 1) << 12)
-const unsigned char *afl_postprocess(const unsigned char *in_buf,
- unsigned int * len) {
+typedef struct post_state {
- static unsigned char *saved_buf;
- static unsigned int saved_len;
+ unsigned char *buf;
+ size_t size;
+
+} post_state_t;
+
+void *afl_postprocess_init(void *afl) {
+
+ post_state_t *state = malloc(sizeof(post_state_t));
+ if (!state) {
+
+ perror("malloc");
+ return NULL;
+
+ }
+
+ state->buf = calloc(sizeof(unsigned char), 4096);
+ if (!state->buf) { return NULL; }
+
+ return state;
+
+}
+
+size_t afl_postprocess(post_state_t *data, const unsigned char *in_buf,
+ unsigned int len, const unsigned char **out_buf) {
unsigned char *new_buf = (unsigned char *)in_buf;
unsigned int pos = 8;
@@ -47,12 +69,17 @@ const unsigned char *afl_postprocess(const unsigned char *in_buf,
/* Don't do anything if there's not enough room for the PNG header
(8 bytes). */
- if (*len < 8) return in_buf;
+ if (len < 8) {
+
+ *out_buf = in_buf;
+ return len;
+
+ }
/* Minimum size of a zero-length PNG chunk is 12 bytes; if we
don't have that, we can bail out. */
- while (pos + 12 <= *len) {
+ while (pos + 12 <= len) {
unsigned int chunk_len, real_cksum, file_cksum;
@@ -62,7 +89,7 @@ const unsigned char *afl_postprocess(const unsigned char *in_buf,
/* Bail out if chunk size is too big or goes past EOF. */
- if (chunk_len > 1024 * 1024 || pos + 12 + chunk_len > *len) break;
+ if (chunk_len > 1024 * 1024 || pos + 12 + chunk_len > len) break;
/* Chunk checksum is calculated for chunk ID (dword) and the actual
payload. */
@@ -82,17 +109,23 @@ const unsigned char *afl_postprocess(const unsigned char *in_buf,
if (new_buf == in_buf) {
- if (*len <= saved_len) {
+ if (len <= data->size) {
- new_buf = saved_buf;
+ new_buf = data->buf;
} else {
- new_buf = realloc(saved_buf, UP4K(*len));
- if (!new_buf) return in_buf;
- saved_buf = new_buf;
- saved_len = UP4K(*len);
- memcpy(new_buf, in_buf, *len);
+ new_buf = realloc(data->buf, UP4K(len));
+ if (!new_buf) {
+
+ *out_buf = in_buf;
+ return len;
+
+ }
+
+ data->buf = new_buf;
+ data->size = UP4K(len);
+ memcpy(new_buf, in_buf, len);
}
@@ -108,7 +141,15 @@ const unsigned char *afl_postprocess(const unsigned char *in_buf,
}
- return new_buf;
+ *out_buf = new_buf;
+ return len;
}
+/* Gets called afterwards */
+void afl_postprocess_deinit(post_state_t *data) {
+
+ free(data->buf);
+ free(data);
+
+}