Imagine a space shooter with a scrolling level. What methods are there for preventing a malicious player from modifying the game to their benefit? Things he could do that are hard to limit server-side is auto-aiming, peeking outside the visible area, speed hacking and other things.

What ways are there of preventing this? Assume that the server is any language and that the clients are connected via WebSocket.

Always assume that the code is 100% hackable. Think of ways to prevent a client completely rewritten (for the purposes of cheating) from cheating. These can be things such as methods for writing a secure game protocol, server-side detection, etc.

7 Answers
7

Send game actions to the server and receive game state from the server.

You render the game state. and you send input to the server.

auto aiming - this one is hard to solve. You have to go for realism. If a user hits 10 headshots in 10ms then you kick him. Write a clever cheat detection algorithm.

peeking outside the visibile area - solved by only sending the visible area to each client

speeding hacking - solved by handling input correctly. You receive an event that user a moved forward and you control how fast he goes.

You can NOT solve these problems by minifying code. Code on the client is ONLY there to handle input and display output. ALL logic has to be done on the server.

You simply need to write server side validation . The only thing is that a game input is significantly harder to validate then form input due to complexity. It's the exact same thing you would do to make forms secure.

You need to be really careful with your "input is valid" detection though. You do not want to kick/ban highly skilled players from your game. It's very hard to hit the balance of too lax on bot detection and too strict on bot detection. The whole realm of bot detection is very hard overall. For example Quake had an auto aim detection that kicked legitedly skilled players back in the day.

As for stopping a bots from connecting to your websocket directly set up a seperate HTTP or HTTPS verification channel on your multiplayer game for added security. Use multiple Http/https/ws channels to validate a client as being "official", acting as some form of handshake. This will make connecting to the ws directly harder.

Example:

Think of a simple multiplayer game. A 2D room based racing game. Upto n users go on a flat 2D platformer map and race to get from A to B.

Let's say for arguments sake that you have a foolsafe system where there's a complex authetication going over a HTTPS channel so that users can not access your websocket channel directly and are forced to go through the browser. You might have a chrome extension that deals with the authentication and you force users to use that. This reduces the problem domain.

Your server is going to send all the visual data that the client needs to render the screen. You can not obscure this data away. No matter what you try a silled hacker can take your code and slow it down in the debugger editing it as he goes along until all he's left with is a primitive wrapper around your websocket. He let's you run the entire authentication but there is nothing you can do to stop him from stripping out any JavaScript you write from stopping him doing that. All you can achieve with that is limit the amount of hackers skilled enough of accessing your websocket.

So the hacker now has your websocket in a chrome sandbox. He sees the input. Of course your race course is dynamically and uniquely generated. If you had a set amount of them then the hacker could pre engineer the optimum race route. The data you send to visualise this map can be rendered faster then human interaction with your game and the optimum moves to win your racing game can be calculated and send to your server.

If you were to try and ban players who reacted too fast to your map data and call them bots then the hacker adjusts this and adds a delay. If you try and ban players who play too perfectly then the hacker adjusts this and plays less then perfect using random numbers. If you place traps in your map that only algorithmic bots fall into then they can be avoided by learning about them, through trial and error or a machine learning algorithm. There is nothing you can do to be absolutely secure.

You have only ONE option to absolutely avoid hackers. That is to build your own browser which cannot be hacked. Build the security mechanisms into the browser. Do not allow users to edit javascript at runtime in realtime.

Thanks for your input! Some of the points are pretty normal ways of cheat prevention, I would appreciate tips on some more advanced methods too. But all input is good. :)
–
BlixtMar 9 '11 at 19:02

@Blixt advanced method are game domain specific ;) I can go into details of how to detect FPS cheaters but that doesn't help with detection the platformer cheaters which make almost impossibly perfect jumps.
–
RaynosMar 9 '11 at 19:05

@Blixt Write a secure browser with a javascript game engine build into it ;) Whilst your at make it have p2p sockets and make the browser run as a node.js server.
–
RaynosMar 9 '11 at 19:24

The only way I can even think of implementing this is by modifying your Javascript to function as a client and then designing a central server mechanism to validate data sent from that client. This is probably a big change to implement and will most likely make your project more complex. However, as was said earlier, if the application runs entirely on the client, the client can pretty much do whatever they want with your script. The only way to secure it to use a trusted machine to handle validation.

This is the only real solution. Put any logic you can on the server. The server consumes events from each client and responds with updated state. If the client is simply rendering the current state and sending events for each user interaction the you have nothing to worry about.
–
PrestaulMar 9 '11 at 18:54

@BobBaller as long as a javascript game is single player there is no point in placing validation into it. If a user wants to cheat then he ruins his own game experience. For single player javascript games that do not effect anything outside a single instance of that game you should allow the user to do whatever he wants.
–
RaynosMar 9 '11 at 18:55

@Raynos that is true, but the original question specifically addresses multiplayer javascript games where validation is required to maintain fairness amongst all parties involved.
–
Bob BallerMar 15 '11 at 17:39

@BobBailer if the game is already multiplayer then the logic should already be running on a central server. A p2p multiplayer game on the client would be a nightmare to control.
–
RaynosMar 15 '11 at 17:54

@Raynos I just reread the question and I think it has been edited since my first post. I don't remember any initial mention of a server so I assumed it was p2p.
–
Bob BallerMar 15 '11 at 18:33

They don't have to touch your client-side code -- they could just sniff and implement your Websocket protocol and write a tiny agent that pretends to be a human player.

Update: The problem has a few parts, and I don't have answers off the top of my head, but the various options could be evaluated with these questions in mind:

How far are you willing to go to prevent cheating? If you only care about casual cheating, how many barriers are enough to discourage the casual cheater? The intermediate Javascript programmer? A serious expert? Weighing this against the benefits of cheating, is there anything of real value at stake, like cash and prizes, or just reputation?

How do you get a high confidence that a human is providing inputs to your game? For example, with a good enough computer vision library I could model your game on a separate machine feed inputs to the computer pretending to be the mouse, but this has a high relative cost (not worth my time).

How can you create a chain of trust in your protocol such that knowledge of (2) can be passed to the server, and that your server is relatively confident your client code is sending the messages?

Sure many of the roadblocks you throw up can be side-stepped, but what is the cost to the player and you? See "Attrition warfare".

Yup, that is the exact assumption I'm looking for a solution to. I've read some papers that talk about this and come up with reasonable ways of mitigating it, but I'm looking for more input.
–
BlixtMar 9 '11 at 18:42

Each client sends their "actions" to the server. The server executes them and sends relevant data back. e.g. a ship wants to move north, the server calculates its new position and sends it back. The server also sends a list of visible ships (solving maphacks), etcetera.

2) Full client-side game

Each client still sends their actions to the server. But to reduce workload on the server, the server doesn't execute the actions but forwards them to all other clients. The clients then resolve all actions simultaneously. As a result, each client should end up with an identical game. Periodically, each client sends their absolute data (ship positions, etc.) to the server and the server checks if all client data is identical. Otherwise, the games are out of sync and someone must be hacking.

Disadvantage of the second method is that some hacks remain undetected: A maphack for example. A cheater could inject code so he sees everything, but still only sends the data he should normally be able to see to the server.

--

At the client-side, there is 1 option:
A javascript component that scans the game code to see if anything has been modified (e.g. code modified to render objects that aren't visible but send different validation data to the server).

Obviously, a hacker could easily disable this component. To fix that, you could force the client to periodically reload the component from the server (The server can check if the script file was requested by the user periodically). This introduces a new problem: the hacker simply periodically requests the component via AJAX but prevents it from running. To avoid that: have the component redownload itself, but a slightly modified version of itself.

For example: have the component be located at yoursite/cheatdetect.js?control=5.
The server will generate a slightly modified cheatdetect.js so that in the next iteration, cheatdetect.js?control=22 (for example) must be downloaded. If the control mechanism is sufficiently complicated, the hacker won't be able to predict which control number to request next, and cheatdetect.js must be executed in order to continue the game.

There's nothing you can really do to prevent anyone from modifying your JS or writing a GreaseMonkey script. However you can make it hard for them by minifying your script as well as making your code as cryptic as possible. Maybe even throwing in some fake methods or variables that do nothing but are used to throw an attacker off. But given enough time, none of these methods are completely foolproof, as once your code goes to the client, it is no longer yours.

Yes, I was actually looking for ways to prevent cheating despite code modification. Because that is of course what a cheater would do. :) I added a third paragraph with another assumption.
–
BlixtMar 9 '11 at 18:41

These methods of "delaying" attackers are just a nuisance for modders / users and maintainers of code base.
–
RaynosMar 9 '11 at 18:53

1

Security through obfuscation is never a good thing and advicing it in any place is downright stupid. Even you know it, so your answer should not have been written. :)
–
redMar 9 '11 at 19:06

I'd use a combination of minification and AJAX. If all of the functions and data aren't loaded into the page, it'd be more difficult to cheat.

On the other hand, modding turned out to be a very profitable tool for companies like Id Software. Perhaps allowing the system to be modded might make the game that much more enjoyable to the community at large.

Yeah the idea is that people should be allowed to see and modify the source code but still be prevented from cheating. There will of course always be benefits to the user modifying the code, but I'm looking for ways to decrease them without impacting gameplay.
–
BlixtMar 9 '11 at 18:43