Nguyen Tan Cong | 09559a6 | 2015-05-06 07:53:04 +0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Nguyen Anh Quynh | 5778087 | 2015-05-06 10:39:28 +0800 | [diff] [blame] | 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. |
Nguyen Tan Cong | 09559a6 | 2015-05-06 07:53:04 +0700 | [diff] [blame] | 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) |