Advertisement
alien_fx_fiend

PE-Explorer GUI (Command-Line Support Paths 4 Shortcuts) *FINAL RELEASE 2.8*

Nov 27th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 57.32 KB | Source Code | 0 0
  1. ==++ Here's the full source code for (file 1/1) "main.cpp":: ++==
  2. #define STRICT
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <winternl.h>
  6. #include <CommCtrl.h>
  7. #include <commdlg.h>
  8. #include <string>
  9. #include <strsafe.h>
  10. #include <sstream>
  11. #include <iomanip>
  12. #include <stdio.h>
  13. #include <vector>
  14. #include <shellapi.h>
  15. //#include "helpers.h"
  16. #include "resource.h"
  17. #pragma comment(lib, "comctl32.lib")
  18.  
  19. namespace PEHelpers {
  20.    std::wstring GetImageCharacteristics(DWORD characteristics) {
  21.        if (characteristics & IMAGE_FILE_DLL) return L"(DLL)";
  22.        if (characteristics & IMAGE_FILE_SYSTEM) return L"(DRIVER)";
  23.        if (characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) return L"(EXE)";
  24.        return L"(UNKNOWN)";
  25.    }
  26.  
  27.    std::wstring GetSubsystem(WORD subsystem) {
  28.        switch (subsystem) {
  29.        case IMAGE_SUBSYSTEM_NATIVE: return L"(NATIVE/DRIVER)";
  30.        case IMAGE_SUBSYSTEM_WINDOWS_GUI: return L"(GUI)";
  31.        case IMAGE_SUBSYSTEM_WINDOWS_CUI: return L"(CONSOLE)";
  32.        default: return L"(UNKNOWN)";
  33.        }
  34.    }
  35.  
  36.    std::wstring GetDataDirectoryName(int DirectoryNumber) {
  37.        switch (DirectoryNumber) {
  38.        case 0: return L"Export Table";
  39.        case 1: return L"Import Table";
  40.        case 2: return L"Resource Table";
  41.        case 3: return L"Exception Entry";
  42.        case 4: return L"Security Entry";
  43.        case 5: return L"Relocation Table";
  44.        case 6: return L"Debug Entry";
  45.        case 7: return L"Copyright Entry";
  46.        case 8: return L"Global PTR Entry";
  47.        case 9: return L"TLS Entry";
  48.        case 10: return L"Configuration Entry";
  49.        case 11: return L"Bound Import Entry";
  50.        case 12: return L"IAT";
  51.        case 13: return L"Delay Import Descriptor";
  52.        case 14: return L"COM Descriptor";
  53.        default: return L"Unknown";
  54.        }
  55.    }
  56.  
  57.    std::wstring GetSectionProtection(DWORD characteristics) {
  58.        std::wstring protection = L"(";
  59.        bool needsSeparator = false;
  60.  
  61.        if (characteristics & IMAGE_SCN_MEM_EXECUTE) {
  62.            protection += L"EXECUTE";
  63.            needsSeparator = true;
  64.        }
  65.  
  66.        if (characteristics & IMAGE_SCN_MEM_READ) {
  67.            if (needsSeparator) protection += L" | ";
  68.            protection += L"READ";
  69.            needsSeparator = true;
  70.        }
  71.  
  72.        if (characteristics & IMAGE_SCN_MEM_WRITE) {
  73.            if (needsSeparator) protection += L" | ";
  74.            protection += L"WRITE";
  75.        }
  76.  
  77.        protection += L")";
  78.        return protection;
  79.    }
  80.  
  81.    PIMAGE_SECTION_HEADER GetExportSection(const PIMAGE_SECTION_HEADER pImageSectionHeader,
  82.        const int NumberOfSections,
  83.        const DWORD_PTR dExportAddress) {
  84.        for (int i = 0; i < NumberOfSections; ++i) {
  85.            const auto pCurrentSectionHeader =
  86.                (PIMAGE_SECTION_HEADER)((DWORD_PTR)pImageSectionHeader + i * sizeof(IMAGE_SECTION_HEADER));
  87.  
  88.            if (dExportAddress >= pCurrentSectionHeader->VirtualAddress &&
  89.                dExportAddress < pCurrentSectionHeader->VirtualAddress + pCurrentSectionHeader->Misc.VirtualSize)
  90.                return pCurrentSectionHeader;
  91.        }
  92.        return nullptr;
  93.    }
  94. }
  95.  
  96. using namespace std;
  97.  
  98. // At the top of your file, change the window class name to wide string
  99. #define WINDOW_CLASS_NAME L"PEAnalyzerWindow"
  100. //const wchar_t* const WINDOW_CLASS_NAME = L"PEAnalyzerWindow";
  101.  
  102. // Use ANSI versions explicitly
  103. //#undef CreateWindow
  104. //#undef CreateWindowEx
  105. //#define CreateWindow  CreateWindowW
  106. //#define CreateWindowEx CreateWindowExW
  107.  
  108. // Helper function to replace printf with GUI output
  109. #define OUTPUT(format, ...) AppendToOutput(L##format, ##__VA_ARGS__)
  110. //#define OUTPUT(format, ...) AppendToOutput(format, ##__VA_ARGS__)
  111. //#define printf(format, ...) AppendToOutput(format, ##__VA_ARGS__)
  112.  
  113. // Window dimensions
  114. #define WINDOW_WIDTH    1024
  115. #define WINDOW_HEIGHT   768
  116. #define EDIT_MARGIN    10
  117.  
  118. // Global variables
  119. HWND g_hMainWindow = NULL;
  120. HWND g_hEditControl = NULL;
  121. HFONT g_hFont = NULL;
  122. std::wstringstream g_OutputText;
  123. WCHAR filePathW[MAX_PATH];
  124. std::vector<wchar_t> g_OutputBuffer;
  125. std::wstring tempBuffer; // Declare tempBuffer globally
  126. HWND g_hStatusBar = NULL;
  127.  
  128. // Function declarations
  129. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  130. LRESULT CALLBACK EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
  131. void CreateMainWindow(HINSTANCE hInstance);
  132. void InitializeControls(HWND hwnd);
  133. void AddMenus(HWND hwnd);
  134. void OpenFileDialog(HWND hwnd);
  135. //void AnalyzePEFile(const WCHAR* filePathW);
  136. void AnalyzePEFile(const wchar_t* filePathW);
  137. HANDLE GetFileContent(const wchar_t* lpFilePath);
  138. /*void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory);
  139. PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader,
  140.    int NumberOfSections, DWORD dImportAddress);
  141. void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
  142.    DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection);
  143. void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
  144.    DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection);*/
  145. void AppendToOutput(const wchar_t* format, ...);
  146. void UpdateEditControl();
  147.  
  148. // Main window class name
  149. //const char* const WINDOW_CLASS_NAME = "PEAnalyzerWindow";
  150.  
  151. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  152.    INITCOMMONCONTROLSEX icc = { sizeof(INITCOMMONCONTROLSEX), ICC_WIN95_CLASSES };
  153.    InitCommonControlsEx(&icc);
  154.  
  155.    // Get command line parameters in Unicode
  156.    int argc;
  157.    LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  158.  
  159.    CreateMainWindow(hInstance);
  160.    if (!g_hMainWindow) {
  161.        LocalFree(argv);
  162.        return -1;
  163.    }
  164.  
  165.    ShowWindow(g_hMainWindow, nCmdShow);
  166.    UpdateWindow(g_hMainWindow);
  167.  
  168.    // If there's a command line parameter, process it
  169.     if (argc > 1) {
  170.         // Process the first parameter as file path
  171.         SetWindowTextW(g_hEditControl, L"");
  172.         g_OutputText.str(L"");
  173.         g_OutputText.clear();
  174.         AnalyzePEFile(argv[1]);
  175.         UpdateEditControl();
  176.     }
  177.  
  178.     LocalFree(argv);
  179.  
  180.     MSG msg = {};
  181.     while (GetMessage(&msg, NULL, 0, 0)) {
  182.         TranslateMessage(&msg);
  183.         DispatchMessage(&msg);
  184.     }
  185.  
  186.     if (g_hFont) DeleteObject(g_hFont);
  187.     return (int)msg.wParam;
  188. }
  189.  
  190. void CreateMainWindow(HINSTANCE hInstance) {
  191.     WNDCLASSEXW wc = { sizeof(WNDCLASSEXW), 0, WindowProc, 0, 0, hInstance,
  192.         LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)),
  193.         LoadCursor(NULL, IDC_ARROW),
  194.         (HBRUSH)(COLOR_WINDOW + 1),
  195.         NULL, WINDOW_CLASS_NAME,
  196.         LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)) };
  197.     RegisterClassExW(&wc);
  198.  
  199.     // Get screen dimensions
  200.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  201.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  202.  
  203.     // Calculate center position
  204.     int windowX = (screenWidth - WINDOW_WIDTH) / 2;
  205.     int windowY = (screenHeight - WINDOW_HEIGHT) / 2;
  206.  
  207.     // Remove WS_MAXIMIZEBOX and WS_THICKFRAME from the window style
  208.     DWORD style = (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX) & ~WS_THICKFRAME;
  209.     //WS_OVERLAPPEDWINDOW ~~> style
  210.     g_hMainWindow = CreateWindowExW(0, WINDOW_CLASS_NAME, L"PE File Analyzer",
  211.         style, windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT,
  212.         nullptr, nullptr, hInstance, nullptr);
  213. }
  214.  
  215. void InitializeControls(HWND hwnd) {
  216.     // Create Status Bar
  217.     g_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  218.         WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
  219.         0, 0, 0, 0, hwnd, NULL,
  220.         (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  221.  
  222.     // Get status bar height
  223.     RECT rcStatus;
  224.     GetWindowRect(g_hStatusBar, &rcStatus);
  225.     int statusHeight = rcStatus.bottom - rcStatus.top;
  226.  
  227.     // Create Edit Control with adjusted height
  228.     RECT rcClient;
  229.     GetClientRect(hwnd, &rcClient);
  230.     g_hEditControl = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"",
  231.         WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
  232.         ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
  233.         EDIT_MARGIN, EDIT_MARGIN,
  234.         rcClient.right - (2 * EDIT_MARGIN),
  235.         rcClient.bottom - statusHeight - (2 * EDIT_MARGIN),
  236.         hwnd, nullptr,
  237.         (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), nullptr);
  238.  
  239.     g_hFont = CreateFont(-14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
  240.         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  241.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_MODERN, L"Consolas");
  242.  
  243.     if (g_hFont)
  244.         SendMessage(g_hEditControl, WM_SETFONT, (WPARAM)g_hFont, TRUE);
  245.  
  246.     // Add this line to subclass the Edit control
  247.     SetWindowSubclass(g_hEditControl, EditSubclassProc, 0, 0);
  248. }
  249.  
  250. void AddMenus(HWND hwnd) {
  251.     HMENU hMenuBar = CreateMenu();
  252.     HMENU hFileMenu = CreateMenu();
  253.     AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hFileMenu, L"&File");
  254.     AppendMenu(hFileMenu, MF_STRING, 1, L"&Open");
  255.     AppendMenu(hFileMenu, MF_STRING, 2, L"E&xit");
  256.     SetMenu(hwnd, hMenuBar);
  257. }
  258.  
  259. LRESULT CALLBACK EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
  260.     if (uMsg == WM_KEYDOWN) {
  261.         switch (wParam) {
  262.         case VK_F1:    // F1 key
  263.             SendMessage(GetParent(hwnd), WM_KEYDOWN, VK_F1, 0);
  264.             return 0;
  265.         case VK_ESCAPE: // Escape key
  266.             SendMessage(GetParent(hwnd), WM_KEYDOWN, VK_ESCAPE, 0);
  267.             return 0;
  268.         }
  269.     }
  270.     return DefSubclassProc(hwnd, uMsg, wParam, lParam);
  271. }
  272.  
  273. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  274.     switch (uMsg) {
  275.     case WM_CREATE: InitializeControls(hwnd); AddMenus(hwnd); return 0;
  276.     case WM_SIZE:
  277.     {
  278.         RECT rcClient;
  279.         GetClientRect(hwnd, &rcClient);
  280.  
  281.         // Resize status bar (it will auto-adjust its height)
  282.         SendMessage(g_hStatusBar, WM_SIZE, 0, 0);
  283.  
  284.         // Get status bar height
  285.         RECT rcStatus;
  286.         GetWindowRect(g_hStatusBar, &rcStatus);
  287.         int statusHeight = rcStatus.bottom - rcStatus.top;
  288.  
  289.         // Resize edit control
  290.         SetWindowPos(g_hEditControl, NULL,
  291.             EDIT_MARGIN,
  292.             EDIT_MARGIN,
  293.             rcClient.right - (2 * EDIT_MARGIN),
  294.             rcClient.bottom - statusHeight - (2 * EDIT_MARGIN),
  295.             SWP_NOZORDER);
  296.     }
  297.     return 0;
  298.     case WM_KEYDOWN:
  299.         switch (wParam) {
  300.         case VK_F1:
  301.             MessageBoxW(hwnd,
  302.                 L"PE Header Parser 2.8 GUI-based Programmed in C++ Win32 API (1351 lines of code) by Entisoft Software(c) Evans Thorpemorton",
  303.                 L"About",
  304.                 MB_OK | MB_ICONINFORMATION);
  305.             return 0;
  306.  
  307.         case VK_ESCAPE:
  308.             PostQuitMessage(0);
  309.             return 0;
  310.         }
  311.         break;
  312.     case WM_COMMAND: if (LOWORD(wParam) == 1) OpenFileDialog(hwnd); if (LOWORD(wParam) == 2) PostQuitMessage(0); return 0;
  313.     case WM_DESTROY:
  314.         if (g_hStatusBar) DestroyWindow(g_hStatusBar);
  315.         PostQuitMessage(0);
  316.         return 0;
  317.     }
  318.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  319. }
  320.  
  321. void SetStatusText(const wchar_t* text) {
  322.     SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)text);
  323. }
  324.  
  325. void ShowProgress(int percentage) {
  326.     wchar_t status[256];
  327.     swprintf_s(status, L"Analyzing... %d%%", percentage);
  328.     SetStatusText(status);
  329. }
  330.  
  331. void OpenFileDialog(HWND hwnd) {
  332.     WCHAR fileName[MAX_PATH] = L"";
  333.     OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW), hwnd, NULL, L"Executable Files (*.exe;*.dll)\0*.exe;*.dll\0All Files (*.*)\0*.*\0", NULL, 0, 1, fileName, MAX_PATH, NULL, 0, NULL, NULL, OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, 0, 0, L"exe", NULL, NULL, NULL };
  334.     if (GetOpenFileNameW(&ofn)) {
  335.         SetWindowTextW(g_hEditControl, L"");
  336.         g_OutputText.str(L"");
  337.         g_OutputText.clear();
  338.         AnalyzePEFile(ofn.lpstrFile);
  339.         UpdateEditControl();
  340.     }
  341. }
  342.  
  343. class FileMapper {
  344. private:
  345.     HANDLE hFile = INVALID_HANDLE_VALUE;
  346.     HANDLE hMapping = nullptr;
  347.     LPVOID lpView = nullptr;
  348.  
  349. public:
  350.     ~FileMapper() {
  351.         if (lpView) UnmapViewOfFile(lpView);
  352.         if (hMapping) CloseHandle(hMapping);
  353.         if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
  354.     }
  355.  
  356.     bool Initialize(const wchar_t* path) {
  357.         hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, nullptr,
  358.             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  359.         if (hFile == INVALID_HANDLE_VALUE) return false;
  360.  
  361.         hMapping = CreateFileMappingW(hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
  362.         if (!hMapping) return false;
  363.  
  364.         lpView = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
  365.         return lpView != nullptr;
  366.     }
  367.  
  368.     LPVOID GetView() const { return lpView; }
  369. };
  370.  
  371. // Modified AppendToOutput to handle newlines properly:
  372. void AppendToOutput(const wchar_t* format, ...) {
  373.     std::vector<wchar_t> buffer(1024);
  374.     va_list args;
  375.     va_start(args, format);
  376.  
  377.     while (true) {
  378.         int result = _vsnwprintf(buffer.data(), buffer.size(), format, args);
  379.         if (result >= 0) break;
  380.         buffer.resize(buffer.size() * 2);
  381.     }
  382.     va_end(args);
  383.  
  384.     // Convert \n to \r\n
  385.     std::wstring output = buffer.data();
  386.     size_t pos = 0;
  387.     while ((pos = output.find(L'\n', pos)) != std::wstring::npos) {
  388.         if (pos == 0 || output[pos - 1] != L'\r') {
  389.             output.insert(pos, L"\r");
  390.             pos += 2;
  391.         }
  392.         else {
  393.             pos++;
  394.         }
  395.     }
  396.  
  397.     g_OutputText << output;
  398.     UpdateEditControl();
  399. }
  400.  
  401. /*
  402. void AppendToOutput(const wchar_t* format, ...) {
  403.     wchar_t buffer[16384]; // Ensure sufficient buffer size
  404.     va_list args;
  405.     va_start(args, format);
  406.     StringCchVPrintfW(buffer, ARRAYSIZE(buffer), format, args);
  407.     va_end(args);
  408.  
  409.     tempBuffer += buffer;
  410.  
  411.     // Update Edit Control periodically to improve performance
  412.     if (tempBuffer.size() > 8000) {
  413.         g_OutputText << tempBuffer;
  414.         tempBuffer.clear();
  415.  
  416.         SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
  417.         SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
  418.         SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
  419.     }
  420. }
  421.  
  422.  
  423. // Final update to flush any remaining content
  424. void FlushOutput() {
  425.     if (!tempBuffer.empty()) {
  426.         g_OutputText << tempBuffer;
  427.         tempBuffer.clear();
  428.  
  429.         SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
  430.         SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
  431.         SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
  432.     }
  433. }
  434. */
  435.  
  436.  
  437. /*
  438. //use below ^^ ALWAYS
  439. void AppendToOutput(const wchar_t* format, ...) {
  440.     va_list args;
  441.     va_start(args, format);
  442.  
  443.     // Use a vector as a dynamically resizable buffer
  444.     std::vector<wchar_t> buffer(16384); // Start with an initial size
  445.     int result = -1;
  446.  
  447.     while (true) {
  448.         // Attempt to format the string
  449.         result = _vsnwprintf(buffer.data(), buffer.size(), format, args);
  450.  
  451.         if (result >= 0 && static_cast<size_t>(result) < buffer.size()) {
  452.             // Successfully formatted within the current buffer size
  453.             break;
  454.         }
  455.  
  456.         // Resize the buffer and try again
  457.         buffer.resize(buffer.size() * 2);
  458.     }
  459.     va_end(args);
  460.  
  461.     // Convert `\n` to `\r\n` for proper display in the EditBox
  462.     std::wstring formattedOutput(buffer.data());
  463.     size_t pos = 0;
  464.     while ((pos = formattedOutput.find(L'\n', pos)) != std::wstring::npos) {
  465.         formattedOutput.replace(pos, 1, L"\r\n");
  466.         pos += 2; // Move past the replacement
  467.     }
  468.  
  469.     // Append to the global output buffer
  470.     g_OutputText << formattedOutput;
  471.  
  472.     // Update the EditBox periodically to prevent overloading
  473.     if (g_OutputText.str().size() > 8000) {
  474.         SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
  475.         SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
  476.         SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
  477.     }
  478. }
  479. //use above ^^ ALWAYS
  480. */
  481.  
  482. //currentlatest
  483. /*void AppendToOutput(const wchar_t* format, ...) {
  484.     wchar_t buffer[4096];
  485.     va_list args;
  486.     va_start(args, format);
  487.     StringCchVPrintfW(buffer, ARRAYSIZE(buffer), format, args);
  488.     va_end(args);
  489.     g_OutputText << buffer;
  490.     SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
  491.     SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
  492.     SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
  493. }
  494. */
  495.  
  496. //use-below-basic-failsafe-working vv
  497. //basic test function
  498. /*void AnalyzePEFile(const wchar_t* filePathW) {
  499.     OUTPUT("[+] Analyzing file: %s\n", filePathW);
  500.     LPVOID lpFileContent = GetFileContent(filePathW);
  501.     if (!lpFileContent) { OUTPUT("[-] Could not read file.\n"); return; }
  502.     PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)lpFileContent;
  503.     if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) { OUTPUT("[-] Invalid DOS signature.\n"); HeapFree(GetProcessHeap(), 0, lpFileContent); return; }
  504.     PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((DWORD_PTR)lpFileContent + dosHeader->e_lfanew);
  505.     if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) { OUTPUT("[-] Invalid NT signature.\n"); HeapFree(GetProcessHeap(), 0, lpFileContent); return; }
  506.     OUTPUT("[+] PE file analyzed successfully.\n");
  507.     HeapFree(GetProcessHeap(), 0, lpFileContent);
  508.     UpdateEditControl();
  509. }*/
  510. //use-above-basic-failsafe-working vv
  511.  
  512. //use vectors for unlimited size growth of buffer, use an alternative to editbox, check for fast loops overloading, in its primitive form it works properly! try w/o getimports3264 datadirectories getsections
  513.  
  514. void AnalyzePEFile(const wchar_t* filePathW) {
  515.     OUTPUT("[+] Starting PE Analysis for: %s\n\n", filePathW);
  516.  
  517.     FileMapper mapper;
  518.     if (!mapper.Initialize(filePathW)) {
  519.         SetStatusText(L"Failed to open file!");
  520.         OUTPUT("[-] Failed to open file! Error: %d\r\n", GetLastError());
  521.         return;
  522.     }
  523.  
  524.     ShowProgress(20);  // After initial file opening
  525.  
  526.    
  527.     // Open and read file
  528.     HANDLE hFile = CreateFileW(filePathW, GENERIC_READ, FILE_SHARE_READ, nullptr,
  529.         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  530.     if (hFile == INVALID_HANDLE_VALUE) {
  531.         OUTPUT("[-] Failed to open file! Error: %d\n", GetLastError());
  532.         return;
  533.     }
  534.  
  535.     DWORD fileSize = GetFileSize(hFile, nullptr);
  536.     if (fileSize == INVALID_FILE_SIZE) {
  537.         CloseHandle(hFile);
  538.         OUTPUT("[-] Failed to get file size! Error: %d\n", GetLastError());
  539.         return;
  540.     }
  541.  
  542.     HANDLE hFileMapping = CreateFileMappingW(hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
  543.     if (!hFileMapping) {
  544.         CloseHandle(hFile);
  545.         OUTPUT("[-] Failed to create file mapping! Error: %d\n", GetLastError());
  546.         return;
  547.     }
  548.  
  549.     LPVOID lpFileContent = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  550.     if (!lpFileContent) {
  551.         CloseHandle(hFileMapping);
  552.         CloseHandle(hFile);
  553.         OUTPUT("[-] Failed to map view of file! Error: %d\n", GetLastError());
  554.         return;
  555.     }
  556.  
  557.     const auto pImageDosHeader = static_cast<PIMAGE_DOS_HEADER>(mapper.GetView());
  558.     if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
  559.         OUTPUT("[-] Invalid DOS signature!\r\n");
  560.         return;
  561.     }
  562.  
  563.     ShowProgress(40);
  564.  
  565.     const auto pImageNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>(
  566.         static_cast<BYTE*>(mapper.GetView()) + pImageDosHeader->e_lfanew);
  567.     if (pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE) {
  568.         OUTPUT("[-] Invalid NT signature!\r\n");
  569.         return;
  570.     }
  571.  
  572.     ShowProgress(60);
  573.  
  574.     // Print File Header Information
  575.     OUTPUT("[+] PE FILE HEADER\n");
  576.     OUTPUT("\tMachine: 0x%X\n", pImageNtHeaders->FileHeader.Machine);
  577.     OUTPUT("\tNumberOfSections: 0x%X\n", pImageNtHeaders->FileHeader.NumberOfSections);
  578.     OUTPUT("\tTimeDateStamp: 0x%X\n", pImageNtHeaders->FileHeader.TimeDateStamp);
  579.     OUTPUT("\tPointerToSymbolTable: 0x%X\r\n",
  580.         static_cast<DWORD>(pImageNtHeaders->FileHeader.PointerToSymbolTable));
  581.     OUTPUT("\tNumberOfSymbols: 0x%X\r\n",
  582.         static_cast<DWORD>(pImageNtHeaders->FileHeader.NumberOfSymbols));
  583.     OUTPUT("\tSizeOfOptionalHeader: 0x%X\r\n",
  584.         static_cast<DWORD>(pImageNtHeaders->FileHeader.SizeOfOptionalHeader));
  585.     OUTPUT("\tCharacteristics: 0x%X %s\n\n",
  586.         pImageNtHeaders->FileHeader.Characteristics,
  587.         PEHelpers::GetImageCharacteristics(pImageNtHeaders->FileHeader.Characteristics).c_str());
  588.  
  589.     // Print Optional Header Information
  590.     OUTPUT("[+] PE OPTIONAL HEADER\n");
  591.     OUTPUT("\tMagic: 0x%X\n", pImageNtHeaders->OptionalHeader.Magic);
  592.     OUTPUT("\tAddressOfEntryPoint: 0x%X\n", pImageNtHeaders->OptionalHeader.AddressOfEntryPoint);
  593.     OUTPUT("\tImageBase: 0x%llX\n", (ULONGLONG)pImageNtHeaders->OptionalHeader.ImageBase);
  594.     OUTPUT("\tSectionAlignment: 0x%X\n", pImageNtHeaders->OptionalHeader.SectionAlignment);
  595.     OUTPUT("\tFileAlignment: 0x%X\n", pImageNtHeaders->OptionalHeader.FileAlignment);
  596.    
  597.     // Added missing Optional Header fields
  598.     OUTPUT("\tMajorOperatingSystemVersion: 0x%X\r\n",
  599.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MajorOperatingSystemVersion));
  600.     OUTPUT("\tMinorOperatingSystemVersion: 0x%X\r\n",
  601.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MinorOperatingSystemVersion));
  602.     OUTPUT("\tMajorImageVersion: 0x%X\r\n",
  603.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MajorImageVersion));
  604.     OUTPUT("\tMinorImageVersion: 0x%X\r\n",
  605.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MinorImageVersion));
  606.     OUTPUT("\tMajorSubsystemVersion: 0x%X\r\n",
  607.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MajorSubsystemVersion));
  608.     OUTPUT("\tMinorSubsystemVersion: 0x%X\r\n",
  609.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.MinorSubsystemVersion));
  610.     OUTPUT("\tWin32VersionValue: 0x%X\r\n",
  611.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.Win32VersionValue));
  612.     OUTPUT("\tSizeOfImage: 0x%X\r\n",
  613.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.SizeOfImage));
  614.     OUTPUT("\tSizeOfHeaders: 0x%X\r\n",
  615.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.SizeOfHeaders));
  616.     OUTPUT("\tCheckSum: 0x%X\r\n",
  617.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.CheckSum));
  618.     OUTPUT("\tSubsystem: 0x%X %s\r\n",
  619.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.Subsystem),
  620.         PEHelpers::GetSubsystem(pImageNtHeaders->OptionalHeader.Subsystem).c_str());
  621.     OUTPUT("\tDllCharacteristics: 0x%X\r\n",
  622.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.DllCharacteristics));
  623.     OUTPUT("\tSizeOfStackReserve: 0x%llX\r\n",
  624.         static_cast<ULONGLONG>(pImageNtHeaders->OptionalHeader.SizeOfStackReserve));
  625.     OUTPUT("\tSizeOfStackCommit: 0x%llX\r\n",
  626.         static_cast<ULONGLONG>(pImageNtHeaders->OptionalHeader.SizeOfStackCommit));
  627.     OUTPUT("\tSizeOfHeapReserve: 0x%llX\r\n",
  628.         static_cast<ULONGLONG>(pImageNtHeaders->OptionalHeader.SizeOfHeapReserve));
  629.     OUTPUT("\tSizeOfHeapCommit: 0x%llX\r\n",
  630.         static_cast<ULONGLONG>(pImageNtHeaders->OptionalHeader.SizeOfHeapCommit));
  631.     OUTPUT("\tLoaderFlags: 0x%X\r\n",
  632.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.LoaderFlags));
  633.     OUTPUT("\tNumberOfRvaAndSizes: 0x%X\r\n\r\n",
  634.         static_cast<DWORD>(pImageNtHeaders->OptionalHeader.NumberOfRvaAndSizes));
  635.    
  636.     OUTPUT("\tSubsystem: 0x%X %s\n\n",
  637.         pImageNtHeaders->OptionalHeader.Subsystem,
  638.         PEHelpers::GetSubsystem(pImageNtHeaders->OptionalHeader.Subsystem).c_str());
  639.  
  640.     // Print Data Directories
  641.     OUTPUT("[+] PE DATA DIRECTORIES\n");
  642.     for (int i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; i++) {
  643.         const auto& dir = pImageNtHeaders->OptionalHeader.DataDirectory[i];
  644.         if (dir.VirtualAddress != 0) {
  645.             OUTPUT("\t%s:\n", PEHelpers::GetDataDirectoryName(i).c_str());
  646.             OUTPUT("\t\tVirtualAddress: 0x%X\n", dir.VirtualAddress);
  647.             OUTPUT("\t\tSize: 0x%X\n", dir.Size);
  648.         }
  649.     }
  650.  
  651.     // Analyze Sections
  652.     OUTPUT("\n[+] PE IMAGE SECTIONS\r\n");
  653.     auto pSection = IMAGE_FIRST_SECTION(pImageNtHeaders);
  654.     for (WORD i = 0; i < pImageNtHeaders->FileHeader.NumberOfSections; i++, pSection++) {
  655.         // Create a null-terminated string from the section name
  656.         char sectionName[IMAGE_SIZEOF_SHORT_NAME + 1] = {};
  657.         memcpy(sectionName, pSection->Name, IMAGE_SIZEOF_SHORT_NAME);
  658.  
  659.         // Remove any non-printable characters
  660.         for (int j = 0; j < IMAGE_SIZEOF_SHORT_NAME; j++) {
  661.             if (!isprint(static_cast<unsigned char>(sectionName[j]))) {
  662.                 sectionName[j] = '\0';
  663.             }
  664.         }
  665.  
  666.         // Convert to wide string
  667.         wchar_t wideSectionName[IMAGE_SIZEOF_SHORT_NAME + 1] = {};
  668.         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
  669.             sectionName, -1,
  670.             wideSectionName, IMAGE_SIZEOF_SHORT_NAME + 1);
  671.  
  672.         OUTPUT("\tSECTION : %s\r\n", wideSectionName);
  673.         OUTPUT("\t\tMisc (PhysicalAddress) : 0x%X\r\n", pSection->Misc.PhysicalAddress);
  674.         OUTPUT("\t\tMisc (VirtualSize) : 0x%X\r\n", pSection->Misc.VirtualSize);
  675.         OUTPUT("\t\tVirtualAddress : 0x%X\r\n", pSection->VirtualAddress);
  676.         OUTPUT("\t\tSizeOfRawData : 0x%X\r\n", pSection->SizeOfRawData);
  677.         OUTPUT("\t\tPointerToRawData : 0x%X\r\n", pSection->PointerToRawData);
  678.         OUTPUT("\t\tPointerToRelocations : 0x%X\r\n", pSection->PointerToRelocations);
  679.         OUTPUT("\t\tPointerToLinenumbers : 0x%X\r\n", pSection->PointerToLinenumbers);
  680.         OUTPUT("\t\tNumberOfRelocations : 0x%X\r\n", pSection->NumberOfRelocations);
  681.         OUTPUT("\t\tNumberOfLinenumbers : 0x%X\r\n", pSection->NumberOfLinenumbers);
  682.         OUTPUT("\t\tCharacteristics : 0x%X %s\r\n\r\n",
  683.             pSection->Characteristics,
  684.             PEHelpers::GetSectionProtection(pSection->Characteristics).c_str());
  685.     }
  686.  
  687.     ShowProgress(80);
  688.  
  689.     // Analyze Imports
  690.     const auto& importDir = pImageNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
  691.     if (importDir.VirtualAddress && importDir.Size) {
  692.         OUTPUT("\n[+] IMPORTED DLLS AND FUNCTIONS\r\n");
  693.  
  694.         // Find the section containing imports
  695.         auto pSection = IMAGE_FIRST_SECTION(pImageNtHeaders);
  696.         PIMAGE_SECTION_HEADER pImportSection = nullptr;
  697.  
  698.         // Find the import section
  699.         for (WORD i = 0; i < pImageNtHeaders->FileHeader.NumberOfSections; i++, pSection++) {
  700.             if (importDir.VirtualAddress >= pSection->VirtualAddress &&
  701.                 importDir.VirtualAddress < (pSection->VirtualAddress + pSection->Misc.VirtualSize)) {
  702.                 pImportSection = pSection;
  703.                 break;
  704.             }
  705.         }
  706.  
  707.         if (!pImportSection) {
  708.             OUTPUT("[-] Could not find import section\r\n");
  709.             return;
  710.         }
  711.  
  712.         // Get the import descriptor
  713.         auto pImportDesc = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
  714.             (BYTE*)lpFileContent +
  715.             pImportSection->PointerToRawData +
  716.             (importDir.VirtualAddress - pImportSection->VirtualAddress));
  717.  
  718.         // Process all DLLs
  719.         while (pImportDesc && pImportDesc->Name != 0) {
  720.             // Get DLL name
  721.             const char* dllName = reinterpret_cast<const char*>(
  722.                 (BYTE*)lpFileContent +
  723.                 pImportSection->PointerToRawData +
  724.                 (pImportDesc->Name - pImportSection->VirtualAddress));
  725.  
  726.             if (!IsBadReadPtr(dllName, 1)) {
  727.                 std::vector<wchar_t> wideDllName(MAX_PATH);
  728.                 MultiByteToWideChar(CP_ACP, 0, dllName, -1,
  729.                     wideDllName.data(), MAX_PATH);
  730.  
  731.                 // Print DLL information
  732.                 OUTPUT("\n\tDLL NAME : %s\r\n", wideDllName.data());
  733.                 OUTPUT("\tCharacteristics : 0x%X\r\n", pImportDesc->Characteristics);
  734.                 OUTPUT("\tOriginalFirstThunk : 0x%X\r\n", pImportDesc->OriginalFirstThunk);
  735.                 OUTPUT("\tTimeDateStamp : 0x%X\r\n", pImportDesc->TimeDateStamp);
  736.                 OUTPUT("\tForwarderChain : 0x%X\r\n", pImportDesc->ForwarderChain);
  737.                 OUTPUT("\tFirstThunk : 0x%X\r\n", pImportDesc->FirstThunk);
  738.  
  739.                 OUTPUT("\n\tImported Functions :\r\n\r\n");
  740.  
  741.                 // Process functions using OriginalFirstThunk
  742.                 if (pImportDesc->OriginalFirstThunk) {
  743.                     auto pThunk = reinterpret_cast<PIMAGE_THUNK_DATA>(
  744.                         (BYTE*)lpFileContent +
  745.                         pImportSection->PointerToRawData +
  746.                         (pImportDesc->OriginalFirstThunk - pImportSection->VirtualAddress));
  747.  
  748.                     // Process all functions for this DLL
  749.                     while (pThunk && pThunk->u1.AddressOfData) {
  750.                         if (!(pThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG)) {
  751.                             auto pImportByName = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(
  752.                                 (BYTE*)lpFileContent +
  753.                                 pImportSection->PointerToRawData +
  754.                                 (pThunk->u1.AddressOfData - pImportSection->VirtualAddress));
  755.  
  756.                             if (!IsBadReadPtr(pImportByName, sizeof(IMAGE_IMPORT_BY_NAME))) {
  757.                                 std::vector<wchar_t> wideFuncName(MAX_PATH);
  758.                                 MultiByteToWideChar(CP_ACP, 0,
  759.                                     reinterpret_cast<const char*>(pImportByName->Name),
  760.                                     -1, wideFuncName.data(), MAX_PATH);
  761.  
  762.                                 OUTPUT("\t\t%s\r\n", wideFuncName.data());
  763.                             }
  764.                         }
  765.                         pThunk++;
  766.                     }
  767.                 }
  768.             }
  769.             pImportDesc++;
  770.         }
  771.     }
  772.  
  773.     ShowProgress(90);
  774.  
  775.     // In your AnalyzePEFile function, add this code after the imports section (before the cleanup label):
  776.  
  777.     // Handle Exports
  778.     const auto& exportDir = pImageNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
  779.     if (exportDir.VirtualAddress && exportDir.Size) {
  780.         OUTPUT("\n[+] EXPORTED FUNCTIONS\n");
  781.  
  782.         auto pSection = IMAGE_FIRST_SECTION(pImageNtHeaders);
  783.         PIMAGE_SECTION_HEADER pExportSection = PEHelpers::GetExportSection(
  784.             pSection,
  785.             pImageNtHeaders->FileHeader.NumberOfSections,
  786.             exportDir.VirtualAddress
  787.         );
  788.  
  789.         if (!pExportSection) {
  790.             OUTPUT("[-] Could not find export section\n");
  791.         }
  792.         else {
  793.             auto pExportDir = reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(
  794.                 (BYTE*)lpFileContent +
  795.                 pExportSection->PointerToRawData +
  796.                 (exportDir.VirtualAddress - pExportSection->VirtualAddress));
  797.  
  798.             const DWORD_PTR dRawOffset = reinterpret_cast<DWORD_PTR>(lpFileContent) +
  799.                 pExportSection->PointerToRawData;
  800.  
  801.             OUTPUT("\tNumber of Functions: %d\n", pExportDir->NumberOfFunctions);
  802.             OUTPUT("\tNumber of Names: %d\n", pExportDir->NumberOfNames);
  803.             OUTPUT("\tBase: %d\n", pExportDir->Base);
  804.  
  805.             // Fix for DLL name display
  806.             const char* dllName = reinterpret_cast<const char*>(
  807.                 (BYTE*)lpFileContent +
  808.                 pExportSection->PointerToRawData +
  809.                 (pExportDir->Name - pExportSection->VirtualAddress));
  810.  
  811.             std::vector<wchar_t> wideDllName(MAX_PATH);
  812.             MultiByteToWideChar(CP_ACP, 0, dllName, -1,
  813.                 wideDllName.data(), MAX_PATH);
  814.             OUTPUT("\tName: %s\n\n", wideDllName.data());
  815.  
  816.             const auto pArrayOfFunctionsNames = reinterpret_cast<DWORD*>(
  817.                 dRawOffset + (pExportDir->AddressOfNames - pExportSection->VirtualAddress));
  818.  
  819.             OUTPUT("\tExported Functions:\n\n");
  820.             for (DWORD i = 0; i < pExportDir->NumberOfNames; ++i) {
  821.                 const char* functionName = reinterpret_cast<const char*>(
  822.                     dRawOffset + (pArrayOfFunctionsNames[i] - pExportSection->VirtualAddress));
  823.  
  824.                 std::vector<wchar_t> wideFuncName(MAX_PATH);
  825.                 MultiByteToWideChar(CP_ACP, 0, functionName, -1,
  826.                     wideFuncName.data(), MAX_PATH);
  827.  
  828.                 OUTPUT("\t\t%s\n", wideFuncName.data());
  829.             }
  830.         }
  831.     }
  832.  
  833.     ShowProgress(100);
  834.  
  835.     SetStatusText(L"Analysis complete");
  836.  
  837. cleanup:
  838.     if (lpFileContent) {
  839.         UnmapViewOfFile(lpFileContent);
  840.     }
  841.     if (hFileMapping) {
  842.         CloseHandle(hFileMapping);
  843.     }
  844.     if (hFile) {
  845.         CloseHandle(hFile);
  846.     }
  847.  
  848.     UpdateEditControl();
  849. }
  850.  
  851. /*
  852. //use below vv ALWAYS
  853. void AnalyzePEFile(const wchar_t* filePathW) {
  854.     OUTPUT("[+] Starting PE Analysis for: %s\n\n", filePathW);
  855.  
  856.     LPVOID lpFileContent = GetFileContent(filePathW);
  857.     if (!lpFileContent) {
  858.         OUTPUT("[-] Failed to read file content!\n");
  859.         return;
  860.     }
  861.  
  862.     const auto pImageDosHeader = static_cast<PIMAGE_DOS_HEADER>(lpFileContent);
  863.     if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
  864.         OUTPUT("[-] Invalid DOS signature!\n");
  865.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  866.         return;
  867.     }
  868.  
  869.     const auto pImageNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>((DWORD_PTR)lpFileContent + pImageDosHeader->e_lfanew);
  870.     if (pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE) {
  871.         OUTPUT("[-] Invalid NT signature!\n");
  872.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  873.         return;
  874.     }
  875.  
  876.     //UpdateEditControl(); //added just now remove line!
  877.  
  878.     OUTPUT("[+] PE FILE HEADER\n");
  879.     OUTPUT("\tMachine : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.Machine);
  880.     OUTPUT("\tNumberOfSections : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSections);
  881.     OUTPUT("\tTimeDateStamp : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.TimeDateStamp);
  882.     OUTPUT("\tPointerToSymbolTable : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.PointerToSymbolTable);
  883.     OUTPUT("\tNumberOfSymbols : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSymbols);
  884.     OUTPUT("\tSizeOfOptionalHeader : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.SizeOfOptionalHeader);
  885.     OUTPUT("\tCharacteristics : 0x%X %s\n\n", (uintptr_t)pImageNtHeaders->FileHeader.Characteristics, GetImageCharacteristics(pImageNtHeaders->FileHeader.Characteristics));
  886.  
  887.     OUTPUT("[+] PE OPTIONAL HEADER\n");
  888.     OUTPUT("\tMagic : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Magic);
  889.     OUTPUT("\tAddressOfEntryPoint : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.AddressOfEntryPoint);
  890.     OUTPUT("\tImageBase : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.ImageBase);
  891.     OUTPUT("\tSectionAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SectionAlignment);
  892.     OUTPUT("\tFileAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.FileAlignment);
  893.     OUTPUT("\tMajorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorOperatingSystemVersion);
  894.     OUTPUT("\tMinorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorOperatingSystemVersion);
  895.     OUTPUT("\tMajorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorImageVersion);
  896.     OUTPUT("\tMinorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorImageVersion);
  897.     OUTPUT("\tMajorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorSubsystemVersion);
  898.     OUTPUT("\tMinorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorSubsystemVersion);
  899.     OUTPUT("\tWin32VersionValue : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Win32VersionValue);
  900.     OUTPUT("\tSizeOfImage : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfImage);
  901.     OUTPUT("\tSizeOfHeaders : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeaders);
  902.     OUTPUT("\tCheckSum : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.CheckSum);
  903.     OUTPUT("\tSubsystem : 0x%X %s\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Subsystem, GetSubsystem(pImageNtHeaders->OptionalHeader.Subsystem));
  904.     OUTPUT("\tDllCharacteristics : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.DllCharacteristics);
  905.     OUTPUT("\tSizeOfStackReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackReserve);
  906.     OUTPUT("\tSizeOfStackCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackCommit);
  907.     OUTPUT("\tSizeOfHeapReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapReserve);
  908.     OUTPUT("\tSizeOfHeapCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapCommit);
  909.     OUTPUT("\tLoaderFlags : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.LoaderFlags);
  910.     OUTPUT("\tNumberOfRvaAndSizes : 0x%X\n\n", (uintptr_t)pImageNtHeaders->OptionalHeader.NumberOfRvaAndSizes);
  911.  
  912.     //UpdateEditControl(); //added just now remove line!
  913.  
  914.     GetDataDirectories(&pImageNtHeaders->OptionalHeader.DataDirectory[0]);
  915.  
  916.     const auto pImageSectionHeader = reinterpret_cast<PIMAGE_SECTION_HEADER>((DWORD_PTR)pImageNtHeaders + sizeof(IMAGE_NT_HEADERS));
  917.     const auto pImageImportSection = GetSections(pImageSectionHeader, pImageNtHeaders->FileHeader.NumberOfSections, pImageNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress);
  918.  
  919.     if (!pImageImportSection) {
  920.         OUTPUT("[-] Error: Could not find import section!\n");
  921.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  922.         return;
  923.     }
  924.  
  925.     const auto pImageImportDescriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>((DWORD_PTR)lpFileContent + pImageImportSection->PointerToRawData);
  926.     if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_I386) {
  927.         GetImports32(pImageImportDescriptor, (DWORD)lpFileContent + pImageImportSection->PointerToRawData, pImageImportSection);
  928.     }
  929.     else if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64) {
  930.         GetImports64(pImageImportDescriptor, (DWORD)lpFileContent + pImageImportSection->PointerToRawData, pImageImportSection);
  931.     }
  932.     else {
  933.         OUTPUT("[-] Unsupported architecture!\n");
  934.     }
  935.  
  936.     HeapFree(GetProcessHeap(), 0, lpFileContent);
  937.     UpdateEditControl();
  938. }
  939.  
  940. void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory) {
  941.     OUTPUT("[+] PE DATA DIRECTORIES\n");
  942.     for (int i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; ++i, ++pImageDataDirectory) {
  943.         if (pImageDataDirectory->VirtualAddress == 0) continue;
  944.         OUTPUT("\tDataDirectory (%s) VirtualAddress : 0x%X\n", GetDataDirectoryName(i), (uintptr_t)pImageDataDirectory->VirtualAddress);
  945.         OUTPUT("\tDataDirectory (%s) Size : 0x%X\n\n", GetDataDirectoryName(i), (uintptr_t)pImageDataDirectory->Size);
  946.     }
  947. }
  948.  
  949. PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader, int NumberOfSections, DWORD dImportAddress) {
  950.     PIMAGE_SECTION_HEADER pImageImportHeader = nullptr;
  951.     OUTPUT("\n[+] PE IMAGE SECTIONS\n");
  952.     for (int i = 0; i < NumberOfSections; ++i) {
  953.         const auto pCurrentSectionHeader = reinterpret_cast<PIMAGE_SECTION_HEADER>((DWORD_PTR)pImageSectionHeader + i * sizeof(IMAGE_SECTION_HEADER));
  954.         OUTPUT("\n\tSECTION : %s\n", (wchar_t*)pCurrentSectionHeader->Name);
  955.         OUTPUT("\t\tMisc (PhysicalAddress) : 0x%X\n", (uintptr_t)pCurrentSectionHeader->Misc.PhysicalAddress);
  956.         OUTPUT("\t\tMisc (VirtualSize) : 0x%X\n", (uintptr_t)pCurrentSectionHeader->Misc.VirtualSize);
  957.         OUTPUT("\t\tVirtualAddress : 0x%X\n", (uintptr_t)pCurrentSectionHeader->VirtualAddress);
  958.         OUTPUT("\t\tSizeOfRawData : 0x%X\n", (uintptr_t)pCurrentSectionHeader->SizeOfRawData);
  959.         OUTPUT("\t\tPointerToRawData : 0x%X\n", (uintptr_t)pCurrentSectionHeader->PointerToRawData);
  960.         OUTPUT("\t\tCharacteristics : 0x%X %s\n", (uintptr_t)pCurrentSectionHeader->Characteristics, GetSectionProtection(pCurrentSectionHeader->Characteristics));
  961.  
  962.         if (dImportAddress >= pCurrentSectionHeader->VirtualAddress && dImportAddress < pCurrentSectionHeader->VirtualAddress + pCurrentSectionHeader->Misc.VirtualSize) {
  963.             pImageImportHeader = pCurrentSectionHeader;
  964.         }
  965.     }
  966.     return pImageImportHeader;
  967. }
  968.  
  969. void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor, DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection) {
  970.     OUTPUT("\n[+] IMPORTED DLL\n");
  971.     while (pImageImportDescriptor->Name != 0) {
  972.         OUTPUT("\n\tDLL NAME : %s\n", (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
  973.         if (pImageImportDescriptor->OriginalFirstThunk == 0) {
  974.             ++pImageImportDescriptor;
  975.             continue;
  976.         }
  977.         auto pOriginalFirstThunk = reinterpret_cast<PIMAGE_THUNK_DATA32>(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
  978.         while (pOriginalFirstThunk->u1.AddressOfData != 0) {
  979.             const auto pImageImportByName = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(dRawOffset + (pOriginalFirstThunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
  980.             if (pImageImportByName) {
  981.                 OUTPUT("\t\tFunction: %s\n", (char*)pImageImportByName->Name);
  982.             }
  983.             pOriginalFirstThunk++;
  984.         }
  985.         pImageImportDescriptor++;
  986.     }
  987. }
  988.  
  989. void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor, DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection) {
  990.     OUTPUT("\n[+] IMPORTED DLL\n");
  991.     while (pImageImportDescriptor->Name != 0) {
  992.         OUTPUT("\n\tDLL NAME : %s\n", (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
  993.         if (pImageImportDescriptor->OriginalFirstThunk == 0) {
  994.             ++pImageImportDescriptor;
  995.             continue;
  996.         }
  997.         auto pOriginalFirstThunk = reinterpret_cast<PIMAGE_THUNK_DATA64>(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
  998.         while (pOriginalFirstThunk->u1.AddressOfData != 0) {
  999.             const auto pImageImportByName = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(dRawOffset + (pOriginalFirstThunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
  1000.             if (pImageImportByName) {
  1001.                 OUTPUT("\t\tFunction: %s\n", (char*)pImageImportByName->Name);
  1002.             }
  1003.             pOriginalFirstThunk++;
  1004.         }
  1005.         pImageImportDescriptor++;
  1006.     }
  1007. }
  1008. //use above ^^ ALWAYS
  1009. */
  1010.  
  1011.  
  1012. /*
  1013. // older-orig-deprecated vv
  1014. // Main PE Analysis function
  1015. void AnalyzePEFile(const wchar_t* filePathW)
  1016. {
  1017.     //WCHAR filePathW[MAX_PATH];
  1018.     //MultiByteToWideChar(CP_ACP, 0, filePathA, -1, filePathW, MAX_PATH);
  1019.     OUTPUT("[+] Starting PE Analysis for: %s\n\n", filePathW);
  1020.     //AppendToOutput(L"[+] Starting PE Analysis for: %ls\n\n", filePathW);
  1021.  
  1022.     // Get file content
  1023.     LPVOID lpFileContent = GetFileContent(filePathW);
  1024.     if (!lpFileContent)
  1025.     {
  1026.         OUTPUT("[-] Failed to read file content!\n");
  1027.         return;
  1028.     }
  1029.  
  1030.     // Get DOS header
  1031.     const auto pImageDosHeader = static_cast<PIMAGE_DOS_HEADER>(lpFileContent);
  1032.     if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  1033.     {
  1034.         OUTPUT("[-] Invalid DOS signature!\n");
  1035.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  1036.         return;
  1037.     }
  1038.  
  1039.     // Get NT headers
  1040.     const auto pImageNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>((DWORD_PTR)lpFileContent + pImageDosHeader->e_lfanew);
  1041.     if (pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE)
  1042.     {
  1043.         OUTPUT("[-] Invalid NT signature!\n");
  1044.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  1045.         return;
  1046.     }
  1047.  
  1048.     // Display File Header information
  1049.     OUTPUT("[+] PE FILE HEADER\n");
  1050.     OUTPUT("\tMachine : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.Machine);
  1051.     OUTPUT("\tNumberOfSections : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSections);
  1052.     OUTPUT("\tTimeDateStamp : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.TimeDateStamp);
  1053.     OUTPUT("\tPointerToSymbolTable : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.PointerToSymbolTable);
  1054.     OUTPUT("\tNumberOfSymbols : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSymbols);
  1055.     OUTPUT("\tSizeOfOptionalHeader : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.SizeOfOptionalHeader);
  1056.     OUTPUT("\tCharacteristics : 0x%X %s\n\n",
  1057.         (uintptr_t)pImageNtHeaders->FileHeader.Characteristics,
  1058.         GetImageCharacteristics(pImageNtHeaders->FileHeader.Characteristics));
  1059.  
  1060.     // Display Optional Header information
  1061.     OUTPUT("[+] PE OPTIONAL HEADER\n");
  1062.     OUTPUT("\tMagic : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Magic);
  1063.     OUTPUT("\tAddressOfEntryPoint : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.AddressOfEntryPoint);
  1064.     OUTPUT("\tImageBase : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.ImageBase);
  1065.     OUTPUT("\tSectionAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SectionAlignment);
  1066.     OUTPUT("\tFileAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.FileAlignment);
  1067.     OUTPUT("\tMajorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorOperatingSystemVersion);
  1068.     OUTPUT("\tMinorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorOperatingSystemVersion);
  1069.     OUTPUT("\tMajorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorImageVersion);
  1070.     OUTPUT("\tMinorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorImageVersion);
  1071.     OUTPUT("\tMajorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorSubsystemVersion);
  1072.     OUTPUT("\tMinorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorSubsystemVersion);
  1073.     OUTPUT("\tWin32VersionValue : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Win32VersionValue);
  1074.     OUTPUT("\tSizeOfImage : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfImage);
  1075.     OUTPUT("\tSizeOfHeaders : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeaders);
  1076.     OUTPUT("\tCheckSum : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.CheckSum);
  1077.     OUTPUT("\tSubsystem : 0x%X %s\n",
  1078.         (uintptr_t)pImageNtHeaders->OptionalHeader.Subsystem,
  1079.         GetSubsystem(pImageNtHeaders->OptionalHeader.Subsystem));
  1080.     OUTPUT("\tDllCharacteristics : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.DllCharacteristics);
  1081.     OUTPUT("\tSizeOfStackReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackReserve);
  1082.     OUTPUT("\tSizeOfStackCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackCommit);
  1083.     OUTPUT("\tSizeOfHeapReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapReserve);
  1084.     OUTPUT("\tSizeOfHeapCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapCommit);
  1085.     OUTPUT("\tLoaderFlags : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.LoaderFlags);
  1086.     OUTPUT("\tNumberOfRvaAndSizes : 0x%X\n\n", (uintptr_t)pImageNtHeaders->OptionalHeader.NumberOfRvaAndSizes);
  1087.  
  1088.     // Get Data Directories
  1089.     GetDataDirectories(&pImageNtHeaders->OptionalHeader.DataDirectory[0]);
  1090.  
  1091.     // Get the import section
  1092.     const auto pImageSectionHeader = reinterpret_cast<PIMAGE_SECTION_HEADER>(
  1093.         (DWORD_PTR)pImageNtHeaders + sizeof(IMAGE_NT_HEADERS));
  1094.  
  1095.     const auto pImageImportSection = GetSections(
  1096.         pImageSectionHeader,
  1097.         pImageNtHeaders->FileHeader.NumberOfSections,
  1098.         pImageNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress);
  1099.  
  1100.     if (!pImageImportSection)
  1101.     {
  1102.         OUTPUT("[-] Error: Could not find import section!\n");
  1103.         HeapFree(GetProcessHeap(), 0, lpFileContent);
  1104.         return;
  1105.     }
  1106.  
  1107.     // Get imports based on architecture
  1108.     const auto pImageImportDescriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
  1109.         (DWORD_PTR)lpFileContent + pImageImportSection->PointerToRawData);
  1110.  
  1111.     if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
  1112.     {
  1113.         GetImports32(
  1114.             pImageImportDescriptor,
  1115.             (DWORD)lpFileContent + pImageImportSection->PointerToRawData,
  1116.             pImageImportSection);
  1117.     }
  1118.     else if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
  1119.     {
  1120.         GetImports64(
  1121.             pImageImportDescriptor,
  1122.             (DWORD)lpFileContent + pImageImportSection->PointerToRawData,
  1123.             pImageImportSection);
  1124.     }
  1125.     else
  1126.     {
  1127.         OUTPUT("[-] Unsupported architecture!\n");
  1128.     }
  1129.  
  1130.     // Cleanup
  1131.     HeapFree(GetProcessHeap(), 0, lpFileContent);
  1132.  
  1133.     // Update the GUI with the analysis results
  1134.     UpdateEditControl();
  1135. }
  1136.  
  1137. void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory)
  1138. {
  1139.     OUTPUT("[+] PE DATA DIRECTORIES\n");
  1140.     for (int i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; ++i, ++pImageDataDirectory)
  1141.     {
  1142.         if (pImageDataDirectory->VirtualAddress == 0)
  1143.             continue;
  1144.  
  1145.         OUTPUT("\tDataDirectory (%s) VirtualAddress : 0x%X\n",
  1146.             GetDataDirectoryName(i),
  1147.             (uintptr_t)pImageDataDirectory->VirtualAddress);
  1148.         OUTPUT("\tDataDirectory (%s) Size : 0x%X\n\n",
  1149.             GetDataDirectoryName(i),
  1150.             (uintptr_t)pImageDataDirectory->Size);
  1151.     }
  1152. }
  1153.  
  1154. PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader,
  1155.     int NumberOfSections, DWORD dImportAddress)
  1156. {
  1157.     PIMAGE_SECTION_HEADER pImageImportHeader = nullptr;
  1158.  
  1159.     OUTPUT("\n[+] PE IMAGE SECTIONS\n");
  1160.  
  1161.     for (int i = 0; i < NumberOfSections; ++i)
  1162.     {
  1163.         const auto pCurrentSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD_PTR)pImageSectionHeader +
  1164.             i * sizeof(IMAGE_SECTION_HEADER));
  1165.  
  1166.         OUTPUT("\n\tSECTION : %s\n", (wchar_t*)pCurrentSectionHeader->Name);
  1167.         OUTPUT("\t\tMisc (PhysicalAddress) : 0x%X\n",
  1168.             (uintptr_t)pCurrentSectionHeader->Misc.PhysicalAddress);
  1169.         OUTPUT("\t\tMisc (VirtualSize) : 0x%X\n",
  1170.             (uintptr_t)pCurrentSectionHeader->Misc.VirtualSize);
  1171.         OUTPUT("\t\tVirtualAddress : 0x%X\n",
  1172.             (uintptr_t)pCurrentSectionHeader->VirtualAddress);
  1173.         OUTPUT("\t\tSizeOfRawData : 0x%X\n",
  1174.             (uintptr_t)pCurrentSectionHeader->SizeOfRawData);
  1175.         OUTPUT("\t\tPointerToRawData : 0x%X\n",
  1176.             (uintptr_t)pCurrentSectionHeader->PointerToRawData);
  1177.         OUTPUT("\t\tPointerToRelocations : 0x%X\n",
  1178.             (uintptr_t)pCurrentSectionHeader->PointerToRelocations);
  1179.         OUTPUT("\t\tPointerToLinenumbers : 0x%X\n",
  1180.             (uintptr_t)pCurrentSectionHeader->PointerToLinenumbers);
  1181.         OUTPUT("\t\tNumberOfRelocations : 0x%X\n",
  1182.             (uintptr_t)pCurrentSectionHeader->NumberOfRelocations);
  1183.         OUTPUT("\t\tNumberOfLinenumbers : 0x%X\n",
  1184.             (uintptr_t)pCurrentSectionHeader->NumberOfLinenumbers);
  1185.         OUTPUT("\t\tCharacteristics : 0x%X %s\n",
  1186.             (uintptr_t)pCurrentSectionHeader->Characteristics,
  1187.             GetSectionProtection(pCurrentSectionHeader->Characteristics));
  1188.  
  1189.         if (dImportAddress >= pCurrentSectionHeader->VirtualAddress &&
  1190.             dImportAddress < pCurrentSectionHeader->VirtualAddress +
  1191.             pCurrentSectionHeader->Misc.VirtualSize)
  1192.         {
  1193.             pImageImportHeader = pCurrentSectionHeader;
  1194.         }
  1195.     }
  1196.  
  1197.     return pImageImportHeader;
  1198. }
  1199. void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
  1200.     DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
  1201. {
  1202.     OUTPUT("\n[+] IMPORTED DLL\n");
  1203.  
  1204.     while (pImageImportDescriptor->Name != 0)
  1205.     {
  1206.         OUTPUT("\n\tDLL NAME : %s\n",
  1207.             (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
  1208.         OUTPUT("\tCharacteristics : 0x%X\n",
  1209.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
  1210.         OUTPUT("\tOriginalFirstThunk : 0x%X\n",
  1211.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
  1212.         OUTPUT("\tTimeDateStamp : 0x%X\n",
  1213.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
  1214.         OUTPUT("\tForwarderChain : 0x%X\n",
  1215.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
  1216.         OUTPUT("\tFirstThunk : 0x%X\n",
  1217.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
  1218.  
  1219.         if (pImageImportDescriptor->OriginalFirstThunk == 0)
  1220.         {
  1221.             ++pImageImportDescriptor;
  1222.             continue;
  1223.         }
  1224.  
  1225.         auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA32)(dRawOffset +
  1226.             (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
  1227.  
  1228.         OUTPUT("\n\tImported Functions : \n\n");
  1229.  
  1230.         while (pOriginalFirstThrunk->u1.AddressOfData != 0)
  1231.         {
  1232.             if (pOriginalFirstThrunk->u1.AddressOfData >= IMAGE_ORDINAL_FLAG32)
  1233.             {
  1234.                 ++pOriginalFirstThrunk;
  1235.                 continue;
  1236.             }
  1237.  
  1238.             const auto pImageImportByName = (PIMAGE_IMPORT_BY_NAME)(dRawOffset +
  1239.                 (pOriginalFirstThrunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
  1240.  
  1241.             if (pImageImportByName == nullptr)
  1242.             {
  1243.                 ++pOriginalFirstThrunk;
  1244.                 continue;
  1245.             }
  1246.  
  1247.             if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG32)
  1248.             {
  1249.                 OUTPUT("\t\t0x%X (Ordinal) : %s\n",
  1250.                     (uintptr_t)pOriginalFirstThrunk->u1.AddressOfData,
  1251.                     (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
  1252.             }
  1253.             else
  1254.             {
  1255.                 OUTPUT("\t\t%s\n",
  1256.                     (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
  1257.             }
  1258.  
  1259.             ++pOriginalFirstThrunk;
  1260.         }
  1261.  
  1262.         ++pImageImportDescriptor;
  1263.     }
  1264. }
  1265.  
  1266. void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
  1267.     DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
  1268. {
  1269.     OUTPUT("\n[+] IMPORTED DLL\n");
  1270.  
  1271.     while (pImageImportDescriptor->Name != 0)
  1272.     {
  1273.         OUTPUT("\n\tDLL NAME : %s\n",
  1274.             (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
  1275.         OUTPUT("\tCharacteristics : 0x%X\n",
  1276.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
  1277.         OUTPUT("\tOriginalFirstThunk : 0x%X\n",
  1278.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
  1279.         OUTPUT("\tTimeDateStamp : 0x%X\n",
  1280.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
  1281.         OUTPUT("\tForwarderChain : 0x%X\n",
  1282.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
  1283.         OUTPUT("\tFirstThunk : 0x%X\n",
  1284.             (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
  1285.  
  1286.         if (pImageImportDescriptor->OriginalFirstThunk == 0)
  1287.         {
  1288.             ++pImageImportDescriptor;
  1289.             continue;
  1290.         }
  1291.  
  1292.         auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA64)(dRawOffset +
  1293.             (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
  1294.  
  1295.         OUTPUT("\n\tImported Functions : \n\n");
  1296.  
  1297.         while (pOriginalFirstThrunk->u1.AddressOfData != 0)
  1298.         {
  1299.             if (pOriginalFirstThrunk->u1.AddressOfData >= IMAGE_ORDINAL_FLAG64)
  1300.             {
  1301.                 ++pOriginalFirstThrunk;
  1302.                 continue;
  1303.             }
  1304.  
  1305.             const auto pImageImportByName = (PIMAGE_IMPORT_BY_NAME)((DWORD_PTR)dRawOffset +
  1306.                 (pOriginalFirstThrunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
  1307.  
  1308.             if (pImageImportByName == nullptr)
  1309.             {
  1310.                 ++pOriginalFirstThrunk;
  1311.                 continue;
  1312.             }
  1313.  
  1314.             if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG64)
  1315.             {
  1316.                 OUTPUT("\t\t0x%llX (Ordinal) : %s\n",
  1317.                     pOriginalFirstThrunk->u1.AddressOfData,
  1318.                     (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
  1319.             }
  1320.             else
  1321.             {
  1322.                 OUTPUT("\t\t%s\n",
  1323.                     (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
  1324.             }
  1325.  
  1326.             ++pOriginalFirstThrunk;
  1327.         }
  1328.  
  1329.         ++pImageImportDescriptor;
  1330.     }
  1331. }
  1332. // older-orig-deprecated ^^
  1333. */
  1334.  
  1335. //filePathW
  1336. //lpFilePath
  1337. HANDLE GetFileContent(const wchar_t* lpFilePath) {
  1338.     HANDLE hFile = CreateFileW(lpFilePath, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
  1339.     if (hFile == INVALID_HANDLE_VALUE) return nullptr;
  1340.     DWORD fileSize = GetFileSize(hFile, nullptr);
  1341.     auto lpFileContent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, fileSize);
  1342.     DWORD bytesRead;
  1343.     ReadFile(hFile, lpFileContent, fileSize, &bytesRead, nullptr);
  1344.     CloseHandle(hFile);
  1345.     return lpFileContent;
  1346. }
  1347.  
  1348. void UpdateEditControl() {
  1349.     SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
  1350.     SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
  1351.     SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
  1352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement