Frida Trace (frida-trace)
frida-trace is a frida script used to trace the classes and functions within an application
Using frida-trace it is possible to create hooking functions. To use frida-trace you need to first find the functions to hook into. To do this you can use frida-ps to find the correct process running, like so:
1
$ frida-ps -Ua
2
​
3
PID Name Identifier
4
----- -------- ---------------------------------
5
15929 DVIA-v2 com.highaltitudehacks.DVIAswiftv2
6
15916 Settings com.apple.Preferences
Copied!
Once you have identified the application you want to hook in to you can start either tracing all of the functions within the application, which could take a long time, or you can filter them out by specifying exactly what you want to look for. frida-trace also accepts wild card searching.
1
$ frida-trace -U -m "*[*cryp* *]" -p 15929 ξ‚² βœ”
2
​
3
Instrumenting...
4
-[NEIKEv2EncryptionProtocol isEqual:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/isEqual_.js"
5
-[NEIKEv2EncryptionProtocol hash]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/hash.js"
6
-[NEIKEv2EncryptionProtocol description]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/description.js"
7
-[NEIKEv2EncryptionProtocol copyWithZone:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/copyWithZone_.js"
8
-[NEIKEv2EncryptionProtocol isAuthenticated]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/isAuthenticated.js"
9
-[NEIKEv2EncryptionProtocol setWireType:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/setWireType_.js"
10
-[NEIKEv2EncryptionProtocol setIs256Bit:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/setIs256Bit_.js"
11
-[NEIKEv2EncryptionProtocol wireType]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/wireType.js"
12
-[NEIKEv2EncryptionProtocol is256Bit]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/is256Bit.js"
13
-[NEIKEv2EncryptionProtocol initWithEncryptionWireType:is256Bit:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/initWithEncryptionWireType_is256Bit_.js"
14
-[NEIKEv2EncryptionProtocol isGCM]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/isGCM.js"
15
-[NEIKEv2EncryptionProtocol initWithEncryptionType:]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/initWithEncryptionType_.js"
16
-[NEIKEv2EncryptionProtocol keyLength]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/keyLength.js"
17
-[NEIKEv2EncryptionProtocol blockLength]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/blockLength.js"
18
-[NEIKEv2EncryptionProtocol ccAlgorithm]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/ccAlgorithm.js"
19
-[NEIKEv2EncryptionProtocol ivLength]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/ivLength.js"
20
-[NEIKEv2EncryptionProtocol icvLength]: Auto-generated handler at "/home/mantis/__handlers__/NEIKEv2EncryptionProtocol/icvLength.js"
21
+[RNOpenSSLDecryptor decryptData:withSettings:password:error:]: Auto-generated handler at "/home/mantis/__handlers__/RNOpenSSLDecryptor/decryptData_withSettings_password_error_.js"
22
+[RNOpenSSLDecryptor decryptData:withPassword:error:]: Auto-generated handler at "/home/mantis/__handlers__/RNOpenSSLDecryptor/decryptData_withPassword_error_.js"
23
+[RNOpenSSLDecryptor decryptData:withEncryptionKey:HMACKey:error:]: Auto-generated handler at "/home/mantis/__handlers__/RNOpenSSLDecryptor/decryptData_withEncryptionKey_HM_72d173d3.js"
24
+[RNOpenSSLDecryptor decryptData:withSettings:encryptionKey:IV:error:]: Auto-generated handler at "/home/mantis/__handlers__/RNOpenSSLDecryptor/decryptData_withSettings_encrypt_7a7b5d2a.js"
25
<Removed for brevity>
26
​
27
Started tracing 406 functions. Press Ctrl+C to stop.
28
​
Copied!
Once these functions have been instrumented you can go back to using the application until you find where and how these functions are called. frida-trace automatically creates a handler for each class and method it finds. This means that you can modify the handler script to do more useful actions such as dumping the arguments and return values.
The example below shows that we have instrumented all of the *[RNDecryptor *] functions.
1
2110 ms +[RNDecryptor decryptData:0x280ba1720 withPassword:0x2805fcc60 error:0x16b981d48]
2
2110 ms | -[RNDecryptor initWithPassword:0x2805fcc60 handler:0x104823378]
3
2110 ms | | -[RNDecryptor initWithEncryptionKey:0x0 HMACKey:0x0 handler:0x104823378]
4
2110 ms | -[RNDecryptor addData:0x280ba1720]
5
2110 ms | | -[RNDecryptor inData]
6
2110 ms | | -[RNDecryptor inData]
7
2110 ms | | -[RNDecryptor consumeHeaderFromData:0x2805fdcb0]
8
2110 ms | | | -[RNDecryptor updateOptionsForPreamble:0x280ba0880]
9
2110 ms | | | -[RNDecryptor settings]
10
2110 ms | | | -[RNDecryptor settings]
11
2110 ms | | | -[RNDecryptor settings]
12
2110 ms | | | -[RNDecryptor encryptionKey]
13
2110 ms | | | -[RNDecryptor HMACKey]
14
2110 ms | | | -[RNDecryptor settings]
15
2110 ms | | | -[RNDecryptor settings]
16
2110 ms | | | -[RNDecryptor password]
17
2110 ms | | | -[RNDecryptor settings]
18
2118 ms | | | -[RNDecryptor setEncryptionKey:0x2805fee80]
19
2118 ms | | | -[RNDecryptor password]
20
2118 ms | | | -[RNDecryptor settings]
21
2126 ms | | | -[RNDecryptor setHMACKey:0x28059ee80]
22
2126 ms | | | -[RNDecryptor setPassword:0x0]
23
2126 ms | | | -[RNDecryptor settings]
24
2126 ms | | | -[RNDecryptor settings]
25
2126 ms | | | -[RNDecryptor encryptionKey]
26
2127 ms | | | -[RNDecryptor setEncryptionKey:0x0]
27
2127 ms | | | -[RNDecryptor HMACKey]
28
2127 ms | | | -[RNDecryptor settings]
29
2127 ms | | | -[RNDecryptor HMACKey]
30
2127 ms | | | -[RNDecryptor HMACKey]
31
2127 ms | | | -[RNDecryptor settings]
32
2127 ms | | | -[RNDecryptor setHMACKey:0x0]
33
2127 ms | | | -[RNDecryptor hasV1HMAC]
34
2127 ms | | -[RNDecryptor inData]
35
2127 ms | | -[RNDecryptor inData]
36
2127 ms | | -[RNDecryptor inData]
37
2127 ms | | -[RNDecryptor decryptData:0x280bab120]
38
2127 ms | -[RNDecryptor finish]
39
/* TID 0x11f5b */
40
2127 ms -[RNDecryptor inData]
41
/* TID 0x403 */
42
2127 ms | -[RNDecryptor .cxx_destruct]
43
​
Copied!
We can use the handler functions to grab the password from the password function as an example. So we open up the __handlers__/RNDecryptor/password.js file, as this is where the function can be manipulated.
As seen in the onLeave function we can log out the return value of the hooked function call. Due to the way that frida works we have to cast the return value to a string. This means that we have to use the ObjC.Object(retval).toString() method.
1
/*
2
* Auto-generated by Frida. Please modify to match the signature of -[RNDecryptor password].
3
* This stub is currently auto-generated from manpages when available.
4
*
5
* For full API reference, see: https://frida.re/docs/javascript-api/
6
*/
7
​
8
{
9
/**
10
* Called synchronously when about to call -[RNDecryptor password].
11
*
12
* @this {object} - Object allowing you to store state for use in onLeave.
13
* @param {function} log - Call this function with a string to be presented to the user.
14
* @param {array} args - Function arguments represented as an array of NativePointer objects.
15
* For example use args[0].readUtf8String() if the first argument is a pointer to a C string encoded as UTF-8.
16
* It is also possible to modify arguments by assigning a NativePointer object to an element of this array.
17
* @param {object} state - Object allowing you to keep state across function calls.
18
* Only one JavaScript function will execute at a time, so do not worry about race-conditions.
19
* However, do not use this to store function arguments across onEnter/onLeave, but instead
20
* use "this" which is an object for keeping state local to an invocation.
21
*/
22
onEnter(log, args, state) {
23
log(`-[RNDecryptor password]`);
24
},
25
​
26
/**
27
* Called synchronously when about to return from -[RNDecryptor password].
28
*
29
* See onEnter for details.
30
*
31
* @this {object} - Object allowing you to access state stored in onEnter.
32
* @param {function} log - Call this function with a string to be presented to the user.
33
* @param {NativePointer} retval - Return value represented as a NativePointer object.
34
* @param {object} state - Object allowing you to keep state across function calls.
35
*/
36
onLeave(log, retval, state) {
37
log("Return Value: " + ObjC.Object(retval).toString());
38
}
39
}
40
​
Copied!
When we perform the action again within the application we can see that frida-trace has successfully logged out the encryption password.
1
...
2
1783 ms | | | -[RNDecryptor setEncryptionKey:0x280513cc0]
3
1783 ms | | | -[RNDecryptor password]
4
1783 ms | | | Return Value: @daloq3as$qweasdlasasjdnj
5
...
Copied!
Last modified 8mo ago
Copy link