Skip to content

Instantly share code, notes, and snippets.

@KokoseiJ
Created August 8, 2024 09:52
Show Gist options
  • Select an option

  • Save KokoseiJ/69b7142dfa2a41a05dcbd0c13ad5bd02 to your computer and use it in GitHub Desktop.

Select an option

Save KokoseiJ/69b7142dfa2a41a05dcbd0c13ad5bd02 to your computer and use it in GitHub Desktop.
Generate DLL Export redirection, both in pragma format and .def file format, for DLL proxying
import re
import pefile
dllpath = input("DLL dir: ")
dll = pefile.PE(dllpath)
if "drive_c" in dllpath:
dllpath = re.sub(r".*?drive_c", "C:", dllpath).replace("/", "\\")
repath = re.sub(r"\.[dD][lL]{2}", "", dllpath)
repath_escaped = repath.replace("\\", "\\\\")
pragma = [
f"""#pragma comment(linker, "/export:{symbol.name.decode()}=\\"{repath_escaped}.{symbol.name.decode()}\\"")"""
for symbol in dll.DIRECTORY_ENTRY_EXPORT.symbols
if symbol.name
]
defs = [
f""" {symbol.name.decode()}="{repath}".{symbol.name.decode()}"""
for symbol in dll.DIRECTORY_ENTRY_EXPORT.symbols
if symbol.name
]
pragmastr = "\n".join(pragma)
defsstr = "EXPORTS\n" + "\n".join(defs)
print(pragmastr)
print()
print(defsstr)
with open("pragma.cpp", "w") as f:
f.write(pragmastr)
with open("export.def", "w") as f:
f.write(defsstr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment