H5游戏外挂 - 变速齿轮

3,316 阅读1分钟

在H5上复刻外挂界经典外挂,变速齿轮

0.变速齿轮原理分析

使用扫扫描工具扫描被变速齿轮变速的程序 发现变速齿轮hook掉了window系统提供的时间函数

image.png 简单的反编译代码+简单的理解

image.png

hook系统提供的时间函数,返回修改后的时间

1.分析游戏引擎时间计算方法

通过 new Date().getTime()Date.now()来获取当前本地时间
performance.now()来获取程序运行时间

2.思路分析

方法一:hook Date的getTimenow和performance的now方法返回倍率的时间
方法二:返回增量倍率的时间

注意方法一直接返回倍率的时间,会造成时间戳过大,在需要大致正确的时间戳的地方会出错

3.代码实现

/*
 * 变速齿轮.js
 * Created by 还有醋v on 2021/7/18.
 * Copyright © 2021 haiyoucuv. All rights reserved.
 */

// 写个配置,方便修改
const __pxConfig = {
   px: 4,
   date_last_t: Date.now(),        // Date上一次的真实值
   date_last_c: Date.now(),        // Date上一次的计算值
   p_last_t: performance.now(),    // performance上一次的真实值
   p_last_c: performance.now(),    // performance上一次的计算值
};

/**
 * 计算方法
 * 当前时间 = 上一次计算时间 + (当前真实时间 - 上一次真实时间) * 当前倍率
 */

// es5写法
window.Date._now = Date.now;
window.Date.prototype._getTime = window.Date.prototype.getTime;
window.Date.prototype.getTime = function () {
   const { date_last_c, date_last_t, px } = __pxConfig;
   const t = __pxConfig.date_last_t = this._getTime();
   return date_last_c + (t - date_last_t) * px;
};

// 重写performance
window.performance._now = window.performance.now;
window.performance.now = function () {
   const { p_last_c, p_last_t, px } = __pxConfig;
   const t = window.performance._now();
   return p_last_c + (t - p_last_t) * px;
}

以下使用es6写法更清晰

// es6写法
window.Date = class Date extends window.Date {
   getTime() {
      const { date_last_c, date_last_t, px } = __pxConfig;
      const t = __pxConfig.date_last_t = super.getTime();
      return __pxConfig.date_last_c = date_last_c + (t - date_last_t) * px;
   };

   static now() {
      return super.now() * __pxConfig.px;
   }
}

4.测试

打开 chrome://dino/ 看到我们的小恐龙
在控制台中输入以上代码,现在是4倍速 再次输入__pxConfig.px = 1;改为正常速度
输入__pxConfig.px = 100;改为改成100倍速度

未命名.gif