resilar / crchack

Reversing CRC for fun and profit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clarification: prepending instead of appending, using -O option with negative values?

reikred opened this issue · comments

I'm trying to calculate a 4 byte value to PREPEND to a 8192 byte data file so as to get a specified CRC32 value 0x224E16C3 . My reading of the manual made me think I needed to use the option -O -8192 or maybe even -O -8196. But that does not work. Can you please clarify how to get a value to prepend? Test case below.

## create test file with hex AB repeated the entire file
perl -e '$x="AB" x 8192; print pack("H*", $x);' > fileAB.8192
hexdump fileAB.8192

## test regular append-mode
crchack fileAB.8192 224E16C3 > test-append
crchack test-append
# observe that the padding value is appended
hexdump test-append

##trying to create a value to PREPEND
crchack  -O -8192  fileAB.8192 224E16C3 > test-prepend
crchack test-prepend
# observe that the padding value is appended but I thought it would prepended?
hexdump test-prepend

crchack  -O -8196 fileAB.8192 224E16C3 > test-prepend
leads to error message: bits[0]=65568 exceeds message length (65567 bits)

Unfortunately the current version of crchack supports only appending (not prepending) a padding to the input in order to force the CRC checksum to the desired value. The README is maybe a bit confusing, but the -O option with a negative argument -8192 is basically equivalent to -o 8192. Similarly -o -8192 is the same as -O 8192: both of them specify a position corresponding to the beginning of the input file (in this case crchack modifies the first 4 bytes of the input file and does not prepend new data).

I made the error messages a bit more clearer about the fact that crchack rejects -o/-O offsets pointing to positions before the start of the input message.

% printf 0123456789 | ./crchack -O 14 - 42424242
offset '-O 14' starts 32 bits before the message
% printf 0123456789 | ./crchack -o -14 - 42424242
offset '-o -14' starts 32 bits before the message

And yes, offering two separate -o/-O options is kinda redundant when either one of them can perform the functionality of the other by simply changing the sign of the option argument value.

Anyway, there are no immediate plans to add prepend support to crchack. Usually prepending data to a file is technically ugly and results in poor performance, but given that crchack writes the modified file to stdout (as opposed to in-place replacing the file on the disk), I don't see any major reason not to implement prepending support in the future.

But until then, you can use this ugly workaround or something similar:

% perl -e '$x="AB" x 8192; print pack("H*", $x);' > fileAB.8192
% printf 'AAAA' | cat - fileAB.8192 | crchack -o 0 - 224E16C3 > test-prepend
% crchack test-prepend
224e16c3
% hexdump -C test-prepend
00000000  8f 6b 52 64 ab ab ab ab  ab ab ab ab ab ab ab ab  |.kRd............|
00000010  ab ab ab ab ab ab ab ab  ab ab ab ab ab ab ab ab  |................|
*
00002000  ab ab ab ab                                       |....|
00002004
%