-
Notifications
You must be signed in to change notification settings - Fork 0
/
peinfo.py
49 lines (41 loc) · 2 KB
/
peinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
import datetime
import struct
def hexread(file, offset, length):
file.seek(offset)
return struct.unpack('<I', file.read(length))[0]
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python peinfo.py <game_code> <dll_path>")
else:
game_code = sys.argv[1]
dll_path = sys.argv[2]
try:
with open(dll_path, 'rb') as file:
# Read DOS header to get PE header offset
pe_header_offset = hexread(file, 0x3C, 4)
# Check for "PE\0\0" signature
file.seek(pe_header_offset)
if file.read(4) != b'PE\0\0':
raise ValueError(f"File '{dll_path}' is not a valid PE file.")
# Read TimeDateStamp
timestamp_offset = pe_header_offset + 8
timestamp = hexread(file, timestamp_offset, 4)
readable_timestamp = datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
# Read AddressOfEntryPoint
optional_header_offset = pe_header_offset + 24
entry_point_offset = optional_header_offset + 16
entry_point = hexread(file, entry_point_offset, 4)
# Concatenate GameCode, TimeDateStamp, and AddressOfEntryPoint
identifier = f"{game_code.upper()}-{timestamp:x}_{entry_point:x}"
# Output results
print(f"TimeDateStamp: {readable_timestamp} (unix:{timestamp}) (hex:0x{timestamp:08X}) (offset:0x{timestamp_offset:08X})")
print(f"AddressOfEntryPoint: 0x{entry_point:08X} (offset:0x{entry_point_offset:08X})")
print(f"PE Identifier: {identifier}")
print(f"JSON File Name: {identifier}.json")
except FileNotFoundError:
print(f"Error: File '{dll_path}' not found.")
except ValueError as ve:
print(f"Error: {ve}")
except Exception as e:
print(f"Error: {str(e)}")