binfmt-detector-cli: rewrite to support PE32+ binaries (#38)

Rewrite with hard-coded offsets into the PE file format to discern
if a binary is PE32 or PE32+, and then to determine if it contains
a "CLR Data Directory" entry that looks valid.

Tested with PE32 and PE32+ compiled Mono binaries, PE32 and PE32+ native
binaries, and a random assortment of garbage files.

Former-commit-id: 9e7ac86ec84f653a2f79b87183efd5b0ebda001b
This commit is contained in:
Pat Tullmann
2023-10-16 11:16:47 -07:00
committed by GitHub
parent bb231c5b06
commit 0cb742dafb
4772 changed files with 11391249 additions and 2669 deletions

View File

@@ -1,45 +1,28 @@
binfmt-pe
binfmt-detector-cli
This utility determines the Microsoft PE executable file's
type (Native, .NET CLR) and runs it using the appropriate
runtime (WINE, CLI). It also detects and refuses to run
MS-DOS (non-PE) executables.
It is inteded to be used in a Linux binfmt configuration,
since binfmt itself is incapable of reliably distinguishing
between various PE file types (since they have no different
"magic string") and runtimes refuse to run files which
they don't support (CLR runtimes refuse to run
Native images and vice versa).
type (Native or .NET CLR).
It is inteded to be used as a filter a Linux binfmt
configuration, since binfmt itself is incapable of reliably
distinguishing between various PE file types (since they have
no different "magic string").
Technical information
The file's type is determined from certain characteristics
in the PE / COFF file header. It should be noted that the
techniques used might not be standard and are not throughtly
tested to work, so false detections might occur.
In short:
- An MS-DOS executable is assumed if the PE offset in the MS-DOS
header is NULL or points to an offset beyond the file's length
- A CLR file is assumed if the PE header's directory entry
for "CLI header" is not NULL and points to a valid offset in
the executable file.
- A native executable file is assumed otherwise.
The runtime names are hardcoded into the utilit (that is -
not configurable) and are exec'ed from the utility --
"wine" is used for native images, "cli" is used for CLR images.
Credits
To be considered a CLR executable, the PE header's directory
entry for the "CLR Header" must have a non-zero address and
a non-zero size.
This utility is based on the PE / COFF header structures from
Ximian's Mono .NET runtime project ( http://www.go-mono.com/ ).
Author
Original Author
Ilya Konstantinov <future@shiny.co.il>
Licenses
This utility is covered by the GPL license.