开发者

Server-side highscores for a Javascript-written game

开发者 https://www.devze.com 2022-12-20 13:16 出处:网络
I\'m implementing a simple game in Javascript, and am interested in having an online highscores table for it, so that players can compete against one another. I\'ve two concerns about this:

I'm implementing a simple game in Javascript, and am interested in having an online highscores table for it, so that players can compete against one another. I've two concerns about this:

  1. What is the simplest server-side program I need for this purpose? I don't need a full-fledged "web application", just something simple that gets POST requests with highscores, updates a database and sends back list开发者_JAVA技巧s of scores. I'm familiar with Django. What are your suggestions?
  2. How can I make the highscores table reasonably secure? I'm aware that making it bulletproof against competent and dedicated hackers is difficult, but I wouldn't want anyone with access to the Javascript sourcecode to be able to submit fictitious scores too simply. Any tools for this purpose?


It's going to be pretty hard to secure the high scores. I mean, it's not enough to ensure that it comes from your page, because if, say, the JavaScript function is submitHighScore(n) then they can always type javascript:submitHighScore(10000000) in the address bar on that page and have it work.

What comes to mind is perhaps some sort of hash function that generates specific codes that match certain levels in the game. When submitting the score it would also submit this hash, so users would have had to get to this level in order to get that equivalent score.

Another option would be for the game to pull in some kind of key that only works temporarily, so as you went along the key would change and then the score would be submitted to a central server intermittently.

Keep in mind that really determined individuals can always just track the data being sent to your data and decompile it.

You could go the Broderbund route and ask the player trivia questions which are validated server-side to ensure they really did pass the level they said they did...something like "What color was the monster in the previous level?"


To submit score securely, sign it (you'd also need to ensure that the score isn't faked before it's signed and sent, but that's another problem).

Hide a secret in JS code, and send highscore + hash(highscore + secret) to the server. The hash could be MD5/SHA1 — there are easy to find JS implementations.

Of course it won't stand anyone carefully analysing the JS code, but at least someone won't be able to submit fake highscore just by tampering with HTTP traffic.

On hiding stuff in JS:

You can't really hide it (it's ultimately futile like DRM), but you can try hard to obfuscate it and make debugging painful.

  1. Don't put the secret as a literal in the source code - compute it at run time combining results of several functions, local and global-ish variables.
  2. Minify all code, remove sourcemaps.
  3. Add bits of code that don't do anything, but seem to be important, to make debugging more confusing.
  4. Don't put anything in global scope, but do rely on shared mutable state by passing closures and arrays around.
  5. Rely on Date and timers to cause race conditions to make your code produce wrong results if it's paused in the debugger (just don't make it too tight to allow it to run on slow machines).

If the game is deterministic (like a puzzle game), then users could submit highscore in form of a log of steps taken to win (user's input) that you'd replay on the server to calculate the score.

This would change attack from finding/emulating score-submitting function to witing AI or hacking the game itself to make it easier to play (but still within its basic rules).


1.) Any CGI script that can talk to a database and understand JSON, or other format of your choice, will do the work.

However, if you're familiar with Django, building your server on top of Django would be the most simple, in the sense of what you have to learn and how much application code you have to write. Seemingly simple CGI script can turn out rather complex if you write it from scratch.

I found django-piston to be a handy Django app to quickly write a REST-style API server. It supports JSON so it should be easy to interface with your JavaScript game.

2.) The most casual cracker will go for a replay attack and its variants: peek at the page source and execute a JavaScript function, intercept HTTP requests and resend it (should be easy with a Firefox add-on like Tamper Data).

To counteract the former, you can obfuscate the source code and HTTP body;

  • Minify the JavaScript code
  • Encode the message you send to the server with Base64 or other encoding algorithm

The latter can be prevented by requiring all update requests to include an one-time password ("session token" in the Wikipedia article) that was recently acquired from the server.


I am thinking about this myself. What seems to be the most reasonable solution to me is this:

1) Sessions, to disallow tampering with the scoretable outside the game.

2) Log every action in the game and send it to the score server. The server will then calculate if those actions actually give such score. If you also log the time spent playing the game, you can further minimize the chance of an attacker to bother himself enough to break your game. This will also enable you to make a replay script like Arcade servers with hi-score tables have and in case of a suspicious score, you can watch the replay and decide for yourself if the score is real. The cheater would have to use a clever bot to play your game - and unless you have a game for real prizes, noone will try that hard.

If the cheater won't even analyze your code, sessions will stop him. If he reads your code, he would quickly break anything similar to hashed scores, secrets, tokens and whatsoever. But if you make the game-logging script thorough enough, he will give up.


In answer to your question:

1.) This depends on your environment and coding preference. PHP, Python, ASP.NET are a few that come to mind. Sense you already know Python (from your profile) you can use a Python CGI script to do this or use one of the many frameworks for Python (Zope, Django, Pylons,...).

see: http://www.python.org/doc/essays/ppt/sd99east/index.htm for info on Python CGI.

2.) A few tricks for security: (none or full-proof)

  • Hidden Text Box in HTML with encoded value that server checks to match a cookie to ensure high score comes from your page.
  • Server Script only accepts values from a specific domain


You could use a combination of one of the methods above, as well as simply requiring the user to be registered to be able to post high scores. Non registered users could view their current score compared to existing high scores, but in order to post your high score online, you must have already logged in with your registered account, or provide it when the app goes to update the score online.

A simple message along the lines of "Your high score is X, and ranks ### in the high score table. To post this score online, please register with us first".


The better I think, is to make the calculation of the score directly into the python files of your django app. Instead of calculate it in the javascript file. You send the datas to compare with your data basis with a POST request, then you calculate the score and you store it in your data basis. Like this, you don't make circulate the score across the web to your servers. Which is completely safe because you are only doing local stuffs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号