Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to surreptitiously modify a function in Javascript

Tags:

javascript

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.

like image 963
Nils André Avatar asked Mar 10 '26 14:03

Nils André


1 Answers

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;
}
like image 113
Bergi Avatar answered Mar 13 '26 04:03

Bergi