Advertisement
Guest User

Untitled

a guest
Apr 1st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <stdio.h>
  3.  
  4. void _log(UINT64 data) {
  5.     char text[200];
  6.  
  7.     sprintf_s(text, "%p", data);
  8.  
  9.     MessageBoxA(0, text, "123", 0);
  10. }
  11.  
  12. typedef struct _UNICODE_STRING_WOW64 {
  13.     USHORT Length;
  14.     USHORT MaximumLength;
  15.     PVOID64 Buffer;
  16. } UNICODE_STRING;
  17.  
  18. typedef struct {
  19.     LIST_ENTRY Orders[3];
  20.     PVOID64    base;
  21.     PVOID64      EntryPoint;
  22.     UINT       Size;
  23.     UNICODE_STRING dllFullPath;
  24.     UNICODE_STRING dllname;
  25. } LDR_ENTRY;
  26.  
  27. void _logStr(CHAR* data) {
  28.     char text[200];
  29.  
  30.     sprintf_s(text, "%s", data);
  31.  
  32.     MessageBoxA(0, text, "123", 0);
  33. }
  34.  
  35. PVOID64 GetModuleBase(LPWSTR moduleName) {
  36.     UINT64 peb = (UINT64)__readgsqword(0x60);
  37.     UINT64 moduleListAddr = *(UINT64*)(peb + 0x18);
  38.     PVOID64 start = *(PVOID64*)(moduleListAddr + 0x18);
  39.  
  40.     LDR_ENTRY *mod = (LDR_ENTRY*)start;
  41.     mod = (LDR_ENTRY*)mod->Orders[0].Flink;
  42.  
  43.     while ((UINT64)start != (UINT64)mod) {
  44.  
  45.         if (mod->base != NULL)
  46.         {
  47.             if (!lstrcmpiW((LPCWSTR)mod->dllname.Buffer, moduleName))
  48.             {
  49.                 return mod->base;
  50.             }
  51.         }
  52.  
  53.         mod = (LDR_ENTRY*)mod->Orders[0].Flink;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. UINT64 GetFunction(UINT64 base, LPCSTR function) {
  60.  
  61.     IMAGE_DOS_HEADER *dosHeader = (IMAGE_DOS_HEADER*)base;
  62.     IMAGE_NT_HEADERS64 *ntHeaders = (IMAGE_NT_HEADERS64*)(base + dosHeader->e_lfanew);
  63.  
  64.     IMAGE_EXPORT_DIRECTORY *exportTable =
  65.         (IMAGE_EXPORT_DIRECTORY*)(base + ntHeaders->OptionalHeader.DataDirectory[0].VirtualAddress);
  66.  
  67.     DWORD* functions = (DWORD*)(base + exportTable->AddressOfFunctions);
  68.     WORD* ords = (WORD*)(base + exportTable->AddressOfNameOrdinals);
  69.     DWORD* names = (DWORD*)(base + exportTable->AddressOfNames);
  70.  
  71.     for (int i = 0; i < exportTable->NumberOfNames; i++) {
  72.         char* data = (char*)(base + (UINT64)names[i]);
  73.        
  74.         if (lstrcmpA(function, data) == 0) {
  75.             return base + (UINT64)functions[ords[i]];
  76.         }
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
  82. int main() {
  83.  
  84.     UINT64 p = (UINT64)GetModuleBase((LPWSTR)L"ntdll.dll");
  85.     UINT64 func = GetFunction(p, "NtCreateThreadEx");
  86.  
  87.     printf("%p\n", func);
  88.  
  89.     system("pause");
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement