18 lines
865 B
Python
Executable File
18 lines
865 B
Python
Executable File
#!/usr/bin/env python3
|
|
"""Structural PE32 check; not a Windows ABI or execution test."""
|
|
import pathlib, struct, sys
|
|
p=pathlib.Path(sys.argv[1]); b=p.read_bytes(); kind=sys.argv[2]
|
|
def u16(o): return struct.unpack_from('<H',b,o)[0]
|
|
def u32(o): return struct.unpack_from('<I',b,o)[0]
|
|
try:
|
|
assert b[:2]==b'MZ', 'DOS signature'
|
|
o=u32(0x3c); assert b[o:o+4]==b'PE\0\0', 'PE signature'
|
|
assert u16(o+4)==0x14c, 'not x86'
|
|
h=o+24; assert u16(h)==0x10b, 'not PE32'
|
|
assert u32(h+16)>0, 'no entry point'
|
|
sub=u16(h+68); assert sub==(1 if kind=='sys' else 3), 'unexpected subsystem'
|
|
assert not u16(o+22)&0x2000, 'DLL rather than executable/driver'
|
|
print(f'{p.name}: PE32 x86, subsystem {sub}, OS {u16(h+40)}.{u16(h+42)}, subsystem version {u16(h+48)}.{u16(h+50)}')
|
|
except (AssertionError,struct.error) as e:
|
|
sys.exit(f'{p}: invalid PE: {e}')
|