// Hook JNI Function
varjni_func=Module.findExportByName("libnative.so","Java_com_example_app_Crypto_encrypt");if(jni_func){Interceptor.attach(jni_func,{onEnter:function(args){console.log("[JNI Hook] encrypt() called");// args[0] = JNIEnv*
// args[1] = jclass/jobject
// args[2] = first Java parameter
// Read jstring parameter
if(args[2]){varenv=Java.vm.getEnv();varjstr=args[2];varcstr=env.getStringUtfChars(jstr,null);console.log("Input: "+cstr.readCString());env.releaseStringUtfChars(jstr,cstr);}},onLeave:function(retval){// Read returned jstring
if(retval&&!retval.isNull()){varenv=Java.vm.getEnv();varcstr=env.getStringUtfChars(retval,null);console.log("Output: "+cstr.readCString());env.releaseStringUtfChars(retval,cstr);}},});}// Also hook the native method call from Java layer
Java.perform(function(){varCrypto=Java.use("com.example.app.Crypto");Crypto.encrypt.implementation=function(input){console.log("[Java Hook] encrypt called with: "+input);varresult=this.encrypt(input);console.log("[Java Hook] encrypt returned: "+result);returnresult;};});
枚举 JNI 函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionenumerateJNIFunctions(moduleName){varmodule=Process.getModuleByName(moduleName);varexports=module.enumerateExports();console.log("[JNI Enumeration] "+moduleName);exports.forEach(function(exp){if(exp.name.startsWith("Java_")){console.log(" "+exp.name+" @ "+exp.address);}});}// Usage example
enumerateJNIFunctions("libnative.so");
Hook Java 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
Java.perform(function(){varMyClass=Java.use("com.example.SecretClass");MyClass.secretMethod.implementation=function(arg1,arg2){console.log("secretMethod called with:",arg1,arg2);// Call original method and get return value
varretval=this.secretMethod(arg1,arg2);console.log("Original return value:",retval);returnretval;// Return original value
};});
Java.perform(function(){varUser=Java.use("com.example.User");User.$init.implementation=function(name,age){console.log("User object created with name:",name,"and age:",age);// Call original constructor
this.$init(name,age);};});
Hook Native 函数 (SSL_write)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
varssl_write=Module.findExportByName("libssl.so","SSL_write");Interceptor.attach(ssl_write,{onEnter:function(args){// args[0] is the SSL context
// args[1] is the buffer
// args[2] is the size
console.log("Intercepted SSL_write, size:",args[2].toInt32());// You can use hexdump(args[1]) to view the data
},onLeave:function(retval){// retval is the original return value
console.log("SSL_write returned:",retval.toInt32());},});
RPC 导出函数
JavaScript 脚本 (script.js):
1
2
3
4
5
6
7
8
9
10
11
12
functiongetSecretValueFromApp(){varsecret="";Java.perform(function(){// Assume there's a method to get the secret value
varUtils=Java.use("com.example.Utils");secret=Utils.getSecret();});returnsecret;}// Export function
rpc.exports.getsecret=getSecretValueFromApp;
Python 调用:
1
2
3
4
5
6
7
8
9
10
importfrida# ... Connect to device and attach to process ...# script = session.create_script(js_code)# ...# script.load()# Call the exported function from the scriptsecret=script.exports.getsecret()print("Secret from app:",secret)