frida calls the wrong constructor

╄→гoц情女王★ 提交于 2021-01-07 03:42:53

问题


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?


回答1:


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);
  };
});


来源:https://stackoverflow.com/questions/60851184/frida-calls-the-wrong-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!