Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing code from being executed by the user

So I'm writing a game with JavaScript, and the biggest problem with that is that whoever is playing it who might have a decent knowledge of JS could open up my source code, figure out how it works, then plug a game.score = 100000000000000; into the console. However, it seems that aside from obfuscating the script, wrapping everything in an anonymous function does the trick.

(function() {
    game_start = function() {
        //etc
    }
    //etc
 })();

When I try and run any of the code from the Chrome Console, it comes up with undefined. This is the desired result, but is there a way around my method that would render it useless? Or even a better way to prevent things being run from the console?

like image 597
Polyov Avatar asked Sep 19 '12 00:09

Polyov


People also ask

What does DEP protect from?

Data Execution Prevention (DEP) is a Microsoft security feature that monitors and protects certain pages or regions of memory, preventing them from executing (usually malicious) code. When DEP is enabled, all data regions are marked as non-executable by default.

What is Data Execution Prevention and how does it work?

Data Execution Prevention (DEP) is a memory-protection feature in Microsoft Windows that prevents malicious code exploits. It monitors certain memory regions or pages and prevents them from executing malicious codes.

What is Data Execution Prevention in BIOS?

Data Execution Prevention (DEP) is a set of hardware and software technologies that perform additional checks on memory to help protect against malicious code exploits. Hardware-enforced DEP marks all memory locations in a process as non-executable unless the location explicitly contains executable code.


2 Answers

There is no way to ensure that person will not cheat. Even with badly obfuscated code (I'm talking about stuff like H5b(b){if(b.pd()&&b.Ca&&b.gKa){var a=b.ra(RT.Nwa)) you can still watch the UI changes or network requests and trace it from there.

Some people use AJAX calls to validate the score, e.g. every time the score increases/decreses the app will make an AJAX call to back-end and you track the score on back-end too. When user submits the final score you check it against back-end score and if it's different then you will know that the person was cheating.

However this will not prevent people from writing a script that makes those ajax calls and that way makes it's score valid.

tl;dr: no, there is no way to ensure that user-side code is not compromised.

like image 94
valentinas Avatar answered Oct 23 '22 19:10

valentinas


Sorry, but there is no way to truly prevent a user from cheating when the game runs on his machine. Any code which runs on the users machine can be manipulated by the user. When you want to make it impossible to cheat, you have to implement all the game logic on a server, don't sent any information the player isn't supposed to know and don't accept any commands the player isn't allowed to do.

like image 33
Philipp Avatar answered Oct 23 '22 17:10

Philipp