# How do I prevent memory-modification cheats?

There are many memory-altering programs out there nowadays (Cheat Engine & co). Is there an efficient way to keep a constant variable actually constant?

1. Make a separate thread updating the constant variable to a constant value, e.g. 200 every minute. Problem: One can also alter the second value.
2. Store the value on the server. Problem: I want to keep the network traffic as low as possible.

I need this mechanism for both constants and variables only allowed to contain a certain range of values.

Are there better ways to do this?

• Neither of those will work. In situation one, the cheater simply has to overwrite the value '200' that you are copying and it is no harder than overwriting the original. In situation 2, the cheater now can change the value without even altering your program simply by changing the bytes received on the network (for example, with a local proxy) – Jimmy Feb 3 '13 at 9:57
• I've yet to see a cheat protection that can't be disabled by a hacker. Just be sure you don't make the game a living nightmare for legitimate users, in your crusade against the cheaters :) – user15805 Feb 3 '13 at 10:01
• Related; more specific, but otherwise the same. – Anko Feb 3 '13 at 10:50
• Most games have "Trainers". I mean, hell, even Mass Effect: Andromeda has a trainer, and it's only been out for a little over a week. I say don't even worry about it. If major game companies can't avoid this, what makes you think you have a chance? – Krythic Apr 9 '17 at 16:27

If your game is singleplayer: it's not possible, but you shouldn't care.

If your game is multiplayer: then you should store all your important state on the server, which is much harder to hack than a local machine.

• It's very easy to protect against multiplayer cheating by making an authoritative server and only predict state on the client side. Basically, just send player input from the client to the server, not "I did x damage to y". – jmegaffin Feb 3 '13 at 18:16
• Indeed @BorealGames, that's what I mean by "store all important state on the server". – jcora Feb 3 '13 at 21:10
• Just wanted to clarify a bit :) – jmegaffin Feb 4 '13 at 1:43

It can't be done, https://security.stackexchange.com/a/4639 is about DRM, but the same applies to anything which the user doesn't want on his computer including Anti-Cheat mechanisms. (Which has the tendency to be even harder to do than DRM.)

But why would you, really why would you? It's your customers computer, not yours, the customer does with his computer whatever he pleases to do with it. You have no rights about it. Your program is removed if the customer doesn't like it.

For multiplayer games it's not about designing the binary running on, but designing the communication. Letting the users just send "I've won with 20000 points" doesn't work, neither does "I've 2000 Gold". You'll need to simulate the game yourself and just let the players send what they are doing like "I ordered the soldier to move to point X/Y" or "I've bought Item number 43", not what the result of that actions are. The server decides what the results are, and who won in the end.

If it is a single player game with online high score, a option would be to have the player send you a replay, simulate it and decide the result from that.

• Small correction suggestion: The client shouldn't send "I bought item number 43" but instead "I want to buy item number 43". The server should then answer with "OK, you lose 200 GP, you gain one Item43", "No, you don't have enough gold" or "No, the shop you are currently browsing has no item number 43". The server should behave like the game master of a pen&paper RPG. The players say what they want to do, and the server tells them if they succeed. – Philipp Feb 3 '13 at 19:32
• Also I am not sure on altering the in use memory of a personal computer, but altering the binary of a game itself (even though its on your computer) is usually against the EULA and may be further defined in copyrighting laws etc. – lozzajp Sep 28 '16 at 7:19
• Not that violating a EULA, the digital equivalent of jaywalking, ever stopped anyone. – jkmartindale Aug 21 '19 at 0:57

The first is not effective and won't stop any determined person from attacking your game - and it's also error prone. However, you can make it a lot tougher, not full proof, if you would like. Some common strategies are available all over the web but as other people have mentioned - you have to ask yourself if it's worth it.

For arguments sake, here's a couple ways to prevent hacking in a single player environment:

1. Open a handle to every other process on the users machine (invasive and not portable) and over-write their WriteProcessMemory with a dummy function or warning. The choice is yours. This can be defeated because... applications are free to do the same and over-write the handle once again or do things to prevent this.
2. Checksum your application and have it verify itself through a hack shield - this is useful for many permanent edits to your application but won't stop a common memory attack. This can be defeated because... tools like ADA, OllyDbg and even simple hex edits can make permanent patches and changes to your application to remove the offending code - this includes your threaded timer idea!
3. Scan for known hack tools. This also can be defeated by recompiling a tool or attacking your client directly. This can be defeated because... a recompile, rename of the application or change in what your scanner uses as a heuristic will render this useless. Consider using a special portion of memory an application is known to use if you opt for this.

The alternative, of course, is to just store everything on the server and make a game server that is a master. Not only is this expensive, requires upkeep, and increases development time... fans are not so impressed from the implementation of popular games like Diablo III. Or here. I should note for an online flash RPG like Adventure Quest this model has actually been working pretty well for them, however.

I can't provide much guidance on how to implement this type of approach as it's very broad, wide and requires a lot of planning. The basis to know is that the server decides everything and the client is nothing more than a mere input and display console.

A lot of people use these programs to search for a score stored in a variable e.g

int score = 10;


to do this they need to know the score, ususaly from the score displayed in game.

I was thinking, would this help put them off...

public static float randomVal; // at start of level generate random float

private float 1a3sf5vhh4; //represents score

void hjgkkj4(int val) //add score method
{
var 5vhh4 =  1a3sf5vhh4/randomVal+val;
1a3sf5vhh4 =  5vhh4*randomVal;
}


then when you need to display score do...

string displayScore = ""+ 1a3sf5vhh4/randomVal;


this will make the cheaters search for what they think is the score( the score being displayed) so they will only find the displayed value.

The real score will be Score*randomVal stored else where. They cant change the score by changing the displayed value.

would this help?

p.s I'm no expert so take it easy on me lol.

• Just found this vid, makes some interesting points...youtube.com/watch?v=G25YhoE04Xc – Raimi Nov 26 '14 at 15:05
• Cheat tools can be configured to detect such things as well. This just takes more time to track. – Kromster Nov 26 '14 at 21:25
• but for a small, offline, single player game, would something like this be an ok solution? looks like it could at least stop a few of the apps out there – Raimi Nov 27 '14 at 15:50
• Yeah, that's just the case of EffortSpent vs ProtectionAchieved, where second part can never reach 100%. – Kromster Nov 27 '14 at 17:15
• Random names in source code won't help either, at least in most cases. The program is usually compiled to IL/bytecode or straight to binary etc. – lozzajp Sep 28 '16 at 7:22

In regards to single player games, you can not prevent cheating, and you should not.

Why can't I prevent cheating in my own game?

As other answers have pointed out, even the best efforts could be potentially circumnavigated by a single hacker, and provided to the rest of your player base. You can still make effort against this, but look to other answers for suggestions how. This answer means to save you the trouble, if your game only aims to provide single player gameplay.

Why should I let players cheat in a single player game?

To quote Sam,

Spend more time on creating a great game and less time on preventing morons from spoiling their own fun - simple.

This misses the point entirely. Games are a form of entertainment. Generally, your player base is playing your game to have fun. If a player uses cheats in your game, there is a reason they are using cheats. It is safe to assume that that we can not guarantee each individual player's definition of fun; if they use cheats, perhaps that is their way of making your game more enjoyable, to them.

How does cheating make a game more enjoyable?

While every individual is unique, there are a few conditions in which cheats could make a game more entertaining, off the top of my head:

• The game is a little slow, and the player wishes to use cheats to bring out the action before they are put off, completely.
• The player is unable to get past a certain challenge; they would not normally cheat, but they are close to giving up, in frustration. They can stop playing your game, or they can use a cheat to bypass the obstacle, and go on to play the second half of your game.
• The player can not devote the amount of time your game requires for the intended experience. By applying cheats, the user can get a "running start" and be able to enjoy the intended content of your game. If you think this does not sound plausible, consider that this is the reason I have personally played very little of Fallout 4. In comparison, it is the only game I preordered in the past 5 years, and the only game I have ever preordered and purchased for more than one platform. I love the game; I don't have the time for it.
• Your game has a bug, and this stops the player from progressing further. A cheat bypasses the bug, so the player can keep playing. Recent Elder Scrolls games are notorious for this situation.

Why does this only apply to single player games?

It is important to stress that this only works in single player games; if the player tells you they want to use cheats to have more fun in their game, no harm done. As soon as there are multiple players in the game, the exploits of that first player could become an unfair disadvantage to the other players. This is the only real situation where you should not allow cheating.

The only way to protect your game from modding and memory editing is making it ONLINE only and processing vital values server-side.

Maybe you can encrypt the values so that rookie memory edoits wont get to them, but all the communities, such as Cheat Engine, GameGuarian for Android, ArtMoney, Game Hacker ect have people that are going to be able to hack anything you process client-side and show the others how to do it or publish trainers.

Its is a futile effort to try and deter cheaters cleint-side in my opinion. Also not really fair, since it is their device and if they want to memory edit stuff happening on their device, why would you hinder them? The customer is king afterall.

What does a memory hack tool do?

There are two ways to operate the hack tool.

1. It either accepts a known address where the player's variables are stored.
2. You feed in the changes in the variable and it scans the memory for changes.

What can you do about it?

1. You can use dynamically allocated variables to store player variables so that the address keeps changing on startup. There won't be any fixed address which a cheat website could list.

2. Encrypt the player variables: scaling the value, adding a constant, etc. This brings a new problem: a hardcore hacker could disassemble your program and find the numbers you use for encrypting your player variables. For this, use random numbers. You can also scale up the values of your player variables and then add a random number such that the added number does not significantly alter the value (adding 10 or 20 to a scaled up value of 2000 does not make a lot of difference).

Of course, some serious hacker can still find ways to overcome these hurdles but it won't be easy.

  std::default_random_engine myRNG;
class Player
{
public:
Player()
{
Health = new float;
Armor = new float;

std::uniform_int_distribution<int> myDist_scale(1000,10000);
scale = myDist_scale(myRNG);
}
float GetPlayerHealth() const;
void SetPlayerHealth(float health);
private:
float *Health;
float *Armor;
int scale, constant;
}

//wrappers for Set/GetPlayerHealth
void Player::SetPlayerHealth(float health)
{
this->Health = health * scale + constant + myDist_padding(myRNG);     //random padding; adding 10 or 100 to a number that is scaled up by 1000 will cause an increase in 1%
//1% of error is ok I guess?
}
int Player::GetPlayerHealth() const
{
return (this->Health - constant)/scale;
}


While the above code solves some serious issues, it is still crackable. The only information the hacker will need is: the player variables are accessed through a pointer (he could also find out by disassembling your game but it is going to be annoying and hard). Once the hacker has the address to your player variables along with other secret random variables, it is going to be an easy task for him to develop a cheat.

In my opinion, without disasembling, you cannot really write a hack tool for such a system. The result is unpredictable as you are going to add a random number to the player variabels each time it is modified. The range in which the player variables are stored isn't the same on every startup either.

What more can you do?

Keep changing the address of the player variables occasionally. Does it hamper the readablity of the code? Well, it does to some extent but you can abstract the entire process in a single class which has well-explained comments. The interface provided by the class will have simple Set/Get functions. The frontend programmer wouldn't have to worry about the underlying complexity.

Economics:

If you want to harden your game against cheaters, the best way is to buy a software solution that does that for you. There are several providers, and you can find quite a few of them by googling for "anti cheat breaks my game".

The professionals are much better at actually preventing cheats than you are, and much better at not breaking your game than you are (and still don't succeed 100%, at either of these). So if you feel there's value in putting such a solution into your game, buy it. Doing your own will be vastly more expensive.

A related question is "is there value in using anti cheat software?". The answer is: Not unless a decent part of your game relies on an online competitive mode.

I mean its not that hard. You can keep 99% of the memory edit cheaters (those Game Guardian, Cheat Engine ect at bay by simple multiplying values. Even multiplying by 10 in the memory will keep most of the at bay.

I remember a lot of flash games back in the day would multiply by 8 or 6. Or if you are really serious about it, code some checks or encrypt the values in memory.

• Clearly you have never used those tools. In CE there are scan modes for detecting increased, decreased and otherwise changed values. – Shadows In Rain Jun 26 '20 at 8:02

You can't - I repeat you can't. There is no answer to this question that cannot be undone by a good hacker.

But - so what? If someone wants to cheat at your game (assuming no monetary loss or achievement is involved), who cares? They only cheat themselves.

Spend more time on creating a great game and less time on preventing morons from spoiling their own fun - simple.

• Down voting as I believe fun plays an important part in this answer, but is missed entirely. Instead of further explanation, I am submitting an alternate answer. – Gnemlock Jan 11 '17 at 2:59

### The best way to do this is...

Let's say you have a score variable, very important.

Make a duplicate variable, let's call it scoreAntiHack.

scoreAntiHack will be encrypted with your choice of encryption.

The best way to encrypt your scoreAntiHack is to make a multiplyVariable, divisionVariable, and additionVarible.

mulitply, division, and addition variables will be a random number generated at the beginning of the game.

Your score variable will be equal to the scoreAntiHack * mulitplyVariable / divisionVariable + additionVariable;

when you update your Score also update the scoreAntiHack (Unencrypted).

Every frame check your score to scoreAntiHack. If they don't match up, end the game.

It will be nearly impossible for someone to hack the score. It will take shear luck to find both variables and guess the encryption.

• That's defeated trivially by scanning for variables that change together (unknown initial value changed/unchanged in CE) then freezing them all together. – Shadows In Rain Jun 26 '20 at 7:49

Encrypt the hell out of it! Store important numbers broken up in multiple locations conjoined with other values so that they will constantly change! Use dummy numbers that if altered tells them they are cheaters.

Everything can be cracked, but most wont take the time to do so.

• ...and then your sourcecode will be an unreadable, unmaintainable mess. Good luck finding a bug or making a balance change without blowing the whole thing up. – Philipp Feb 13 '15 at 13:04
• If I had to implement this form of cheat prevention, I would be sincerely disappointed if hackers caught on to my evil scheme and just gave up. That would mean I've wasted nobody's time but my own. – Marcks Thomas Feb 13 '15 at 14:08
• @MarcksThomas Well if the hackers give up, then you didn't waste your time. That was the goal, after all, wasn't it? – Dan Jan 19 '16 at 14:29
• I don't fully agree with you on that @Philipp - there are ways to keep the source code clear, yet scramble the production code when it leaves. In practice that involves encryption and some numerical and especially timing & threading mambojambo :-). We test with the unaltered source code and if there is an error in the production code, we let it brutally crash (with a small delay, just to be evil :-)). A small change somewhere also causes that. – Stormwind Sep 28 '16 at 9:48
• Thoght i'd upvote this as the best answer in this chain, but i seem to have done it already. There are lots of mathematical rules to use, that will be impossible to reverse engineer just because of complexity and above all timing. One can for example mark the application for delayed crashing or let the sensibility of it diverge into a useless (unplayable) state. Of course thee must be a good reason for this. – Stormwind Apr 9 '17 at 11:47

The way I deal with it in online games is this.

I'm not going to help the hackers that much so bear with me. I will not mention any programs that I use to do this. You will have to figure that out. The only way to truly stop them is to know one thing. They do not want their hack program to be detected.

So far, all of the hacker/trainer users that I have defeated. The developer of the hack will put a detector on it to see if something is watching. If something is, then the hacker/trainer program will either close automatically on their end or it will only half work.

For instance in dirt 3, trainer users can make your car spinout automatically. But if you put up a detector on your end the cheats own detector will detect it and it will close part of the cheat. Not all of it.

The only part of the trainer program that I have seen effected from my meddling is super brakes(perfect braking) and the spinout(car losing control for no reason). I have mostly racing games as you can see. They are all from codemasters and sadly those games do get abused by cheaters quite often.

I believe that I am only able to stop them because of the way that we connect to each other while playing. It's probably the same reason that they could cause a spinout for a normal player that has no clue about security.

My biggest suggestion is to get a very good internet security antivirus(Norton, Mcafee, Avast, and all the other shitty av's will not help you). Be willing to spend $50 to$90 on a very good av that I have not listed and then learn how to experiment with it to effect online play.

It takes a lot of patience and a lot of guinea pigs. I've had a lot of success with this but also a lot of years of experience in dealing with trainer users. I don't know any code whatsoever. It has nothing to do with it. The code part, is locked down and you can't do anything about it. You could think that you can but it's just not possible to actually stop what they are doing on their end.

You can still ban them with things like VAC because Vac detects signatures and certain behaviors and modifications of files that it already knows about. It only bans the account associated with the cheat.

They don't actually stop the cheat though. They can't be bothered with it. They just ban your account. The other thing that I would like to mention has to do with Wolfenstein ET. A very old game. But to me it had the best and harshest security measure of all.

I never got to see how they did it. I did give them the idea on what would actually stop cheaters and suggested to one of the jaymod devolopers to ban by way of guid. What has an easily accesible guid when playing games? Graphics cards. The reason they implemented that is because people would just change ip addresses and you didn't need an account for the game so you could just change your name, your mac address which would also change your ip address.

But when they did the graphics card bans. If you wanted to play again you had to RMA your card or buy a new one. That's off the subject though. This is supposed to be about detection and I went off on a tangent. Sadly I don't play games nearly as much now. But man I loved all my gunea pigs. Especially the ones from project cars. The only racing game that was not codemasters. But still effected by the same nonsense.

• I see several issues with this answer. 1. Format. It is hard to read due to the structure, and due to grammatical inconsistency, there are parts where I can not understand what you mean to say. 2. You appear to talk a lot about the subject as a player playing against other cheaters. That would be relevant on Arqade, but we are more concerned with Game Development. 3. You say "don't use the ones I mentioned", in regards to anti virus. You mention " Norton, mcafe, avast, and all the shitty ones. This doesn't tell us what to use, especially when you name some of the highest ranked. – Gnemlock Jan 11 '17 at 2:47
• The entire last half seems to be an anecdote about how you think a video game company took your advice (I believe you go to the effort of saying your not sure how its done, so I interpret that your further explanation of how it is done must be based off speculation, not fact). – Gnemlock Jan 11 '17 at 2:49