I thought I would share my experience on this; if you're working on Multiplayer, you will need to protect your packets. The solution is simple, let's go through how we can achieve this by implementing what Valve calls "challenge codes". (Some reading on the topic from Valve here: https://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol#Challenge_response).
Disclaimer: this doesn't cover other security techniques like authoritative server or encryption.
So, I've worked on Border Recon last year (I think) and I needed a way to protect my server/client packets. There was no need for me to re-invent the wheel, I just had to copy what Valve has had for a long time - challenge codes.
The idea behind challenge codes is similar to Captcha, but not exactly. Think of it like this: for every packet submitted to the server, it must be verified - how? By requiring the client to solve challenges our server provides.
To implement this we need to have the following:
- A randomised formula in the server i.e.: a = b * c / d + e or a = b / c + d - e, be creative - it can be any combination of basic arithmetic or some fancy logic you like and can be however long as you want - do consider that the longer the formula, the more work your server has to do to make the computation.
- Copy the same formula to the client.
- A random number generator.
So the idea here is:
- (Server) Generate a random number (see 3 above) of which the result would become the challenge code,
- (Server) run it through our formula and record the result.
- (Client) And then, we hand over the challenge code to the client to solve (an authentic client would have the same formula implemented in its program as we have on the server).
- For every packet received from the player, a new challenge code is created (and the player is notified of this change by the server in response). For every other packet, a new challenge code is created.
- (Client) Every packet sent to the server by the client must have a challenge code and its answer embedded.
- (Server receives the packet) Run the challenge code again to our formula and compare the result to the answer embedded on the client's packet.
- (Server) If the answers are different, reject the packet, no changes to the player's state.
The advantage(s) of this strategy in terms of achieving the protection we need to secure our server:
- For every packet sent, new challenge code is created. Typically, game clients (especially FPS) will update its state in a matter of ms so even if a cheater is successful at sniffing the answer to a challenge code it would be invalidated almost instantaneously.
- Lightweight solution. No encryption needed.
- The formula to answering the challenge code is embedded to the client, a cheater can de-compile the client and uncover the formula. Luckily, we have other anti-cheat solutions for that; you can implement another anti-cheat solution i.e. checking file checksums to verify the integrity of your game files and more (there are third-party anti cheat solutions out there that you can use to protect your game files).