Skip to content

Instantly share code, notes, and snippets.

@petewarden
Created July 6, 2024 20:33
Show Gist options
  • Save petewarden/5fd5df28b94fa2391e381c15c8b6cd2f to your computer and use it in GitHub Desktop.
Save petewarden/5fd5df28b94fa2391e381c15c8b6cd2f to your computer and use it in GitHub Desktop.
Python script to determine the architecture type of a DLL on Windows
#!/usr/bin/env python
import os
import struct
import sys
# adapted from: http://stackoverflow.com/a/495305/1338797 and
# https://github.com/tgandor/meats/blob/master/missing/arch_of.py
def arch_of(dll_file):
with open(dll_file, 'rb') as f:
doshdr = f.read(64)
magic, padding, offset = struct.unpack('2s58si', doshdr)
# print magic, offset
if magic != b'MZ':
return None
f.seek(offset, os.SEEK_SET)
pehdr = f.read(6)
# careful! H == unsigned short, x64 is negative with signed
magic, padding, machine = struct.unpack('2s2sH', pehdr)
# print(magic, hex(machine))
if magic != b'PE':
return None
if machine == 0x014c:
return 'i386'
if machine == 0x0200:
return 'IA64'
if machine == 0x8664:
return 'x64'
if machine == 0xaa64:
return 'arm64'
return 'unknown'
print(arch_of(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment