-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinderHook.js
More file actions
275 lines (236 loc) · 8.42 KB
/
BinderHook.js
File metadata and controls
275 lines (236 loc) · 8.42 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//Source Help From:https://github.com/mame82/misc/blob/master/native_binder.ts#L119
class CPPParcelMapper {
constructor(binderClassName) {
this.className = binderClassName;
this.libBinder = Module.load("libbinder.so");
this.funPattern = this.createRegex(binderClassName);
this.exports = this.libBinder.enumerateExports();
this.filteredExports = this.exports.filter(expDetails => expDetails.name.match(this.funPattern));
this.hasInit = false;
this.exportMap = new Map();
this.initClass();
}
createRegex(binderClassName) { return new RegExp(`.*${binderClassName}.*transact.*`); }
initClass() {
if(this.hasInit === false) {
const reParcel = /.*android[0-9]{1,3}Parcel.*/;
this.libParcelExports = this.exports.filter(exp => exp.name.match(reParcel));
const requiredExports = [
"data", //_ZNK7android6Parcel4dataEv
"setDataPosition", //_ZNK7android6Parcel15setDataPositionEm (dosnt pick this up)
//"dataAvail",
//"dataPosition",
//"ipcData",
//"ipcDataSize",
"dataPosition", //_ZNK7android6Parcel12dataPositionEv
"readInt32", //_ZNK7android6Parcel9readInt32Ev
"readInt64", //_ZNK7android6Parcel9readInt64Ev
"readUint32", //_ZNK7android6Parcel10readUint32Ev
"readUint64", //_ZNK7android6Parcel10readUint64Ev
"readString16Inplace",
"readString8Inplace",
"dataSize" //_ZNK7android6Parcel8dataSizeEv
];
for (let expName of requiredExports) {
const exp = this.getExportByMethodName(expName);
console.log(exp.name + "\n");
if (exp)
this.exportMap.set(expName, exp);
else
console.log( `Can not find export for Parcel member function '${expName}'`);
}
this.hasInit = true;
}
}
getExportByMethodName(name) {
if (!this.libParcelExports)
return null;
const patZer = new RegExp(`.*android[0-9]{1,3}Parcel[0-9]{1,3}${name}E[RPabvfdjim]`); //If that fails then Check for a (M) at the end
const patOne = new RegExp(`.*android[0-9]{1,3}Parcel[0-9]{1,3}${name}E[RPabvfdji]`); //If the (2) char check (E) Followed with (RPabvfdji) Fails then use this Pattern
const patTwo = new RegExp(`.*android[0-9]{1,3}Parcel[0-9]{1,3}${name}E[RPabvfdji](?![a-zA-Z0-9])`); //Some end in 3 chars so we ignore the (3) char ones
let matches = this.libParcelExports.filter(e => e.name.match(patTwo));
if(matches.length <= 0)
matches = this.libParcelExports.filter(e => e.name.match(patOne));
if(matches.length <= 0)
matches = this.libParcelExports.filter(e => e.name.match(patZer));
if(matches.length >= 1) {
for(let m of matches)
if(m.type === "function")
return m;
}
return null;
}
get(exportName) {
return this.exportMap.get(exportName);
}
}
class CPPParcelEx {
constructor(addr, map) {
this.thisAddr = addr;
this.exportMap = map;
this.position = 0;
}
readInt64() {
const exp = this.exportMap.get("readInt64")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "int64", ["pointer"]);
return dynFunc(this.thisAddr);
}
readUint64() {
const exp = this.exportMap.get("readUint64")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "uint64", ["pointer"]);
return dynFunc(this.thisAddr);
}
readInt32() {
const exp = this.exportMap.get("readInt32")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "int", ["pointer"]);
return dynFunc(this.thisAddr);
}
readUint32() {
const exp = this.exportMap.get("readUint32")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "uint", ["pointer"]);
return dynFunc(this.thisAddr);
}
restorePosition() {
this.setDataPosition(this.position);
}
savePosition() {
this.position = this.dataPosition();
}
readString8() {
let allc = Memory.alloc(Process.pointerSize);
const exp = this.exportMap.get("readString8Inplace");
const dynFunc = new NativeFunction(exp.address, "pointer", ["pointer", "pointer"]);
let ret = dynFunc(this.thisAddr, allc);
let sz = allc.readU32();
if(sz === 0)
return "";
return Memory.readUtf16String(ret, sz);
}
readString16() {
let allc = Memory.alloc(Process.pointerSize);
const exp = this.exportMap.get("readString16Inplace");
const dynFunc = new NativeFunction(exp.address, "pointer", ["pointer", "pointer"]);
let ret = dynFunc(this.thisAddr, allc);
let sz = allc.readU32();
if(sz === 0)
return "";
return Memory.readUtf16String(ret, sz);
}
setDataPosition(position) {
const exp = this.exportMap.get("setDataPosition")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "void", ["pointer", "uint"]);
const result = dynFunc(this.thisAddr, position);
}
dataPosition() {
const exp = this.exportMap.get("dataPosition")
if (!exp) return 0
const dynFunc = new NativeFunction(exp.address, "uint", ["pointer"]);
return dynFunc(this.thisAddr);
}
dump() {
const pData = this.data();
const dataSize = this.dataSize();
if (dataSize && pData) return hexdump(pData, { length: dataSize });
return "";
}
dataSize() {
const dataSizeFuncExport = this.exportMap.get("dataSize");
if (!dataSizeFuncExport) return 0;
const funcDataSize = new NativeFunction(dataSizeFuncExport.address, "int", ["pointer"]);
const result = funcDataSize(this.thisAddr);
return result;
}
data() {
const dataFuncExport = this.exportMap.get("data");
if (!dataFuncExport) return null;
const funcData = new NativeFunction(dataFuncExport.address, "pointer", ["pointer"]);
const result = funcData(this.thisAddr);
return result;
}
javaInstance() {
if (!Java.available) return null;
const clazzParcel = Java.use("android.os.Parcel");
const nativePtr = this.thisAddr.toUInt32();
const parcelFromPool = clazzParcel.obtain(nativePtr);
return parcelFromPool;
}
}
let FLAG_ONEWAY = 0x00000001;
let FIRST_CALL_TRANSACTION = 0x00000001;
let LAST_CALL_TRANSACTION = 0x00ffffff;
function hookBinder() {
try {
let bpBinder = new CPPParcelMapper("BpBinder");
let bBinder = new CPPParcelMapper("BBinder");
//let allTransactExports = bpBinder.filteredExports.concat(bBinder.filteredExports);
hookBinderTransact(bpBinder);
hookBinderTransact(bBinder);
} catch(e) {
console.log("[E] [Outter Error]:" + e);
}
}
function hookBinderTransact(mapper) {
try {
for(let exp of mapper.filteredExports) {
console.log("[E] " + exp.name + " Address:" + exp.address + "\n");
Interceptor.attach(exp.address, {
onEnter: function(args) {
try {
this.binderInstance = args[0];
this.code = (args[1]).toUInt32(); // uint32_t
this.pData = args[2];
this.pReply = args[3];
this.flags = (args[4]).toUInt32(); // uint32_t
//We prepare before as we want to attack after No matter what look at XPLEX exmaple code
//For AD ID we wait after so it can polute it with data (it wont return to the target app yet)
}catch(e) {
console.log("[" + mapper.className + ":transact] Exception: " + e);
}
},
onLeave: function(retval) {
try {
let selfInstance = this.binderInstance; // not used, would allow accessing other BBinder instance functionality
let code = this.code; // uint32_t
let pData = this.pData;
let pReply = this.pReply;
let flags = this.flags; // uint32_t
let isOneWay = (flags & FLAG_ONEWAY) > 0;
let data = new CPPParcelEx(pData, mapper);
let reply = new CPPParcelEx(pReply, mapper);
// let out = `${exp.name} called (code=${code}, pData=${pData}, pReply=${pReply}, flags=${flags} (oneWay: ${isOneWay}))`
//console.log("Code=" + code + " pData=" + pData + " pReply=" + pReply + " Flags=" + flags + " OneWay=" + isOneWay);
//console.log("\n\n");
if (data !== null && data.dataSize() > 0) {
data.savePosition();
data.setDataPosition(12); //Skip first 12 Bytes (seems to be other data)
let interfaceName = data.readString16();
data.restorePosition();
console.log("[" + mapper.className + "]");
console.log(interfaceName + "\n");
//To view a Dump of the Parcel you can do
//data.dump();
}
}catch(e) {
console.log("[" + mapper.className + ":transact] End Exception: " + e);
}
},
});
}
}catch(e) {
console.log("[E] [" + mapper.className + "] " + e);
}
}
//Remove Java.perform since Java is not used and Some Services dont have Java Environment
hookBinder();
if(Java.isAvailable) {
setTimeout(function() {
Java.perform(function() {
//hookBinder();
});
}, 0);
}