Search code examples
javaandroidfrida

frida calls the wrong constructor


I'm trying to hook the following constructor of java.net.URL: URL(URL context, String spec, URLStreamHandler handler)

My code is as follows:

import argparse
import frida
import sys

jscode = """
Java.perform(function () {
    Java.use('java.net.URL').$init.overload('java.net.URL', 'java.lang.String', 'java.net.URLStreamHandler').implementation = function(arg0,arg1,arg2) {
          return this.$init(arg0,arg1,arg2);
      };
});
"""

parser = argparse.ArgumentParser()
parser.add_argument("target")
args = parser.parse_args()
process = frida.get_usb_device().attach(args.target)
script = process.create_script(jscode)
script.load()
sys.stdin.read()

This causes the application (and sometimes frida-server) to crash. Inspecting the logcat shows:

03-25 16:09:03.347 23490 23563 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(URL.java:391)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(URL.java:316)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(URL.java:339)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(Native Method)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(URL.java:498)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at java.net.URL.<init>(URL.java:447)
03-25 16:09:03.347 23490 23563 E AndroidRuntime:        at okhttp3.HttpUrl.url(HttpUrl.kt:1)
...

This means that the init I tried to call is the wrong init function, as the URL.java:339 maps to the URL(String protocol, String host, String file) constructor. I suspect this might be because both arg0 and arg2 are null in this call chain.

Is there a way to call to a specific overload?


Solution

  • I got an answer on the frida github. The solution is:

    Java.perform(function () {
      var URL = Java.use('java.net.URL');
    
      var ctor = URL.$init.overload('java.net.URL', 'java.lang.String', 'java.net.URLStreamHandler');
      ctor.implementation = function (arg0, arg1, arg2) {
        return ctor.call(this, arg0, arg1, arg2);
      };
    });