|
| 1 | +package pe |
| 2 | + |
| 3 | +// https://osandamalith.com/2020/07/19/exploring-the-ms-dos-stub/ |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/wader/fq/format" |
| 7 | + "github.com/wader/fq/pkg/decode" |
| 8 | + "github.com/wader/fq/pkg/interp" |
| 9 | + "github.com/wader/fq/pkg/scalar" |
| 10 | +) |
| 11 | + |
| 12 | +// TODO: probe? |
| 13 | + |
| 14 | +func init() { |
| 15 | + interp.RegisterFormat(decode.Format{ |
| 16 | + Name: format.PE_MSDOS_STUB, // TODO: not PE_ prefix? |
| 17 | + Description: "MS-DOS Stub", |
| 18 | + DecodeFn: msDosStubDecode, |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +func msDosStubDecode(d *decode.D, _ any) any { |
| 23 | + d.Endian = decode.LittleEndian |
| 24 | + |
| 25 | + d.FieldU16("e_magic", scalar.Description("Magic number"), d.AssertU(0x5a4d), scalar.ActualHex) |
| 26 | + d.FieldU16("e_cblp", scalar.Description("Bytes on last page of file")) |
| 27 | + d.FieldU16("e_cp", scalar.Description("Pages in file")) |
| 28 | + d.FieldU16("e_crlc", scalar.Description("Relocations")) |
| 29 | + d.FieldU16("e_cparhdr", scalar.Description("Size of header in paragraphs")) |
| 30 | + d.FieldU16("e_minalloc", scalar.Description("Minimum extra paragraphs needed")) |
| 31 | + d.FieldU16("e_maxalloc", scalar.Description("Maximum extra paragraphs needed")) |
| 32 | + d.FieldU16("e_ss", scalar.Description("Initial (relative) SS value")) |
| 33 | + d.FieldU16("e_sp", scalar.Description("Initial SP value")) |
| 34 | + d.FieldU16("e_csum", scalar.Description("Checksum")) |
| 35 | + d.FieldU16("e_ip", scalar.Description("Initial IP value")) |
| 36 | + d.FieldU16("e_cs", scalar.Description("Initial (relative) CS value")) |
| 37 | + d.FieldU16("e_lfarlc", scalar.Description("File address of relocation table")) |
| 38 | + d.FieldU16("e_ovno", scalar.Description("Overlay number")) |
| 39 | + d.FieldRawLen("e_res", 4*16, scalar.Description("Reserved words")) |
| 40 | + d.FieldU16("e_oemid", scalar.Description("OEM identifier (for e_oeminfo)")) |
| 41 | + d.FieldU16("e_oeminfo", scalar.Description("OEM information; e_oemid specific")) |
| 42 | + d.FieldRawLen("e_res2", 10*16, scalar.Description("Reserved words")) |
| 43 | + lfanew := d.FieldU32("e_lfanew", scalar.Description("File address of new exe header")) |
| 44 | + |
| 45 | + // TODO: x86 format in the future |
| 46 | + d.FieldRawLen("stub", 64*8, scalar.Description("Sub program")) |
| 47 | + |
| 48 | + subEndPos := d.Pos() |
| 49 | + |
| 50 | + // TODO: is not padding i guess? |
| 51 | + padding := lfanew*8 - uint64(subEndPos) |
| 52 | + d.FieldRawLen("padding", int64(padding)) |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
0 commit comments