OLD | NEW |
(Empty) | |
| 1 /* |
| 2 |
| 3 american fuzzy lop - extract tokens passed to strcmp / memcmp |
| 4 ------------------------------------------------------------- |
| 5 |
| 6 Written and maintained by Michal Zalewski <lcamtuf@google.com> |
| 7 |
| 8 Copyright 2016 Google Inc. All rights reserved. |
| 9 |
| 10 Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 you may not use this file except in compliance with the License. |
| 12 You may obtain a copy of the License at: |
| 13 |
| 14 http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
| 16 This Linux-only companion library allows you to instrument strcmp(), |
| 17 memcmp(), and related functions to automatically extract tokens. |
| 18 See README.tokencap for more info. |
| 19 |
| 20 */ |
| 21 |
| 22 #include <stdio.h> |
| 23 #include <string.h> |
| 24 #include <ctype.h> |
| 25 |
| 26 #include "../types.h" |
| 27 #include "../config.h" |
| 28 |
| 29 #ifndef __linux__ |
| 30 # error "Sorry, this library is Linux-specific for now!" |
| 31 #endif /* !__linux__ */ |
| 32 |
| 33 |
| 34 /* Mapping data and such */ |
| 35 |
| 36 #define MAX_MAPPINGS 1024 |
| 37 |
| 38 static struct mapping { |
| 39 void *st, *en; |
| 40 } __tokencap_ro[MAX_MAPPINGS]; |
| 41 |
| 42 static u32 __tokencap_ro_cnt; |
| 43 static u8 __tokencap_ro_loaded; |
| 44 static FILE* __tokencap_out_file; |
| 45 |
| 46 |
| 47 /* Identify read-only regions in memory. Only parameters that fall into these |
| 48 ranges are worth dumping when passed to strcmp() and so on. Read-write |
| 49 regions are far more likely to contain user input instead. */ |
| 50 |
| 51 static void __tokencap_load_mappings(void) { |
| 52 |
| 53 u8 buf[MAX_LINE]; |
| 54 FILE* f = fopen("/proc/self/maps", "r"); |
| 55 |
| 56 __tokencap_ro_loaded = 1; |
| 57 |
| 58 if (!f) return; |
| 59 |
| 60 while (fgets(buf, MAX_LINE, f)) { |
| 61 |
| 62 u8 rf, wf; |
| 63 void* st, *en; |
| 64 |
| 65 if (sscanf(buf, "%p-%p %c%c", &st, &en, &rf, &wf) != 4) continue; |
| 66 if (wf == 'w' || rf != 'r') continue; |
| 67 |
| 68 __tokencap_ro[__tokencap_ro_cnt].st = (void*)st; |
| 69 __tokencap_ro[__tokencap_ro_cnt].en = (void*)en; |
| 70 |
| 71 if (++__tokencap_ro_cnt == MAX_MAPPINGS) break; |
| 72 |
| 73 } |
| 74 |
| 75 fclose(f); |
| 76 |
| 77 } |
| 78 |
| 79 |
| 80 /* Check an address against the list of read-only mappings. */ |
| 81 |
| 82 static u8 __tokencap_is_ro(const void* ptr) { |
| 83 |
| 84 u32 i; |
| 85 |
| 86 if (!__tokencap_ro_loaded) __tokencap_load_mappings(); |
| 87 |
| 88 for (i = 0; i < __tokencap_ro_cnt; i++) |
| 89 if (ptr >= __tokencap_ro[i].st && ptr <= __tokencap_ro[i].en) return 1; |
| 90 |
| 91 return 0; |
| 92 |
| 93 } |
| 94 |
| 95 |
| 96 /* Dump an interesting token to output file, quoting and escaping it |
| 97 properly. */ |
| 98 |
| 99 static void __tokencap_dump(const u8* ptr, size_t len, u8 is_text) { |
| 100 |
| 101 u8 buf[MAX_AUTO_EXTRA * 4 + 1]; |
| 102 u32 i; |
| 103 u32 pos = 0; |
| 104 |
| 105 if (len < MIN_AUTO_EXTRA || len > MAX_AUTO_EXTRA) return; |
| 106 |
| 107 for (i = 0; i < len; i++) { |
| 108 |
| 109 if (is_text && !ptr[i]) break; |
| 110 |
| 111 switch (ptr[i]) { |
| 112 |
| 113 case 0 ... 31: |
| 114 case 127 ... 255: |
| 115 case '\"': |
| 116 case '\\': |
| 117 |
| 118 sprintf(buf + pos, "\\x%02x", ptr[i]); |
| 119 pos += 4; |
| 120 break; |
| 121 |
| 122 default: |
| 123 |
| 124 buf[pos++] = ptr[i]; |
| 125 |
| 126 } |
| 127 |
| 128 } |
| 129 |
| 130 buf[pos] = 0; |
| 131 |
| 132 fprintf(__tokencap_out_file, "\"%s\"\n", buf); |
| 133 |
| 134 } |
| 135 |
| 136 |
| 137 /* Replacements for strcmp(), memcmp(), and so on. Note that these will be used |
| 138 only if the target is compiled with -fno-builtins and linked dynamically. */ |
| 139 |
| 140 #undef strcmp |
| 141 |
| 142 int strcmp(const char* str1, const char* str2) { |
| 143 |
| 144 if (__tokencap_is_ro(str1)) __tokencap_dump(str1, strlen(str1), 1); |
| 145 if (__tokencap_is_ro(str2)) __tokencap_dump(str2, strlen(str2), 1); |
| 146 |
| 147 while (1) { |
| 148 |
| 149 unsigned char c1 = *str1, c2 = *str2; |
| 150 |
| 151 if (c1 != c2) return (c1 > c2) ? 1 : -1; |
| 152 if (!c1) return 0; |
| 153 str1++; str2++; |
| 154 |
| 155 } |
| 156 |
| 157 } |
| 158 |
| 159 |
| 160 #undef strncmp |
| 161 |
| 162 int strncmp(const char* str1, const char* str2, size_t len) { |
| 163 |
| 164 if (__tokencap_is_ro(str1)) __tokencap_dump(str1, len, 1); |
| 165 if (__tokencap_is_ro(str2)) __tokencap_dump(str2, len, 1); |
| 166 |
| 167 while (len--) { |
| 168 |
| 169 unsigned char c1 = *str1, c2 = *str2; |
| 170 |
| 171 if (!c1) return 0; |
| 172 if (c1 != c2) return (c1 > c2) ? 1 : -1; |
| 173 str1++; str2++; |
| 174 |
| 175 } |
| 176 |
| 177 return 0; |
| 178 |
| 179 } |
| 180 |
| 181 |
| 182 #undef strcasecmp |
| 183 |
| 184 int strcasecmp(const char* str1, const char* str2) { |
| 185 |
| 186 if (__tokencap_is_ro(str1)) __tokencap_dump(str1, strlen(str1), 1); |
| 187 if (__tokencap_is_ro(str2)) __tokencap_dump(str2, strlen(str2), 1); |
| 188 |
| 189 while (1) { |
| 190 |
| 191 unsigned char c1 = tolower(*str1), c2 = tolower(*str2); |
| 192 |
| 193 if (c1 != c2) return (c1 > c2) ? 1 : -1; |
| 194 if (!c1) return 0; |
| 195 str1++; str2++; |
| 196 |
| 197 } |
| 198 |
| 199 } |
| 200 |
| 201 |
| 202 #undef strncasecmp |
| 203 |
| 204 int strncasecmp(const char* str1, const char* str2, size_t len) { |
| 205 |
| 206 if (__tokencap_is_ro(str1)) __tokencap_dump(str1, len, 1); |
| 207 if (__tokencap_is_ro(str2)) __tokencap_dump(str2, len, 1); |
| 208 |
| 209 while (len--) { |
| 210 |
| 211 unsigned char c1 = tolower(*str1), c2 = tolower(*str2); |
| 212 |
| 213 if (!c1) return 0; |
| 214 if (c1 != c2) return (c1 > c2) ? 1 : -1; |
| 215 str1++; str2++; |
| 216 |
| 217 } |
| 218 |
| 219 return 0; |
| 220 |
| 221 } |
| 222 |
| 223 |
| 224 #undef memcmp |
| 225 |
| 226 int memcmp(const void* mem1, const void* mem2, size_t len) { |
| 227 |
| 228 if (__tokencap_is_ro(mem1)) __tokencap_dump(mem1, len, 0); |
| 229 if (__tokencap_is_ro(mem2)) __tokencap_dump(mem2, len, 0); |
| 230 |
| 231 while (len--) { |
| 232 |
| 233 unsigned char c1 = *(const char*)mem1, c2 = *(const char*)mem2; |
| 234 if (c1 != c2) return (c1 > c2) ? 1 : -1; |
| 235 mem1++; mem2++; |
| 236 |
| 237 } |
| 238 |
| 239 return 0; |
| 240 |
| 241 } |
| 242 |
| 243 |
| 244 /* Init code to open the output file (or default to stderr). */ |
| 245 |
| 246 __attribute__((constructor)) void __tokencap_init(void) { |
| 247 |
| 248 u8* fn = getenv("AFL_TOKEN_FILE"); |
| 249 if (fn) __tokencap_out_file = fopen(fn, "a"); |
| 250 if (!__tokencap_out_file) __tokencap_out_file = stderr; |
| 251 |
| 252 } |
| 253 |
OLD | NEW |