user5454775
user5454775

Reputation:

Is it possible in JS to detect whether any JS was run from the browser's console?

I'm making a JavaScript game, and I don't want anyone to cheat. Is there any way that I can detect, and possibly log, commands issue in the JavaScript console? The skeleton of what I'd be looking for is something like:

consoleCommands = "";
window.console.command = function ( thiscmd ) 
{
   consoleCommands += thiscmd;
}
window.onbeforeunload = function ( )
{
    // send log of commands to server
    $.ajax({url:'recordNewCommandLog',method:'POST',data:{cmdlog:consoleCommands}});
};

Of course, an attacker could overwrite that, but I'll try to obfuscate it enough to make it difficult for him to figure out that his commands are getting logged in the first place.

Upvotes: 2

Views: 892

Answers (2)

Madara's Ghost
Madara's Ghost

Reputation: 174957

In addition to the answers (and duplicate links) you've received, I want you to consider this:

Why do you care someone cheated?

  • If it's a single player game, let them cheat, they only cheat themselves and you can't stop them. If you block the console, they'll use a user script. If you block that, they'll use a memory altering program (like CheatEngine) and change values from there.
  • If it's a multi-player game, it makes sense to invest in anti-cheat so that they don't ruin it for everyone else. In that case, make the server make all the important decisions. Don't take the word of the client for anything, it's the server that's supposed to be secure, and it's the server that's supposed to be able to tell that a player couldn't have gotten 9999999 money from the monster drop, just because it said so.

Not to mention, how will you do your debugging without the console? You'll have a hard time reproducing bugs and fixing problems.


TL;DR

Don't block the console. Secure your server if applicable and leave the client "hackable" the client is the user's domain anyway, not yours.

Upvotes: 4

guest271314
guest271314

Reputation: 1

var arr = [];

console.log = function () {
  arr.push([].slice.call(arguments));
  // if `arr.length` , do stuff
  return console.log
}

Upvotes: 1

Related Questions