|
| 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( |
| 16 | + format.MSDOS_Stub, |
| 17 | + &decode.Format{ |
| 18 | + Description: "MS-DOS Stub", |
| 19 | + DecodeFn: msDosStubDecode, |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func msDosStubDecode(d *decode.D) any { |
| 24 | + d.Endian = decode.LittleEndian |
| 25 | + |
| 26 | + d.FieldU16("e_magic", scalar.UintDescription("Magic number"), d.UintAssert(0x5a4d), scalar.UintHex) |
| 27 | + d.FieldU16("e_cblp", scalar.UintDescription("Bytes on last page of file")) |
| 28 | + d.FieldU16("e_cp", scalar.UintDescription("Pages in file")) |
| 29 | + d.FieldU16("e_crlc", scalar.UintDescription("Relocations")) |
| 30 | + d.FieldU16("e_cparhdr", scalar.UintDescription("Size of header in paragraphs")) |
| 31 | + d.FieldU16("e_minalloc", scalar.UintDescription("Minimum extra paragraphs needed")) |
| 32 | + d.FieldU16("e_maxalloc", scalar.UintDescription("Maximum extra paragraphs needed")) |
| 33 | + d.FieldU16("e_ss", scalar.UintDescription("Initial (relative) SS value")) |
| 34 | + d.FieldU16("e_sp", scalar.UintDescription("Initial SP value")) |
| 35 | + d.FieldU16("e_csum", scalar.UintDescription("Checksum")) |
| 36 | + d.FieldU16("e_ip", scalar.UintDescription("Initial IP value")) |
| 37 | + d.FieldU16("e_cs", scalar.UintDescription("Initial (relative) CS value")) |
| 38 | + d.FieldU16("e_lfarlc", scalar.UintDescription("File address of relocation table")) |
| 39 | + d.FieldU16("e_ovno", scalar.UintDescription("Overlay number")) |
| 40 | + d.FieldRawLen("e_res", 4*16, scalar.BitBufDescription("Reserved words")) |
| 41 | + d.FieldU16("e_oemid", scalar.UintDescription("OEM identifier (for e_oeminfo)")) |
| 42 | + d.FieldU16("e_oeminfo", scalar.UintDescription("OEM information; e_oemid specific")) |
| 43 | + d.FieldRawLen("e_res2", 10*16, scalar.BitBufDescription("Reserved words")) |
| 44 | + lfanew := d.FieldU32("e_lfanew", scalar.UintDescription("File address of new exe header")) |
| 45 | + |
| 46 | + // TODO: x86 format in the future |
| 47 | + d.FieldRawLen("stub", 64*8, scalar.BitBufDescription("Sub program")) |
| 48 | + |
| 49 | + subEndPos := d.Pos() |
| 50 | + |
| 51 | + // TODO: is not padding i guess? |
| 52 | + padding := lfanew*8 - uint64(subEndPos) |
| 53 | + d.FieldRawLen("padding", int64(padding)) |
| 54 | + |
| 55 | + return format.MS_DOS_Out{ |
| 56 | + LFANew: int(lfanew), |
| 57 | + } |
| 58 | +} |
0 commit comments