简体   繁体   English

阻止用户在浏览器控制台中更改游戏值

[英]Prevent user from changing game values in the browser console

Any player can easily cheat on this game by changing the amount of cupcakes through the browser console. 任何玩家都可以通过浏览器控制台更改纸杯蛋糕的数量轻松欺骗这款游戏。 I wanted to know if there was a way to prevent the user from doing so. 我想知道是否有办法阻止用户这样做。 This is also posted on github . 这也发布在github上

var numberOfCupcakesDisplay = document.getElementById("numberOfCupcakes");
var cupcake = document.getElementById("cupcake");

function updateValues() {
    numberOfCupcakesDisplay.innerHTML = amountOfCupcakes.toString();
    //displays number of cupcakes to screen
    document.getElementById("amountOfToasters").innerHTML = amountOfToasters.toString();
    //displays number of toasters to screen
    document.getElementById("toasterButton").innerHTML = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + Math.round(costOfToasters).toString() + " cc";
    //changes cost of toaster every time it is bought by 110%
}

cupcake.addEventListener('webkitAnimationEnd', function(e){
    e.preventDefault();
    this.style.webkitAnimationName = '';
}, false);

function clickCupcake() {
    ++amountOfCupcakes;
    cupcake.style.animation = "shiftCupcake";
    cupcake.style.webkitAnimationName = "shiftCupcake";
}

updateValues();

Yeah!! 是啊! As other answers mention it is IMPOSSIBLE to prevent users from changing the values from browser console. 正如其他答案所述,阻止用户更改浏览器控制台中的值是不可能的 In other words, You are simply asking How to write JS code so that user can not debug it . 换句话说,您只是在询问如何编写JS代码,以便用户无法对其进行调试 You can always use some methods which make life hard for people who want to cheat on the game. 你可以随时使用一些方法,让想要在游戏中作弊的人生活艰难。

1.Obfuscate the code. 1.批量处理代码。

Look at these links for more information on obfuscation. 有关混淆的更多信息,请查看这些链接。

2.Do not store the game's control values in global variable. 2.不要将游戏的控制值存储在全局变量中。

Do not store control values in global variables. 不要将控制值存储在全局变量中。 Instead define a class and have these variables as private to it So that user has to dig in deep in order to find out where to put the breakpoints. 而是定义一个类并将这些变量作为私有变量,以便用户必须深入挖掘才能找到断点的位置。

3.Minify/Compress your javascripts. 3.Minify /压缩你的javascripts。

Obfuscation more or less covers minification as well. 混淆或多或少也包括缩小。

You cannot prevent this. 你不能阻止这一点。 The best you can do is minify the JS so it's harder for players to find the right values to change. 你能做的最好就是缩小JS,这样玩家就更难找到合适的值来改变。 In fact, you can't prevent this with any game or indeed application; 事实上,你不能用任何游戏或实际应用来防止这种情况; the user's computer controls all of the information so they can do anything they want to it. 用户的计算机控制所有信息,以便他们可以做任何他们想做的事情。

The only way to be secure against this is to do all of the processing on a server you control. 防止这种情况的唯一方法是在您控制的服务器上执行所有处理。 Even though, players can lie about their input data (hence aimbotting or other hacks). 即使玩家可以撒谎他们的输入数据(因此瞄准或其他黑客攻击)。

by changing the amount of cupcakes through the browser console 通过浏览器控制台更改纸杯蛋糕的数量

Well, technically you can prevent this by creating a new scope: 那么,从技术上讲,您可以通过创建新范围来防止这种情况:

(function(){
    var cupcakes = 10;
})();

console.log(cupcakes); // ReferenceError: cupcakes is not defined

But note that there are still ways to "cheat," even with this protection (ie edit the JS source code). 但请注意,即使有这种保护(即编辑JS源代码),仍然有“欺骗”的方法。 And if you are sending the result to a server ... well, that will be even harder if not impossible to secure, since you can easily send a simple HTTP request via XMLHttpRequest . 如果您要将结果发送到服务器 ......那么,即使不是不可能保护也会更加困难,因为您可以通过XMLHttpRequest轻松发送简单的HTTP请求。

There may actually be a way to reach your goal. 实际上可能有办法达到目标。 But it depends on the game's logic. 但这取决于游戏的逻辑。

If you can simulate the game on your server, you can generate random IDs for the cakes and when the game creates a new cake (on client), it would request an ID for it. 如果您可以在服务器上模拟游戏,则可以为蛋糕生成随机ID,当游戏创建新蛋糕(在客户端上)时,它会为其请求ID。 Later when the cake is "destroyed", it would contact the server and provide it with the cake's ID. 蛋糕被“销毁”后,它会联系服务器并提供蛋糕ID。 The final score is then computed on the server from the IDs it received. 然后根据收到的ID在服务器上计算最终得分。

This way you can track what was actually done by the user. 这样,您可以跟踪用户实际执行的操作。

But an adversary can listen the network requests and determine the request for creating a new ID. 但攻击者可以监听网络请求并确定创建新ID的请求。 The same way he can determine the request to tell the server this ID has been "destroyed". 他可以通过相同的方式确定告诉服务器此ID已被“销毁”的请求。

If your game logic allows, you can eg tell the server to only generate one ID every 5 seconds or so. 如果您的游戏逻辑允许,您可以告诉服务器每5秒左右只生成一个ID。 You can also mark as spoofed all results where all cakes were "destroyed" and none was missed (if the game is difficult enough). 您还可以将所有结果标记为欺骗性所有结果,其中所有蛋糕都被“销毁”并且没有错过(如果游戏足够困难)。

So, it's still not 100% secure, but you might be able to reach a relatively high level of spoofing protection. 因此,它仍然不是100%安全,但您可能能够达到相对较高级别的欺骗保护。 On the other hand, it adds a lot of code complications and you must weigh the pros and cons. 另一方面,它增加了许多代码复杂性,你必须权衡利弊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM