| 1 |
/* Attempt to find CRC-32 collisions for QVM files. |
| 2 |
* |
| 3 |
* Usage: qvmbrute.c DESIRED-CRC [OUTPUT-FILENAME [SUBDIRECTORY]] |
| 4 |
* |
| 5 |
* Copyright 2010 Simon McVittie <[email protected]> |
| 6 |
* Copying and distribution of this file, with or without modification, are |
| 7 |
* permitted in any medium without royalty provided this notice is preserved. |
| 8 |
* This file is offered as-is, without any warranty. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <arpa/inet.h> |
| 12 |
#include <assert.h> |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <time.h> |
| 18 |
|
| 19 |
#include <zlib.h> |
| 20 |
|
| 21 |
int |
| 22 |
main (int argc, char **argv) |
| 23 |
{ |
| 24 |
struct timespec start = { 0 }; |
| 25 |
struct timespec end = { 0 }; |
| 26 |
u_int32_t i; |
| 27 |
unsigned char qvm[1024] = { 0 }; |
| 28 |
u_int32_t target = strtoul (argv[1], NULL, 0); |
| 29 |
char *subdir = ""; |
| 30 |
size_t fixed_len; |
| 31 |
u_int32_t crc; |
| 32 |
double delta; |
| 33 |
|
| 34 |
clock_gettime (CLOCK_MONOTONIC, &start); |
| 35 |
|
| 36 |
/* subdirectory to embed in the dummy file */ |
| 37 |
if (argc > 3) { |
| 38 |
subdir = argv[3]; |
| 39 |
} |
| 40 |
|
| 41 |
/* length of fixed part */ |
| 42 |
fixed_len = strlen (subdir) + 8; |
| 43 |
|
| 44 |
/* The fixed part is "NTVE" + subdir + four reserved '\0' bytes |
| 45 |
* (the first of which acts as '\0' termination for subdir). |
| 46 |
* |
| 47 |
* CCCC is replaced by the complement of the CRC-32 of the fixed part, |
| 48 |
* yielding crc32 (fixed part + CCCC) = FFFFFFFF. |
| 49 |
* XXXX is replaced by a brute-forced number such that |
| 50 |
* crc32 (fixed part + CCCC + XXXX) is as desired. */ |
| 51 |
snprintf ((char *) qvm, sizeof (qvm) - 1, "NTVE%s%c%c%c%cCCCCXXXX", |
| 52 |
subdir, 0, 0, 0, 0); |
| 53 |
|
| 54 |
/* calculate CRC of first fixed_len bytes */ |
| 55 |
crc = crc32 (crc32 (0, NULL, 0), qvm, fixed_len); |
| 56 |
/* put the complement of it, in little-endian, in the next 4 */ |
| 57 |
qvm[fixed_len + 0] = ~(crc & 0xFF); |
| 58 |
qvm[fixed_len + 1] = ~((crc >> 8) & 0xFF); |
| 59 |
qvm[fixed_len + 2] = ~((crc >> 16) & 0xFF); |
| 60 |
qvm[fixed_len + 3] = ~((crc >> 24) & 0xFF); |
| 61 |
/* by mathematical properties of CRC32, the CRC up to that point is |
| 62 |
* 0xFFFFFFFF */ |
| 63 |
crc = crc32 (crc32 (0, NULL, 0), qvm, fixed_len + 4); |
| 64 |
assert (crc == 0xFFFFFFFF); |
| 65 |
|
| 66 |
/* it's possible to do the last bit by mathematics, but brute force |
| 67 |
* is fairly quick */ |
| 68 |
printf ("searching for suffix that turns CRC32 from FFFFFFFF to 0x%.8x\n", |
| 69 |
target); |
| 70 |
|
| 71 |
for (i = 0; ; i++) { |
| 72 |
if (i % 0x10000 == 0) { |
| 73 |
printf ("%.8x\r", i); |
| 74 |
} |
| 75 |
|
| 76 |
if (crc32(0xFFFFFFFF, (const unsigned char *) &i, sizeof (i)) == target) { |
| 77 |
printf ("suffix found: 0x%.8x (in this machine's endianness)\n", |
| 78 |
i); |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
if (i == 0xFFFFFFFF) { |
| 83 |
printf ("collision not found within 4 bytes\n"); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* check our working */ |
| 89 |
memcpy (qvm + fixed_len + 4, &i, 4); |
| 90 |
crc = crc32 (crc32 (0, NULL, 0), qvm, fixed_len + 8); |
| 91 |
assert (crc == target); |
| 92 |
|
| 93 |
printf ("crc32(\"NTVE%s\" 00000000 %.8x %.8x) == 0x%.8x\n", |
| 94 |
subdir, |
| 95 |
ntohl(*((u_int32_t *) (qvm + fixed_len))), |
| 96 |
ntohl(*((u_int32_t *) (qvm + fixed_len + 4))), |
| 97 |
crc); |
| 98 |
|
| 99 |
if (argc > 2) { |
| 100 |
FILE *f; |
| 101 |
|
| 102 |
printf ("writing to file %s\n", argv[2]); |
| 103 |
f = fopen (argv[2], "w"); |
| 104 |
|
| 105 |
if (f == NULL || |
| 106 |
fwrite (qvm, fixed_len + 8, 1, f) < 1 || |
| 107 |
fclose (f) < 0) { |
| 108 |
perror ("writing fake QVM"); |
| 109 |
return 1; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
clock_gettime (CLOCK_MONOTONIC, &end); |
| 114 |
delta = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec) / 1e9; |
| 115 |
printf ("QVM file %s generated in %.2f seconds\n", argv[2], delta); |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |