Hi, 🙂 many android applications depends on okhttp3 java package to communicate with online services. If you want to perform dynamic testing on this package to analyze messages, this post will be helpful. I specifically write this post since I found it troublesome at first to hook this method and read the response body. This is due to two reasons; first, the read methods cannot hook directly since they are abstract; second, since the buffer read consumes the data, it affects the application running on the device throwing java.io.EOFException. To avoid these problems you can follow the following solution.
Steps:
- Hook the constructor of “okhttp3.Response”. The constructor argument is of type “okhttp3.Response$Build”.
- Get the object field “body “of the param.args[0].
- Next, if it is not null, get the object field “source”, which is of type “okio. BufferedSource”.
- Next, call the method buffer(), which returns an “okio.Buffer” instance.
- Before reading from the Buffer, obtained a clone by calling clone() method.
- Finally, call the readUtf8() on the cloned buffer instance.
XposedHelpers.findAndHookConstructor("okhttp3.Response", loadPackageParam.classLoader, "okhttp3.Response$Builder", new XC_MethodHook() { Object responseBody = XposedHelpers.getObjectField(param.args[0], "body"); if (responseBody.getClass().getCanonicalName().trim().equals("okhttp3.internal.http.RealResponseBody".trim())) { Object source = XposedHelpers.getObjectField(responseBody, "source"); Object buffer = XposedHelpers.callMethod(source, "buffer"); Object copy = XposedHelpers.callMethod(buffer, "clone"); Object body = XposedHelpers.callMethod(copy, "readUtf8"); Log.v("xposed1", "okhttp3.Response constructor body : " + body); } });
Cheers !