Hello TeamBuilder,It's good that you at least showed the names of your custom functions, otherwise I thought you would limit yourself to simply demonstrating the results from dumpbin.
Posted in the wrong thread. You need to fill in this in yours, in the selling one
Did you know that if you use the standard GetProcAddress, the result will be the same in dumpbin? Or do you return the default ones in custom functions?)
And I don't quite understand, why call the lib itself by "hash" if it still lights up even in static? It seems useless to me, right? Or am I missing something again?
#include <windows.h>
#include <iostream>
// Пример хэш-функции для получения хэша имени функции.
DWORD HashFunctionIAT(const char* funcName) {
DWORD hash = 0;
while (*funcName) {
hash = (hash >> 13) | (hash << 19); // ROTR 13
hash += *funcName++;
}
return hash;
}
// Функция для поиска адреса функции по её хэшу в IAT
FARPROC GetProcAddressByHashIAT(HMODULE hModule, DWORD hash) {
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)((BYTE*)hModule + pDosHeader->e_lfanew);
PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE*)hModule +
pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (pImportDesc->Name) {
LPCSTR moduleName = (LPCSTR)((BYTE*)hModule + pImportDesc->Name);
HMODULE hImportModule = LoadLibraryA(moduleName);
if (hImportModule) {
std::cout << "Loaded module: " << moduleName << std::endl;
PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((BYTE*)hModule + pImportDesc->OriginalFirstThunk);
PIMAGE_THUNK_DATA pIAT = (PIMAGE_THUNK_DATA)((BYTE*)hModule + pImportDesc->FirstThunk);
while (pThunk->u1.AddressOfData) {
if (!IMAGE_SNAP_BY_ORDINAL(pThunk->u1.Ordinal)) {
PIMAGE_IMPORT_BY_NAME pImportByName = (PIMAGE_IMPORT_BY_NAME)((BYTE*)hModule + pThunk->u1.AddressOfData);
DWORD currentHash = HashFunctionIAT((char*)pImportByName->Name);
std::cout << "Checking function: " << pImportByName->Name << " - Hash: " << currentHash << std::endl;
if (currentHash == hash) {
return (FARPROC)pIAT->u1.Function;
}
}
pThunk++;
pIAT++;
}
}
else {
std::cerr << "Failed to load module: " << moduleName << std::endl;
}
pImportDesc++;
}
return nullptr; // Функция не найдена
}
int main() {
#pragma comment(lib, "user32.lib")
// Принудительно загружаем библиотеку user32.dll
HMODULE hUser32 = LoadLibraryA("user32.dll");
if (!hUser32) {
std::cerr << "Failed to load user32.dll" << std::endl;
return 1;
}
HMODULE hModule = GetModuleHandle(NULL); // Получение хэндла текущего модуля
// Пример использования
const char* funcName = "MessageBoxA";
DWORD hashMessageBoxA = HashFunctionIAT(funcName);
DWORD hashGetProcAddress = HashFunctionIAT("GetProcAddress");
// Получаем адрес GetProcAddress через IAT
FARPROC addrGetProcAddress = GetProcAddressByHashIAT(hModule, hashGetProcAddress);
if (!addrGetProcAddress) {
std::cerr << "Failed to find GetProcAddress by hash." << std::endl;
return 1;
}
typedef FARPROC(WINAPI* GETPROCADDRESS)(HMODULE, LPCSTR);
GETPROCADDRESS pGetProcAddress = (GETPROCADDRESS)addrGetProcAddress;
// Теперь используем найденный адрес GetProcAddress для получения адреса MessageBoxA
FARPROC addrMessageBoxA = pGetProcAddress(hUser32, funcName);
if (addrMessageBoxA) {
typedef int (WINAPI* MESSAGEBOXA)(HWND, LPCSTR, LPCSTR, UINT);
MESSAGEBOXA pMessageBoxA = (MESSAGEBOXA)addrMessageBoxA;
pMessageBoxA(NULL, "Hello", "Hash Function Call", MB_OK);
}
else {
std::cerr << "Failed to find MessageBoxA by GetProcAddress." << std::endl;
}
return 0;
}
#include <windows.h>
#include <iostream>
#include <tlhelp32.h>
#include <string>
// Пример хэш-функции для получения хэша имени.
DWORD HashFunction(const char* name) {
DWORD hash = 0;
while (*name) {
hash = (hash >> 13) | (hash << 19); // ROTR 13
hash += *name++;
}
return hash;
}
// Функция для преобразования WCHAR в char
std::string ConvertWCharToString(const WCHAR* wchar) {
int len = (int)wcslen(wchar) + 1;
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wchar, len, NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wchar, len, &strTo[0], size_needed, NULL, NULL);
return strTo;
}
// Функция для поиска загруженной библиотеки по её хэшу
HMODULE GetModuleHandleByHash(DWORD dllHash) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, GetCurrentProcessId());
if (hSnapshot == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create module snapshot. Error: " << GetLastError() << std::endl;
return nullptr;
}
MODULEENTRY32 me32 = {0};
me32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnapshot, &me32)) {
do {
std::string moduleName = ConvertWCharToString(me32.szModule);
DWORD currentHash = HashFunction(moduleName.c_str());
std::cout << "Module: " << moduleName << " - Hash: " << currentHash << std::endl; // Отладочное сообщение
if (currentHash == dllHash) {
CloseHandle(hSnapshot);
return me32.hModule;
}
} while (Module32Next(hSnapshot, &me32));
}
CloseHandle(hSnapshot);
return nullptr;
}
// Функция для получения адреса таблицы экспортируемых функций
PIMAGE_EXPORT_DIRECTORY GetExportDirectory(HMODULE hModule) {
if (!hModule) return nullptr;
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)((BYTE*)hModule + pDosHeader->e_lfanew);
DWORD exportDirRVA = pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
return (PIMAGE_EXPORT_DIRECTORY)((BYTE*)hModule + exportDirRVA);
}
// Функция для поиска функции по хэшу
FARPROC GetProcAddressByHash(HMODULE hModule, DWORD funcHash) {
if (!hModule) return nullptr;
PIMAGE_EXPORT_DIRECTORY pExportDir = GetExportDirectory(hModule);
if (!pExportDir) {
std::cerr << "Failed to get export directory. Error: " << GetLastError() << std::endl;
return nullptr;
}
PDWORD pNames = (PDWORD)((BYTE*)hModule + pExportDir->AddressOfNames);
for (DWORD i = 0; i < pExportDir->NumberOfNames; i++) {
const char* funcName = (const char*)((BYTE*)hModule + pNames[i]);
if (HashFunction(funcName) == funcHash) {
WORD ordinal = ((WORD*)((BYTE*)hModule + pExportDir->AddressOfNameOrdinals))[i];
DWORD funcRVA = ((PDWORD)((BYTE*)hModule + pExportDir->AddressOfFunctions))[ordinal];
return (FARPROC)((BYTE*)hModule + funcRVA);
}
}
return nullptr;
}
// Функция для выполнения функции по хэшу библиотеки и хэшу функции
void ExecuteFunctionByHash(DWORD dllHash, DWORD funcHash) {
HMODULE hModule = GetModuleHandleByHash(dllHash);
if (!hModule) {
std::cerr << "Failed to get module handle by hash. Trying to load dynamically." << std::endl;
// Попробуйте загрузить библиотеку динамически
hModule = LoadLibraryA("user32.dll"); // Имя библиотеки
if (!hModule) {
std::cerr << "Failed to load module dynamically." << std::endl;
return;
}
}
FARPROC funcAddress = GetProcAddressByHash(hModule, funcHash);
if (!funcAddress) {
std::cerr << "Function not found." << std::endl;
return;
}
typedef int (WINAPI* MESSAGEBOXA)(HWND, LPCSTR, LPCSTR, UINT);
MESSAGEBOXA pMessageBoxA = (MESSAGEBOXA)funcAddress;
pMessageBoxA(NULL, "Hello", "Hash Function Call", MB_OK);
}
int ExecFunc(DWORD dllHash, DWORD funcHash) {
// Хэши библиотеки и функции, которые вы хотите использовать
dllHash = 1412361766; // Хэш для "user32.dll"
funcHash = 3159204520; // Хэш для "MessageBoxA"
ExecuteFunctionByHash(dllHash, funcHash);
return 0;
}