-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathraptor_frida_ios_trace.js
More file actions
166 lines (139 loc) · 4.01 KB
/
raptor_frida_ios_trace.js
File metadata and controls
166 lines (139 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* raptor_frida_ios_trace.js - ObjC and Module tracer for iOS
* Copyright (c) 2017-2025 Marco Ivaldi <raptor@0xdeadbeef.info>
*
* "Life is not like water. Things in life don't necessarily
* flow over the shortest possible route."
* -- Haruki Murakami, 1Q84
*
* Frida.re JS code to trace arbitrary ObjC methods and Module functions calls
* in an iOS app for debugging and reverse engineering. See https://www.frida.re/
* and https://codeshare.frida.re/ for further information on this world-class
* dynamic instrumentation toolkit.
*
* Example usage:
* $ uv tool install frida-tools
* $ frida -U -f com.target.app -l raptor_frida_ios_trace.js
*
* Tested with:
* Frida 17.3.2 on macOS 15.6.1 with iPhone 8 (iOS 16.5 + https://palera.in/)
*
* Thanks:
* @inode-, @federicodotta, @mrmacete, @dankluev
*
* Get the latest version at:
* https://github.com/0xdea/frida-scripts/
*/
// Generic trace
// TODO: support the "swift" type
function trace(pattern)
{
var type = (pattern.indexOf(" ") === -1) ? "module" : "objc";
var res = new ApiResolver(type);
var matches = res.enumerateMatches(pattern);
var targets = uniqBy(matches, JSON.stringify);
targets.forEach(function(target) {
if (type === "objc")
traceObjC(target.address, target.name);
else if (type === "module")
traceModule(target.address, target.name);
});
}
// Remove duplicates from array
function uniqBy(array, key)
{
var seen = {};
return array.filter(function(item) {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
}
// Trace ObjC methods
function traceObjC(impl, name)
{
console.log("Tracing " + name);
Interceptor.attach(impl, {
onEnter: function(args) {
// Trace only the intended calls
this.flag = 0;
// if (ObjC.Object(args[2]).toString() === "1234567890abcdef1234567890abcdef12345678")
this.flag = 1;
if (this.flag) {
console.warn("\n*** entered " + name);
// Print full backtrace
// console.log("\nBacktrace:\n" + Thread.backtrace(this.context, Backtracer.ACCURATE)
// .map(DebugSymbol.fromAddress).join("\n"));
// Print caller
console.log("\nCaller: " + DebugSymbol.fromAddress(this.returnAddress));
// Print args
if (name.indexOf(":") !== -1) {
console.log();
var par = name.split(":");
par[0] = par[0].split(" ")[1];
for (var i = 0; i < par.length - 1; i++)
printArg(par[i] + ": ", args[i + 2]);
}
}
},
onLeave: function(retval) {
if (this.flag) {
// Print retval
printArg("\nretval: ", retval);
console.warn("\n*** exiting " + name);
}
}
});
}
// Trace Module functions
function traceModule(impl, name)
{
console.log("Tracing " + name);
Interceptor.attach(impl, {
onEnter: function(args) {
// Trace only intended calls
this.flag = 0;
// var filename = args[0].readCString();
// if (filename.indexOf("Bundle") === -1 && filename.indexOf("Cache") === -1) // exclusion list
// if (filename.indexOf("my.interesting.file") !== -1) // inclusion list
this.flag = 1;
if (this.flag) {
console.warn("\n*** entered " + name);
// Print backtrace
console.log("\nBacktrace:\n" + Thread.backtrace(this.context, Backtracer.ACCURATE)
.map(DebugSymbol.fromAddress).join("\n"));
}
},
onLeave: function(retval) {
if (this.flag) {
// Print retval
printArg("\nretval: ", retval);
console.warn("\n*** exiting " + name);
}
}
});
}
// Print helper
// TODO: implement a safe way to print ObjC objects and especially NSStrings/CStrings
function printArg(desc, arg)
{
/*
try {
console.log(desc + ObjC.Object(arg));
}
catch(err) {
console.log(desc + arg);
}
*/
console.log(desc + arg);
}
// Usage examples
if (ObjC.available) {
// trace("-[CredManager setPassword:]");
// trace("*[CredManager *]");
// trace("*[* *Password:*]");
// trace("exports:libSystem.B.dylib!CCCrypt");
// trace("exports:libSystem.B.dylib!open");
// trace("exports:*!open*");
} else {
send("error: Objective-C Runtime is not available!");
}