Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.07 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <TlHelp32.h>
  5.  
  6. #include "resource.h"
  7.  
  8. CHAR    szMultipartString[]     = {0x2E, 0x7A, 0x69, 0x70, 0x2E, 0x30, 0x3F, 0x3F, 0x3C, 0x2F, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3E};
  9. CHAR    mkMultipartString[]     = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  10. CHAR    szSinglepartString[]    = {0x2E, 0x65, 0x78, 0x65, 0x3C, 0x2F, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3E};
  11. CHAR    mkSinglepartString[]    = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  12.  
  13. CHAR    szServer[]              = {0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x62, 0x69, 0x6E, 0x73, 0x2E, 0x62, 0x69, 0x67, 0x66, 0x69, 0x73, 0x68, 0x67, 0x61, 0x6D, 0x65, 0x73, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x64, 0x6F, 0x77, 0x6E, 0x6C, 0x6F, 0x61, 0x64, 0x73, 0x2F};
  14. CHAR    szServerMask[]          = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  15.  
  16. UINT Search( PBYTE lpTargetAddress,
  17.             PBYTE lpSearchPattern,
  18.             PBYTE lpSearchMask,
  19.             UINT cbPatternSize,
  20.             UINT cbSearchSize,
  21.             BOOL bOffset )
  22. {
  23.     ULONG ReturnValue = 0;                                       // Set return value to 0
  24.     UINT i = 0;                                                  // Set counter 'i' to 0
  25.     UINT j = 0;                                                  // Set counter 'j' to 0
  26.  
  27.     do                                                           // Do {STEPS} while {CONDITION == TRUE}
  28.     {
  29.         j = 0;                                                   // In loop, set counter 'j' to 0;
  30.  
  31.         do                                                       // Do {STEPS} while {CONDITION == TRUE}
  32.         {
  33.             if ( lpSearchMask[j] == 0 )                          // If byte is 0x00, check byte, else skip
  34.             {
  35.                 if ( lpTargetAddress[i+j] != lpSearchPattern[j] )// If current byte isn't a match, break loop
  36.                 {
  37.                     break;                                       // Break loop
  38.                 }
  39.             }
  40.  
  41.             j++;                                                 // Else, check next mask byte in pattern
  42.  
  43.         } while ( j < cbPatternSize );                           // CONDITION: While counter 'j' is less than pattern size
  44.  
  45.         if (j ==cbPatternSize )
  46.         {
  47.             ReturnValue = i;                                     // If it makes it here, we've got a match
  48.  
  49.             if ( !bOffset )
  50.             {
  51.                 ReturnValue += ( ( UINT ) lpTargetAddress );     // Add lpTargetAddress to return value
  52.             }
  53.  
  54.             break;                                               // Break loop          
  55.         }
  56.  
  57.         i++;                                                     // Next byte in search buffer
  58.  
  59.     } while ( i <= cbSearchSize - cbPatternSize );               // CONDITION: While we havent searched the entire search range
  60.  
  61.     return ReturnValue;                                          // Return result
  62. }
  63.  
  64. HANDLE OpenGameManager()
  65. {
  66.     HANDLE hSnapshot = NULL;
  67.     HANDLE hTemp = NULL;
  68.     PROCESSENTRY32 pe32 = {0};
  69.    
  70.     // Attempt to create the snapshot of all processes
  71.     hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );
  72.    
  73.     // Check result
  74.     if ( hSnapshot == INVALID_HANDLE_VALUE )
  75.     {
  76.         // Notify of failure to create snapshot
  77.         return NULL;
  78.     }
  79.    
  80.     // Set member to size of structure to prevent failure
  81.     pe32.dwSize = sizeof ( pe32 );
  82.  
  83.     // Attempt to begin iterating the processes found in the snapshot
  84.     if ( !Process32First( hSnapshot, &pe32 ) )
  85.     {
  86.         // Close our snapshot handle
  87.         CloseHandle( hSnapshot );
  88.  
  89.         // Notify of failure with Process32First
  90.         return NULL;
  91.     }
  92.  
  93.     // Enter enumeration loop, checking original results before calling Process32Next until either process
  94.     // is found or Process32Next returns FALSE
  95.  
  96.     do
  97.     {
  98.         // String compare the szExeFile member with "bfgclient.exe"
  99.         if ( !lstrcmp( pe32.szExeFile, "bfgclient.exe" ) )
  100.         {
  101.             // Found a hit, continue checking this process
  102.            
  103.             // Close handle to snapshot
  104.             CloseHandle( hSnapshot );
  105.            
  106.             // Attempt to open process with basic read privileges
  107.             hTemp = OpenProcess( PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID );
  108.  
  109.             if ( !hTemp )
  110.             {
  111.                 // Failed to open process for basic read privileges, found but not accessible
  112.                 // Notify of failure
  113.                 return NULL;
  114.             }
  115.             else
  116.             {
  117.                 // Successfully opened handle, we can read from this process
  118.                 return hTemp;
  119.             }
  120.  
  121.         }
  122.  
  123.     } while ( Process32Next( hSnapshot, &pe32 ) );
  124.  
  125.  
  126.     CloseHandle( hSnapshot );
  127.     // Didn't find the process, notify and return
  128.     return NULL;
  129. }
  130.  
  131. BOOL FindMemorySection ( HANDLE hProcess, MEMORY_BASIC_INFORMATION *pRegion, BOOL *IsMultipart )
  132. {
  133.     MEMORY_BASIC_INFORMATION TempMBI = {0};
  134.     UINT dwCurrentBase = 0;
  135.     BYTE *pMemory = 0;
  136.     UINT flOldProtect = 0;
  137.     BOOL bReadOK = FALSE;
  138.  
  139.     for ( dwCurrentBase = 0; dwCurrentBase < 0x7FFFFFFF; dwCurrentBase += TempMBI.RegionSize )
  140.     {
  141.         if ( VirtualQueryEx( hProcess, (LPCVOID)dwCurrentBase, &TempMBI, sizeof ( TempMBI ) ) )
  142.         {
  143.             if ( TempMBI.State == MEM_COMMIT )
  144.             {
  145.                 pMemory = LocalAlloc( LMEM_ZEROINIT, TempMBI.RegionSize );
  146.  
  147.                 if ( !pMemory )
  148.                 {
  149.                     //  Notify of failure to allocate memory
  150.                     return FALSE;
  151.                 }
  152.                 if ( VirtualProtectEx( hProcess, TempMBI.BaseAddress, TempMBI.RegionSize, PAGE_READWRITE, &flOldProtect ))
  153.                 {
  154.                     bReadOK = ReadProcessMemory( hProcess, TempMBI.BaseAddress, pMemory, TempMBI.RegionSize, NULL );
  155.                
  156.                     VirtualProtectEx( hProcess, TempMBI.BaseAddress, TempMBI.RegionSize, flOldProtect, &flOldProtect );
  157.                 }
  158.                
  159.                
  160.                 if ( bReadOK )
  161.                 {
  162.                     // Search through the memory now to see if we have a match for either of the search strings
  163.                     if ( Search( pMemory, szMultipartString, mkMultipartString, sizeof (szMultipartString), TempMBI.RegionSize, TRUE ) )
  164.                     {
  165.                         *IsMultipart = TRUE;
  166.  
  167.                         // Copy across MBI information
  168.                         memcpy( pRegion, &TempMBI, sizeof ( TempMBI ) );
  169.  
  170.                         // Free memory used
  171.                         LocalFree( pMemory );
  172.                         return TRUE;
  173.                     }
  174.                     else if ( Search (pMemory, szSinglepartString, mkSinglepartString, sizeof (szSinglepartString), TempMBI.RegionSize, TRUE ) )
  175.                     {
  176.                         *IsMultipart = FALSE;
  177.  
  178.                         // Copy across MBI information
  179.                         memcpy( pRegion, &TempMBI, sizeof ( TempMBI ) );
  180.  
  181.                         // Free memory used
  182.                         LocalFree( pMemory );
  183.                         return TRUE;
  184.                     }
  185.                     else if ( Search (pMemory, szServer, szServerMask, sizeof (szServer), TempMBI.RegionSize, TRUE ) )
  186.                     {
  187.                         // Copy across MBI information
  188.                         memcpy( pRegion, &TempMBI, sizeof ( TempMBI ) );
  189.  
  190.                         // Free memory used
  191.                         LocalFree( pMemory );
  192.                         return TRUE;
  193.                        
  194.                     }
  195.  
  196.                 }
  197.                 // Free memory used
  198.                 LocalFree( pMemory );
  199.             }
  200.         }
  201.         else
  202.         {
  203.             return FALSE;
  204.         }
  205.     }
  206.     return FALSE;
  207. }
  208.  
  209.  
  210.  
  211. VOID ThreadProc( VOID *pParam )
  212. {
  213.     HANDLE hGameManager = OpenGameManager();
  214.     BOOL bIsMultipart = FALSE;
  215.     MEMORY_BASIC_INFORMATION Region = {0};
  216.     CHAR szMessage[256];
  217.  
  218.     // Find BFG game manager and open a handle
  219.     if ( hGameManager )
  220.     {
  221.         // Find memory region which contains string(s)
  222.         if ( FindMemorySection( hGameManager, &Region, &bIsMultipart ) )
  223.         {
  224.             sprintf_s(szMessage, 256, "%08X", Region.BaseAddress);
  225.             MessageBox(0,szMessage,0,0);
  226.             // Scan memory for string(s)
  227.  
  228.             // Output string(s)
  229.  
  230.         }
  231.        
  232.        
  233.  
  234.         // Close handle to game manager process
  235.         CloseHandle( hGameManager );
  236.     }
  237.    
  238.     //ExitThread( ERROR_SUCCESS );
  239. }
  240.  
  241. BOOL CALLBACK WndProc( HWND hWin, UINT uMsg, LPARAM lParam, WPARAM wParam )
  242. {
  243.     switch ( uMsg )
  244.     {
  245.     case WM_COMMAND:
  246.         switch LOWORD ( lParam )
  247.         {
  248.         case IDOK:
  249.             ThreadProc(0);
  250.             break;
  251.  
  252.         case IDCANCEL:
  253.             SendMessage( hWin, WM_CLOSE, 0, 0 );
  254.             break;
  255.         }
  256.         break;
  257.  
  258.     case WM_INITDIALOG:
  259.         break;
  260.  
  261.     case WM_CLOSE:
  262.         EndDialog( hWin, ERROR_SUCCESS );
  263.         break;
  264.     }
  265.     return FALSE;
  266. }
  267.  
  268. INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd )
  269. {
  270.     return DialogBoxParam( hInstance, MAKEINTRESOURCE(IDD_MAIN), HWND_DESKTOP, (DLGPROC)WndProc, 0 );
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement