-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
jdwplib.py
493 lines (422 loc) · 15.6 KB
/
jdwplib.py
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""
A minimal, modern, asyncio-based Python 3 implementation of the Java Debug Wire Protocol.
The implemented functionality is just enough to execute commands and load libraries.
When working with JDWP, make sure to enable debug logging:
```python
logging.getLogger("jdwplib").setLevel("DEBUG")
```
References:
- <https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-spec.html>
- <https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-protocol.html>
"""
from __future__ import annotations
import asyncio
import enum
import io
import logging
import struct
from dataclasses import dataclass
from pathlib import Path
log = logging.getLogger("jdwplib")
REPLY_PACKET = 0x80
HANDSHAKE = b"JDWP-Handshake"
class JDWPClient:
"""
A Java Debug Wire Protocol Client. Usage example:
```python
import asyncio
async def run():
async with jdwplib.JDWPClient("127.0.0.1", 1234) as client:
thread_id = await client.advance_to_breakpoint("Landroid/app/Activity;", "onCreate")
response = await client.exec(thread_id, "sleep 5")
asyncio.run(run())
```
ADB shortcut:
```python
async with await jdwplib.JDWPClient.connect_adb() as client:
...
```
"""
sizes: IDSizes
def __init__(
self,
host: str,
port: int,
):
self.host: str = host
self.port: int = port
self._reply_waiter: dict[int, asyncio.Event] = {}
self._replies: dict[int, Packet] = {}
self.current_id: int = 0
self.server_commands: asyncio.Queue[Packet] = asyncio.Queue()
self._classes_cache: dict[str, bytes] = {}
self._methods_cache: dict[bytes, bytes] = {}
@classmethod
async def connect_adb(cls, adb_binary: Path | None = None) -> JDWPClient:
"""Take the first (!) debuggable PID found via ADB, forward it via TCP, and connect to it."""
log.info("Obtaining jdwp pid from adb...")
if adb_binary is None:
adb_binary = Path("adb")
async def try_read_pid() -> int:
proc = await asyncio.create_subprocess_shell(
f"{adb_binary} jdwp",
stdout=asyncio.subprocess.PIPE,
)
try:
assert proc.stdout
pid = int(await proc.stdout.readline())
finally:
proc.kill()
proc._transport.close() # https://bugs.python.org/issue43884
return pid
pid = None
for i in range(1, 4):
try:
pid = await asyncio.wait_for(try_read_pid(), i)
break
except asyncio.TimeoutError:
log.info("Timeout...")
if pid is None:
raise RuntimeError("`adb jdwp` did not return a process id.")
log.info(f"{pid=}")
log.info("Forwarding to local port...")
proc = await asyncio.create_subprocess_shell(
f"{adb_binary} forward tcp:0 jdwp:{pid}",
stdout=asyncio.subprocess.PIPE,
)
assert proc.stdout
local_port = int(await proc.stdout.readline())
await proc.wait()
log.info(f"{local_port=}")
return JDWPClient("127.0.0.1", local_port)
async def __aenter__(self) -> JDWPClient:
log.info("Establishing connection...")
self.reader, self.writer = await asyncio.open_connection(self.host, self.port)
log.info("Starting handshake...")
self.writer.write(HANDSHAKE)
reply = await self.reader.readexactly(len(HANDSHAKE))
if reply != HANDSHAKE:
raise RuntimeError(f"Handshake failed: {reply=}")
self._reader_task_instance = asyncio.create_task(self._reader_task())
log.info("Obtaining Java id sizes...")
sizes = await self.send_command(Commands.GET_ID_SIZES)
assert not sizes.message
self.sizes = IDSizes(sizes.data)
log.info("Getting version info...")
version_info = await self.send_command(Commands.VERSION)
buf = io.BytesIO(version_info.data)
description = _read_str(buf)
versions_ = buf.read(8)
vm_version_ = _read_str(buf)
vm_name_ = _read_str(buf)
log.info(f"JDWP Version: {description}")
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.writer.close()
self._reader_task_instance.cancel("connection closed")
async def _reader_task(self):
while True:
header = await self.reader.readexactly(11)
length, id, flags, message = struct.unpack_from("!IIBH", header)
data = await self.reader.readexactly(length - 11)
packet = Packet(id, flags, message, data)
if packet.is_reply:
self._replies[packet.id] = packet
self._reply_waiter[packet.id].set()
else:
await self.server_commands.put(packet)
log.debug(f"<< {packet}")
if packet.is_reply and packet.message:
log.error(f"Command errored: {packet}")
async def send_command(self, command: Commands, data: bytes = b"") -> Packet:
"""
Send a generic request to the VM, wait for the response, and return it.
"""
cmd = Packet(self.current_id, 0, command.value, data)
log.debug(f">> {cmd}")
self.writer.write(bytes(cmd))
self._reply_waiter[cmd.id] = asyncio.Event()
self.current_id += 1
await self._reply_waiter[cmd.id].wait()
del self._reply_waiter[cmd.id]
return self._replies.pop(cmd.id)
async def get_first_class_id(self, cls_sig: str) -> bytes | None:
"""
Get the class id for the first class matching the signature,
e.g. "Ljava/lang/Runtime;".
"""
if cls_sig not in self._classes_cache:
resp = await self.send_command(
Commands.CLASSES_BY_SIGNATURE, _encode_jdwp_str(cls_sig)
)
(classes,) = struct.unpack_from("!I", resp.data)
if not classes:
raise ValueError(f"Class not found: {cls_sig}")
else:
self._classes_cache[cls_sig] = resp.data[5 : 5 + self.sizes.reference]
return self._classes_cache[cls_sig]
async def get_first_method_id(
self,
cls_id: bytes,
method_sig: str,
) -> bytes:
"""
Get the method id for the first method matching the signature in the given class,
e.g. "getRuntime". If multiple implementation are available, you can additionally specify the signature,
e.g. "getRuntime()Ljava/lang/Runtime;".
"""
i = method_sig.find("(")
if i != -1:
name = method_sig[:i]
signature = method_sig[i:]
else:
name = method_sig
signature = None
if cls_id not in self._methods_cache:
resp = await self.send_command(Commands.METHODS, cls_id)
self._methods_cache[cls_id] = resp.data
buf = io.BytesIO(self._methods_cache[cls_id])
(methods,) = struct.unpack("!I", buf.read(4))
for _ in range(methods):
id = buf.read(self.sizes.method)
n = _read_str(buf)
sig = _read_str(buf)
(mod_bits,) = struct.unpack("!I", buf.read(4))
is_a_match = name == n and (signature is None or signature == sig)
if is_a_match:
return id
raise ValueError(f"Method not found: {method_sig}")
async def advance_to_breakpoint(self, cls_sig: str, method_name: str) -> bytes:
"""
Set a breakpoint at a given location, and then resume the VM until the breakpoint is hit.
This dance yields a correct thread id.
"""
cls_id = await self.get_first_class_id(cls_sig)
assert cls_id
meth_id = await self.get_first_method_id(cls_id, method_name)
assert meth_id
# set breakpoint
resp = await self.send_command(
Commands.SET_BREAKPOINT,
b"\x02" # EventKind: Breakpoint
b"\x02" # SuspendPolicy: all
b"\x00\x00\x00\x01" # one modifier
b"\x07" # location only
b"\x01" + cls_id + meth_id + b"\x00" * 8,
)
# resume vm
await self.send_command(Commands.RESUME_VM)
# wait for breakpoint event
while True:
command = await self.server_commands.get()
if command.message == Commands.EVENT_COMPOSITE.value:
buf = io.BytesIO(command.data)
suspend_policy_ = buf.read(1)
events = buf.read(4)
kind = buf.read(1)
request_id = buf.read(4)
if (
events == b"\x00\x00\x00\x01"
and kind == b"\x02"
and request_id == resp.data
):
thread_id = buf.read(self.sizes.object)
break
log.debug(f"Command did not match expected event and got discarded.")
return thread_id
async def get_runtime(self, thread_id: bytes) -> bytes:
"""
Get the instance id of the current runtime.
"""
runtime_class_id = await self.get_first_class_id("Ljava/lang/Runtime;")
assert runtime_class_id
get_runtime = await self.get_first_method_id(
runtime_class_id, "getRuntime()Ljava/lang/Runtime;"
)
assert get_runtime
resp = await self.send_command(
Commands.INVOKE_STATIC_METHOD,
runtime_class_id
+ thread_id
+ get_runtime
+ b"\x00\x00\x00\x00"
+ b"\x00\x00\x00\x00",
)
runtime_id = resp.data[1 : 1 + self.sizes.object]
return runtime_id
async def create_string(self, s: str) -> bytes:
"""
Create a string on the VM, get the string id in return.
"""
resp = await self.send_command(Commands.CREATE_STRING, _encode_jdwp_str(s))
assert resp.data
return resp.data
async def invoke_method(
self,
object_id: bytes,
thread_id: bytes,
class_sig: str,
method_sig: str,
arguments: bytes = b"\x00\x00\x00\x00",
) -> bytes:
class_id = await self.get_first_class_id(class_sig)
assert class_id
method_id = await self.get_first_method_id(class_id, method_sig)
assert method_id
resp = await self.send_command(
Commands.INVOKE_METHOD,
object_id
+ thread_id
+ class_id
+ method_id
+ arguments
+ b"\x00\x00\x00\x00",
)
assert resp.message == 0
exception = resp.data[-self.sizes.object :]
if exception != b"\x00\x00\x00\x00\x00\x00\x00\x00":
throwable = await self.get_first_class_id("Ljava/lang/Throwable;")
assert throwable
get_message = await self.get_first_method_id(
throwable, "toString()Ljava/lang/String;"
)
assert get_message
resp = await self.send_command(
Commands.INVOKE_METHOD,
exception
+ thread_id
+ throwable
+ get_message
+ b"\x00\x00\x00\x00"
+ b"\x00\x00\x00\x00",
)
assert resp.message == 0
assert (
resp.data[-self.sizes.object :] == b"\x00\x00\x00\x00\x00\x00\x00\x00"
)
resp = await self.send_command(
Commands.STRING_VALUE, resp.data[1 : self.sizes.reference + 1]
)
val = _read_str(io.BytesIO(resp.data))
raise RuntimeError(
f"Method invocation of {class_sig}.{method_sig} failed: {val}"
)
return resp.data[: -(self.sizes.object + 1)]
async def exec(self, thread_id: bytes, cmd: str) -> int:
"""
Execute a command using `Runtime.getRuntime().exec(cmd)`.
"""
runtime = await self.get_runtime(thread_id)
cmd_str = await self.create_string(cmd)
resp = await self.invoke_method(
runtime,
thread_id,
"Ljava/lang/Runtime;",
"exec(Ljava/lang/String;)Ljava/lang/Process;",
b"\x00\x00\x00\x01L" + cmd_str,
)
process = resp[1:]
# wait for process to exit
resp = await self.invoke_method(
process, thread_id, "Ljava/lang/Process;", "waitFor()I"
)
(exit_code,) = struct.unpack_from("!I", resp, 1)
if exit_code:
logging.error(f"Command {cmd!r} return exit code {exit_code}")
return exit_code
async def load(self, thread_id: bytes, path: str):
"""
Load a library using `Runtime.getRuntime().load(cmd)`.
"""
runtime_id = await self.get_runtime(thread_id)
assert runtime_id
cmd_str = await self.create_string(path)
args = b"\x00\x00\x00\x01L" + cmd_str
resp = await self.invoke_method(
runtime_id,
thread_id,
"Ljava/lang/Runtime;",
"load(Ljava/lang/String;)V",
args,
)
assert resp == b"V"
@dataclass
class Packet:
"""
A packet sent over the connection, can be either a command or a reply.
<https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-spec.html#jdwp-packets>
"""
id: int
flags: int
message: int
"""
Bytes 10-11 of the packet as a big-endian integer.
For commands, this is the command set and the command id.
For replies, this is the error code.
"""
data: bytes
@property
def is_reply(self) -> bool:
return bool(self.flags & REPLY_PACKET)
def __repr__(self):
if self.is_reply:
typ = "Reply"
message = f"0x{self.message:04x}"
else:
typ = "Commd"
try:
message = Commands(self.message).name
except ValueError:
message = f"0x{self.message:04x}"
return f"{typ}(0x{self.id:04x}, {message}, {self.data!r})"
def __bytes__(self):
total_len = 11 + len(self.data)
return (
struct.pack("!IIBH", total_len, self.id, self.flags, self.message)
+ self.data
)
@dataclass
class IDSizes:
"""
Container type holding the size information of various data type on the VM.
<https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-protocol.html#JDWP_VirtualMachine_IDSizes>
"""
field: int
method: int
object: int
reference: int
frame: int
def __init__(self, data: bytes):
(
self.field,
self.method,
self.object,
self.reference,
self.frame,
) = struct.unpack("!IIIII", data)
class Commands(enum.IntEnum):
"""
Incomplete enumeration of command constants taken from
<https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-protocol.html>.
For example, the IDSizes command is command set 1 and command 7. We represent it as
`0x0107`.
"""
VERSION = 0x0101
CLASSES_BY_SIGNATURE = 0x0102
GET_ID_SIZES = 0x0107
RESUME_VM = 0x0109
CREATE_STRING = 0x010B
METHODS = 0x0205
INVOKE_STATIC_METHOD = 0x0303
INVOKE_METHOD = 0x0906
SET_BREAKPOINT = 0x0F01
EVENT_COMPOSITE = 0x4064
STRING_VALUE = 0x0A01
def _read_str(buf: io.BytesIO) -> str:
"""Read a length-prefixed UTF8 string from a buffer."""
(l,) = struct.unpack("!I", buf.read(4))
return buf.read(l).decode()
def _encode_jdwp_str(x: str) -> bytes:
"""Encode a string as length-prefixed UTF8."""
xb = x.encode()
return len(xb).to_bytes(4, "big") + xb