Skip to content

Instantly share code, notes, and snippets.

@ReimuNotMoe
Last active December 7, 2019 19:57
Show Gist options
  • Save ReimuNotMoe/f19ebc3215e4e893803144d3c717ed99 to your computer and use it in GitHub Desktop.
Save ReimuNotMoe/f19ebc3215e4e893803144d3c717ed99 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name NoPerformanceNow
// @namespace https://yuki.network/
// @version 0.2
// @description Disables performance.now() to make JavaScript exploits harder.
// @author ReimuNotMoe, ClassicOldSong
// @match *://*/*
// @grant unsafeWindow
// ==/UserScript==
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
const oldConsoleError = console.error.bind(console);
const oldNow = performance.now;
const oldToLocalString = Function.prototype.toLocalString;
const oldToString = Function.prototype.toString;
const oldToSource = Function.prototype.toSource;
const originalFns = [];
const overwrittenFns = [];
let fakeNow = 0;
const now = () => {
oldConsoleError("performance.now() is being used");
return fakeNow += Math.random() * 1000;
};
function toLocalString(...args) {
const idx = overwrittenFns.indexOf(this);
if (idx >= 0) return oldToLocalString.call(originalFns[idx]);
return oldToLocalString.call(this, ...args);
}
function toString(...args) {
const idx = overwrittenFns.indexOf(this);
if (overwrittenFns.indexOf(this) >= 0) return oldToString.call(originalFns[idx]);
return oldToString.call(this, ...args);
};
function toSource(...args) {
const idx = overwrittenFns.indexOf(this);
if (overwrittenFns.indexOf(this) >= 0) return oldToSource.call(originalFns[idx]);
return oldToSource.call(this, ...args);
};
originalFns.push(performance.now, oldToLocalString, oldToString, oldToSource);
overwrittenFns.push(now, toLocalString, toString, toSource);
(function() {
'use strict';
unsafeWindow.performance.__proto__.now = now;
if (oldToLocalString) Function.prototype.toLocalString = toLocalString;
if (oldToString) Function.prototype.toString = toString;
if (oldToSource) Function.prototype.toSource = toSource;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment