Skip to content

Instantly share code, notes, and snippets.

@integeruser
Last active July 6, 2019 16:17
Show Gist options
  • Save integeruser/f6df45e1e0f05787d7799fe54af98b85 to your computer and use it in GitHub Desktop.
Save integeruser/f6df45e1e0f05787d7799fe54af98b85 to your computer and use it in GitHub Desktop.
Preload read() to exit() on EOF
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
/* gcc -shared -fPIC exit-on-EOF.c -o exit-on-EOF.so -ldl */
/* LD_PRELOAD=./exit-on-EOF.so ./test */
/* AFL_PRELOAD=./exit-on-EOF.so afl-fuzz -i in -o out -n -- ./test */
typedef int (*orig_read_f_type)(int fd, void *buf, int count);
int read(int fd, void *buf, int count) {
orig_read_f_type orig_read = (orig_read_f_type)dlsym(RTLD_NEXT, "read");
for (int i = 0; i < count; ++i) {
int n = orig_read(fd, buf++, 1);
if (n == 0) {
printf("EOF!\n");
exit(-1337);
}
}
return count;
}
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[]) {
char c;
do {
read(STDIN_FILENO, &c, 1);
printf("%c", c);
}
while(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment