Skip to content

Instantly share code, notes, and snippets.

@PotatoPwn
Last active December 9, 2024 03:12
Show Gist options
  • Save PotatoPwn/cb72f1fe7a8235f109da0c40b3c61a9f to your computer and use it in GitHub Desktop.
Save PotatoPwn/cb72f1fe7a8235f109da0c40b3c61a9f to your computer and use it in GitHub Desktop.

Revisions

  1. PotatoPwn revised this gist Dec 9, 2024. 1 changed file with 20 additions and 10 deletions.
    30 changes: 20 additions & 10 deletions GetExportAndImport.py
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,26 @@
    import os
    import json

    def Error_Info(OrdinalOffset: int, Ordinalname : str):
    if Ordinalname is None:
    return f"Ordinal_{OrdinalOffset}"
    else:
    return Ordinalname.decode()


    def GetExportInformation(FileName: str):
    try:
    directory = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
    file_content = pefile.PE(FileName, fast_load=True)
    file_content.parse_data_directories(directories=directory)
    exportslist = []
    for exports in file_content.DIRECTORY_ENTRY_EXPORT.symbols:
    export_name = Error_Info(OrdinalOffset=exports.ordinal, Ordinalname=exports.name)
    exportslist.append(export_name)
    return exportslist
    except Exception as e:
    return []

    try:
    directory = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
    file_content = pefile.PE(FileName, fast_load=True)
    file_content.parse_data_directories(directories=directory)
    exports = [e.name.decode() for e in file_content.DIRECTORY_ENTRY_EXPORT.symbols if e.name]
    return sorted(exports)
    except Exception as e:
    return []

    def GetImportInformation(FileName: str):
    try:
    @@ -45,7 +55,7 @@ def GetImportInformation(FileName: str):
    "error": "Failed to process this file."
    }

    with open("results.json", "w") as json_file:
    with open("Results.json", "w") as json_file:
    json.dump(results, json_file, indent=4)

    print("Output written to results.json")
    print("Output written to Results.json")
  2. PotatoPwn revised this gist Dec 9, 2024. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions GetExportAndImport.py
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,7 @@ def GetImportInformation(FileName: str):
    "error": "Failed to process this file."
    }

    # Save the results to a JSON file
    with open("pe_info.json", "w") as json_file:
    with open("results.json", "w") as json_file:
    json.dump(results, json_file, indent=4)

    print("Output written to pe_info.json")
    print("Output written to results.json")
  3. PotatoPwn created this gist Dec 9, 2024.
    52 changes: 52 additions & 0 deletions GetExportAndImport.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import pefile
    import os
    import json

    def GetExportInformation(FileName: str):

    try:
    directory = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
    file_content = pefile.PE(FileName, fast_load=True)
    file_content.parse_data_directories(directories=directory)
    exports = [e.name.decode() for e in file_content.DIRECTORY_ENTRY_EXPORT.symbols if e.name]
    return sorted(exports)
    except Exception as e:
    return []

    def GetImportInformation(FileName: str):
    try:
    file_content = pefile.PE(FileName, fast_load=True)
    file_content.parse_data_directories()
    imports = []
    for entry in file_content.DIRECTORY_ENTRY_IMPORT:
    dll_name = entry.dll.decode() if entry.dll else "Unknown"
    functions = [imp.name.decode() for imp in entry.imports if imp.name]
    imports.append({"dll": dll_name, "functions": functions})
    return imports
    except Exception as e:
    return []

    if __name__ == "__main__":
    results = {}
    file_names = os.listdir()

    for file in file_names:
    try:
    print(f"Processing file: {file}")
    imports = GetImportInformation(file)
    exports = GetExportInformation(file)
    results[file] = {
    "imports": imports,
    "exports": exports
    }
    except Exception as e:
    print(f"Failed to process {file}: {str(e)}")
    results[file] = {
    "error": "Failed to process this file."
    }

    # Save the results to a JSON file
    with open("pe_info.json", "w") as json_file:
    json.dump(results, json_file, indent=4)

    print("Output written to pe_info.json")