blob: d5036e887921096cd76540103bd923572ae00adf [file] [log] [blame]
Nguyen Tan Cong09559a62015-05-06 07:53:04 +07001#!/usr/bin/env python
Nguyen Anh Quynh57780872015-05-06 10:39:28 +08002# 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.
Nguyen Tan Cong09559a62015-05-06 07:53:04 +07007
8import sys, struct
9
10if len(sys.argv) < 2:
11 print("Usage: %s <pe_file_path>" % sys.argv[0])
12 sys.exit(0)
13
14pe_file_path = sys.argv[1]
15
16with open(pe_file_path, "rb") as f:
17 b = f.read()
18
19if not b.startswith("MZ"):
20 print("Not a PE file")
21 sys.exit(0)
22
23e_lfanew = struct.unpack_from("<I", b, 0x3C)[0]
24vb = struct.pack("<HHHHH", 5, 0, 0, 0, 5) # encode versions
25# patches MajorOperatingSystemVersion and MajorSubsystemVersion
26b = b[0:e_lfanew + 0x40] + vb + b[e_lfanew + 0x4A:]
27# write back to file
28with open(pe_file_path, "wb") as f:
29 f.write(b)