Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==++ Here's the full code for (file 1/2) "main.cpp"::++==
- #define STRICT
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <winternl.h>
- #include <CommCtrl.h>
- #include <commdlg.h>
- #include <string>
- #include <strsafe.h>
- #include <sstream>
- #include <iomanip>
- #include <stdio.h>
- #include "helpers.h"
- #include "resource.h"
- #pragma comment(lib, "comctl32.lib")
- using namespace std;
- // At the top of your file, change the window class name to wide string
- #define WINDOW_CLASS_NAME L"PEAnalyzerWindow"
- //const wchar_t* const WINDOW_CLASS_NAME = L"PEAnalyzerWindow";
- // Use ANSI versions explicitly
- //#undef CreateWindow
- //#undef CreateWindowEx
- //#define CreateWindow CreateWindowW
- //#define CreateWindowEx CreateWindowExW
- // Helper function to replace printf with GUI output
- #define OUTPUT(format, ...) AppendToOutput(L##format, ##__VA_ARGS__)
- //#define OUTPUT(format, ...) AppendToOutput(format, ##__VA_ARGS__)
- //#define printf(format, ...) AppendToOutput(format, ##__VA_ARGS__)
- // Window dimensions
- #define WINDOW_WIDTH 1024
- #define WINDOW_HEIGHT 768
- #define EDIT_MARGIN 10
- // Global variables
- HWND g_hMainWindow = NULL;
- HWND g_hEditControl = NULL;
- HFONT g_hFont = NULL;
- std::wstringstream g_OutputText;
- WCHAR filePathW[MAX_PATH];
- // Function declarations
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- void CreateMainWindow(HINSTANCE hInstance);
- void InitializeControls(HWND hwnd);
- void AddMenus(HWND hwnd);
- void OpenFileDialog(HWND hwnd);
- void AnalyzePEFile(const WCHAR* filePathW);
- HANDLE GetFileContent(const wchar_t* lpFilePath);
- void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory);
- PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader,
- int NumberOfSections, DWORD dImportAddress);
- void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
- DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection);
- void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
- DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection);
- void AppendToOutput(const wchar_t* format, ...);
- void UpdateEditControl();
- // Main window class name
- //const char* const WINDOW_CLASS_NAME = "PEAnalyzerWindow";
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // Initialize Common Controls
- INITCOMMONCONTROLSEX icc;
- icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
- icc.dwICC = ICC_WIN95_CLASSES;
- InitCommonControlsEx(&icc);
- // Create the main window
- CreateMainWindow(hInstance);
- if (!g_hMainWindow)
- return -1;
- // Show the window
- ShowWindow(g_hMainWindow, nCmdShow);
- UpdateWindow(g_hMainWindow);
- // Message loop
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- // Cleanup
- if (g_hFont)
- DeleteObject(g_hFont);
- return (int)msg.wParam;
- }
- void CreateMainWindow(HINSTANCE hInstance)
- {
- WNDCLASSEXW wc = {};
- wc.cbSize = sizeof(WNDCLASSEXW);
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = WINDOW_CLASS_NAME;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- if (!RegisterClassExW(&wc))
- return;
- // Create the window
- g_hMainWindow = CreateWindowExW(
- 0, // Optional window styles
- (LPCWSTR)WINDOW_CLASS_NAME, // Window class name (explicitly cast)
- L"PE File Analyzer", // Window text
- WS_OVERLAPPEDWINDOW, // Window style
- CW_USEDEFAULT, CW_USEDEFAULT, // Position
- WINDOW_WIDTH, WINDOW_HEIGHT, // Size
- nullptr, // Parent window
- nullptr, // Menu
- hInstance, // Instance handle
- nullptr // Additional application data
- );
- }
- void InitializeControls(HWND hwnd)
- {
- // Create edit control
- g_hEditControl = CreateWindowExW(
- WS_EX_CLIENTEDGE,
- L"EDIT",
- L"",
- WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
- EDIT_MARGIN, EDIT_MARGIN,
- WINDOW_WIDTH - (2 * EDIT_MARGIN),
- WINDOW_HEIGHT - (2 * EDIT_MARGIN),
- hwnd,
- nullptr,
- (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
- nullptr
- );
- // Create and set font
- g_hFont = CreateFont(
- -14, // Height
- 0, // Width
- 0, // Escapement
- 0, // Orientation
- FW_NORMAL, // Weight
- FALSE, // Italic
- FALSE, // Underline
- 0, // StrikeOut
- ANSI_CHARSET, // CharSet
- OUT_DEFAULT_PRECIS, // OutPrecision
- CLIP_DEFAULT_PRECIS, // ClipPrecision
- DEFAULT_QUALITY, // Quality
- DEFAULT_PITCH | FF_MODERN, // PitchAndFamily
- L"Consolas" // Face Name
- );
- if (g_hFont)
- SendMessage(g_hEditControl, WM_SETFONT, (WPARAM)g_hFont, TRUE);
- }
- void AddMenus(HWND hwnd)
- {
- HMENU hMenuBar = CreateMenu();
- HMENU hFileMenu = CreateMenu();
- AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hFileMenu, L"&File");
- AppendMenu(hFileMenu, MF_STRING, ID_FILE_OPEN, L"&Open");
- AppendMenu(hFileMenu, MF_SEPARATOR, 0, NULL);
- AppendMenu(hFileMenu, MF_STRING, ID_FILE_EXIT, L"E&xit");
- SetMenu(hwnd, hMenuBar);
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- InitializeControls(hwnd);
- AddMenus(hwnd);
- return 0;
- case WM_SIZE:
- {
- // Resize edit control to match window size
- int clientWidth = LOWORD(lParam);
- int clientHeight = HIWORD(lParam);
- SetWindowPos(g_hEditControl, NULL,
- EDIT_MARGIN, EDIT_MARGIN,
- clientWidth - (2 * EDIT_MARGIN),
- clientHeight - (2 * EDIT_MARGIN),
- SWP_NOZORDER);
- return 0;
- }
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case ID_FILE_OPEN:
- OpenFileDialog(hwnd);
- return 0;
- case ID_FILE_EXIT:
- PostQuitMessage(0);
- return 0;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- void OpenFileDialog(HWND hwnd)
- {
- WCHAR fileName[MAX_PATH] = L"";
- OPENFILENAMEW ofn = { 0 };
- ofn.lStructSize = sizeof(OPENFILENAMEW);
- ofn.hwndOwner = hwnd;
- ofn.lpstrFilter = L"Executable Files (*.exe;*.dll)\0*.exe;*.dll\0All Files (*.*)\0*.*\0";
- ofn.lpstrFile = fileName;
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
- ofn.lpstrDefExt = L"exe";
- if (GetOpenFileNameW(&ofn))
- {
- try {
- // Clear previous content
- SetWindowTextW(g_hEditControl, L"");
- g_OutputText.str(L"");
- g_OutputText.clear();
- // Analyze the selected PE file
- AnalyzePEFile(ofn.lpstrFile);
- // Update the edit control with the analysis results
- UpdateEditControl();
- }
- catch (...) {
- MessageBoxW(hwnd, L"An error occurred while analyzing the file.", L"Error", MB_OK | MB_ICONERROR);
- }
- }
- }
- // Corrected AppendToOutput function
- void AppendToOutput(const wchar_t* format, ...) {
- wchar_t buffer[4096];
- va_list args;
- va_start(args, format);
- StringCchPrintfW(buffer, 4096, format, args);
- va_end(args);
- g_OutputText << buffer;
- // Update the edit control immediately
- SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
- // Auto-scroll to the bottom
- SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
- SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
- }
- // Keep the original GetFileContent function as is, but modify the error reporting
- HANDLE GetFileContent(const wchar_t* lpFilePath)
- {
- HANDLE hFile = CreateFileW(lpFilePath, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- OUTPUT("[-] Error opening PE file!\n");
- return nullptr;
- }
- DWORD fileSize = GetFileSize(hFile, nullptr);
- if (fileSize == INVALID_FILE_SIZE)
- {
- OUTPUT("[-] Error getting PE file size!\n");
- CloseHandle(hFile);
- return nullptr;
- }
- LPVOID lpFileContent = HeapAlloc(GetProcessHeap(), 0, fileSize);
- if (lpFileContent == nullptr)
- {
- OUTPUT("[-] Error allocating memory for PE file content!\n");
- //HeapFree(GetProcessHeap(), 0, lpFileContent);
- CloseHandle(hFile);
- return nullptr;
- }
- DWORD dwBytesRead;
- if (!ReadFile(hFile, lpFileContent, fileSize, &dwBytesRead, nullptr))
- {
- OUTPUT("[-] Error reading PE file content!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- CloseHandle(hFile);
- return nullptr;
- }
- CloseHandle(hFile);
- return lpFileContent;
- }
- // Main PE Analysis function
- void AnalyzePEFile(const wchar_t* filePathW)
- {
- //WCHAR filePathW[MAX_PATH];
- //MultiByteToWideChar(CP_ACP, 0, filePathA, -1, filePathW, MAX_PATH);
- OUTPUT("[+] Starting PE Analysis for: %s\n\n", filePathW);
- //AppendToOutput(L"[+] Starting PE Analysis for: %ls\n\n", filePathW);
- // Get file content
- LPVOID lpFileContent = GetFileContent(filePathW);
- if (!lpFileContent)
- {
- OUTPUT("[-] Failed to read file content!\n");
- return;
- }
- // Get DOS header
- const auto pImageDosHeader = static_cast<PIMAGE_DOS_HEADER>(lpFileContent);
- if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
- {
- OUTPUT("[-] Invalid DOS signature!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- return;
- }
- // Get NT headers
- const auto pImageNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>((DWORD_PTR)lpFileContent + pImageDosHeader->e_lfanew);
- if (pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE)
- {
- OUTPUT("[-] Invalid NT signature!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- return;
- }
- // Display File Header information
- OUTPUT("[+] PE FILE HEADER\n");
- OUTPUT("\tMachine : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.Machine);
- OUTPUT("\tNumberOfSections : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSections);
- OUTPUT("\tTimeDateStamp : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.TimeDateStamp);
- OUTPUT("\tPointerToSymbolTable : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.PointerToSymbolTable);
- OUTPUT("\tNumberOfSymbols : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.NumberOfSymbols);
- OUTPUT("\tSizeOfOptionalHeader : 0x%X\n", (uintptr_t)pImageNtHeaders->FileHeader.SizeOfOptionalHeader);
- OUTPUT("\tCharacteristics : 0x%X %s\n\n",
- (uintptr_t)pImageNtHeaders->FileHeader.Characteristics,
- GetImageCharacteristics(pImageNtHeaders->FileHeader.Characteristics));
- // Display Optional Header information
- OUTPUT("[+] PE OPTIONAL HEADER\n");
- OUTPUT("\tMagic : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Magic);
- OUTPUT("\tAddressOfEntryPoint : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.AddressOfEntryPoint);
- OUTPUT("\tImageBase : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.ImageBase);
- OUTPUT("\tSectionAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SectionAlignment);
- OUTPUT("\tFileAlignment : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.FileAlignment);
- OUTPUT("\tMajorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorOperatingSystemVersion);
- OUTPUT("\tMinorOperatingSystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorOperatingSystemVersion);
- OUTPUT("\tMajorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorImageVersion);
- OUTPUT("\tMinorImageVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorImageVersion);
- OUTPUT("\tMajorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MajorSubsystemVersion);
- OUTPUT("\tMinorSubsystemVersion : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.MinorSubsystemVersion);
- OUTPUT("\tWin32VersionValue : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.Win32VersionValue);
- OUTPUT("\tSizeOfImage : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfImage);
- OUTPUT("\tSizeOfHeaders : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeaders);
- OUTPUT("\tCheckSum : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.CheckSum);
- OUTPUT("\tSubsystem : 0x%X %s\n",
- (uintptr_t)pImageNtHeaders->OptionalHeader.Subsystem,
- GetSubsystem(pImageNtHeaders->OptionalHeader.Subsystem));
- OUTPUT("\tDllCharacteristics : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.DllCharacteristics);
- OUTPUT("\tSizeOfStackReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackReserve);
- OUTPUT("\tSizeOfStackCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfStackCommit);
- OUTPUT("\tSizeOfHeapReserve : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapReserve);
- OUTPUT("\tSizeOfHeapCommit : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.SizeOfHeapCommit);
- OUTPUT("\tLoaderFlags : 0x%X\n", (uintptr_t)pImageNtHeaders->OptionalHeader.LoaderFlags);
- OUTPUT("\tNumberOfRvaAndSizes : 0x%X\n\n", (uintptr_t)pImageNtHeaders->OptionalHeader.NumberOfRvaAndSizes);
- // Get Data Directories
- GetDataDirectories(&pImageNtHeaders->OptionalHeader.DataDirectory[0]);
- // Get the import section
- const auto pImageSectionHeader = reinterpret_cast<PIMAGE_SECTION_HEADER>(
- (DWORD_PTR)pImageNtHeaders + sizeof(IMAGE_NT_HEADERS));
- const auto pImageImportSection = GetSections(
- pImageSectionHeader,
- pImageNtHeaders->FileHeader.NumberOfSections,
- pImageNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress);
- if (!pImageImportSection)
- {
- OUTPUT("[-] Error: Could not find import section!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- return;
- }
- // Get imports based on architecture
- const auto pImageImportDescriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
- (DWORD_PTR)lpFileContent + pImageImportSection->PointerToRawData);
- if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
- {
- GetImports32(
- pImageImportDescriptor,
- (DWORD)lpFileContent + pImageImportSection->PointerToRawData,
- pImageImportSection);
- }
- else if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
- {
- GetImports64(
- pImageImportDescriptor,
- (DWORD)lpFileContent + pImageImportSection->PointerToRawData,
- pImageImportSection);
- }
- else
- {
- OUTPUT("[-] Unsupported architecture!\n");
- }
- // Cleanup
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- // Update the GUI with the analysis results
- UpdateEditControl();
- }
- void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory)
- {
- OUTPUT("[+] PE DATA DIRECTORIES\n");
- for (int i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; ++i, ++pImageDataDirectory)
- {
- if (pImageDataDirectory->VirtualAddress == 0)
- continue;
- OUTPUT("\tDataDirectory (%s) VirtualAddress : 0x%X\n",
- GetDataDirectoryName(i),
- (uintptr_t)pImageDataDirectory->VirtualAddress);
- OUTPUT("\tDataDirectory (%s) Size : 0x%X\n\n",
- GetDataDirectoryName(i),
- (uintptr_t)pImageDataDirectory->Size);
- }
- }
- PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader,
- int NumberOfSections, DWORD dImportAddress)
- {
- PIMAGE_SECTION_HEADER pImageImportHeader = nullptr;
- OUTPUT("\n[+] PE IMAGE SECTIONS\n");
- for (int i = 0; i < NumberOfSections; ++i)
- {
- const auto pCurrentSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD_PTR)pImageSectionHeader +
- i * sizeof(IMAGE_SECTION_HEADER));
- OUTPUT("\n\tSECTION : %s\n", (wchar_t*)pCurrentSectionHeader->Name);
- OUTPUT("\t\tMisc (PhysicalAddress) : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->Misc.PhysicalAddress);
- OUTPUT("\t\tMisc (VirtualSize) : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->Misc.VirtualSize);
- OUTPUT("\t\tVirtualAddress : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->VirtualAddress);
- OUTPUT("\t\tSizeOfRawData : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->SizeOfRawData);
- OUTPUT("\t\tPointerToRawData : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->PointerToRawData);
- OUTPUT("\t\tPointerToRelocations : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->PointerToRelocations);
- OUTPUT("\t\tPointerToLinenumbers : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->PointerToLinenumbers);
- OUTPUT("\t\tNumberOfRelocations : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->NumberOfRelocations);
- OUTPUT("\t\tNumberOfLinenumbers : 0x%X\n",
- (uintptr_t)pCurrentSectionHeader->NumberOfLinenumbers);
- OUTPUT("\t\tCharacteristics : 0x%X %s\n",
- (uintptr_t)pCurrentSectionHeader->Characteristics,
- GetSectionProtection(pCurrentSectionHeader->Characteristics));
- if (dImportAddress >= pCurrentSectionHeader->VirtualAddress &&
- dImportAddress < pCurrentSectionHeader->VirtualAddress +
- pCurrentSectionHeader->Misc.VirtualSize)
- {
- pImageImportHeader = pCurrentSectionHeader;
- }
- }
- return pImageImportHeader;
- }
- void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
- DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
- {
- OUTPUT("\n[+] IMPORTED DLL\n");
- while (pImageImportDescriptor->Name != 0)
- {
- OUTPUT("\n\tDLL NAME : %s\n",
- (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
- OUTPUT("\tCharacteristics : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
- OUTPUT("\tOriginalFirstThunk : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
- OUTPUT("\tTimeDateStamp : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
- OUTPUT("\tForwarderChain : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
- OUTPUT("\tFirstThunk : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
- if (pImageImportDescriptor->OriginalFirstThunk == 0)
- {
- ++pImageImportDescriptor;
- continue;
- }
- auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA32)(dRawOffset +
- (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
- OUTPUT("\n\tImported Functions : \n\n");
- while (pOriginalFirstThrunk->u1.AddressOfData != 0)
- {
- if (pOriginalFirstThrunk->u1.AddressOfData >= IMAGE_ORDINAL_FLAG32)
- {
- ++pOriginalFirstThrunk;
- continue;
- }
- const auto pImageImportByName = (PIMAGE_IMPORT_BY_NAME)(dRawOffset +
- (pOriginalFirstThrunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
- if (pImageImportByName == nullptr)
- {
- ++pOriginalFirstThrunk;
- continue;
- }
- if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG32)
- {
- OUTPUT("\t\t0x%X (Ordinal) : %s\n",
- (uintptr_t)pOriginalFirstThrunk->u1.AddressOfData,
- (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
- }
- else
- {
- OUTPUT("\t\t%s\n",
- (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
- }
- ++pOriginalFirstThrunk;
- }
- ++pImageImportDescriptor;
- }
- }
- void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor,
- DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
- {
- OUTPUT("\n[+] IMPORTED DLL\n");
- while (pImageImportDescriptor->Name != 0)
- {
- OUTPUT("\n\tDLL NAME : %s\n",
- (wchar_t*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
- OUTPUT("\tCharacteristics : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
- OUTPUT("\tOriginalFirstThunk : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
- OUTPUT("\tTimeDateStamp : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
- OUTPUT("\tForwarderChain : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
- OUTPUT("\tFirstThunk : 0x%X\n",
- (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
- if (pImageImportDescriptor->OriginalFirstThunk == 0)
- {
- ++pImageImportDescriptor;
- continue;
- }
- auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA64)(dRawOffset +
- (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
- OUTPUT("\n\tImported Functions : \n\n");
- while (pOriginalFirstThrunk->u1.AddressOfData != 0)
- {
- if (pOriginalFirstThrunk->u1.AddressOfData >= IMAGE_ORDINAL_FLAG64)
- {
- ++pOriginalFirstThrunk;
- continue;
- }
- const auto pImageImportByName = (PIMAGE_IMPORT_BY_NAME)((DWORD_PTR)dRawOffset +
- (pOriginalFirstThrunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
- if (pImageImportByName == nullptr)
- {
- ++pOriginalFirstThrunk;
- continue;
- }
- if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG64)
- {
- OUTPUT("\t\t0x%llX (Ordinal) : %s\n",
- pOriginalFirstThrunk->u1.AddressOfData,
- (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
- }
- else
- {
- OUTPUT("\t\t%s\n",
- (wchar_t*)((DWORD_PTR)dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress)));
- }
- ++pOriginalFirstThrunk;
- }
- ++pImageImportDescriptor;
- }
- }
- // Add this at the end of the analysis to ensure proper display
- void UpdateEditControl() {
- SetWindowTextW(g_hEditControl, g_OutputText.str().c_str());
- SendMessage(g_hEditControl, EM_SETSEL, -1, -1);
- SendMessage(g_hEditControl, EM_SCROLLCARET, 0, 0);
- }
- ==++ Here's the full code for (file 2/2) "helpers.h"::++==
- #pragma once
- #include <Windows.h>
- #include <strsafe.h>
- // Helper function declarations
- const wchar_t* GetImageCharacteristics(DWORD dCharacteristics);
- const wchar_t* GetSubsystem(WORD Subsystem);
- const wchar_t* GetDataDirectoryName(int DirectoryNumber);
- const wchar_t* GetSectionProtection(DWORD dCharacteristics);
- // Helper function implementations
- const wchar_t* GetImageCharacteristics(DWORD dCharacteristics)
- {
- if (dCharacteristics & IMAGE_FILE_DLL)
- return L"(DLL)";
- if (dCharacteristics & IMAGE_FILE_SYSTEM)
- return L"(DRIVER)";
- if (dCharacteristics & IMAGE_FILE_EXECUTABLE_IMAGE)
- return L"(EXE)";
- return L"(UNKNOWN)";
- }
- const wchar_t* GetSubsystem(WORD Subsystem)
- {
- if (Subsystem == 1)
- return L"(NATIVE / DRIVER)";
- if (Subsystem == 2)
- return L"(GUI APP)";
- if (Subsystem == 3)
- return L"(CONSOLE APP)";
- return L"(UNKNOWN)";
- }
- const wchar_t* GetDataDirectoryName(int DirectoryNumber)
- {
- switch (DirectoryNumber)
- {
- case 0: return L"Export Table";
- case 1: return L"Import Table";
- case 2: return L"Resource Table";
- case 3: return L"Exception Entry";
- case 4: return L"Security Entry";
- case 5: return L"Relocation Table";
- case 6: return L"Debug Entry";
- case 7: return L"Copyright Entry";
- case 8: return L"Global PTR Entry";
- case 9: return L"TLS Entry";
- case 10: return L"Configuration Entry";
- case 11: return L"Bound Import Entry";
- case 12: return L"IAT";
- case 13: return L"Delay Import Descriptor";
- case 14: return L"COM Descriptor";
- default: return nullptr;
- }
- }
- const wchar_t* GetSectionProtection(DWORD dCharacteristics)
- {
- static wchar_t lpSectionProtection[1024] = {};
- StringCchCatW(lpSectionProtection, 1024, L"(");
- bool bExecute = false, bRead = false;
- if (dCharacteristics & IMAGE_SCN_MEM_EXECUTE)
- {
- bExecute = true;
- StringCchCatW(lpSectionProtection, 1024, L"EXECUTE");
- }
- if (dCharacteristics & IMAGE_SCN_MEM_READ)
- {
- bRead = true;
- if (bExecute)
- StringCchCatW(lpSectionProtection, 1024, L" | ");
- StringCchCatW(lpSectionProtection, 1024, L"READ");
- }
- if (dCharacteristics & IMAGE_SCN_MEM_WRITE)
- {
- if (bExecute || bRead)
- StringCchCatW(lpSectionProtection, 1024, L" | ");
- StringCchCatW(lpSectionProtection, 1024, L"WRITE");
- }
- StringCchCatW(lpSectionProtection, 1024, L")");
- return lpSectionProtection;
- }
- ==++ Here's the full code for (file 3/2) "helpers.h" (Original Working Backup Version Possible Fallback)::++==
- /*
- //#pragma once
- #pragma once
- #include <Windows.h>
- // Helper function declarations
- const char* GetImageCharacteristics(DWORD dCharacteristics);
- const char* GetSubsytem(WORD Subsystem);
- const char* GetDataDirectoryName(int DirectoryNumber);
- const char* GetSectionProtection(DWORD dCharacteristics);
- // Helper function implementations
- const char* GetImageCharacteristics(DWORD dCharacteristics)
- {
- if (dCharacteristics & IMAGE_FILE_DLL)
- return "(DLL)";
- if (dCharacteristics & IMAGE_FILE_SYSTEM)
- return "(DRIVER)";
- if (dCharacteristics & IMAGE_FILE_EXECUTABLE_IMAGE)
- return "(EXE)";
- return "(UNKNOWN)";
- }
- const char* GetSubsytem(WORD Subsystem)
- {
- if (Subsystem == 1)
- return "(NATIVE / DRIVER)";
- if (Subsystem == 2)
- return "(GUI APP)";
- if (Subsystem == 3)
- return "(CONSOLE APP)";
- return "(UNKNOWN)";
- }
- const char* GetDataDirectoryName(int DirectoryNumber)
- {
- switch (DirectoryNumber)
- {
- case 0: return "Export Table";
- case 1: return "Import Table";
- case 2: return "Ressource Table";
- case 3: return "Exception Entry";
- case 4: return "Security Entry";
- case 5: return "Relocation Table";
- case 6: return "Debug Entry";
- case 7: return "Copyright Entry";
- case 8: return "Global PTR Entry";
- case 9: return "TLS Entry";
- case 10: return "Configuration Entry";
- case 11: return "Bound Import Entry";
- case 12: return "IAT";
- case 13: return "Delay Import Descriptor";
- case 14: return "COM Descriptor";
- default: return nullptr;
- }
- }
- const char* GetSectionProtection(DWORD dCharacteristics)
- {
- static char lpSectionProtection[1024] = {};
- StringCchCatA(lpSectionProtection, 1024, "(");
- bool bExecute = false, bRead = false;
- if (dCharacteristics & IMAGE_SCN_MEM_EXECUTE)
- {
- bExecute = true;
- StringCchCatA(lpSectionProtection, 1024, "EXECUTE");
- }
- if (dCharacteristics & IMAGE_SCN_MEM_READ)
- {
- bRead = true;
- if (bExecute)
- StringCchCatA(lpSectionProtection, 1024, " | ");
- StringCchCatA(lpSectionProtection, 1024, "READ");
- }
- if (dCharacteristics & IMAGE_SCN_MEM_WRITE)
- {
- if (bExecute || bRead)
- StringCchCatA(lpSectionProtection, 1024, " | ");
- StringCchCatA(lpSectionProtection, 1024, "WRITE");
- }
- StringCchCatA(lpSectionProtection, 1024, ")");
- return lpSectionProtection;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement