Author Topic: C++ Memory Trainer (Copy and Paste Example)  (Read 3356 times)

0 Members and 1 Guest are viewing this topic.

Rav3n

  • Klass Klown
  • ***
  • Posts: 419
    • View Profile
C++ Memory Trainer (Copy and Paste Example)
« on: October 06, 2011, 09:04:32 pm »
This is simple C++ code to make an application that can manipulate memory in any program / game, works with all versions of windows.

Simply copy and paste into a new C++ main.c file, build your Form and your good to go.


Code: [Select]
#include <windows.h>
#include <tlhelp32.h>
#include "resource.h"



#pragma comment(linker,"/FILEALIGN:512 /MERGE:.rdata=.text /MERGE:.data=.text /SECTION:.text,EWR /IGNORE:4078")
BOOL GameRunning;

/** Start of Declarations here **/
BOOL GetProcessList( );

// Below is the about text that is shown when "About" button is clicked

char *about   =
"C++ Memory Changer /n"
"Vietcong Hud on/off"
"Thanks to Medic and Subsky";
/////////////////////////////////////////////////////////////////////

char *gameWindow = "vietcong.exe"; // exe name here
DWORD pid; HWND hwndWindow; DWORD bytes; HANDLE hand = NULL;



HANDLE pFile; //Used for logging address to file (not implimented in this build)

//below you will list the BOOLs for function toggles
BOOL IsHack1On,FirstTime1;
BOOL dlgReadSuccess = FALSE;


///////////////////////////////////////////////////////
////Global Variables


Modapp on Code
BYTE Hud[1] = {0x92};
// add more below here


Modapp off Code
BYTE original_code[1] = {0x96};
// Dont forget to add the original code to turn it off

///////////////////////////////////////////////////////

/** End of Declarations here **/

void aboutButton(HWND hwnd)
{
MessageBox(hwnd,about,"About",MB_ICONINFORMATION);
}


void Initialize(HWND hwnd,WPARAM wParam, LPARAM lParam) {
GetProcessList();
if(GameRunning==TRUE)
{
         GetWindowThreadProcessId(hwndWindow, &pid);
hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
SetTimer(hwnd, 1, 200, NULL); //Timer speed is 200ms, you can change it here
}
else
{ //Error message for when game not found in process list
MessageBox(NULL, "Vietcong not detected. Please run the game before running the trainer", "Error", MB_OK + MB_ICONWARNING);
}




FirstTime1=TRUE; //This is the true / false flag for "is this the first time the trainers read the game code

IsHack1On=FALSE;
if(GameRunning==TRUE)
{
         GetWindowThreadProcessId(hwndWindow, &pid);
hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
SetTimer(hwnd, 1, 200, NULL); //Timer speed is 200ms, you can change it here
}
else
{ //Error message for when game not found in process list
MessageBox(NULL, "Vietcong not detected, please run the game before running the trainer", "Error", MB_OK + MB_ICONWARNING);
}
}

void HookExe() //This function ensures we are attatched to the game at all times
{

CloseHandle(hand);
    GetProcessList( );
    GetWindowThreadProcessId(hwndWindow, &pid);
hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

}

/*----- Here comes the good stuff -----*/


void timerCall() //functions in here run according to timer above
{
//char name = (); //this is our buffer to catch the current value
//int bytes = 0; //used temporarily for Read/WriteProcessMemory functions
//int prevAccessProtection = 0; //used temporarily for VirtualProtectEx function
HookExe(); //Call to function above (game always attatched)


/////////////////////////////////////////////////////////////////////////
/////ReadProcMem arrays are used to read and store original code so we
/////toggle the code on and off


if(FirstTime1==TRUE) //checks to see if this is the first time its run, if it is continue
{

ReadProcessMemory(hand, (void*) 0xEB0F44 , &original_code, 1, &bytes); // reads the bytes at address 0xEB0F44 and stores them
    FirstTime1=FALSE;
}
// What we are doing here is reading 3 bytes of the games code for VC Hud and storing them in a variable called "original_code"
// The number in sqaure brackets is the number of bytes, this has to match the number after our variable
// "original_code" in the ReadProcessMemory line.

// You can add more addresses in, just be sure to have unique varible names and specify the right number of bytes.


///////////////////////////////////////////////////////////////////////////
/////Start Hotkey Functions Below

/* --Vietcong HUD on/off Example Function-- --------------------------------------- */



if(GetAsyncKeyState(VK_NUMPAD1)) // User Pressed the NumPad1 to switch on HUD
{
   
if(IsHack1On==FALSE) //if this modapp is not on do this........

WriteProcessMemory(hand, (void*)0xEB0F44, &Hud,1, &bytes);  //Change the memory to activate the Modapp


IsHack1On=TRUE; //Sets our "Is On" flag to "on"
}
else // .... do this
{

WriteProcessMemory(hand, (void*)0xEB0F44, &original_code,1, &bytes); // Write the original code into memory

IsHack1On=FALSE; //Sets our "Is On" flag to "off"
}




/// Copy and paste the above function and change the variables to add another modapp


}




//The function above will toggle between hack on and hack off status. For a list of virtual keys please visit:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
     
/* --Example Function --END------------------------------------ */



/** End **/
}

// YOU DONT NEED TO EDIT BELOW THIS LINE

BOOL GetProcessList( )
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;
  int PidTest;
  GameRunning=FALSE;
 
 
  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE ) return( FALSE );
 

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn
 
  do
  {
    // Retrieve the priority class.
    dwPriorityClass = 0;
    hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
    if( hProcess != NULL )
    {
      dwPriorityClass = GetPriorityClass( hProcess );
      if( !dwPriorityClass )
       
      CloseHandle( hProcess );
    }

    PidTest=strcmp(gameWindow, pe32.szExeFile);
if(PidTest==0){ pid=pe32.th32ProcessID; GameRunning=TRUE;}

  } while( Process32Next( hProcessSnap, &pe32 ) );

  // Don't forget to clean up the snapshot object!
  CloseHandle( hProcessSnap );
  return( TRUE );
}

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message)
{
case WM_INITDIALOG:
Initialize(hwnd,wParam,lParam);
return TRUE;

case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ABOUT:
aboutButton(hwnd);
return TRUE;

case IDC_EXIT:
EndDialog (hwnd, 0);
return TRUE;
}
return TRUE;

case WM_DESTROY:
CloseHandle(pFile);
PostQuitMessage(0);
return TRUE;

case WM_CLOSE:
PostQuitMessage(0);
return TRUE;
case WM_TIMER:
timerCall();
return TRUE;
    }
    return FALSE;
}


 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{


DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINDLG), NULL,DialogProc);
return 0;
}


Enjoy  :icon_thumbsup

Free Rapidshare Prem Accounts
http://rapidshare dot com/files/111551586/Free-PremAccs.rar

MrMedic

  • MasstKer
  • ********
  • Posts: 8900
  • programmer/dev/software engineer
    • View Profile
Re: C++ Memory Trainer (Copy and Paste Example)
« Reply #1 on: October 06, 2011, 10:27:21 pm »
nice allways wanted one of these , where do i save it to?  :icon_biggrin2
EnCoded Message: i3iy9yl8kr2xf3g2Txs3pr6ye3ya7jg5ty2z

https://www.youtube.com/watch?v=62_7-AYfdkQ
you need a paypal account for the private versions.

Website:
http://bit.ly/medic101

Teamspeak 3: 85.236.101.5:10157

Jurugi

  • Online Villain
  • ***
  • Posts: 190
    • View Profile
Re: C++ Memory Trainer (Copy and Paste Example)
« Reply #2 on: October 07, 2011, 02:26:55 am »
Yeh, good job, but room for LOTS of improvement. It's very clear someone made this originally and was just glad that it compiled and worked (barely).

Code: [Select]
// forgot to add // infront? Modapp on Code
BYTE Hud[1] = {0x92};
Why? This just makes it sloppier. Put the byte as a hexidecimal (dword) value into ReadProcessMemory..

This just shows that this comes from the same source code made by some noob being copied over and over, and then it gets worse and worse as more people edit/redistribute that crappy code. You end up with a messy file like what you just posted.

Code: [Select]
WriteProcessMemory(Handle, (LPCVOID)0x00EB0F44, 0x92,1,0);
Now doesn't that look a lot cleaner?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681674%28v=vs.85%29.aspx

Use documents to guide you, but copy and pasting will get you nowhere..

Code: [Select]
// YOU DONT NEED TO EDIT BELOW THIS LINE

BOOL GetProcessList( )
..
{
..
}
Woops? Looks like you forgot to edit something.

Code: [Select]
//Call to function above (game always attatched)
Bad comment, the handle might not have opened successfully and this is an .exe which uses RPM/WPM (external) so it is never actually 'hooked' like a .dll, nor are any functions hooked (by dll) or scanned/modified (advanced external editing). The only way you can read and modify is with RPM/WPM (and scanning externally, re-projecting and changing the EIP to hook without editing anything using breakpoints but I doubt you are into that yet).

There are lots of issues and optimizations to be done with this copy and paste code.. and I'm starting to think I can just punch it into google and it'll pop up 100 times with different authors.. You can probably also google and find a better one although public mod-apping source code generally sucks and is leeched/renamed all the time.

Not hating, but as I said you should improve this a lot. So far the only custom code I see is very messy and overly commented stuff like
Code: [Select]
if(GetAsyncKeyState(Key)) // This makes sure Key is pressed!111 omg
{
int Horse = Strcmp ( ... );
if(Horse==false) FUNC(); // If STRCMP is successful
{
..
}
}

Fix:
If you do if(Horse), it initializes as a bool and if it is not 0 (false) then it will run, else you do if(!Horse) if the success return value is 0. You don't need to initialize a new variable unless you're using it across multiple scopes, so just do:
Code: [Select]
if(!StrCmp(...) && GetAsyncKeyState(0xkey))  { PseudoCode; }
instead of those 3 unneeded and sloppy looking lines of code.

I mean really I can probably change every line on this, and then it wouldn't matter because it's somewhat worthless except as a learning utensil. Keep on trying I guess, you'll slowly get better.

(Footnote: this might just be C, because C++ generally uses .cpp files and not old .c files, although they are still backwards compatible.)
« Last Edit: October 07, 2011, 02:37:47 am by Jurugi »

[TKC]Symantic

  • The Indifferent Character
  • The Central Committee
  • Heckler
  • *
  • Posts: 1647
  • I can has personal text?
    • View Profile
Re: C++ Memory Trainer (Copy and Paste Example)
« Reply #3 on: October 07, 2011, 07:04:50 am »
hey rav3n, long time no see.
Thanks for the contribution m8, im sure this will get used.
+1
Enjoy your creative game-play, whatever that may include.

Rav3n

  • Klass Klown
  • ***
  • Posts: 419
    • View Profile
Re: C++ Memory Trainer (Copy and Paste Example)
« Reply #4 on: October 08, 2011, 07:30:30 pm »
Hello all,

@Medic - you crack me up mate good to see your still active  :icon_thumbsup
Quote
nice allways wanted one of these , where do i save it to?
  :icon_laugh

@[TKC]Symantic - Thanks mate, its really OLD but does the job.

Posted this OLD code so people can actually make a hack rather than just asking for them.

Its OLD code so of course it can be improved, but for anyone wanting to just make a hack with their memory findings this will do it.

Any questions, just post back.






Free Rapidshare Prem Accounts
http://rapidshare dot com/files/111551586/Free-PremAccs.rar

pdk747

  • First Post
  • *
  • Posts: 1
    • View Profile
Re: C++ Memory Trainer (Copy and Paste Example)
« Reply #5 on: February 05, 2015, 08:21:00 am »
Nice discussion on memory related stuff in c++. Thanks for posting this here. It helped me a lot!