MCPcopy Create free account
hub / github.com/d35ha/CallObfuscator / get_symbol_ptr

Method get_symbol_ptr

src/shellcode.cpp:353–464  ·  view source on GitHub ↗

Get the dll base by name.

Source from the content-addressed store, hash-verified

351
352// Get the dll base by name.
353PVOID shellcode::get_symbol_ptr(HANDLE pe_base, PDWORD sh_funs, HANDLE dll_handle, u_sym_info sym_info, BOOL by_name)
354{
355 // Resolving the needed functions.
356 auto fun_hash_string = sh_resolve(pe_base, sh_funs, hash_string);
357 auto fun_str_cpy = sh_resolve(pe_base, sh_funs, str_cpy);
358 auto fun_str_chr = sh_resolve(pe_base, sh_funs, str_chr);
359 auto fun_load_dll = sh_resolve(pe_base, sh_funs, load_dll);
360 auto fun_get_symbol_ptr = sh_resolve(pe_base, sh_funs, get_symbol_ptr);
361 auto fun_str_toi = sh_resolve(pe_base, sh_funs, str_toi);
362
363 // Dereferencing the headers.
364 PIMAGE_DOS_HEADER dos_hdr = (PIMAGE_DOS_HEADER)dll_handle;
365 PIMAGE_NT_HEADERS nt_hdrs = (PIMAGE_NT_HEADERS)((PBYTE)dll_handle + dos_hdr->e_lfanew);
366 PIMAGE_OPTIONAL_HEADER opt_hdr = &nt_hdrs->OptionalHeader;
367
368 // Dereferencing the export table data directory.
369 PIMAGE_DATA_DIRECTORY data_dir = &opt_hdr->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
370
371 // Check if exists.
372 if (!data_dir->VirtualAddress || !data_dir->Size) {
373 return NULL;
374 };
375
376 // Get the export table pointer.
377 PIMAGE_EXPORT_DIRECTORY export_table = (PIMAGE_EXPORT_DIRECTORY)((PBYTE)dll_handle +
378 data_dir->VirtualAddress);
379
380 // In case an ordinal is resolved.
381 DWORD f_address;
382 if (!by_name)
383 {
384 // Invalid ordinal.
385 if (sym_info.sym_ord < export_table->Base ||
386 sym_info.sym_ord >= export_table->Base + export_table->NumberOfFunctions)
387 return NULL;
388
389 // Getting the address directly.
390 f_address = ((DWORD*)((PBYTE)dll_handle + export_table->AddressOfFunctions))
391 [sym_info.sym_ord - export_table->Base];
392 }
393 else
394 {
395 // My be no names exported.
396 if (!export_table->AddressOfNames) {
397 return NULL;
398 };
399
400 // Resolve by name.
401 size_t idx = 0;
402 DWORD* p_names = (DWORD*)((PBYTE)dll_handle +
403 export_table->AddressOfNames);
404 for (; idx < export_table->NumberOfNames; idx++)
405 {
406 // Get the name.
407 PCHAR sym_name = (PCHAR)dll_handle + p_names[idx];
408 if (fun_hash_string(sym_name) != sym_info.sym_hash)
409 continue;
410

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected