intprocess(char *input) { char *out; char *rest; int len; if (strncmp(input, "u ", 2) == 0) { // upper case command char *rest; len = strtol(input + 2, &rest, 10); // how many characters of the string to upper-case rest += 1; // skip the first char (should be a space) out = malloc(len + strlen(input)); // could be shorter, but play it safe if (len > (int)strlen(input)) { printf("Specified length %d was larger than the input!\n", len); return1; } elseif (out == NULL) { printf("Failed to allocate memory\n"); return1; } for (int i = 0; i != len; i++) { out[i] = rest[i] - 32; // only handles ASCII } out[len] = 0; strcat(out, rest + len); // append the remaining text printf("%s", out); free(out); } elseif (strncmp(input, "head ", 5) == 0) { // head command if (strlen(input) > 6) { len = strtol(input + 4, &rest, 10); rest += 1; // skip the first char (should be a space) rest[len] = '\0'; // truncate string at specified offset printf("%s\n", rest); } else { fprintf(stderr, "head input was too small\n"); } } elseif (strcmp(input, "surprise!\n") == 0) { // easter egg! *(char *)1 = 2; } else { return1; } return0; }
intmain(int argc, char *argv[]) { char *usage = "Usage: %s\n" "Text utility - accepts commands and data on stdin and prints results to stdout.\n" "\tInput | Output\n" "\t------------------+-----------------------\n" "\tu <N> <string> | Uppercased version of the first <N> bytes of <string>.\n" "\thead <N> <string> | The first <N> bytes of <string>.\n"; char input[INPUTSIZE] = {0};
youlin@ubuntu:~/afl/afl-training/quickstart$ ls afl-screenshot.png inputs Makefile out README.md vulnerable vulnerable.c youlin@ubuntu:~/afl/afl-training/quickstart$ ls inputs/ head u youlin@ubuntu:~/afl/afl-training/quickstart$ cat inputs/head head 20This string is going to be truncated at the 20th position. youlin@ubuntu:~/afl/afl-training/quickstart$ cat inputs/u u 4 capsme youlin@ubuntu:~/afl/afl-training/quickstart$