-
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy path_peclean.py
More file actions
executable file
·64 lines (55 loc) · 2.17 KB
/
_peclean.py
File metadata and controls
executable file
·64 lines (55 loc) · 2.17 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# Copyright (C) Viktor Szakats. See LICENSE.md
# SPDX-License-Identifier: MIT
# Sets internal timestamps in PE executables.
import datetime
import glob
import os
import sys
import pefile
if len(sys.argv) > 2:
# https://docs.python.org/3/library/os.path.html#os.path.getmtime
# https://docs.python.org/3/library/time.html
fref = os.path.normpath(sys.argv[1])
try:
ts = int(os.path.getmtime(fref))
except OSError as e:
print(f"Error: Cannot access reference file '{fref}': {e}", file=sys.stderr)
sys.exit(1)
for argv in sys.argv[2:]:
for fname in glob.glob(argv):
print(
datetime.datetime.fromtimestamp(ts, datetime.timezone.utc).isoformat()
+ " -> "
+ fname
)
try:
pe = pefile.PE(fname)
except pefile.PEFormatError as e:
print(f"Error: Not a PE file '{fname}': {e}", file=sys.stderr)
continue
# https://learn.microsoft.com/cpp/build/reference/dependentloadflag
# https://learn.microsoft.com/windows/win32/dlls/dynamic-link-library-search-order#search-order-using-load_library_search-flags
try:
pe.DIRECTORY_ENTRY_LOAD_CONFIG.struct.DependentLoadFlags = 0x800 # 0x800 = LOAD_LIBRARY_SEARCH_SYSTEM32, for binutils ld
except AttributeError:
# Silently ignore if there is no such item
pass
pe.FILE_HEADER.TimeDateStamp = ts
try:
pe.DIRECTORY_ENTRY_EXPORT.struct.TimeDateStamp = ts
except AttributeError:
# Silently ignore if there is no such item
pass
try:
for entry in pe.DIRECTORY_ENTRY_DEBUG:
entry.struct.TimeDateStamp = ts
except AttributeError:
# Silently ignore if there is no such item
pass
pe.OPTIONAL_HEADER.CheckSum = pe.generate_checksum()
pe.write(fname)
pe.close()
else:
print("Usage: _peclean.py <reference-file> <exe-file[s]>", file=sys.stderr)
sys.exit(1)