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
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)
30