Rizin
unix-like reverse engineering framework and cli tools
patch_major_os_version.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # By Daniel Pistelli & Nguyen Tan Cong
3 
4 # This script is to patch DLL/EXE MajorVersion to 5,
5 # so they can be loaded by Windows XP.
6 # This is the problem introduced by compiling on Windows 7, using VS2013.
7 
8 import sys, struct
9 
10 if len(sys.argv) < 2:
11  print("Usage: %s <pe_file_path>" % sys.argv[0])
12  sys.exit(0)
13 
14 pe_file_path = sys.argv[1]
15 
16 with open(pe_file_path, "rb") as f:
17  b = f.read()
18 
19 if not b.startswith("MZ"):
20  print("Not a PE file")
21  sys.exit(0)
22 
23 e_lfanew = struct.unpack_from("<I", b, 0x3C)[0]
24 vb = struct.pack("<HHHHH", 5, 0, 0, 0, 5) # encode versions
25 # patches MajorOperatingSystemVersion and MajorSubsystemVersion
26 b = b[0:e_lfanew + 0x40] + vb + b[e_lfanew + 0x4A:]
27 # write back to file
28 with open(pe_file_path, "wb") as f:
29  f.write(b)
size_t len
Definition: 6502dis.c:15