Created
April 17, 2024 22:10
-
-
Save komen205/25d3ed4e3faf60c2948c9b215b1c69ec to your computer and use it in GitHub Desktop.
okhttp hook modified by @komen205
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hexToAscii = function(input){ | |
var hex = input.toString(); | |
var str = ''; | |
for (var i = 0; i < hex.length; i += 2) | |
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); | |
return str; | |
} | |
function hook_okhttp3() { | |
Java.perform(function() { | |
var ByteString = Java.use("com.android.okhttp.okio.ByteString"); | |
var Buffer = Java.use("okio.Buffer"); | |
var Interceptor = Java.use("okhttp3.Interceptor"); | |
var MyInterceptor = Java.registerClass({ | |
name: "okhttp3.MyInterceptor", | |
implements: [Interceptor], | |
methods: { | |
intercept: function(chain) { | |
var request = chain.request(); | |
console.log("MyInterceptor.intercept onEnter:", request, "\nrequest headers:\n", request.headers()); | |
var requestBody = request.body(); | |
var contentLength = requestBody ? requestBody.contentLength() : 0; | |
if (contentLength > 0) { | |
var BufferObj = Buffer.$new(); | |
requestBody.writeTo(BufferObj); | |
try{ | |
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new())) | |
console.log("\nrequest body String:\n", BufferObj.readString(), "\n"); | |
} | |
catch(error){ | |
console.log(hexToAscii(ByteString.of(BufferObj.readByteArray()).hex())); | |
} | |
} | |
console.log("\n\n\n") | |
var response = chain.proceed(request); | |
try { | |
var ContentType = response.headers().get("Content-Type"); | |
console.log("MyInterceptor.intercept onLeave:", response, "\nresponse headers:\n", response.headers()); | |
console.log("ContentType:", ContentType); | |
// console.log("ContentType:", ContentType); | |
var responseBody = response.body(); | |
console.log("responseBody", hexToAscii(responseBody.source().readByteString().hex())) | |
var contentLength = responseBody.source().readByteString().hex().length; | |
// console.log("contentLength",responseBody.contentLength()) | |
} catch (error) { | |
console.log("error 3:", error); | |
} | |
return response; | |
} | |
} | |
}); | |
var ArrayList = Java.use("java.util.ArrayList"); | |
var OkHttpClient = Java.use("okhttp3.OkHttpClient"); | |
console.log(OkHttpClient); | |
OkHttpClient.$init.overload('okhttp3.OkHttpClient$Builder').implementation = function(Builder) { | |
console.log("OkHttpClient.$init:", this, Java.cast(Builder.interceptors(), ArrayList)); | |
this.$init(Builder); | |
}; | |
var MyInterceptorObj = MyInterceptor.$new(); | |
var Builder = Java.use("okhttp3.OkHttpClient$Builder"); | |
console.log(Builder); | |
Builder.build.implementation = function() { | |
this.interceptors().clear(); | |
//var MyInterceptorObj = MyInterceptor.$new(); | |
this.interceptors().add(MyInterceptorObj); | |
var result = this.build(); | |
return result; | |
}; | |
Builder.addInterceptor.implementation = function(interceptor) { | |
this.interceptors().clear(); | |
//var MyInterceptorObj = MyInterceptor.$new(); | |
this.interceptors().add(MyInterceptorObj); | |
return this; | |
//return this.addInterceptor(interceptor); | |
}; | |
console.log("hook_okhttp3..."); | |
}); | |
} | |
hook_okhttp3() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment