Created
October 12, 2021 04:56
-
-
Save herrcore/acac434e2015714373f807f3024455bf to your computer and use it in GitHub Desktop.
Code examples from https://nickharbour.wordpress.com/2010/07/01/writing-shellcode-with-a-c-compiler/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ref: Writing Shellcode with a C Compiler (https://nickharbour.wordpress.com/) | |
PPEB __declspec(naked) get_peb(void) | |
{ | |
__asm { | |
mov eax, fs:[0x30] | |
ret | |
} | |
} | |
HMODULE __stdcall ResolveModuleHash(DWORD hash) | |
{ | |
PPEB peb; | |
LDR_DATA_TABLE_ENTRY *module_ptr, *first_mod; | |
peb = get_peb(); | |
module_ptr = (PLDR_DATA_TABLE_ENTRY)peb->Ldr->InMemoryOrderModuleList.Flink; | |
first_mod = module_ptr; | |
do { | |
if (hash_algorithm((WCHAR *)module_ptr->FullDllName.Buffer) == hash) | |
return (HMODULE)module_ptr->Reserved2[0]; | |
else | |
module_ptr = (PLDR_DATA_TABLE_ENTRY)module_ptr->Reserved1[0]; | |
} while (module_ptr && module_ptr != first_mod); // because the list wraps, | |
return INVALID_HANDLE_VALUE; | |
} | |
FARPROC __stdcall ResolveHash(DWORD moduleHash, DWORD functionHash) | |
{ | |
IMAGE_DOS_HEADER *dos_header; | |
IMAGE_NT_HEADERS *nt_headers; | |
IMAGE_EXPORT_DIRECTORY *export_dir; | |
DWORD *names, *funcs; | |
WORD *nameords; | |
int i; | |
HMODULE module; | |
module = ResolveModuleHash(moduleHash); | |
dos_header = (IMAGE_DOS_HEADER *)module; | |
nt_headers = (IMAGE_NT_HEADERS *)((char *)module + dos_header->e_lfanew); | |
export_dir = (IMAGE_EXPORT_DIRECTORY *)((char *)module + nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress); | |
names = (DWORD *)((char *)module + export_dir->AddressOfNames); | |
funcs = (DWORD *)((char *)module + export_dir->AddressOfFunctions); | |
nameords = (WORD *)((char *)module + export_dir->AddressOfNameOrdinals); | |
for (i = 0; i < export_dir->NumberOfNames; i++) | |
{ | |
char *string = (char *)module + names[i]; | |
if (functionHash == hash_algorithm(string)) | |
{ | |
WORD nameord = nameords[i]; | |
DWORD funcrva = funcs[nameord]; | |
return (FARPROC)((char *)module + funcrva); | |
} | |
} | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment