Advertisement
VictorCesaroni

SA-MP Tradução Beta

Feb 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define WIN32_LEAN_AND_MEAN
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <Windows.h>
  5. #include <cstdio>
  6. #include <tlhelp32.h>
  7.  
  8. void EnableDebugPriv();
  9. DWORD GetProcessIdByName(LPCWSTR name);
  10. HMODULE GetProcessModuleHandle(DWORD processId, LPCWSTR name);
  11.  
  12. LPVOID AllocString(HANDLE process, const char *string);
  13. BOOL TranslateString(HANDLE process, DWORD module, DWORD opcodeOffset, BYTE opcode, int offset, const char *newString);
  14.  
  15. int main()
  16. {
  17.     ///////////////////////////////////////////////////////////////////////////////////////////////////////
  18.     EnableDebugPriv();
  19.  
  20.     // Wait gta_sa.exe
  21.     DWORD processId = NULL;
  22.     do
  23.     {
  24.         processId = GetProcessIdByName(L"gta_sa.exe");
  25.         Sleep(100);
  26.     } while (processId == NULL);
  27.  
  28.     HANDLE process = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, processId);
  29.  
  30.     // Wait samp.dll to be loaded
  31.     DWORD SampDll = NULL;
  32.     do
  33.     {
  34.         SampDll = (DWORD)GetProcessModuleHandle(processId, L"samp.dll");
  35.         Sleep(100);
  36.     } while (SampDll == NULL);
  37.  
  38.     ///////////////////////////////////////////////////////////////////////////////////////////////////////
  39.  
  40.     // TO DO: usar aobscan
  41.     const int TOTAL_TRANSLATIONS = 3;
  42.     DWORD translateOpcodesOffsets[TOTAL_TRANSLATIONS] =
  43.     {
  44.         0x2647C6,
  45.         0x2EEDAE,
  46.         0x00AE9A,
  47.     };
  48.  
  49.     BYTE translateOpcodes[TOTAL_TRANSLATIONS] =
  50.     {
  51.         0xC7,
  52.         0x68,
  53.         0x68,
  54.     };
  55.  
  56.     BYTE translateAddressOffsets[TOTAL_TRANSLATIONS] =
  57.     {
  58.         3,
  59.         1,
  60.         1,
  61.     };
  62.    
  63.     char translateStrings[TOTAL_TRANSLATIONS][128] =
  64.     {
  65.         "{FFFFFF}SA-MP {00FFAA}0.3.7 {FFFFFF}iniciado e traduzido!",
  66.         "{FFFFFF}Conectando em {00FFAA}%s:%d{FFFFFF}, aguarde um instante...",
  67.         "{FFFFFF}O servidor {FFAAAA}não responde{FFFFFF}. Tentando novamente!"
  68.     };
  69.  
  70.     int result = 0;
  71.  
  72.     for (int i = 0; i < TOTAL_TRANSLATIONS; i++)
  73.     {
  74.         result += TranslateString(process, SampDll, translateOpcodesOffsets[i], translateOpcodes[i], translateAddressOffsets[i], translateStrings[i]);
  75.         printf("\n");
  76.     }
  77.  
  78.     printf("Result: %s (%d)\n", result == TOTAL_TRANSLATIONS ? "sucess" : "error", GetLastError());
  79.  
  80.     // Close Handles
  81.     CloseHandle(process);
  82.  
  83.     getchar();
  84.  
  85.     return 0;
  86. }
  87.  
  88. BOOL TranslateString(HANDLE process, DWORD module, DWORD opcodeOffset, BYTE opcode, int addressOffset, const char *newString)
  89. {
  90.     // Allocate the string in memory
  91.     LPVOID address = AllocString(process, newString);
  92.  
  93.     // Change instruction address
  94.     // module + opcodeOffset = opcode
  95.     // module + opcodeOffset + addressOffset = instruction target address
  96.  
  97.     printf("Translating: 0x%X\n", opcodeOffset);
  98.     printf("%s\n", newString);
  99.     printf("Process Handle: 0x%X\n", process);
  100.     printf("Target Module: 0x%X\n", module);   
  101.     printf("Opcode check: 0x%X\n", opcode);
  102.     printf("Address Offset: %d\n", addressOffset);
  103.     printf("String alloc address: 0x%X\n", address);
  104.     printf("-----------------------------------\n");
  105.  
  106.     BYTE opcodeCheck = 0;
  107.     ReadProcessMemory(process, (LPVOID)(module + opcodeOffset), &opcodeCheck, sizeof(BYTE), NULL);
  108.  
  109.     if (opcodeCheck != opcode)
  110.     {
  111.         printf("[Error] Opcode check failed. (%X)\n\n", opcodeCheck);
  112.         return FALSE;
  113.     }
  114.  
  115.     int result = WriteProcessMemory(process, (LPVOID)(module + opcodeOffset + addressOffset), &address, sizeof(DWORD), NULL);
  116.  
  117.     if (result == 0)
  118.     {
  119.         printf("[Error] WriteProcessMemory failed. (Error: %X)\n\n", GetLastError());
  120.         return FALSE;
  121.     }
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. LPVOID AllocString(HANDLE process, const char *string)
  127. {
  128.     LPVOID address;
  129.  
  130.     unsigned int size = strlen(string);
  131.  
  132.     address = VirtualAllocEx(process, NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  133.     WriteProcessMemory(process, address, string, size, NULL);
  134.  
  135.     return address;
  136. }
  137.  
  138. DWORD GetProcessIdByName(LPCWSTR name)
  139. {
  140.     PROCESSENTRY32 entry;
  141.     entry.dwSize = sizeof(PROCESSENTRY32);
  142.  
  143.     DWORD processId = NULL;
  144.  
  145.     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  146.  
  147.     if (Process32First(snapshot, &entry) == TRUE)
  148.     {
  149.         while (Process32Next(snapshot, &entry) == TRUE)
  150.         {
  151.             if (lstrcmp(entry.szExeFile, name) == 0)
  152.                 processId = entry.th32ProcessID;
  153.         }
  154.     }
  155.  
  156.     CloseHandle(snapshot);
  157.  
  158.     return processId;
  159. }
  160.  
  161. void EnableDebugPriv()
  162. {
  163.     HANDLE hToken;
  164.     LUID luid;
  165.     TOKEN_PRIVILEGES tkp;
  166.  
  167.     OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
  168.  
  169.     LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);
  170.  
  171.     tkp.PrivilegeCount = 1;
  172.     tkp.Privileges[0].Luid = luid;
  173.     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  174.  
  175.     AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);
  176.  
  177.     CloseHandle(hToken);
  178. }
  179.  
  180. HMODULE GetProcessModuleHandle(DWORD processId, LPCWSTR name)
  181. {
  182.     HANDLE hModuleSnapshot;
  183.     MODULEENTRY32 moduleInfo;
  184.     HMODULE hResult;
  185.  
  186.     hModuleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId);
  187.  
  188.     if (hModuleSnapshot == INVALID_HANDLE_VALUE)
  189.         return NULL;
  190.  
  191.     moduleInfo.dwSize = sizeof(MODULEENTRY32);
  192.  
  193.     if (Module32First(hModuleSnapshot, &moduleInfo))
  194.     {
  195.         do
  196.         {
  197.             if (!lstrcmp(name, moduleInfo.szModule))
  198.             {
  199.                 hResult = (HMODULE)moduleInfo.modBaseAddr;
  200.                 break;
  201.             }
  202.         } while (Module32Next(hModuleSnapshot, &moduleInfo));
  203.     }
  204.     if (!hResult) SetLastError(126);
  205.  
  206.     CloseHandle(hModuleSnapshot);
  207.  
  208.     return hResult;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement