-
Notifications
You must be signed in to change notification settings - Fork 3
/
android-broadcast-receiver.js
160 lines (150 loc) · 5.77 KB
/
android-broadcast-receiver.js
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
/*
* Android Broadcast Receiver
*
* Usage: frida -U --codeshare leolashkevych/android-broadcast-receiver com.android.systemui
*
* 1. Registering a broadcast receiver with a specified intent filter:
*
* registerReceiver(IntentFilter);
* registerReceiver('com.example.customAction');
*
* 2. Registering a "catch all" broadcast receiver:
* Android SDK does not allow the registration of a receiver without specifying an intent filter
* and does not support wildcard filters. This restriction can be circumvented using Java's
* reflection features to enumerate all applicable events. Then a custom receiver can be
* registered for each event type. The limitation of this method is the inability to catch
* non-standard actions/categories/extras. In such cases, use "registerReceiver(IntentFilter);".
*
* registerReceiverForAll();
*
* 3. Hook an existing broadcast receiver:
*
* frida -U --codeshare leolashkevych/android-broadcast-receiver -f com.application.receiver
* hookReceiver();
* hookReceiver(BroadcastReceiverClass);
*
* 4. Hook an existing broadcast sender (via the Context.sendBroadcast() method):
* Note that intents sent with LocalBroadcastManager will not be hooked.
*
* frida -U --codeshare leolashkevych/android-broadcast-receiver -f com.application.sender
* hookSender();
*/
function registerReceiver(intentFilter) {
Java.perform(function() {
var cxt = getContext();
if (cxt) {
const parent = BroadcastReceiver().$new();
cxt.registerReceiver(ChildBroadcastReceiver().$new(parent), Java.use('android.content.IntentFilter').$new(intentFilter));
}
});
}
function registerReceiverForAll() {
Java.perform(function() {
var Modifier = Java.use('java.lang.reflect.Modifier');
var String = Java.use('java.lang.String');
const parent = BroadcastReceiver().$new();
var intent = Java.use('android.content.Intent').$new();
var fields = intent.getClass().getDeclaredFields();
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers) && field.getType().equals(String.class)) {
var filter = field.get(intent);
if (filter) {
console.log('Global BroadcastReceiver -> Registered: ' + filter);
getContext().registerReceiver(ChildBroadcastReceiver().$new(parent), Java.use('android.content.IntentFilter').$new(filter));
}
}
}
});
}
function hookReceiver(receiverClassID) {
if (typeof receiverClassID == 'undefined') {
receiverClassID = 'android.content.BroadcastReceiver';
}
Java.perform(function() {
var ReceiverClass = Java.use(receiverClassID);
if (ReceiverClass) {
ReceiverClass.onReceive.implementation = function(context, intent) {
printIntent(intent);
return this.onReceive(context, intent);
};
console.log("[+] BroadcastReceiver (" + receiverClassID + ") hook loaded.");
}
});
}
function hookSender() {
Java.perform(function() {
var Context = Java.use('android.content.Context');
Context.sendBroadcast.overload('android.content.Intent').implementation = function(intent) {
printIntent(intent);
return this.sendBroadcast(intent);
};
console.log("[+] Context.sendBroadcast() hook loaded.");
});
}
function getContext() {
return Java.use('android.app.ActivityThread').currentApplication().getApplicationContext();
}
function BroadcastReceiver() {
const ParentBroadcastReceiver = Java.registerClass({
name: 'ParentBroadcastReceiver',
superClass: Java.use('android.content.BroadcastReceiver'),
fields: {
parent: 'android.content.BroadcastReceiver'
},
methods: {
onReceive: [{
returnType: 'void',
argumentTypes: ['android.content.Context', 'android.content.Intent'],
implementation: function(context, intent) {
printIntent(intent);
}
}]
},
});
return ParentBroadcastReceiver;
}
function ChildBroadcastReceiver() {
const ChildBroadcastReceiver = Java.registerClass({
name: 'ChildBroadcastReceiver',
superClass: Java.use('android.content.BroadcastReceiver'),
fields: {
parent: 'android.content.BroadcastReceiver'
},
methods: {
'<init>': [{
returnType: 'void',
argumentTypes: [],
implementation: function() {}
}, {
returnType: 'void',
argumentTypes: ['android.content.BroadcastReceiver'],
implementation: function(parent) {
this.parent.value = parent;
}
}],
onReceive: [{
returnType: 'void',
argumentTypes: ['android.content.Context', 'android.content.Intent'],
implementation: function(context, intent) {
this.parent.value.onReceive(context, intent);
}
}]
},
});
return ChildBroadcastReceiver;
}
function printIntent(intent) {
console.log("\n[*] Intent received. Action: " + intent.getAction());
var keys = intent.getExtras().keySet();
var extras = "";
var iterator = keys.iterator();
for (var i = 0; i < keys.size(); i++) {
if (iterator.hasNext()) {
var key = iterator.next();
extras += "\n" + key + " = " + intent.getExtras().get(key);
}
}
console.log("[*] Extras:" + extras);
}