OldSchoolHack

Register / Login English

Brauche mal Hilfe

icon Thread: Brauche mal Hilfe

Join Date: Apr 2013

Posts: 22

Ich habe mal eine dll erstellt, die als Schnittstelle zwischen dem Opferprogramm und meiner Anwedung dient.
Das hatte mal funktioniert aber nun gehts wieder nichtmehr -.-
Ich habe eigentlich nix daran verändert..

Und nebenbei vielleicht ein paar Tipps, was man sauberer machen kann
wäre nett von euch ^^

CPP Code:
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <Windows.h>
  4. #include <tlhelp32.h>
  5. #include <wchar.h>
  6. #include <cstdarg>
  7. #include <cstdio>
  8.  
  9. #define EXPORT extern "C" __declspec(dllexport)
  10.  
  11. //STRUCTS
  12.  
  13. struct sCreateDialog
  14. {
  15.     int style;
  16.     char titel[65];
  17.     char text[1000];
  18.     char button1[65];
  19.     char button2[65];
  20. };
  21.  
  22.  
  23.  
  24. //GLOBALS
  25. HANDLE g_hMod = NULL;
  26. HANDLE g_hGTA = NULL;
  27. DWORD g_dwGTAPID = 0;
  28.  
  29.  
  30. //EXPORTS
  31. EXPORT BOOL CreateDialog(int,char*,char*,char*,char*);  //style,titel,text,button1,button2
  32. EXPORT void CreateDialogEx(LPVOID);
  33.  
  34. //INTERNE FUNKTIONEN
  35. BOOL ReadProcessMemoryPointer(HANDLE, LPCVOID, LPVOID,SIZE_T,SIZE_T *, int, ...);
  36. BOOL WriteProcessMemoryPointer(HANDLE, LPVOID, LPCVOID,SIZE_T,SIZE_T *, int, ...);
  37. HANDLE getProcHandle();
  38. BOOL checkInjected();
  39. BOOL Inject();
  40. HANDLE getSampBase();
  41. DWORD getPID();
  42.  
  43.  
  44.  
  45.  
  46.  
  47. //FUNKTIONEN
  48.  
  49.  
  50.  
  51. EXPORT BOOL CreateDialog(int style, char* titel, char* text, char* button1, char* button2)
  52. {
  53.     if(!titel || !text || !button1 || !button2)
  54.         return FALSE;
  55.     if(style<0 || style>3 || strlen(titel)>64 || strlen(text)>999 || strlen(button1)>64 || strlen(button2)>64)
  56.         return FALSE;
  57.  
  58.     sCreateDialog s;
  59.     s.style = style;
  60.     strcpy_s(s.titel,titel);
  61.     strcpy_s(s.text,text);
  62.     strcpy_s(s.button1,button1);
  63.     strcpy_s(s.button2,button2);
  64.  
  65.     if(!checkInjected())
  66.         return FALSE;
  67.     HANDLE hProc = getProcHandle();
  68.     if(!hProc)
  69.         return FALSE;
  70.     LPVOID lpAddr = VirtualAllocEx(hProc, 0, sizeof(sCreateDialog), MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  71.     if(!lpAddr)
  72.     {
  73.         return FALSE;
  74.     }
  75.     BOOL bSuccess = WriteProcessMemory(hProc, lpAddr, (LPVOID)&s, sizeof(sCreateDialog), 0);
  76.     if(!bSuccess)
  77.     {
  78.         return FALSE;
  79.     }
  80.     HANDLE hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)GetProcAddress((HMODULE)g_hMod,"CreateDialogEx"), lpAddr, 0, 0);
  81.     if(!hThread)
  82.     {
  83.         return FALSE;
  84.     }
  85.     WaitForSingleObject(hThread,INFINITE);
  86.     CloseHandle(hThread);
  87.     VirtualFreeEx(hProc,lpAddr,sizeof(sCreateDialog),MEM_RELEASE);
  88.  
  89.     return TRUE;
  90. }
  91.  
  92. EXPORT void CreateDialogEx(LPVOID msg)
  93. {
  94.     MessageBox(0,L"CreateDialogEx",0,MB_OK);
  95.     if(!msg)
  96.         return;
  97.     sCreateDialog* sptr = (sCreateDialog*)msg;
  98.     int style = sptr->style;
  99.     char* titel = sptr->titel;
  100.     char* text = sptr->text;
  101.     char* button1 = sptr->button1;
  102.     char* button2 = sptr->button2;
  103.  
  104.     DWORD addr = (DWORD)GetModuleHandle(L"samp.dll");
  105.     if(!addr)
  106.         return;
  107.  
  108.     DWORD dialogptr = addr + 0x2129F8;
  109.     DWORD func = addr + 0x806F0;
  110.     __asm
  111.     {
  112.         mov eax, dword ptr[dialogptr]
  113.         mov ecx, dword ptr[eax]
  114.         push 0
  115.         push button2
  116.         push button1
  117.         push text
  118.         push titel
  119.         push style;
  120.         push 0
  121.         call func
  122.     }
  123. }
  124.  
  125. //////////////////////////////////////////////////////////
  126.  
  127. BOOL ReadProcessMemoryPointer(HANDLE hProcess, LPCVOID lpBaseAddress, LPVOID lpBuffer,SIZE_T nSize,SIZE_T *lpNumberOfBytesRead, int nOffsets, ...)
  128. {
  129.     if(nOffsets == 0)
  130.         return ReadProcessMemory(hProcess,lpBaseAddress,lpBuffer,nSize,lpNumberOfBytesRead);
  131.  
  132.     va_list arguments;
  133.     DWORD ptr = (DWORD)lpBaseAddress;
  134.  
  135.     va_start(arguments, nOffsets);
  136.  
  137.     for(int i=0;i<nOffsets;i++)
  138.     {
  139.         if( !ReadProcessMemory(hProcess,(LPVOID)ptr,&ptr,4,0) )
  140.         {
  141.             va_end(arguments);
  142.             return FALSE;
  143.         }
  144.         ptr += va_arg(arguments,DWORD);
  145.     }
  146.     va_end(arguments);
  147.     return ReadProcessMemory(hProcess,(LPVOID)ptr,lpBuffer,nSize,lpNumberOfBytesRead);
  148. }
  149.  
  150. BOOL WriteProcessMemoryPointer(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer,SIZE_T nSize,SIZE_T *lpNumberOfBytesWritten, int nOffsets, ...)
  151. {
  152.     if(nOffsets == 0)
  153.         return WriteProcessMemory(hProcess,lpBaseAddress,lpBuffer,nSize,lpNumberOfBytesWritten);
  154.     
  155.     va_list arguments;
  156.     DWORD ptr = (DWORD)lpBaseAddress;
  157.  
  158.     va_start(arguments, nOffsets);
  159.  
  160.     for(int i=0;i<nOffsets;i++)
  161.     {
  162.         if( !ReadProcessMemory(hProcess,(LPVOID)ptr,&ptr,4,0) )
  163.         {
  164.             va_end(arguments);
  165.             return FALSE;
  166.         }
  167.         ptr += va_arg(arguments,DWORD);
  168.     }
  169.     va_end(arguments);
  170.     return WriteProcessMemory(hProcess,(LPVOID)ptr,lpBuffer,nSize,lpNumberOfBytesWritten);
  171. }
  172.  
  173. DWORD getPID()
  174. {
  175.     DWORD pid;
  176.     HWND hwnd = FindWindow(0,L"GTA:SA:MP");
  177.     if(!hwnd)
  178.         return 0;
  179.     GetWindowThreadProcessId(hwnd,&pid);
  180.     if(!pid)
  181.         return 0;
  182.     return pid;
  183. }
  184.  
  185.  
  186. HANDLE getProcHandle()
  187. {
  188.     DWORD pid = getPID();
  189.     if(!pid)
  190.     {
  191.         if(g_hGTA) //kein prozess handle aber offen
  192.         {
  193.             CloseHandle(g_hGTA);
  194.             g_hGTA = NULL;
  195.             return NULL;
  196.         }
  197.         return NULL;
  198.     }
  199.     if(pid != g_dwGTAPID) //neuer Prozess
  200.     {
  201.         if(g_hGTA) // Handle noch offen
  202.         {
  203.             CloseHandle(g_hGTA);
  204.             HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
  205.             g_hGTA = hProc;
  206.             return hProc;
  207.         }
  208.         else //beim starten oder so
  209.         {
  210.             HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
  211.             g_hGTA = hProc;
  212.             return hProc;
  213.         }
  214.     }
  215.     return g_hGTA;
  216. }
  217.  
  218.  
  219. HANDLE getSampBase()
  220. {
  221.     DWORD dwPID = getPID();
  222.     if(!dwPID)
  223.         return NULL;
  224.  
  225.     HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  226.     MODULEENTRY32 me32;
  227.     
  228.     // Take a snapshot of all modules in the specified process.
  229.     hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
  230.     if( hModuleSnap == INVALID_HANDLE_VALUE )
  231.     {
  232.         return( 0 );
  233.     }
  234.  
  235.     // Set the size of the structure before using it.
  236.     me32.dwSize = sizeof( MODULEENTRY32 );
  237.  
  238.     // Retrieve information about the first module,
  239.     // and exit if unsuccessful
  240.     if( !Module32First( hModuleSnap, &me32 ) )
  241.     {
  242.         CloseHandle( hModuleSnap );           // clean the snapshot object
  243.         return( 0 );
  244.     }
  245.  
  246.     // Now walk the module list of the process,
  247.     // and display information about each module
  248.     do
  249.     {
  250.         if(wcsstr(me32.szModule,L"samp.dll"))
  251.         {
  252.             CloseHandle( hModuleSnap );
  253.             return me32.hModule;
  254.         }
  255.     } while( Module32Next( hModuleSnap, &me32 ) );
  256.  
  257.     CloseHandle( hModuleSnap );
  258.     return( 0 );
  259. }
  260.  
  261. BOOL checkInjected()
  262. {
  263.     DWORD dwPID = getPID();
  264.     if(!dwPID)
  265.         return FALSE;
  266.  
  267.     WCHAR DllPath[MAX_PATH];
  268.     GetModuleFileName((HMODULE)g_hMod,DllPath,MAX_PATH);
  269.     WCHAR DllName[100];
  270.     _wsplitpath_s(DllPath,0,0,0,0,DllName,100,0,0);
  271.  
  272.     HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  273.     MODULEENTRY32 me32;
  274.     
  275.     // Take a snapshot of all modules in the specified process.
  276.     hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
  277.     if( hModuleSnap == INVALID_HANDLE_VALUE )
  278.     {
  279.         return( FALSE );
  280.     }
  281.  
  282.     // Set the size of the structure before using it.
  283.     me32.dwSize = sizeof( MODULEENTRY32 );
  284.  
  285.     // Retrieve information about the first module,
  286.     // and exit if unsuccessful
  287.     if( !Module32First( hModuleSnap, &me32 ) )
  288.     {
  289.         CloseHandle( hModuleSnap );           // clean the snapshot object
  290.         return( FALSE );
  291.     }
  292.  
  293.     // Now walk the module list of the process,
  294.     // and display information about each module
  295.     do
  296.     {
  297.         if(wcsstr(me32.szModule,DllName))
  298.         {
  299.             CloseHandle( hModuleSnap );
  300.             return TRUE;
  301.         }
  302.     } while( Module32Next( hModuleSnap, &me32 ) );
  303.  
  304.     CloseHandle( hModuleSnap );
  305.     return( Inject() );
  306. }
  307.  
  308.  
  309.  
  310. BOOL Inject()
  311. {
  312.     WCHAR DllPath[MAX_PATH];
  313.     GetModuleFileName((HMODULE)g_hMod,DllPath,MAX_PATH);
  314.     DWORD dwSize = (wcslen(DllPath)+1)*2;
  315.     HANDLE hProc = getProcHandle();
  316.     if(!hProc)
  317.         return FALSE;
  318.     LPVOID lpAddr = VirtualAllocEx(hProc, 0, dwSize, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  319.     if(!lpAddr)
  320.     {
  321.         return FALSE;
  322.     }
  323.     BOOL bSuccess = WriteProcessMemory(hProc, lpAddr, DllPath, dwSize, 0);
  324.     if(!bSuccess)
  325.     {
  326.         return FALSE;
  327.     }
  328.     HANDLE hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(L"kernel32.dll"),"LoadLibraryW"), lpAddr, 0, 0);
  329.     if(!hThread)
  330.     {
  331.         return FALSE;
  332.     }
  333.     WaitForSingleObject(hThread,INFINITE);
  334.     CloseHandle(hThread);
  335.     VirtualFreeEx(hProc,lpAddr,dwSize,MEM_RELEASE);
  336.     return TRUE;
  337. }
  338.  
  339.  
  340.  
  341.  
  342. BOOL APIENTRY DllMain( HANDLE hModule,
  343.                        DWORD  ul_reason_for_call,
  344.                        LPVOID lpReserved)
  345. {
  346.     if(ul_reason_for_call==DLL_PROCESS_ATTACH)
  347.     {
  348.     g_hMod = hModule;
  349.     DisableThreadLibraryCalls((HMODULE)hModule);
  350.     }
  351.     return TRUE;
  352. }