Anti cheating method for memory modification

Memory modification cheating means that users modify memory data to modify scores, gold coins and life. This cheating method is the most common and has the lowest threshold. They only need to simply master the use of CE class modifiers.

Source program

#include <iostream>
#include <windows.h>
#include <conio.h>
int score = 0;
int main(){
    int a,b,c,s;
    while (true){
        a = rand() % 10;
        b = rand() % 10;
        c = a + b;
        system("cls");
        printf("Current score:%d\n",score);
        printf("Please calculate:\n");
        printf("%d + %d = ?\n",a,b);
        scanf("%d",&s);
        if(s == c){
            score++;
        } else if(s < 0){
            break;
        } else{
            score--;
        }
    }
}

The program prints the formula on the screen and requires the user to calculate the result. If the calculation is correct, one point will be added, otherwise one point will be deducted.

Cheating method

Using the CE modifier, search for scores

Soon searched the address of the score

The program was modified

Double verification anti cheating method

Set another variable verify, and set the value of verify to 10 times of score. If score and verify are not matched, it will be deemed as cheating

#include <iostream>
#include <windows.h>
#include <conio.h>
int score = 0;
int verify = 0;
void AddScore();
void MinusScore();
void CheckCheating();
int main(){
    int a,b,c,s;
    while (true){
        a = rand() % 10;
        b = rand() % 10;
        c = a + b;
        system("cls");
        printf("Current score:%d\n",score);
        printf("Please calculate:\n");
        printf("%d + %d = ?\n",a,b);
        scanf("%d",&s);
        if(s == c){
            AddScore();
        } else if(s < 0){
            break;
        } else{
            MinusScore();
        }
    }
}

void AddScore(){
    CheckCheating();
    score++;
    verify = score * 10;
}

void MinusScore(){
    CheckCheating();
    score--;
    verify = score * 10;
}

void CheckCheating(){
    if(score * 10 != verify){
        MessageBox(NULL,"No cheating!","AntiCheat",MB_OK);
        exit(0);
    }
}

Change the address to avoid positioning

The pointer p is defined to point to the score. Each time the score is modified, the memory space is re applied and the old space is released. This will make the CE modifier unable to find the address of the score and naturally cannot be modified. However, it may cause too much system overhead, and it is easy to cause memory leakage by using a large number of pointers

#include <iostream>
#include <windows.h>
#include <conio.h>
int *p;

int main(){
    p = (int*) malloc(sizeof(int));
    *p = 0;
    int a,b,c,s;
    int *last;
    while (true){
        a = rand() % 10;
        b = rand() % 10;
        c = a + b;
        system("cls");
        printf("Current score:%d\n",*p);
        printf("Please calculate:\n");
        printf("%d + %d = ?\n",a,b);
        scanf("%d",&s);
        if(s == c){
            last = p;
            p = (int*) malloc(sizeof(int));
            *p = *last + 1;
        } else if(s < 0){
            break;
        } else{
            last = p;
            p = (int*) malloc(sizeof(int));
            *p = *last - 1;
        }
        free(last);
    }
}

Custom structure

Using unusual ways to save data makes cheating more difficult.

#include <iostream>
#include <windows.h>
#include <conio.h>
int score1 = 0;
int score2 = 0;

int GetScore();
void SetScore(int score);

int main(){
    int a,b,c,s;
    while (true){
        a = rand() % 10;
        b = rand() % 10;
        c = a + b;
        system("cls");
        printf("Current score:%d\n",GetScore());
        printf("Please calculate:\n");
        printf("%d + %d = ?\n",a,b);
        scanf("%d",&s);
        if(s == c){
            SetScore(GetScore() + 1);
        } else if(s < 0){
            break;
        } else{
            SetScore(GetScore() - 1);
        }
    }
}

int GetScore(){
    return score1 * 10 + score2;
}

void SetScore(int score){
    score1 = score / 10;
    score2 = score % 10;
}

The above code divides the score into two parts: score1 and score2. The real score is score1*10+score2. If the cheater still searches for the real score, he will get the wrong address. This is just a simple demonstration. In fact, we can customize many complex structures to make it more difficult to crack. for example

struct Num{
    bool n1:1;
    bool n2:1;
    bool n3:1;
    bool n4:1;
    bool n5:1;
    bool n6:1;
    bool n7:1;
    bool n8:1;
};

The Boolean variables in C + + occupy one byte, but the actual boolean type only needs one bit. Put the eight Boolean variables in a structure, which occupies one bit. At this time, the general modifier will mistake the eight Boolean variables as one number. After testing, the CE modifier is successfully cheated, When all eight Boolean variables are true, Num is recognized as the number 255.

Encrypted save

Save the data after processing. Such processing can be simple addition, subtraction, multiplication and division, or complex encryption

#include <iostream>
#include <windows.h>
#include <conio.h>
int score = 0;
int GetScore();
void SetScore(int s);
int main(){
    int a,b,c,s;
    while (true){
        a = rand() % 10;
        b = rand() % 10;
        c = a + b;
        system("cls");
        printf("Current score:%d\n",GetScore());
        printf("Please calculate:\n");
        printf("%d + %d = ?\n",a,b);
        scanf("%d",&s);
        if(s == c){
            SetScore(GetScore() + 1);
        } else if(s < 0){
            break;
        } else{
            SetScore(GetScore() - 1);
        }
    }
}

int GetScore(){
    return -score / 10;
}

void SetScore(int s){
    score = -s * 10;
}

Keywords: C++ memory management

Added by phifgo on Fri, 14 Jan 2022 22:27:06 +0200