Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==++ Here's the full code for (file 1/2) "source.cpp"::++==
- #include <Windows.h>
- #include <winternl.h>
- #include <cstdio>
- #include <strsafe.h>
- #include <stdio.h>
- #include <string.h>
- #include "concol.h"
- #include <conio.h> // Include for _getch()
- #include "resource.h" // Add this with your other includes
- /**
- * Function to retrieve the PE file content.
- * \param lpFilePath : path of the PE file.
- * \return : address of the content in the explorer memory.
- */
- HANDLE GetFileContent(const char* lpFilePath)
- {
- HANDLE hFile = CreateFileA(lpFilePath, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- printf("[-] Error opening PE file!\n");
- return nullptr;
- }
- DWORD fileSize = GetFileSize(hFile, nullptr);
- if (fileSize == INVALID_FILE_SIZE)
- {
- printf("[-] Error getting PE file size!\n");
- CloseHandle(hFile);
- return nullptr;
- }
- // Allocate memory using HeapAlloc (preferred in Windows)
- LPVOID lpFileContent = HeapAlloc(GetProcessHeap(), 0, fileSize);
- if (lpFileContent == nullptr)
- {
- printf("[-] Error allocating memory for PE file content!\n");
- CloseHandle(hFile);
- return nullptr;
- }
- DWORD dwBytesRead;
- if (!ReadFile(hFile, lpFileContent, fileSize, &dwBytesRead, nullptr))
- {
- printf("[-] Error reading PE file content!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- CloseHandle(hFile);
- return nullptr;
- }
- CloseHandle(hFile);
- return lpFileContent; // Return the allocated memory pointer
- }
- /**
- * Function to identify the PE file characteristics.
- * \param dCharacteristics : characteristics in the file header section.
- * \return : the description of the PE file characteristics.
- */
- 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)";
- }
- /**
- * Function to identify the PE file subsystem.
- * \param Subsystem : subsystem in the optional header.
- * \return : the description of the PE file subsystem.
- */
- 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)";
- }
- /**
- * Function to identify the DataDirectory.
- * \param DirectoryNumber : index of the DataDirectory.
- * \return : the description of the DataDirectory.
- */
- 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;
- }
- }
- /**
- * Retrieve and display the DataDirectory informations.
- * \param pImageDataDirectory : DataDirectory array of the optional header.
- */
- void GetDataDirectories(PIMAGE_DATA_DIRECTORY pImageDataDirectory)
- {
- for (int i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; ++i, ++pImageDataDirectory)
- {
- if (pImageDataDirectory->VirtualAddress == 0)
- continue;
- printf("\tDataDirectory (%s) VirtualAddress : 0x%X\n", GetDataDirectoryName(i), (uintptr_t)pImageDataDirectory->VirtualAddress);
- printf("\tDataDirectory (%s) Size : 0x%X\n\n", GetDataDirectoryName(i), (uintptr_t)pImageDataDirectory->Size);
- }
- }
- /**
- * Retrieve and display the protection of the section.
- * \param dCharacteristics : characteristics of the section.
- * \return : the description of the protection.
- */
- const char* GetSectionProtection(DWORD dCharacteristics)
- {
- 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;
- }
- /**
- * Function to retrieve sections from the PE file and get the section wich contains imports.
- * \param pImageSectionHeader : section header of the PE file.
- * \param NumberOfSections : number of section in the PE file.
- * \param dImportAddress : address of import found into DataDirectory 1.
- * \return : section which contains imports.
- */
- PIMAGE_SECTION_HEADER GetSections(const PIMAGE_SECTION_HEADER pImageSectionHeader, int NumberOfSections, DWORD dImportAddress)
- {
- PIMAGE_SECTION_HEADER pImageImportHeader = nullptr;
- printf("\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));
- printf("\n\tSECTION : %s\n", (char*)pCurrentSectionHeader->Name);
- printf("\t\tMisc (PhysicalAddress) : 0x%X\n", (uintptr_t)pCurrentSectionHeader->Misc.PhysicalAddress);
- printf("\t\tMisc (VirtualSize) : 0x%X\n", (uintptr_t)pCurrentSectionHeader->Misc.VirtualSize);
- printf("\t\tVirtualAddress : 0x%X\n", (uintptr_t)pCurrentSectionHeader->VirtualAddress);
- printf("\t\tSizeOfRawData : 0x%X\n", (uintptr_t)pCurrentSectionHeader->SizeOfRawData);
- printf("\t\tPointerToRawData : 0x%X\n", (uintptr_t)pCurrentSectionHeader->PointerToRawData);
- printf("\t\tPointerToRelocations : 0x%X\n", (uintptr_t)pCurrentSectionHeader->PointerToRelocations);
- printf("\t\tPointerToLinenumbers : 0x%X\n", (uintptr_t)pCurrentSectionHeader->PointerToLinenumbers);
- printf("\t\tNumberOfRelocations : 0x%X\n", (uintptr_t)pCurrentSectionHeader->NumberOfRelocations);
- printf("\t\tNumberOfLinenumbers : 0x%X\n", (uintptr_t)pCurrentSectionHeader->NumberOfLinenumbers);
- printf("\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;
- }
- /**
- * Retrieve and display dll and functions imported (for x86 PE file).
- * \param pImageImportDescriptor : import descriptor of the PE file.
- * \param dRawOffset : address of raw data of the import section.
- * \param pImageImportSection : section wich contains imports.
- */
- void GetImports32(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor, DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
- {
- printf("\n[+] IMPORTED DLL\n");
- while (pImageImportDescriptor->Name != 0)
- {
- printf("\n\tDLL NAME : %s\n", (char*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
- printf("\tCharacteristics : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
- printf("\tOriginalFirstThunk : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
- printf("\tTimeDateStamp : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
- printf("\tForwarderChain : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
- printf("\tFirstThunk : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
- if (pImageImportDescriptor->OriginalFirstThunk == 0)
- continue;
- auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA32)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
- printf("\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)pOriginalFirstThrunk->u1.AddressOfData;
- if (pImageImportByName == nullptr)
- continue;
- if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG32)
- printf("\t\t0x%X (Ordinal) : %s\n", (uintptr_t)pOriginalFirstThrunk->u1.AddressOfData, dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress));
- else
- printf("\t\t%s\n", dRawOffset + (pImageImportByName->Name - pImageImportSection->VirtualAddress));
- ++pOriginalFirstThrunk;
- }
- ++pImageImportDescriptor;
- }
- }
- /**
- * Retrieve and display dll and functions imported (for x64 PE file).
- * \param pImageImportDescriptor : import descriptor of the PE file.
- * \param dRawOffset : address of raw data of the import section.
- * \param pImageImportSection : section wich contains imports.
- */
- void GetImports64(PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor, DWORD dRawOffset, const PIMAGE_SECTION_HEADER pImageImportSection)
- {
- printf("\n[+] IMPORTED DLL\n");
- while (pImageImportDescriptor->Name != 0)
- {
- printf("\n\tDLL NAME : %s\n", (char*)(dRawOffset + (pImageImportDescriptor->Name - pImageImportSection->VirtualAddress)));
- printf("\tCharacteristics : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->Characteristics - pImageImportSection->VirtualAddress)));
- printf("\tOriginalFirstThunk : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress)));
- printf("\tTimeDateStamp : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->TimeDateStamp - pImageImportSection->VirtualAddress)));
- printf("\tForwarderChain : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->ForwarderChain - pImageImportSection->VirtualAddress)));
- printf("\tFirstThunk : 0x%X\n", (uintptr_t)(dRawOffset + (pImageImportDescriptor->FirstThunk - pImageImportSection->VirtualAddress)));
- if (pImageImportDescriptor->OriginalFirstThunk == 0)
- continue;
- auto pOriginalFirstThrunk = (PIMAGE_THUNK_DATA64)(dRawOffset + (pImageImportDescriptor->OriginalFirstThunk - pImageImportSection->VirtualAddress));
- printf("\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)(dRawOffset + (pOriginalFirstThrunk->u1.AddressOfData - pImageImportSection->VirtualAddress));
- if (pImageImportByName == nullptr)
- continue;
- if (pOriginalFirstThrunk->u1.Ordinal & IMAGE_ORDINAL_FLAG64)
- printf("\t\t0x%X (Ordinal) : %s\n", (uintptr_t)pOriginalFirstThrunk->u1.AddressOfData, (char*)pImageImportByName->Name);
- else
- printf("\t\t%s\n", (char*)pImageImportByName->Name);
- ++pOriginalFirstThrunk;
- }
- ++pImageImportDescriptor;
- }
- }
- int main()
- {
- // Get the handle to the console window
- HWND consoleWindow = GetConsoleWindow();
- // Set the console window size 1200,659
- SetWindowPos(consoleWindow, NULL, 0, 0, 800, 500, SWP_NOZORDER | SWP_NOMOVE);
- // Get the screen size
- RECT screenRect;
- GetWindowRect(GetDesktopWindow(), &screenRect);
- // Get the console window size
- RECT consoleRect;
- GetWindowRect(consoleWindow, &consoleRect);
- int consoleWidth = consoleRect.right - consoleRect.left;
- int consoleHeight = consoleRect.bottom - consoleRect.top;
- // Calculate the new console position
- int xPos = (screenRect.right - consoleWidth) / 2;
- int yPos = (screenRect.bottom - consoleHeight) / 2;
- // Set the console window position and size
- SetWindowPos(consoleWindow, NULL, xPos, yPos, consoleWidth, consoleHeight, SWP_NOZORDER | SWP_NOSIZE);
- /* End test injection */
- /* Start test BlockCaret */
- // Get the handle to the console output
- HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- // Get the current console cursor info
- CONSOLE_CURSOR_INFO cursorInfo;
- GetConsoleCursorInfo(hConsoleOutput, &cursorInfo);
- // Change the cursor size to maximum
- cursorInfo.dwSize = 100;
- // Set the new console cursor info
- SetConsoleCursorInfo(hConsoleOutput, &cursorInfo);
- /* End test BlockCaret */
- /* Start test TitleBar ReCaptioning */
- // Set the console window title
- SetConsoleTitle(L"Chat Client CowSay Loop (Serialized)");
- /* End test TitleBar ReCaptioning */
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_FONT_INFOEX fontInfo = { sizeof(CONSOLE_FONT_INFOEX) };
- GetCurrentConsoleFontEx(hConsole, FALSE, &fontInfo);
- fontInfo.FontWeight = 1000; // 700 is the value for bold font weight
- fontInfo.dwFontSize = { 10, 20 }; // set font size
- wcscpy_s(fontInfo.FaceName, L"Source Code Pro Black"); // set font name
- SetCurrentConsoleFontEx(hConsole, FALSE, &fontInfo);
- //This is one way to do it. Taken from stackoverflow.
- system("color DB");
- char filePath[MAX_PATH];
- printf("Enter PE file path: ");
- if (fgets(filePath, MAX_PATH, stdin) == NULL) {
- printf("Error reading input\n");
- printf("\nPress any key to exit...");
- _getch();
- return 1;
- }
- // Remove newline if present
- filePath[strcspn(filePath, "\n")] = 0;
- // Get PE file content
- LPVOID lpFileContent = GetFileContent(filePath);
- if (lpFileContent == nullptr)
- {
- printf("\nPress any key to exit...");
- _getch();
- return 1;
- }
- // Get DOS header
- const auto pImageDosHeader = static_cast<PIMAGE_DOS_HEADER>(lpFileContent);
- if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
- {
- printf("[-] Invalid DOS signature!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- printf("\nPress any key to exit...");
- _getch();
- return 1;
- }
- // Get NT headers
- const auto pImageNtHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>((DWORD_PTR)lpFileContent + pImageDosHeader->e_lfanew);
- if (pImageNtHeaders->Signature != IMAGE_NT_SIGNATURE)
- {
- printf("[-] Invalid NT signature!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- printf("\nPress any key to exit...");
- _getch();
- return 1;
- }
- // Display basic information
- printf("[+] PE FILE INFORMATION\n");
- printf("\tCharacteristics : 0x%X %s\n", pImageNtHeaders->FileHeader.Characteristics,
- GetImageCharacteristics(pImageNtHeaders->FileHeader.Characteristics));
- printf("\tSubsystem : 0x%X %s\n", pImageNtHeaders->OptionalHeader.Subsystem,
- GetSubsytem(pImageNtHeaders->OptionalHeader.Subsystem));
- // Get DataDirectory information
- GetDataDirectories(&pImageNtHeaders->OptionalHeader.DataDirectory[0]);
- // Get section containing imports
- const auto pImageSectionHeader = IMAGE_FIRST_SECTION(pImageNtHeaders);
- const auto pImageImportSection = GetSections(pImageSectionHeader,
- pImageNtHeaders->FileHeader.NumberOfSections,
- pImageNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress);
- if (pImageImportSection == nullptr)
- {
- printf("[-] No import section found!\n");
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- printf("\nPress any key to exit...");
- _getch();
- return 1;
- }
- // Get imports
- const auto pImageImportDescriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>((DWORD_PTR)lpFileContent +
- (pImageImportSection->PointerToRawData + (pImageNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress -
- pImageImportSection->VirtualAddress)));
- // Check machine type and get imports accordingly
- if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
- GetImports32(pImageImportDescriptor, (DWORD_PTR)lpFileContent + pImageImportSection->PointerToRawData, pImageImportSection);
- else if (pImageNtHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
- GetImports64(pImageImportDescriptor, (DWORD_PTR)lpFileContent + pImageImportSection->PointerToRawData, pImageImportSection);
- // Cleanup
- HeapFree(GetProcessHeap(), 0, lpFileContent);
- // Wait for user input before closing
- printf("\nPress any key to exit...");
- _getch();
- return 0;
- }
- ==++ Here's the full code for (file 2/2) "concol.h"::++==
- #pragma once
- //This is a header file taken from cplusplus.com
- //http://www.cplusplus.com/articles/Eyhv0pDG/
- //concol.h
- #ifndef _INC_EKU_IO_CONCOL
- #define _INC_EKU_IO_CONCOL
- /*Header file to color text and background in windows console applications
- Global variables - textcol,backcol,deftextcol,defbackcol,colorprotect*/
- #include<windows.h>
- #include<iosfwd>
- namespace eku
- {
- #ifndef CONCOL
- #define CONCOL
- enum concol
- {
- black = 0,
- dark_blue = 1,
- dark_green = 2,
- dark_aqua, dark_cyan = 3,
- dark_red = 4,
- dark_purple = 5, dark_pink = 5, dark_magenta = 5,
- dark_yellow = 6,
- dark_white = 7,
- gray = 8,
- blue = 9,
- green = 10,
- aqua = 11, cyan = 11,
- red = 12,
- purple = 13, pink = 13, magenta = 13,
- yellow = 14,
- white = 15
- };
- #endif //CONCOL
- HANDLE std_con_out;
- //Standard Output Handle
- bool colorprotect = false;
- //If colorprotect is true, background and text colors will never be the same
- concol textcol, backcol, deftextcol, defbackcol;
- /*textcol - current text color
- backcol - current back color
- deftextcol - original text color
- defbackcol - original back color*/
- inline void update_colors()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(std_con_out, &csbi);
- textcol = concol(csbi.wAttributes & 15);
- backcol = concol((csbi.wAttributes & 0xf0) >> 4);
- }
- inline void setcolor(concol textcolor, concol backcolor)
- {
- if (colorprotect && textcolor == backcolor)return;
- textcol = textcolor; backcol = backcolor;
- unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
- SetConsoleTextAttribute(std_con_out, wAttributes);
- }
- inline void settextcolor(concol textcolor)
- {
- if (colorprotect && textcolor == backcol)return;
- textcol = textcolor;
- unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
- SetConsoleTextAttribute(std_con_out, wAttributes);
- }
- inline void setbackcolor(concol backcolor)
- {
- if (colorprotect && textcol == backcolor)return;
- backcol = backcolor;
- unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
- SetConsoleTextAttribute(std_con_out, wAttributes);
- }
- inline void concolinit()
- {
- std_con_out = GetStdHandle(STD_OUTPUT_HANDLE);
- update_colors();
- deftextcol = textcol; defbackcol = backcol;
- }
- template<class elem, class traits>
- inline std::basic_ostream<elem, traits>& operator<<(std::basic_ostream<elem, traits>& os, concol col)
- {
- os.flush(); settextcolor(col); return os;
- }
- template<class elem, class traits>
- inline std::basic_istream<elem, traits>& operator>>(std::basic_istream<elem, traits>& is, concol col)
- {
- std::basic_ostream<elem, traits>* p = is.tie();
- if (p != NULL)p->flush();
- settextcolor(col);
- return is;
- }
- } //end of namespace eku
- #endif //_INC_EKU_IO_CONCOL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement