captmicro

Unknown

Oct 9th, 2010
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include "functions.h"
  2.  
  3. USHORT xor_ror_hash(CHAR *str)
  4. {
  5.     CHAR *strptr = str;
  6.     USHORT hash = 0;
  7.     while (*strptr != NULL)
  8.     {
  9.         hash ^= *(USHORT*)strptr;
  10.         hash >>= 1;
  11.         strptr++;
  12.     }
  13.     return hash;
  14. }
  15.  
  16. DWORD _declspec(naked) PEB_kernel32base()
  17. {
  18.     __asm
  19.     {
  20.         push ESI
  21.         MOV ESI, FS:[0x30] //PEB base
  22.         MOV ESI, [ESI + 0x0C] //PEB->Ldr
  23.         MOV ESI, [ESI + 0x1C] //PEB->Ldr.InInitOrder
  24. _kernel32base_next_module:
  25.         CMP [ESI + 0x20 + 0x18], 0 //module_name[12] == 0? (unicode)
  26.         JE _kernel32base_found_module //found
  27.         MOV ESI, [ESI] //InInitOrder.flink (next module)
  28.         JMP _kernel32base_next_module //loop
  29. _kernel32base_found_module:
  30.         MOV EAX, [ESI + 0x08] //InInitOrder.base_address
  31.         pop ESI
  32.         RET
  33.     }
  34. }
  35.  
  36. BYTE _declspec(naked) PEB_beingdebugged()
  37. {
  38.     __asm
  39.     {
  40.         MOV EAX, FS:[0x30]
  41.         MOV EAX, [EAX + 0x02]
  42.         RET
  43.     }
  44. }
  45.  
  46. DWORD getfuncaddress(DWORD mbase, CHAR *funcname)
  47. {
  48.     if (mbase == NULL) mbase = PEB_kernel32base();
  49.     if (mbase == NULL) return NULL;
  50.  
  51.     USHORT funcnamehash = xor_ror_hash(funcname);
  52.     DWORD address; address = NULL;
  53.     DWORD name_pointer; name_pointer = NULL;
  54.     DWORD address_pointer; address_pointer = NULL;
  55.     WORD ord_pointer; ord_pointer = NULL;
  56.  
  57.     IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER*)mbase;
  58.     if (dos->e_magic != IMAGE_DOS_SIGNATURE) return 0;
  59.  
  60.     IMAGE_NT_HEADERS *nt = (IMAGE_NT_HEADERS*)((DWORD)dos + dos->e_lfanew);
  61.     if (nt->Signature != IMAGE_NT_SIGNATURE) return 0;
  62.  
  63.     IMAGE_EXPORT_DIRECTORY *exportdir = (IMAGE_EXPORT_DIRECTORY*)((DWORD)dos +
  64.         nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  65.  
  66.     DWORD i; i = 0;
  67.     CHAR *name; name = NULL;
  68.     DWORD *name_ptr = (DWORD*)((DWORD)dos + exportdir->AddressOfNames);
  69.     WORD *ord_ptr = (WORD*)((DWORD)dos + exportdir->AddressOfNameOrdinals);
  70.     DWORD *addr_ptr = (DWORD*)((DWORD)dos + exportdir->AddressOfFunctions);
  71.  
  72.     for (i = 0; i < exportdir->NumberOfNames; i++)
  73.     {
  74.         name = (CHAR*)((DWORD)dos + name_ptr[i]);
  75.         if (xor_ror_hash(name) == funcnamehash) break;
  76.     }
  77.     if (i == exportdir->NumberOfNames) return 0;
  78.  
  79.     DWORD addr = (DWORD)dos + addr_ptr[ord_ptr[i]];
  80.     return addr;
  81. }
  82.  
  83. DWORD kernel32base = PEB_kernel32base();
  84. MUD_HeapCreate = (_HeapCreate)getfuncaddress(kernel32base, "HeapCreate");
  85. MUD_HeapDestroy = (_HeapDestroy)getfuncaddress(kernel32base, "HeapDestroy");
  86. MUD_HeapAlloc = (_HeapAlloc)getfuncaddress(kernel32base, "HeapAlloc");
  87. MUD_HeapReAlloc = (_HeapReAlloc)getfuncaddress(kernel32base, "HeapReAlloc");
  88. MUD_HeapFree = (_HeapFree)getfuncaddress(kernel32base, "HeapFree");
  89. MUD_HeapSize = (_HeapSize)getfuncaddress(kernel32base, "HeapSize");
  90. MUD_GetProcessHeap = (_GetProcessHeap)getfuncaddress(kernel32base, "GetProcessHeap");
Add Comment
Please, Sign In to add comment