-
Notifications
You must be signed in to change notification settings - Fork 6
/
RILSigEvaluator.java_0f4ce652591a0441498a19a272828472
228 lines (211 loc) · 8.11 KB
/
RILSigEvaluator.java_0f4ce652591a0441498a19a272828472
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
package com.android.internal.telephony;
import android.content.Context;
import android.content.Intent;
import android.telephony.Rlog;
import com.android.internal.telephony.SmsMessageBase;
import com.android.internal.telephony.gsm.SmsMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RILSigEvaluator {
Map<String, Object> exp;
public final String TAG = "RILSigEvaluator";
public SmsMessage sms;
public int secLevel;
public RILSigEvaluator(Map<String, Object> e) {
exp = e;
}
public int eval(SmsMessage s) {
this.sms = s;
return evaluate(exp);
}
private int evaluate(Map<String, Object> e) {
if (!e.containsKey("condition") || !e.containsKey("lvalue") || !e.containsKey("rvalue")) {
Rlog.d(TAG, "Invalid expression!");
return -1;
}
if (e.containsKey("securityLevel"))
secLevel = (int) e.get("securityLevel");
else
secLevel = RILDefender.AlertLevel.NOTIFY.getValue(); // default sec level: notify only
String condition = (String) e.get("condition");
int rvalue = evalValue(e.get("rvalue"));
ArrayList<Object> lvalues = (ArrayList<Object>) e.get("lvalue");
List<String> opcode = new ArrayList<>();
if (lvalues.size() > 1)
opcode = (ArrayList<String>) e.get("opcode");
List<Integer> lvs = new ArrayList<>();
for (Object lv: lvalues) {
int val = evalValue(lv);
lvs.add(val);
}
boolean result = compute(lvs, opcode, condition, rvalue);
return result ? 1 : 0;
}
private int evalValue(Object o) {
if (o instanceof String) {
switch ((String) o) {
// SMS Fields
case "sms.pid":
return this.sms.getProtocolIdentifier();
case "sms.dcs":
return this.sms.getDataCodingScheme();
case "sms.mti":
return this.sms.getMessageClass().ordinal();
case "sms.oa":
return Integer.parseInt(this.sms.getOriginatingAddress());
case "sms.smsc":
return Integer.parseInt(this.sms.getServiceCenterAddress());
case "sms.udl":
return this.sms.getUserData().length;
case "sms.scts":
return (int) this.sms.getTimestampMillis();
// SMS Context
case "bs.ss":
return (int) RILDefender.getCurrentCellState().signalStrength;
case "bs.mcc":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().mcc;
else
return 0;
case "bs.mnc":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().mnc;
else
return 0;
case "bs.cid":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().cid;
else
return 0;
case "bs.lac":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().lac;
else
return 0;
case "bs.arfcn":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().arfcn;
else
return 0;
case "bs.rat":
if (RILDefender.getCurrentCellState() != null)
return RILDefender.getCurrentCellState().rat;
else
return 0;
// Constants
case "SEND_SMS":
return 0x13;
case "RUN_AT_CMD":
return 0x34;
case "eventCount":
return RILDefender.historySms.size();
default:
// SMS Events
if (((String) o).startsWith("sms") && ((String) o).contains(".")) {
String[] tokens = ((String) o).split(".");
if (tokens.length != 2)
break;
// extract SMS index in history
int index = Integer.valueOf(tokens[0].replace("sms", ""));
String attribute = tokens[1].trim();
SmsMessage s = RILDefender.getSms(index);
if (sms == null)
break;
// evaluate attributes of specific SMS event:
switch (attribute) {
case "pid":
return s.getProtocolIdentifier();
case "dcs":
return s.getDataCodingScheme();
case "mti":
return s.getMessageClass().ordinal();
case "oa":
return Integer.parseInt(s.getOriginatingAddress());
case "smsc":
return Integer.parseInt(s.getServiceCenterAddress());
case "udl":
return s.getUserData().length;
case "scts":
return (int) s.getTimestampMillis();
default:
break;
}
}
Rlog.d(TAG, "Warning: unimplemented value query for: " + (String) o);
return 0;
}
}
else if (o instanceof Integer) {
return (int) o;
}
else if (o instanceof HashMap) {
// recursively evaluate
return evaluate((Map<String, Object>) o);
}
else {
}
return 0;
}
private boolean compute(List<Integer> lvs, List<String> opcode, String condition, int rv) {
if (!(opcode.size() == lvs.size() - 1)) {
Rlog.d(TAG, "Invalid computation");
}
int res = lvs.get(0);
for (int i=0; i<opcode.size(); ++i) {
int lv = lvs.get(i+1);
switch (opcode.get(i)) {
case "+":
res = res + lv;
break;
case "-":
res = res - lv;
break;
case "*":
res = res * lv;
break;
case "/":
res = res / lv;
break;
case "&":
res = res & lv;
break;
case "|":
res = res | lv;
break;
case "&&":
if (res != 0 && lv != 0)
res = 1;
else
res = 0;
break;
case "||":
if (res != 0 || lv != 0)
res = 1;
else
res = 0;
break;
default:
Rlog.d(TAG, "Warning: unimplemented opcode: " + opcode.get(i));
}
}
switch (condition) {
case "==":
return res == rv;
case "!=":
return res != rv;
case ">":
return res > rv;
case "<":
return res < rv;
case ">=":
return res >= rv;
case "<=":
return res <= rv;
default:
Rlog.d(TAG, "Warning: unimplemented condition code: " + condition);
}
return res == rv;
}
}