Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <limits.h>
- #include <sys/uio.h>
- #include <sys/mman.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <dlfcn.h> // dlopen
- #include <link.h> // link_map
- #include <iostream>
- using namespace std;
- class ExecutableImage{
- public:
- char Signiture[1];
- bool IsPeImage(){
- #if defined (__unix__)
- return Signiture[0] == '\x7F'
- && Signiture[1] == 'E'
- && Signiture[2] == 'L'
- && Signiture[3] == 'F';
- #elif defined (_WIN64) || defined (_WIN32)
- return Signiture[0] == 'P'
- && Signiture[1] == 'E'
- && Signiture[2] == '\x00';
- #endif
- }
- };
- // pop eax, jmp eax
- // pop rax, jmp rax
- unsigned char GetEip[] = { 0x58, 0xFF, 0xE0 };
- char * GetEIP(){
- #if defined (__unix__)
- void *buf = mmap(0, 3, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);
- memcpy(buf, GetEip, 3);
- char * (*fnGetEip)() = (char *(*)())(buf);
- #elif defined (_WIN64) || defined (_WIN32)
- DWORD dwJunk;
- VirtualProtect(GetEip, 3, PAGE_EXECUTE_READWRITE, &dwJunk);
- char * (*fnGetEip)() = (char *(*)())(&GetEip[0]);
- #endif
- return fnGetEip();
- }
- void * GetThreadModule(){
- char * pEip = GetEIP();
- for (int i = 0; i < 4096; i++){
- ExecutableImage * pImage = (ExecutableImage*)((char*)pEip - i);
- if (pImage->IsPeImage()){
- return (ExecutableImage*)((unsigned long long)pImage - (unsigned long long)pImage % 0x100); // executable
- }
- }
- return nullptr;
- }
- int main(int argc, char** argv)
- {
- cout << GetThreadModule() << endl;
- while (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement