Recognize cpu instructions in an arbitrary binary file
Recognize cpu instructions in an arbitrary binary file
cpu_rec is a tool that recognizes cpu instructions
in an arbitrary binary file.
It can be used as a standalone tool, or as a plugin for binwalk
(https://github.com/devttys0/binwalk).
cpu_rec.py and cpu_rec_corpus in the same directory.lzma module installed for your python (this
tool works either with python3 or with python2 >= 2.4) then you
should unxz the corpus files in cpu_rec_corpus.build_default_corpus in the source code.Same as above, but the installation directory must be the binwalk
module directory: $HOME/.config/binwalk/modules.
You'll need a recent version of binwalk, that includes the patch provided by https://github.com/devttys0/binwalk/pull/241 .
Add the flag -% when using binwalk.
Be patient. Waiting a few minutes for the result is to be expected. On my laptop the tool takes 25 seconds and 1 Gb of RAM to create the signatures for 70 architectures, and then the analysis of a binary takes one minute per Mb. If you want the tool to be faster, you can remove some architectures, if you know that your binary is not one of them (typically Cray or MMIX are not found in a firmware).
Just run the tool, with the binary file(s) to analyze as argument(s) The tool will try to match an architecture for the whole file, and then to detect the largest binary chunk that corresponds to a CPU architecture; usually it is the right answer, but one should not forget that this tool is heuristic and that some binary files contain instructions for multiple architectures, therefore a more detailed analysis may be needed.
If the result is not satisfying, prepending twice -v to the arguments
makes the tool very verbose; this is helpful when adding a new
architecture to the corpus or when there are doubts on the raw result
of the tool.
If https://github.com/LRGH/elfesteem is installed, then the tool also extract the text section from ELF, PE, Mach-O or COFF files, and outputs the architecture corresponding to this section; the possibility of extracting the text section is also used when building a corpus from full binary files.
If https://lief-project.github.io/ is installed, then the tool can use it instead of elfesteem. If both are installed, option '-l' gives priority to lief.
Option -d followed by a directory dumps the corpus in that directory;
using this option one can reconstruct the default corpus.
The function which_arch takes a bytestring as input and outputs
the name of the architecture, or None.
Loading the training data is done during the first call of which_arch,
and calling which_arch with no argument does this precomputation only.
For example
>>> from cpu_rec import which_arch
>>> which_arch()
>>> which_arch(b'toto')
>>> which_arch(open('/bin/sh').read())
'X86-64'
Each architecture is defined by a file in cpu_rec_corpus.
Only file names ending with .corpus, which can be compressed with xz.
The corpus file shall contain instructions for the target architecture.
As you can see in build_default_corpus, most of the default corpus has
been created by extracting the TEXT section of an executable.
If you want to add an new architecture (e.g. 78k as described below)
then you have to find a binary, and extract the executable section
(the command line to extract the 78k code from the Metz firmware is
dd if=MB50AF1_NikonV12.bin of=Nec78k.corpus bs=1 skip=0x2ba count=0x7d5a).
Running the tool as a binwalk module typically results in:
…
Important: it is usually a good idea to start the analysis of an unknown
binary with some entropy analysis. cpu_rec assumes that it has been done,
but to protect the user against overlooking this aspect, it displays the
entropy.
If the entropy value is above 0.9, it is probably encrypted or compressed
data, and therefore the result of cpu_rec should be meaningless.
We can notice that during the analysis of ALPHA/NTDLL.DLL
small chunks are wrongly detected as non-Alpha architectures.
They should be ignored.
But some files can contain multiple architectures, e.g. Mach-O/OSXII
which is a Mach-O FAT file with ppc and i386 executables.
The tool has been presented at SSTIC 2017, with a full paper describing why this technique has been used for the recognition of architectures. A video of the presentation and the slides are available.
This presentation was made in French. A translation in English of the slides is available, a translation in English of the paper is in progress.
6502
68HC08
68HC11
8051
Alpha
ARC32eb ARC32el ARcompact
ARM64 ARMeb ARMel ARMhf
AVR
AxisCris
Blackfin
Cell-SPU
CLIPPER
CompactRISC
Cray
CUDA
Epiphany
FR-V
FR30
FT32
H8-300 H8S
HP-Focus
HP-PA
i860
IA-64
IQ2000
M32C
M32R
M68k
M88k
MCore
Mico32
MicroBlaze
MIPS16 MIPSeb MIPSel
MMIX
MN10300
Moxie
MSP430
NDS32
NIOS-II
OCaml
PDP-11
PIC10 PIC16 PIC18 PIC24
PPCeb PPCel
RISC-V
RL78
ROMP
RX
S-390
SPARC
STM8
Stormy16
SuperH
TILEPro
TLCS-90
TMS320C2x TMS320C6x
TriMedia
V850
VAX
Visium
WASM
WE32000
X86-64
X86
Xtensa
Z80
#6502#cc65
Because of licencing issues, the following architectures are not in
the default corpus, but they can be manually added:
78k
TriCore
The cpu_rec.py file is licenced under a Apache Licence, Version 2.0.
The files in the default corpus have been built from various sources. The corpus is a collection of various compressed files, each compressed file is dedicated to the recognition of one architecture and is made by the compression of the concatenation of one or many binary chunks, which come from various origins and have various licences. Therefore, the default corpus is a composite document, each sub-document (the chunk) being redistributed under the appropriate licence.
The origin of each chunk is described in cpu_rec.py, in the function
build_default_corpus. The licences are:
libgmp.so, libc.so, libm.so come from Debian binary
distributions and are distributed under GPLv2 (and LGPLv3 for recent
versions of libgmp) and the source code is available from
http://archive.debian.org/.busybox binaries come from https://busybox.net/downloads/binaries/
and are distributed under GPLv2.C-Kermit binaries come from ftp://kermit.columbia.edu/kermit/bin/
and are distributed under GPLv2
(according to ftp://kermit.columbia.edu/kermit/archives/COPYING but
the status of each binary is not always clear).build_default_corpus as part of the
CROSS_COMPILED subdirectory have been built by myself.
The corresponding source code are
zlib (from http://zlib.net/, distributed under the zlib licence)
or libjpeg (from http://www.ijg.org/, distributed under an unknown licence)
or some other code based on public sources
(e.g. https://anonscm.debian.org/cgit/pkg-games/bsdgames.git/tree/arithmetic/arithmetic.c modified to work with SDCC compilers).camlp4 binary is built from https://github.com/ocaml/camlp4
and distributed under LGPLv2.No open issues yet, or sync has not completed.