I'm trying to modify a function:
console.error = function() {
return "fake";
}
However, it is possible to detect the function was changed by just running toString() on it:
> console.error.toString()
'function() {\nreturn "fake";\n}'
If the function hadn't been modified, 'function () { [native code] }' would be returned instead.
A solution can be to override toString(), however, it is possible to see that toString has been overriden by running toString on it:
> console.error.toString = () => 'function () { [native code] }';
> console.error.toString()
'function () { [native code] }'
> console.error.toString.toString()
"() => 'function () { [ native code ] }'"
Is there anyway to recursively override toString() or any other method such that it is not possible to detect the function was overriden?
The use case would be for a WebExtension to modify some functions while making it as hard as possible for a website to detect it.
You can overwrite Function.prototype.toString itself. However, it might still be possible for other code to circumvent this - easiest by taking a reference to the method before you've overwritten it, rather intricate by restoring a version from a different realm.
{
const Console = console.constructor;
const origToString = Function.prototype.toString;
const origError = Console.prototype.error;
const {error} = {error() { return "fake"; }};
const {toString} = {toString() {
let target = this;
if (this == toString) target = origToString;
if (this == error) target = origError;
return origToString.call(target);
}};
Function.prototype.toString = toString;
Console.prototype.error = error;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With