moar ci changes
This commit is contained in:
19 files changed
+1313
-663
No files matched your search
+259
-144
@@ -1,71 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Verifies that all PE DLL dependencies of an executable and its bundled plugins
|
||||
are satisfied within the target distribution directory.
|
||||
Bundles (optional) and verifies that all non-system PE (.exe / .dll) import
|
||||
dependencies in a Windows distribution folder are present inside the folder.
|
||||
|
||||
Usage:
|
||||
verify-windows-dlls.py /path/to/dist_dir
|
||||
verify-windows-dlls.py <dist-dir> [--bundle-from <dir> ...]
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import struct
|
||||
import sys
|
||||
|
||||
# Standard Windows system and runtime libraries (case-insensitive)
|
||||
# Standard Windows OS system DLLs provided by Windows (System32)
|
||||
SYSTEM_DLLS = {
|
||||
# Core OS & NT subsystems
|
||||
"ntdll.dll",
|
||||
"kernel32.dll",
|
||||
"kernelbase.dll",
|
||||
"user32.dll",
|
||||
"gdi32.dll",
|
||||
"gdiplus.dll",
|
||||
"shell32.dll",
|
||||
"shlwapi.dll",
|
||||
"ole32.dll",
|
||||
"oleaut32.dll",
|
||||
"comdlg32.dll",
|
||||
"comctl32.dll",
|
||||
"advapi32.dll",
|
||||
"version.dll",
|
||||
"ws2_32.dll",
|
||||
"wsock32.dll",
|
||||
"netapi32.dll",
|
||||
"userenv.dll",
|
||||
"winmm.dll",
|
||||
"uxtheme.dll",
|
||||
"dwmapi.dll",
|
||||
"imm32.dll",
|
||||
"crypt32.dll",
|
||||
"bcrypt.dll",
|
||||
"ncrypt.dll",
|
||||
"secur32.dll",
|
||||
"sspicli.dll",
|
||||
"setupapi.dll",
|
||||
"cfgmgr32.dll",
|
||||
"iphlpapi.dll",
|
||||
"dnsapi.dll",
|
||||
"rpcrt4.dll",
|
||||
"mpr.dll",
|
||||
"authz.dll",
|
||||
"wtsapi32.dll",
|
||||
"propsys.dll",
|
||||
"powrprof.dll",
|
||||
"winspool.drv",
|
||||
"shcore.dll",
|
||||
"hid.dll",
|
||||
# Graphics & DirectX
|
||||
"bcrypt.dll",
|
||||
"bcryptprimitives.dll",
|
||||
"cabinet.dll",
|
||||
"cfgmgr32.dll",
|
||||
"combase.dll",
|
||||
"comctl32.dll",
|
||||
"comdlg32.dll",
|
||||
"credui.dll",
|
||||
"crypt32.dll",
|
||||
"cryptbase.dll",
|
||||
"d2d1.dll",
|
||||
"d3d9.dll",
|
||||
"d3d10warp.dll",
|
||||
"d3d11.dll",
|
||||
"d3d12.dll",
|
||||
"dxgi.dll",
|
||||
"d2d1.dll",
|
||||
"d3dcompiler_47.dll",
|
||||
"dbghelp.dll",
|
||||
"dcomp.dll",
|
||||
"dhcpcsvc.dll",
|
||||
"dhcpcsvc6.dll",
|
||||
"dnsapi.dll",
|
||||
"dwmapi.dll",
|
||||
"dwrite.dll",
|
||||
"opengl32.dll",
|
||||
"dxcore.dll",
|
||||
"dxgi.dll",
|
||||
"gdi32.dll",
|
||||
"gdiplus.dll",
|
||||
"glu32.dll",
|
||||
# CRT / UCRT
|
||||
"hid.dll",
|
||||
"imagehlp.dll",
|
||||
"imm32.dll",
|
||||
"iphlpapi.dll",
|
||||
"kernel32.dll",
|
||||
"kernelbase.dll",
|
||||
"ksuser.dll",
|
||||
"mf.dll",
|
||||
"mfplat.dll",
|
||||
"mfreadwrite.dll",
|
||||
"mfuuid.dll",
|
||||
"mmdevapi.dll",
|
||||
"mpr.dll",
|
||||
"mscms.dll",
|
||||
"msimg32.dll",
|
||||
"msvcp_win.dll",
|
||||
"msvcrt.dll",
|
||||
"mswsock.dll",
|
||||
"ncrypt.dll",
|
||||
"netapi32.dll",
|
||||
"netutils.dll",
|
||||
"normaliz.dll",
|
||||
"ntdll.dll",
|
||||
"ole32.dll",
|
||||
"oleaut32.dll",
|
||||
"opengl32.dll",
|
||||
"pdh.dll",
|
||||
"powrprof.dll",
|
||||
"propsys.dll",
|
||||
"psapi.dll",
|
||||
"rpcrt4.dll",
|
||||
"runtimeobject.dll",
|
||||
"secur32.dll",
|
||||
"setupapi.dll",
|
||||
"shcore.dll",
|
||||
"shell32.dll",
|
||||
"shlwapi.dll",
|
||||
"srvcli.dll",
|
||||
"synchronization.dll",
|
||||
"ucrtbase.dll",
|
||||
"uiautomationcore.dll",
|
||||
"urlmon.dll",
|
||||
"user32.dll",
|
||||
"userenv.dll",
|
||||
"usp10.dll",
|
||||
"uxtheme.dll",
|
||||
"version.dll",
|
||||
"vulkan-1.dll",
|
||||
"wevtapi.dll",
|
||||
"winhttp.dll",
|
||||
"wininet.dll",
|
||||
"winmm.dll",
|
||||
"winspool.drv",
|
||||
"wkscli.dll",
|
||||
"wldap32.dll",
|
||||
"ws2_32.dll",
|
||||
"wtsapi32.dll",
|
||||
"xinput1_4.dll",
|
||||
"xmllite.dll",
|
||||
}
|
||||
|
||||
|
||||
@@ -78,137 +116,214 @@ def is_system_dll(name):
|
||||
return False
|
||||
|
||||
|
||||
def get_pe_imports(file_path):
|
||||
try:
|
||||
with open(file_path, "rb") as f:
|
||||
data = f.read()
|
||||
except Exception as e:
|
||||
print(f"Warning: could not read {file_path}: {e}", file=sys.stderr)
|
||||
return []
|
||||
def rva_to_offset(rva, sections):
|
||||
for vaddr, vsize, raw_ptr, raw_size in sections:
|
||||
size = max(vsize, raw_size)
|
||||
if vaddr <= rva < vaddr + size:
|
||||
return raw_ptr + (rva - vaddr)
|
||||
return None
|
||||
|
||||
|
||||
def read_cstring(data, offset):
|
||||
if offset is None or offset < 0 or offset >= len(data):
|
||||
return None
|
||||
end = data.find(b"\x00", offset)
|
||||
if end == -1:
|
||||
return None
|
||||
return data[offset:end].decode("ascii", errors="replace")
|
||||
|
||||
|
||||
def get_pe_imports(filepath):
|
||||
with open(filepath, "rb") as f:
|
||||
data = f.read()
|
||||
|
||||
if len(data) < 0x40 or data[:2] != b"MZ":
|
||||
return []
|
||||
|
||||
pe_offset = struct.unpack_from("<I", data, 0x3c)[0]
|
||||
if pe_offset + 24 > len(data) or data[pe_offset:pe_offset+4] != b"PE\x00\x00":
|
||||
return []
|
||||
|
||||
magic = struct.unpack_from("<H", data, pe_offset + 24)[0]
|
||||
if magic == 0x20b: # PE32+ (64-bit)
|
||||
import_rva_offset = pe_offset + 24 + 120
|
||||
elif magic == 0x10b: # PE32 (32-bit)
|
||||
import_rva_offset = pe_offset + 24 + 104
|
||||
else:
|
||||
return []
|
||||
|
||||
if import_rva_offset + 8 > len(data):
|
||||
return []
|
||||
|
||||
import_rva, import_size = struct.unpack_from("<II", data, import_rva_offset)
|
||||
if import_rva == 0 or import_size == 0:
|
||||
pe_offset = struct.unpack_from("<I", data, 0x3C)[0]
|
||||
if pe_offset + 24 > len(data) or data[pe_offset : pe_offset + 4] != b"PE\x00\x00":
|
||||
return []
|
||||
|
||||
num_sections = struct.unpack_from("<H", data, pe_offset + 6)[0]
|
||||
size_of_opt_header = struct.unpack_from("<H", data, pe_offset + 20)[0]
|
||||
sec_offset = pe_offset + 24 + size_of_opt_header
|
||||
opt_header_size = struct.unpack_from("<H", data, pe_offset + 20)[0]
|
||||
opt_offset = pe_offset + 24
|
||||
|
||||
if opt_offset + opt_header_size > len(data):
|
||||
return []
|
||||
|
||||
magic = struct.unpack_from("<H", data, opt_offset)[0]
|
||||
if magic == 0x20B: # PE32+ (64-bit: x86_64 / ARM64)
|
||||
dd_offset = opt_offset + 112
|
||||
elif magic == 0x10B: # PE32 (32-bit)
|
||||
dd_offset = opt_offset + 96
|
||||
else:
|
||||
return []
|
||||
|
||||
num_rva = struct.unpack_from("<I", data, dd_offset - 4)[0]
|
||||
if num_rva < 2:
|
||||
return []
|
||||
|
||||
import_rva, import_size = struct.unpack_from("<II", data, dd_offset + 8)
|
||||
if import_rva == 0 or import_size == 0:
|
||||
return []
|
||||
|
||||
sec_offset = opt_offset + opt_header_size
|
||||
sections = []
|
||||
for i in range(num_sections):
|
||||
sec = data[sec_offset + i * 40 : sec_offset + (i + 1) * 40]
|
||||
if len(sec) < 40:
|
||||
s_off = sec_offset + i * 40
|
||||
if s_off + 40 > len(data):
|
||||
break
|
||||
vsize, vrva, rsize, roffset = struct.unpack_from("<IIII", sec, 8)
|
||||
sections.append((vrva, vsize, roffset, rsize))
|
||||
vsize, vaddr, raw_size, raw_ptr = struct.unpack_from("<IIII", data, s_off + 8)
|
||||
sections.append((vaddr, vsize, raw_ptr, raw_size))
|
||||
|
||||
def rva_to_off(rva):
|
||||
for vrva, vsize, roffset, rsize in sections:
|
||||
if vrva <= rva < vrva + max(vsize, rsize):
|
||||
return roffset + (rva - vrva)
|
||||
return None
|
||||
|
||||
import_off = rva_to_off(import_rva)
|
||||
if not import_off:
|
||||
imp_offset = rva_to_offset(import_rva, sections)
|
||||
if imp_offset is None:
|
||||
return []
|
||||
|
||||
imports = []
|
||||
while import_off + 20 <= len(data):
|
||||
desc = data[import_off : import_off + 20]
|
||||
if desc == b"\x00" * 20:
|
||||
pos = imp_offset
|
||||
while pos + 20 <= len(data):
|
||||
orig_thunk, ts, fwd, name_rva, first_thunk = struct.unpack_from("<IIIII", data, pos)
|
||||
if orig_thunk == 0 and name_rva == 0 and first_thunk == 0:
|
||||
break
|
||||
name_rva = struct.unpack_from("<I", desc, 12)[0]
|
||||
name_off = rva_to_off(name_rva)
|
||||
if name_off and name_off < len(data):
|
||||
name = data[name_off:].split(b"\x00", 1)[0].decode("latin1", errors="ignore")
|
||||
if name:
|
||||
imports.append(name)
|
||||
import_off += 20
|
||||
name_off = rva_to_offset(name_rva, sections)
|
||||
dll_name = read_cstring(data, name_off)
|
||||
if dll_name:
|
||||
imports.append(dll_name)
|
||||
pos += 20
|
||||
|
||||
return imports
|
||||
|
||||
|
||||
def verify_dist_directory(dist_dir):
|
||||
if not os.path.isdir(dist_dir):
|
||||
print(f"Error: directory does not exist: {dist_dir}", file=sys.stderr)
|
||||
return False
|
||||
def collect_pe_files(dist_dir):
|
||||
pe_files = []
|
||||
for root, _, files in os.walk(dist_dir):
|
||||
for fname in files:
|
||||
lower = fname.lower()
|
||||
if lower.endswith(".dll") or lower.endswith(".exe"):
|
||||
pe_files.append(os.path.join(root, fname))
|
||||
return sorted(pe_files)
|
||||
|
||||
# Collect all available DLLs in the dist directory (case-insensitive)
|
||||
available_dlls = {}
|
||||
binaries = []
|
||||
|
||||
def bundle_transitive_dlls(dist_dir, search_dirs):
|
||||
"""
|
||||
Walks all PE files in dist_dir and recursively copies required non-system DLLs
|
||||
from search_dirs into dist_dir root.
|
||||
"""
|
||||
# Build case-insensitive lookup index of available DLLs in search_dirs
|
||||
available = {}
|
||||
for sdir in search_dirs:
|
||||
if not sdir or not os.path.isdir(sdir):
|
||||
continue
|
||||
for fname in os.listdir(sdir):
|
||||
if fname.lower().endswith(".dll"):
|
||||
lower = fname.lower()
|
||||
if lower not in available:
|
||||
available[lower] = os.path.join(sdir, fname)
|
||||
|
||||
# Track DLLs already present in dist_dir
|
||||
present = set()
|
||||
queue = []
|
||||
for full_path in collect_pe_files(dist_dir):
|
||||
present.add(os.path.basename(full_path).lower())
|
||||
queue.append(full_path)
|
||||
|
||||
copied_count = 0
|
||||
while queue:
|
||||
current_pe = queue.pop(0)
|
||||
current_dir = os.path.dirname(current_pe)
|
||||
local_dlls = {
|
||||
f.lower()
|
||||
for f in os.listdir(current_dir)
|
||||
if f.lower().endswith(".dll") or f.lower().endswith(".exe")
|
||||
}
|
||||
|
||||
for imp in get_pe_imports(current_pe):
|
||||
imp_lower = imp.lower()
|
||||
if is_system_dll(imp_lower):
|
||||
continue
|
||||
if imp_lower in present or imp_lower in local_dlls:
|
||||
continue
|
||||
if imp_lower in available:
|
||||
src_dll = available[imp_lower]
|
||||
dst_dll = os.path.join(dist_dir, os.path.basename(src_dll))
|
||||
shutil.copy2(src_dll, dst_dll)
|
||||
present.add(imp_lower)
|
||||
queue.append(dst_dll)
|
||||
copied_count += 1
|
||||
|
||||
print(f"==> Bundled {copied_count} transitive runtime DLLs into {dist_dir}")
|
||||
|
||||
|
||||
def verify_dist_directory(dist_dir):
|
||||
root_dlls = set()
|
||||
all_pe_files = []
|
||||
|
||||
for root, _, files in os.walk(dist_dir):
|
||||
for f in files:
|
||||
lower = f.lower()
|
||||
rel_path = os.path.relpath(os.path.join(root, f), dist_dir)
|
||||
if lower.endswith(".dll"):
|
||||
available_dlls[lower] = rel_path
|
||||
binaries.append(os.path.join(root, f))
|
||||
elif lower.endswith(".exe"):
|
||||
binaries.append(os.path.join(root, f))
|
||||
for fname in files:
|
||||
lower = fname.lower()
|
||||
if lower.endswith(".dll") or lower.endswith(".exe"):
|
||||
full_path = os.path.join(root, fname)
|
||||
all_pe_files.append(full_path)
|
||||
if os.path.abspath(root) == os.path.abspath(dist_dir):
|
||||
root_dlls.add(lower)
|
||||
|
||||
if not binaries:
|
||||
print(f"Error: no .exe or .dll binaries found in {dist_dir}", file=sys.stderr)
|
||||
return False
|
||||
if not all_pe_files:
|
||||
raise RuntimeError(f"No .exe or .dll files found in {dist_dir}")
|
||||
|
||||
missing_deps = []
|
||||
checked_count = len(binaries)
|
||||
missing_by_dll = {}
|
||||
for pe_file in sorted(all_pe_files):
|
||||
rel_pe = os.path.relpath(pe_file, dist_dir)
|
||||
local_dir = os.path.dirname(pe_file)
|
||||
local_dlls = {
|
||||
f.lower()
|
||||
for f in os.listdir(local_dir)
|
||||
if f.lower().endswith(".dll") or f.lower().endswith(".exe")
|
||||
}
|
||||
|
||||
for bin_path in binaries:
|
||||
rel_bin = os.path.relpath(bin_path, dist_dir)
|
||||
imports = get_pe_imports(bin_path)
|
||||
for imp in imports:
|
||||
if is_system_dll(imp):
|
||||
for imp in get_pe_imports(pe_file):
|
||||
imp_lower = imp.lower()
|
||||
if is_system_dll(imp_lower):
|
||||
continue
|
||||
if imp.lower() not in available_dlls:
|
||||
missing_deps.append((rel_bin, imp))
|
||||
if imp_lower not in root_dlls and imp_lower not in local_dlls:
|
||||
missing_by_dll.setdefault(imp, []).append(rel_pe)
|
||||
|
||||
if missing_deps:
|
||||
print(f"FAILED: Found {len(missing_deps)} missing DLL dependencies in {dist_dir}:", file=sys.stderr)
|
||||
# Deduplicate and group by missing DLL
|
||||
missing_by_dll = {}
|
||||
for rel_bin, imp in missing_deps:
|
||||
missing_by_dll.setdefault(imp, []).append(rel_bin)
|
||||
if missing_by_dll:
|
||||
print(f"FAILED: Found {len(missing_by_dll)} missing DLL dependencies in {dist_dir}:", file=sys.stderr)
|
||||
for dll, importers in sorted(missing_by_dll.items()):
|
||||
print(f" [MISSING] {dll} needed by:", file=sys.stderr)
|
||||
for importer in importers[:5]:
|
||||
print(f" - {importer}", file=sys.stderr)
|
||||
if len(importers) > 5:
|
||||
print(f" ... and {len(importers) - 5} more", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
for imp, users in sorted(missing_by_dll.items()):
|
||||
print(f" [MISSING] {imp} needed by:", file=sys.stderr)
|
||||
for u in users[:5]:
|
||||
print(f" - {u}", file=sys.stderr)
|
||||
if len(users) > 5:
|
||||
print(f" ... and {len(users) - 5} more", file=sys.stderr)
|
||||
return False
|
||||
|
||||
print(f"OK: Verified {checked_count} binaries. All external DLL imports satisfied ({len(available_dlls)} DLLs bundled).")
|
||||
return True
|
||||
print(
|
||||
f"==> DLL verification passed: scanned {len(all_pe_files)} binaries in {dist_dir}, "
|
||||
"0 missing dependencies."
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: verify-windows-dlls.py <dist-dir>", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
parser = argparse.ArgumentParser(description="Bundle and verify PE DLL imports in a Windows dist directory")
|
||||
parser.add_argument("dist_dir", help="Path to the distribution directory containing asteria-qt.exe")
|
||||
parser.add_argument(
|
||||
"--bundle-from",
|
||||
nargs="+",
|
||||
default=[],
|
||||
help="Optional list of bin directories to copy transitive DLL dependencies from before verifying",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
dist_dir = sys.argv[1]
|
||||
if not verify_dist_directory(dist_dir):
|
||||
if not os.path.isdir(args.dist_dir):
|
||||
print(f"Error: directory does not exist: {args.dist_dir}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if args.bundle_from:
|
||||
bundle_transitive_dlls(args.dist_dir, args.bundle_from)
|
||||
|
||||
verify_dist_directory(args.dist_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user