[ Disclaimer | Changelog | Home ]
CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder
Copyright (C) 2010, 2011, 2012 Gregory Cook
This file is part of CRC RevEng.
CRC RevEng is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CRC RevEng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CRC RevEng. If not, see <http://www.gnu.org/licenses/>.
CRC RevEng is an arbitrary-precision, machine word length-independent,
byte order-independent CRC calculator and algorithm finder in ANSI C.
It is a port and expansion of the author's crcbfs.pl script from 2007,
and runs up to 200 times faster on equivalent problems. It is also a
reference implementation of the author's "Catalogue of parametrised CRC
algorithms", with the 61 currently listed models available as presets.
To the author's knowledge CRC RevEng is the first published compiled
application to address the general case of CRC algorithm reversal and
reverse engineering, its predecessor crcbfs.pl being the first published
application of any type to do so. Greg Ewing of Canterbury University
in New Zealand solved a CRC algorithm manually on similar principles in
2010, but partly by feeding chosen plaintexts into an implementation at
hand.
Compiling CRC RevEng is straightforward: in the i386 GNU/Linux and MinGW environments, simply `cd' to the directory containing the source files, and enter
make
Otherwise, enter commands similar to the following to compile CRC RevEng on any ANSI C compliant compiler:
gcc -O3 -Wall -ansi -c bmpbit.c cli.c model.c poly.c reveng.c
gcc -o reveng bmpbit.o cli.o model.o poly.o reveng.o
If your environment does not include a getopt library, an ANSI C
implementation is available under the GPL in several places, for
instance the ASPEX project:
<http://aspex.sourceforge.net/>
The platform-independent profile does not include the preset models. To
compile them, you will need to edit the configuration options in
config.h to suit your architecture. Having done so, define the macro
PRESETS in config.h and recompile as above.
Some enterprise users may wish to disable the -F switch to minimise CPU
usage. To do this, define the macro NOFORCE in config.h or on the
command line.
You can use one of the preset models or specify your own.
reveng -m crc-32
selects the CRC-32 algorithm used in PKZIP and elsewhere. You can dump any preset model as a Williams model record using -d:
reveng -m crc-32 -d
Name : "CRC-32"
Width : 32
Poly : 04C11DB7
Init : FFFFFFFF
RefIn : True
RefOut : True
XorOut : FFFFFFFF
Check : CBF43926
You can specify the parameters of the model instead:
reveng -w 32 -p 04c11db7 -i ffffffff -l -x ffffffff
This is equivalent to
reveng -m crc-32
except the model will have no name when dumped. (The -l switch sets both RefIn = True and RefOut = True. To set RefOut separately, use switches -L and -B.)
The options and switches for specifying a model are:
0x18005 is specified as C002. This
automatically provides the Width value.0x18005 is specified as 8005.0x18005 is specified as A001.Other model-related options:
Messages for CRC RevEng to process can be specified as files or as numerical (typically hexadecimal) string arguments on the command line. Output from CRC RevEng is either as Williams model records (having their own fixed format) or as numerical string arguments printed one per line on standard output.
When passing numerical arguments on the command line, each argument is conceptually divided into characters, each character consisting of one or more hexadecimal digits. For each character, enough hex digits to specify it are read then a number of bits (specified by the -a option) are taken from the least significant end, reversed (if RefIn = True) and appended to the binary representation of the argument; any excess bits are discarded. The -a option (q.v.) permits a number of useful representations of a given underlying binary sequence.
When passing messages as files, the same division into characters applies; bytes of the file are read until enough bits have been collected, then a specified number of bits are taken from the least significant end, reflected (if RefIn = True) and added to the binary representation.
When printing output, the binary representation is again divided into characters, each of which is reversed (if RefOut = True) and printed with the minimum sufficient number of hex digits.
There are a few more options for controlling the presentation of input and output:
When a model has been specified, use -c or -v to calculate CRCs for input messages.
If -V and -v are given together, their respective model reversals cancel
out. CRC RevEng then calculates an ordinary CRC for each argument,
processing the characters from right to left.
Correspondingly, to obtain the same effect as -v using a model reversed
by -V, the user must present the characters of his or her message, and
process those of the returned CRC, in reverse order.
Take care when the CRC width (-w) is not a multiple of the character width (-a). If the result of a calculation is not what you expect, try selecting left-justification (with -t) or right-justification (with -r).
The -c mode is, of course, useful for creating a checksum to append to a message so that the combination will pass a particular CRC check. The -v mode, on the other hand, is useful for editing a message so as to force its checksum to a desired or at least predetermined value. In order to do this there must be some part of the message's data that can be modified freely without observable effect; many network protocols and file formats, including executables, images and word processor documents, have (or can be altered to have) reserved fields or comment sections that cannot be easily viewed, and whose contents are entirely ignored.
Among the simplest ways to control a CRC calculation is to find one such unused space that is both contiguous and large enough to hold a checksum. For example, suppose we have an existing message with an X.25 CRC:
0: 44 6F 67 73 2F 2A 12 34 2A 2F 72 6F 63 6B 4E 47 | Dogs/*.4*/rockNG
Here, 4E 47 is the X.25 checksum, and we wish to alter the message
without either changing the checksum or failing the CRC. We notice that
the 7th and 8th bytes can be replaced at will, and these can contain a
calculated value to force the CRC. Firstly we change the text as we
wish:
0: 43 61 74 73 2F 2A 12 34 2A 2F 72 75 6C 65 4E 47 | Cats/*.4*/ruleNG
Calculate the CRC of the part on the left of the unused space with XorOut = 0:
reveng -m x-25 -x 0 -c 436174732f2a
9dc5
Then reverse-calculate the CRC of the part on the right, including the old CRC, with Init = 0:
reveng -m x-25 -i 0 -v 2a2f72756c654e47
1505
Now exclusive-OR the two returned CRCs together, and insert the result in the unused space. CRC RevEng can be used to do the exclusive-OR if a hex calculator is not to hand:
reveng -w 16 -p 0001 -c 9dc51505
88c0
Our edited message now looks like this:
0: 43 61 74 73 2F 2A 88 C0 2A 2F 72 75 6C 65 4E 47 | Cats/*.A*/ruleNG
To confirm that it still passes the X.25 CRC:
reveng -m x-25 -c 436174732f2a88c02a2f72756c65
4e47
* * *
In Stigge et al. (section 4.1) a polynomial q(x) is calculated as the
multiplicative inverse of XN such that (xN) q(x) = 1 (mod pCRC(x)).
The authors promote the extended Euclidean algorithm as a means of
calculating q(x), however any CRC calculator can also produce it. The
reciprocal of the CRC-32 polynomial is 0xdb710641, as output by:
reveng -w 32 -p 04c11db7 -V -d
The authors' constant CRCINV, a reflected representation of q(x), is the
CRC of the reversal of the desired remainder, 0x00000001:
reveng -w 32 -p db710641 -c 80000000
5b358fd3
Equivalently, the -v function returns q(x) in direct order from the unreflected parameters:
reveng -w 32 -p 04c11db7 -v 00000001
cbf1acda
The most important feature of CRC RevEng is the ability to recover the parameters of a CRC algorithm from a handful of codewords created by that algorithm. In general at least four data points are needed, either as known parameters or as message-CRC pairs. Extra data points help to eliminate false results and to confirm models that are found.
Known parameters are specified using -w, -p, -i and -x (see SPECIFYING A MODEL above). The width, -w, is a required parameter for all searches and counts as one of the data points. The size of characters (words) in the protocol must also be known and set with -a if this is not 8 bits.
The search function is selected with -s, and message-CRC pairs are given as arguments, each message and CRC combined into one argument. There must not be any non-participating characters between each message and its CRC, or the search will not work. Typically, end-of-message markers do not participate in the CRC.
As non-standard algorithms are comparatively rare, the program first tries all the preset models of the given width, reporting and exiting if one is found. Otherwise it will commence a full search.
If -b or -l are specified, CRC RevEng only searches for algorithms with that bit ordering. Otherwise, it tries RefIn = False, RefOut = False then RefIn = True, RefOut = True. Crossed-endian algorithms are also uncommon and the program will only search for them if requested (with -L).
To find the Poly value when Init is not known, at least two arguments must have the same length.
To find both the Init and XorOut values, at least two arguments must have different lengths; otherwise there is only enough information to determine one value, given the other. If all arguments have the same length then, by default, CRC RevEng fixes XorOut at zero and calculates Init accordingly. (In hardware it is easier to set a non-zero Init than to apply a non-zero XorOut.) To set XorOut to another value, specify -x XOROUT; to fix Init and calculate XorOut instead, use -i INIT.
The full list of search options is as follows:
CRC RevEng provides a few additional options for convenience:
reveng -w 16 -l -F -s 31816b 32c16a 31326a0a
reveng -w 32 -p 04c11db7 -l -s c98964f6b9 a5fa49f2fd 13370aee7df0
reveng -w 32 -l -s c98964f6b9 a5fa49f2fd 13370aee7df0
(may take several days)
A comprehensive list is being compiled.
In addition to the disclaimers listed at the top of this file and in the GNU General Public Licence (see file COPYING), remember that CRC RevEng is merely a search tool, and not authoritative. Searching is only statistical and any particular result may be a fluke, especially from a small number of samples. Also any output is only as accurate as the input.
Model reversal (-V, -v) makes little sense on crossed-endian models.
Bies, Lammert; et al. "Computer Interfacing Forum" (section "Error detection and correction").
Cook, Greg (20 February 2012). "Catalogue of parametrised CRC algorithms".
Ewing, Gregory C. (March 2010). "Reverse-Engineering a CRC Algorithm". Christchurch: University of Canterbury.
Koopman, Philip (July 2002). "32-Bit Cyclic Redundancy Codes for Internet Applications". The International Conference on Dependable Systems and Networks: 459-468. doi:10.1109/DSN.2002.1028931.
Koopman, Philip; Chakravarty, Tridib (June 2004). "Cyclic Redundancy Code (CRC) Polynomial Selection For Embedded Networks". The International Conference on Dependable Systems and Networks: 145-154. doi:10.1109/DSN.2004.1311885.
Stigge, Martin; Ploetz, Henryk; Mueller, Wolf; Redlich, Jens-Peter (May 2006). "Reversing CRC – Theory and Practice". Berlin: Humboldt University Berlin.
Williams, Ross N. (24 September 1996). "A Painless Guide to CRC Error Detection Algorithms V3.00".
~0 and 0 first, and stop at the first match (unless -F
is specified, OTOH we don't want to make -F at all attractive).size_t variables wherever appropriate.CRC RevEng came about from the coincidence of four events:
crcbfs.pl:
Greg Cook
<
>
<http://regregex.bbcmicro.net/>
-END-
[ Top of page ]
CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder
Copyright (C) 2010, 2011, 2012 Gregory Cook
This file is part of CRC RevEng.
CRC RevEng is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CRC RevEng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CRC RevEng. If not, see <http://www.gnu.org/licenses/>.
v0.60 2012-02-20
CRC-6/DARC per the CRC catalogue.getopt library has been removed.v0.50 2011-09-07
v0.44 2011-08-28
CRC-64/XZ from the CRC catalogue.v0.43 2011-08-27
BMPBIT and BMPSUB,
which can cause searches to malfunction.v0.42 2011-05-03
#include from bmpbit.c.v0.41 2011-04-30
CRC-16/TMS37157 and CRC-A and the new alias
CRC-B from the CRC catalogue.v0.40 2011-02-10
v0.31 2011-02-04
CRC-32/BZIP2.v0.30 2011-01-18
v0.21 2011-01-15
NOFORCE.v0.20 2011-01-11
v0.13 2011-01-08
v0.12 2011-01-07
v0.11 2011-01-05
v0.10 2011-01-05
[ Top of page ]