January 22, 2012 1 Response

Detecting the Cheat Engine’s Speedhack in AS3

Another cool feature of the Cheat Engine is the so called “Speedhack” which allows you to slow down or speed up the execution of your (Flash) game. Cheat Engine achieves this by replacing some commonly used functions in games (or the Flash Player) to keep the speed constant with its own routine that goes faster or slower.

One approach to detect the speedhack could be to periodically check the difference between the system’s (OS) time and the Flash Player’s time. If the difference is too big the speedhack might be active and a warning could be dispatched or the alleged cheater could be e.g. marked in a black list ;-)

The SpeedWatchDog Class does dispatch a SecurityErrorEvent as I don’t wanted to use a custom event and the “SecurityErrorEvent” seemed pretty bold…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package
{
 import flash.events.Event;
 import flash.events.EventDispatcher;
 import flash.events.SecurityErrorEvent;
 import flash.utils.clearInterval;
 import flash.utils.getTimer;
 import flash.utils.setInterval;
 
 /**
 * @author Björn Acker | www.bjoernacker.de
 */
 public class SpeedWatchDog extends EventDispatcher
 {
 protected var _tolerance:Number;
 protected var _prevDate:Number;
 protected var _prevTimer:int;
 protected var _interval:uint;
 
 [Event(name="securityError",type="flash.events.SecurityErrorEvent")]
 
 public function init(interval:int = 1000, tolerance:Number = 0.35):void
 {
 _tolerance = tolerance;
 _prevTimer = getTimer();
 _prevDate = new Date().time;
 this.interval = interval;
 }
 
 public function stop():void
 {
 clearInterval(_interval);
 }
 
 public function get tolerance():Number
 {
 return _tolerance;
 }
 
 public function set tolerance(value:Number):void
 {
 _tolerance = value;
 }
 
 public function get interval():uint
 {
 return _interval;
 }
 
 public function set interval(value:uint):void
 {
 clearInterval(_interval);
 _interval = setInterval(check, value);
 }
 
 protected function check():void
 {
 var date:Number = new Date().time - _prevDate;
 var timer:Number = getTimer() - _prevTimer;
 var t:Number = date * _tolerance;
 if (timer > date + t || timer < date - t)
 {
 dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
 }
 _prevTimer = getTimer();
 _prevDate = new Date().time;
 }
 }
}
package
{
 import flash.events.Event;
 import flash.events.EventDispatcher;
 import flash.events.SecurityErrorEvent;
 import flash.utils.clearInterval;
 import flash.utils.getTimer;
 import flash.utils.setInterval;

 /**
 * @author Björn Acker | www.bjoernacker.de
 */
 public class SpeedWatchDog extends EventDispatcher
 {
 protected var _tolerance:Number;
 protected var _prevDate:Number;
 protected var _prevTimer:int;
 protected var _interval:uint;

 [Event(name="securityError",type="flash.events.SecurityErrorEvent")]

 public function init(interval:int = 1000, tolerance:Number = 0.35):void
 {
 _tolerance = tolerance;
 _prevTimer = getTimer();
 _prevDate = new Date().time;
 this.interval = interval;
 }

 public function stop():void
 {
 clearInterval(_interval);
 }

 public function get tolerance():Number
 {
 return _tolerance;
 }

 public function set tolerance(value:Number):void
 {
 _tolerance = value;
 }

 public function get interval():uint
 {
 return _interval;
 }

 public function set interval(value:uint):void
 {
 clearInterval(_interval);
 _interval = setInterval(check, value);
 }

 protected function check():void
 {
 var date:Number = new Date().time - _prevDate;
 var timer:Number = getTimer() - _prevTimer;
 var t:Number = date * _tolerance;
 if (timer > date + t || timer < date - t)
 {
 dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
 }
 _prevTimer = getTimer();
 _prevDate = new Date().time;
 }
 }
}

Usage:

1
2
3
4
5
6
7
8
9
var wd:SpeedWatchDog = new SpeedWatchDog();
//Check every 2 seconds with a tolerance of 40%
wd.init(2000, 0.4);
wd.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSpeedHack;
 
function onSpeedHack(e:Event):void
{
   trace("speedhack detected");
}
var wd:SpeedWatchDog = new SpeedWatchDog();
//Check every 2 seconds with a tolerance of 40%
wd.init(2000, 0.4);
wd.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSpeedHack;

function onSpeedHack(e:Event):void
{
   trace("speedhack detected");
}

  

 

 

 


Filed in Flash

One Response

Leave a Reply

Your address will not be published.