/* Find a file in the FW dumps ** ** -- subdub */ #include #include #include #include #include #include #include #include #define SEARCH_SIZE (32) unsigned char *mapfile(const char *path, unsigned long *size) { int fd; struct stat s; unsigned char *ret; if ((fd = open(path, O_RDONLY, 0400)) < 0) { fprintf(stderr, "[X] %s : %s\n", path, strerror(errno)); return NULL; } if (fstat(fd, &s) < 0) { perror("[X] fstat"); return NULL; } *size = (unsigned long) s.st_size; if ((ret = (unsigned char *) mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == NULL) { perror("[X] mmap"); return NULL; } printf("[*] File %s mapped at %p, %ld bytes\n", path, ret, *size); return ret; } int findfile(unsigned char *map_fwA, unsigned long size_fwA, unsigned char *map_fwB, unsigned long size_fwB, unsigned char *map_file, unsigned long size_file) { unsigned char *cur_fwA, *cur_fwB, *cur_file; unsigned char *begin, *end, *begin_tmp; unsigned long count, total; int isA; cur_fwA = map_fwA; cur_fwB = map_fwB; cur_file = map_file; total = 0; while ((size_fwA > 0 || size_fwB >0) || size_file > 0) { /* First we try to find SEARCH_SIZE bytes of our file into the fw dumps */ if ((begin = begin_tmp = memmem(cur_fwA, size_fwA, cur_file, SEARCH_SIZE)) == NULL) { if ((begin = begin_tmp = memmem(cur_fwB, size_fwB, cur_file, SEARCH_SIZE)) == NULL) break; else { size_fwB -= ((long) begin - (long) map_fwB); isA = 0; } } else { size_fwA -= ((long) begin - (long) map_fwA); isA = 1; } size_file -= SEARCH_SIZE; if (isA) { for (count = SEARCH_SIZE; *begin_tmp++ == *cur_file++; count++, size_fwA--, size_file--); printf("\t-> Found %u bytes @ %#.016lx, in dump A\n", count, (long) begin - (long) cur_fwA); } else { for (count = SEARCH_SIZE; *begin_tmp++ == *cur_file++; count++, size_fwB--, size_file--); printf("\t-> Found %u bytes @ %#.016lx, in dump B\n", count, (long) begin - (long) cur_fwB); } total += count; } printf("[*] Matched %u bytes in total\n", total); return 0; } int main(int argc, const char **argv) { unsigned char *mapped_fwA, *mapped_fwB, *mapped_file; unsigned long size_fwA, size_fwB, size_file; if (argc < 4) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } if ((mapped_fwA = mapfile(argv[1], &size_fwA)) == NULL) return 1; if ((mapped_fwB = mapfile(argv[2], &size_fwB)) == NULL) return 1; if ((mapped_file = mapfile(argv[3], &size_file)) == NULL) return 1; printf("--------------- STARTING ---------------\n"); return findfile(mapped_fwA, size_fwA, mapped_fwB, size_fwB, mapped_file, size_file); }