/
ADBDevice.java
311 lines (281 loc) · 8.92 KB
/
ADBDevice.java
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package jadx.gui.device.protocol;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.reactivex.annotations.NonNull;
import jadx.core.utils.StringUtils;
import jadx.core.utils.log.LogUtils;
import jadx.gui.device.protocol.ADB.JDWPProcessListener;
import jadx.gui.device.protocol.ADB.Process;
public class ADBDevice {
private static final Logger LOG = LoggerFactory.getLogger(ADBDevice.class);
private static final String CMD_TRACK_JDWP = "000atrack-jdwp";
private static final Pattern TIMESTAMP_FORMAT = Pattern.compile("^[0-9]{2}\\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}$");
ADBDeviceInfo info;
String androidReleaseVer;
volatile Socket jdwpListenerSock;
public ADBDevice(ADBDeviceInfo info) {
this.info = info;
}
public ADBDeviceInfo getDeviceInfo() {
return info;
}
public boolean updateDeviceInfo(ADBDeviceInfo info) {
if (info.getSerial() == null || info.getSerial().isEmpty()) {
return false;
}
boolean matched = this.info.getSerial().equals(info.getSerial());
if (matched) {
this.info = info;
}
return matched;
}
public String getSerial() {
return info.getSerial();
}
public boolean removeForward(String localPort) throws IOException {
return ADB.removeForward(info.getAdbHost(), info.getAdbPort(), info.getSerial(), localPort);
}
public ForwardResult forwardJDWP(String localPort, String jdwpPid) throws IOException {
try (Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort())) {
String cmd = String.format("host:forward:tcp:%s;jdwp:%s", localPort, jdwpPid);
cmd = String.format("%04x%s", cmd.length(), cmd);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
ForwardResult rst;
if (ADB.setSerial(info.getSerial(), outputStream, inputStream)) {
outputStream.write(cmd.getBytes());
if (!ADB.isOkay(inputStream)) {
rst = new ForwardResult(1, ADB.readServiceProtocol(inputStream));
} else if (!ADB.isOkay(inputStream)) {
rst = new ForwardResult(2, ADB.readServiceProtocol(inputStream));
} else {
rst = new ForwardResult(0, null);
}
} else {
rst = new ForwardResult(1, "Unknown error.".getBytes());
}
return rst;
}
}
public static class ForwardResult {
/**
* 0 for success, 1 for failed at binding to local tcp, 2 for failed at remote.
*/
public int state;
public String desc;
public ForwardResult(int state, byte[] desc) {
if (desc != null) {
this.desc = new String(desc);
} else {
this.desc = "";
}
this.state = state;
}
}
/**
* @return pid otherwise -1
*/
public int launchApp(String fullAppName) throws IOException, InterruptedException {
byte[] res;
try (Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort())) {
String cmd = "am start -D -n " + fullAppName;
res = ADB.execShellCommandRaw(info.getSerial(), cmd, socket.getOutputStream(), socket.getInputStream());
if (res == null) {
return -1;
}
}
String rst = new String(res).trim();
if (rst.startsWith("Starting: Intent {") && rst.endsWith(fullAppName + " }")) {
Thread.sleep(40);
String pkg = fullAppName.split("/")[0];
for (Process process : getProcessByPkg(pkg)) {
return Integer.parseInt(process.pid);
}
}
return -1;
}
/**
* @Return binary output of logcat
*/
public byte[] getBinaryLogcat() throws IOException {
Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort());
String cmd = "logcat -dB";
return ADB.execShellCommandRaw(info.getSerial(), cmd, socket.getOutputStream(), socket.getInputStream());
}
/**
* @Return binary output of logcat after provided timestamp
* Timestamp is in the format 09-08 02:18:03.131
*/
public byte[] getBinaryLogcat(String timestamp) throws IOException {
Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort());
Matcher matcher = TIMESTAMP_FORMAT.matcher(timestamp);
if (!matcher.find()) {
LOG.error("Invalid Logcat Timestamp " + timestamp);
}
String cmd = "logcat -dB -t \"" + timestamp + "\"";
return ADB.execShellCommandRaw(info.getSerial(), cmd, socket.getOutputStream(), socket.getInputStream());
}
/**
* @Return binary output of logcat -c
*/
public void clearLogcat() throws IOException {
Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort());
String cmd = "logcat -c";
ADB.execShellCommandRaw(info.getSerial(), cmd, socket.getOutputStream(), socket.getInputStream());
}
/**
* @return Timezone for the attached android device
*/
public String getTimezone() throws IOException {
Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort());
String cmd = "getprop persist.sys.timezone";
byte[] tz = ADB.execShellCommandRaw(info.getSerial(), cmd, socket.getOutputStream(), socket.getInputStream());
return new String(tz).trim();
}
public String getAndroidReleaseVersion() {
if (!StringUtils.isEmpty(androidReleaseVer)) {
return androidReleaseVer;
}
try {
List<String> list = getProp("ro.build.version.release");
if (list.size() != 0) {
androidReleaseVer = list.get(0);
}
} catch (Exception e) {
LOG.error("Failed to get android release version", e);
androidReleaseVer = "";
}
return androidReleaseVer;
}
public List<String> getProp(String entry) throws IOException {
try (Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort())) {
List<String> props = Collections.emptyList();
String cmd = "getprop";
if (!StringUtils.isEmpty(entry)) {
cmd += " " + entry;
}
byte[] payload = ADB.execShellCommandRaw(info.getSerial(), cmd,
socket.getOutputStream(), socket.getInputStream());
if (payload != null) {
props = new ArrayList<>();
String[] lines = new String(payload).split("\n");
for (String line : lines) {
line = line.trim();
if (!line.isEmpty()) {
props.add(line);
}
}
}
return props;
}
}
public List<Process> getProcessByPkg(String pkg) throws IOException {
return getProcessList("ps | grep " + pkg);
}
@NonNull
public List<Process> getProcessList() throws IOException {
return getProcessList("ps");
}
private List<Process> getProcessList(String cmd) throws IOException {
try (Socket socket = ADB.connect(info.getAdbHost(), info.getAdbPort())) {
List<Process> procs = new ArrayList<>();
byte[] payload = ADB.execShellCommandRaw(info.getSerial(), cmd,
socket.getOutputStream(), socket.getInputStream());
if (payload != null) {
String ps = new String(payload);
String[] psLines = ps.split("\n");
for (String line : psLines) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
Process proc = Process.make(line);
if (proc != null) {
procs.add(proc);
} else {
LOG.error("Unexpected process info data received: \"{}\"", LogUtils.escape(line));
}
}
}
return procs;
}
}
public boolean listenForJDWP(JDWPProcessListener listener) throws IOException {
if (this.jdwpListenerSock != null) {
return false;
}
jdwpListenerSock = ADB.connect(this.info.getAdbHost(), this.info.getAdbPort());
InputStream inputStream = jdwpListenerSock.getInputStream();
OutputStream outputStream = jdwpListenerSock.getOutputStream();
if (ADB.setSerial(info.getSerial(), outputStream, inputStream)
&& ADB.execCommandAsync(outputStream, inputStream, CMD_TRACK_JDWP)) {
Executors.newFixedThreadPool(1).execute(() -> {
for (;;) {
byte[] res = ADB.readServiceProtocol(inputStream);
if (res != null) {
if (listener != null) {
String payload = new String(res);
String[] ids = payload.split("\n");
Set<String> idList = new HashSet<>(ids.length);
for (String id : ids) {
if (!id.trim().isEmpty()) {
idList.add(id);
}
}
listener.jdwpProcessOccurred(this, idList);
}
} else { // socket disconnected
break;
}
}
if (listener != null) {
this.jdwpListenerSock = null;
listener.jdwpListenerClosed(this);
}
});
} else {
jdwpListenerSock.close();
jdwpListenerSock = null;
return false;
}
return true;
}
public void stopListenForJDWP() {
if (jdwpListenerSock != null) {
try {
jdwpListenerSock.close();
} catch (Exception e) {
LOG.error("JDWP socket close failed", e);
}
}
this.jdwpListenerSock = null;
}
@Override
public int hashCode() {
return info.getSerial().hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ADBDevice) {
String otherSerial = ((ADBDevice) obj).getDeviceInfo().getSerial();
return otherSerial.equals(info.getSerial());
}
return false;
}
@Override
public String toString() {
return info.getAllInfo();
}
}