[Novice Tutorial] How to write a game modifier in C language!

This section teaches you how to write a simple game modifier using the basic programming language C.

Tools used:

1.VC++6.0 (usually used on the computer)

2.CE 5.4 (any version will do)

3. Of course I want a game. Here I will use the small C language game I wrote temporarily as the experimental object.


Start of the tutorial:

First of all, we must design an interface:


Here I use a while loop, so that the program will not exit after executing the instruction.

When introducing the key part, I must first introduce the proper terms involved

Handle, PID

Handle: Well, it is a special smart pointer . When an application wants to reference memory blocks or objects managed by other systems (such as databases, operating systems), handles are used . I will quote the explanation of Baidu entry.

PID: No matter what program is run, it will be assigned an ID that uniquely identifies this program by the operating system. If you want to see the specific PID of each program (task manager, just select a process, right-click, select go to detailed information, you can view it. (here I live under the win10 operating system)).


If you really don't understand, treat them as student IDs and ID cards in real life. With these two, you can prove your identity. The procedure is the same.

After introducing the concepts of handle and PID , we officially start the tutorial:

First define a global variable:

HWND hwnd; //This is the variable definition of the handle

DWORD Pid; //This is the definition of PID

After the variables are defined, the process handle of the game to be modified must be obtained

Here we use FindWindow (NULL, "simulated snake game") "" Fill in the window you want to get (here I will fill in the name of my game window)

Of course this function has a return value

hwndFindWindow(NULL, "simulated snake game")   just use the handle variable to receive

After obtaining the handle, the next step is to obtain the PID:

Here, the PID is obtained by using the window handle we obtained earlier:

 GetWindowThreadProcessId(hwnd,&Pid); //Formal parameter 1: Where to get (handle), Formal parameter 2: The stored PID


After finishing the preliminary work:

Define a new type variable

HANDLE hprocess =0;

The function of this variable is to accept the permission of the process to access

hprocess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,Pid); // PROCESS_ALL_ACCESS means to get all access rights

After talking about the previous, now we need to get the address where the game scores to be modified are stored

Use ce for address acquisition

1. Open the game process as shown in the figure

2. Search score information, let the information change, search again


Here we find the address of the game score 0x00328400

Back to my programming interface to define new variables

DWORD score; //Score to be modified
DWORD ADDR = 0x00328400 ; // Address of game score

Let score = 999;

Use the function WriteProcessMemory(hprocess,(LPVOID)ADDR,&score,4,0); to inject the modified address into the original address

You're done everywhere! !

Finally, attach the source code:

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#define Fail 0
#define Succeed 1

int GetHwnd();
void Fixprocess();
HWND hwnd;
DWORD Pid;
HANDLE hprocess = 0;
void main()
{
 int command;
 int ch;
 while(1)
 {
  system("cls");
  printf("------------------------ -\n");
  printf(" 1. Get window handle \n");
  printf(" 2. Inject instructions \n");
  printf(" 3. End the program \n");
  printf("---- ---------------------\n");
  scanf("%d",&command);
  getchar();
  if(command == 3)
  {
   break;
   exit(0);
  }
  else
  {
   switch(command)
   {
     case 1:
    //Get the window handle of the program int GetHwnd()
    ch = GetHwnd();
    if(ch == Succeed)
      printf("The process is opened successfully and the window process is successfully obtained!");
    Sleep(2000);
    break;
     case 2:
      //Inject the instruction
      Fixprocess();
    break;
   
   }
  }
 }

}
int GetHwnd()
{
 hwnd = FindWindow(NULL,"模仿的贪吃蛇游戏");
 if(hwnd!=0)
 {
     GetWindowThreadProcessId(hwnd,&Pid);
  hprocess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,Pid);
  if(hprocess == 0)
  {
   return Fail;
  }
  else
  {
   return Succeed;
  }
 }
 else
 {
      return Fail;
 }
}
void Fixprocess()
{
 //char Add[20];
 DWORD score;
 DWORD ADDR =0x00898730;
 //printf("Enter the address of the game score:");
 //gets(Add);
 //getchar();
 // ADDR = *Add;
 printf("Enter the score you want:");
 scanf("%ld",&score);
 DWORD res = WriteProcessMemory(hprocess,(LPVOID)ADDR,&score,4,0);
 if(res == 0)
 {
  printf("Instruction injection failed!");
 }
 else
 {
  printf("Instruction injection succeeded!");
 }


}


Guess you like

Origin blog.csdn.net/q879897637/article/details/80558462