Advertisement
can1456

Untitled

Oct 18th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/wait.h>
  4. #include <limits.h>
  5. #include <sys/uio.h>
  6. #include <sys/mman.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. #include <dlfcn.h> // dlopen
  13. #include <link.h> // link_map
  14.  
  15. #include <iostream>
  16.  
  17. using namespace std;
  18.  
  19.  
  20.  
  21. class ExecutableImage{
  22. public:
  23.     char Signiture[1];
  24.     bool IsPeImage(){
  25.  
  26. #if defined (__unix__)
  27.         return Signiture[0] == '\x7F'
  28.             && Signiture[1] == 'E'
  29.             && Signiture[2] == 'L'
  30.             && Signiture[3] == 'F';
  31. #elif defined (_WIN64) || defined (_WIN32)
  32.         return Signiture[0] == 'P'
  33.             && Signiture[1] == 'E'
  34.             && Signiture[2] == '\x00';
  35. #endif
  36.  
  37.     }
  38. };
  39. // pop eax, jmp eax
  40. // pop rax, jmp rax
  41. unsigned char GetEip[] = { 0x58, 0xFF, 0xE0 };
  42.  
  43. char * GetEIP(){
  44. #if defined (__unix__)
  45.     void *buf = mmap(0, 3, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);
  46.     memcpy(buf, GetEip, 3);
  47.     char * (*fnGetEip)() = (char *(*)())(buf);
  48. #elif defined (_WIN64) || defined (_WIN32)
  49.     DWORD dwJunk;
  50.     VirtualProtect(GetEip, 3, PAGE_EXECUTE_READWRITE, &dwJunk);
  51.     char * (*fnGetEip)() = (char *(*)())(&GetEip[0]);
  52. #endif
  53.     return fnGetEip();
  54. }
  55.  
  56. void * GetThreadModule(){
  57.  
  58.  
  59.     char * pEip = GetEIP();
  60.  
  61.     for (int i = 0; i < 4096; i++){
  62.         ExecutableImage * pImage = (ExecutableImage*)((char*)pEip - i);
  63.         if (pImage->IsPeImage()){
  64.             return (ExecutableImage*)((unsigned long long)pImage - (unsigned long long)pImage % 0x100); // executable
  65.         }
  66.     }
  67.  
  68.     return nullptr;
  69. }
  70.  
  71.  
  72.  
  73. int main(int argc, char** argv)
  74. {
  75.     cout << GetThreadModule() << endl;
  76.     while (1);
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement