================================================================================ handshake-transcript.txt -- a complete ADB session, byte by byte, annotated. ================================================================================ WHAT THIS IS A real, unedited capture of every message exchanged between a host and adbd for the single command `id`, produced by: python3 -B ../scripts/adb-mini-client.py --port 5555 --service "shell:id" -v No `adb` binary was involved. The host end was ~180 lines of Python. Annotations are indented and prefixed with "#"; everything else is exactly what the tool printed. TARGET Android 16 / API 35 emulator image (sdk_gphone16k_x86_64), listening on 127.0.0.1:5555. This target has ro.adb.secure=0, so the A_AUTH exchange does NOT happen -- see the note at the end, which is important and not a gap in the capture. HOW TO READ IT ">>" is host to device. "<<" is device to host. "header bytes" is the raw 24 bytes; work through them with python3 -B decode_header.py --explain ================================================================================ STEP 1 -- A_CNXN: the host announces itself ================================================================================ >> A_CNXN arg0=0x01000001 arg1=0x00040000 len=33 cksum=0x00000c03 magic=0xb1a7b1bc header bytes: 434e584e010000010000040021000000030c0000bcb1a7b1 0000 68 6f 73 74 3a 3a 66 65 61 74 75 72 65 73 3d 63 |host::features=c| 0010 6d 64 2c 73 74 61 74 5f 76 32 2c 6c 73 5f 76 32 |md,stat_v2,ls_v2| 0020 00 |.| # Decompose the 24 header bytes: # # 43 4e 58 4e command "CNXN", read little-endian = 0x4e584e43 # 01 00 00 01 arg0 0x01000001 -- the protocol version. # This version raised maxdata from 4 KiB to # 256 KiB and abandoned the checksum. # 00 00 04 00 arg1 0x00040000 = 262144 = 256 KiB maxdata. # This is OUR limit; the effective one is the # smaller of the two. # 21 00 00 00 data_length 0x21 = 33 bytes of payload follow. # 03 0c 00 00 data_check 0x0c03 = 3075. That is the SUM of the 33 # payload bytes, not a CRC32. We compute it # honestly; adbd will not check it. # bc b1 a7 b1 magic 0xb1a7b1bc = 0x4e584e43 XOR 0xffffffff. # # The payload is "host::features=cmd,stat_v2,ls_v2" plus one NUL = 33. # The three colon-separated fields are ::. # We deliberately do NOT advertise shell_v2 here -- see step 3. ================================================================================ STEP 2 -- A_CNXN: adbd answers, and the session exists ================================================================================ << A_CNXN arg0=0x01000001 arg1=0x00040000 len=395 cksum=0x00000000 magic=0xb1a7b1bc header bytes: 434e584e01000001000004008b01000000000000bcb1a7b1 0000 64 65 76 69 63 65 3a 3a 72 6f 2e 70 72 6f 64 75 |device::ro.produ| 0010 63 74 2e 6e 61 6d 65 3d 73 64 6b 5f 67 70 68 6f |ct.name=sdk_gpho| 0020 6e 65 31 36 6b 5f 78 38 36 5f 36 34 3b 72 6f 2e |ne16k_x86_64;ro.| 0030 70 72 6f 64 75 63 74 2e 6d 6f 64 65 6c 3d 73 64 |product.model=sd| 0040 6b 5f 67 70 68 6f 6e 65 31 36 6b 5f 78 38 36 5f |k_gphone16k_x86_| 0050 36 34 3b 72 6f 2e 70 72 6f 64 75 63 74 2e 64 65 |64;ro.product.de| ... 299 more payload bytes # ***** THE SINGLE MOST INSTRUCTIVE FIELD IN THIS FILE ***** # # data_length 0x0000018b = 395 bytes of payload # data_check 0x00000000 # # 395 bytes of payload and a checksum of ZERO. adbd does not compute it. # That is the measured proof that the field documented in AOSP's own # protocol.txt as a CRC32 is, in practice, dead. Do not use it for # integrity; there is nothing there to use. # # The full banner, reassembled: # # device::ro.product.name=sdk_gphone16k_x86_64; # ro.product.model=sdk_gphone16k_x86_64; # ro.product.device=emu64xa16k; # features=shell_v2,cmd,stat_v2,ls_v2,fixed_push_mkdir,apex,abb, # fixed_push_symlink_timestamp,abb_exec,remount_shell, # track_app,sendrecv_v2,sendrecv_v2_brotli, # sendrecv_v2_lz4,sendrecv_v2_zstd, # sendrecv_v2_dry_run_send,openscreen_mdns, # devicetracker_proto_format,devraw,app_info,delayed_ack # # 21 features. Note what this leaks BEFORE any authentication on a device # that does not require it: product name, model, device codename, and a # feature list that fingerprints the platform version. On an exposed # port 5555 that is a complete identification with zero interaction. # # The systemtype field is "device". It would be "bootloader", "recovery" # or "sideload" in those modes -- and that string is exactly what # `adb devices` prints in its state column. ================================================================================ STEP 3 -- A_OPEN: ask for a service, and a stream is born ================================================================================ >> A_OPEN arg0=0x00000001 arg1=0x00000000 len=9 cksum=0x0000031f magic=0xb1baafb0 header bytes: 4f50454e0100000000000000090000001f030000b0afbab1 0000 73 68 65 6c 6c 3a 69 64 00 |shell:id.| # 4f 50 45 4e command "OPEN" -> 0x4e45504f # 01 00 00 00 arg0 our LOCAL stream id. We picked 1. Any # non-zero value we are not already using # would have done. # 00 00 00 00 arg1 zero -- we do not know the device's id yet. # 09 00 00 00 data_length 9 = "shell:id" (8) + the NUL terminator. # # The payload is a C string, and the NUL is mandatory. This is where the # protocol's whole extensibility lives: adbd parses the text before the # first ':' and dispatches. "shell:", "exec:", "sync:", "abb_exec:", # "root:", "tcpip:", "reverse:forward:..." -- all the same message type. # # We asked for "shell:" rather than "shell,v2,raw:" because our banner in # step 1 did not advertise shell_v2, so we would not know how to parse the # v2 packet framing. The BANNER advertises a capability; the SERVICE # STRING selects the behaviour. Advertising shell_v2 alone changes # nothing -- verified separately. ================================================================================ STEP 4 -- A_OKAY: the device names its half of the stream ================================================================================ << A_OKAY arg0=0x0000005c arg1=0x00000001 len=0 cksum=0x00000000 magic=0xa6beb4b0 header bytes: 4f4b41595c000000010000000000000000000000b0b4bea6 # arg0 = 0x5c = 92 the DEVICE's id for this stream # arg1 = 1 our id, echoed back # # From here the stream is the PAIR (1, 92). In messages we send, arg0 is # 1 and arg1 is 92. In messages we receive, it is the other way round. # There is no global stream identifier -- you demultiplex on arg1, which # is always your own id echoed back. # # 92 is not meaningful. It is simply the next id this adbd had free; the # same command in four consecutive runs produced 11, 15, 27 and 92. # Never hard-code or infer anything from a stream id. # # Had the service been unavailable, this would have been A_CLSE with # arg0 = 0 instead. There is no error message and no error code in this # protocol: "unknown service", "service unavailable" and "refused by a # permission check" are indistinguishable on the wire. ================================================================================ STEP 5 -- A_WRTE: the answer ================================================================================ << A_WRTE arg0=0x0000005c arg1=0x00000001 len=276 cksum=0x00000000 magic=0xbaabada8 header bytes: 575254455c000000010000001401000000000000a8adabba 0000 75 69 64 3d 32 30 30 30 28 73 68 65 6c 6c 29 20 |uid=2000(shell) | 0010 67 69 64 3d 32 30 30 30 28 73 68 65 6c 6c 29 20 |gid=2000(shell) | 0020 67 72 6f 75 70 73 3d 32 30 30 30 28 73 68 65 6c |groups=2000(shel| 0030 6c 29 2c 31 30 30 34 28 69 6e 70 75 74 29 2c 31 |l),1004(input),1| 0040 30 30 37 28 6c 6f 67 29 2c 31 30 31 31 28 61 64 |007(log),1011(ad| 0050 62 29 2c 31 30 31 35 28 73 64 63 61 72 64 5f 72 |b),1015(sdcard_r| ... 180 more payload bytes # The full 276 bytes, reassembled: # # uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input), # 1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw), # 1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet), # 3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) # context=u:r:shell:s0 # # This is the identity every ADB session gets on a production build: # uid 2000, SELinux domain shell, and a specific set of supplementary # groups. 3003(inet) is why a shell can open sockets at all. 3009(readproc) # is why it can see other processes in /proc. 1011(adb) exists purely for # this daemon. # # Note the framing: 276 raw bytes, no packet header, stdout and stderr # merged, and NO exit code anywhere. That is the v1 shell service. With # "shell,v2,raw:id" the payload would have begun 01 14 01 00 00 -- packet # id 1 (stdout), length 0x114 = 276 -- and a second packet with id 3 would # have carried the exit status. ================================================================================ STEP 6 -- A_OKAY: the flow control, in one message ================================================================================ >> A_OKAY arg0=0x00000001 arg1=0x0000005c len=0 cksum=0x00000000 magic=0xa6beb4b0 header bytes: 4f4b4159010000005c0000000000000000000000b0b4bea6 # This is not optional and it is not a courtesy. ADB's entire flow control # is a window of exactly ONE unacknowledged A_WRTE per stream. Until this # A_OKAY arrives, the device will not send another A_WRTE on stream 92. # # Two practical consequences: # 1. A hand-written client that forgets this hangs, and its author # blames the device. # 2. A single stream cannot pipeline, so throughput is bounded by # round-trip-time / maxdata. That is why maxdata was raised to # 256 KiB, and why adb push over TCP to a distant device crawls. # The `delayed_ack` feature in this device's banner is the modern # fix -- a real window instead of one message. ================================================================================ STEP 7 -- A_CLSE, both directions ================================================================================ << A_CLSE arg0=0x0000005c arg1=0x00000001 len=0 cksum=0x00000000 magic=0xbaacb3bc header bytes: 434c53455c000000010000000000000000000000bcb3acba >> A_CLSE arg0=0x00000001 arg1=0x0000005c len=0 cksum=0x00000000 magic=0xbaacb3bc header bytes: 434c5345010000005c0000000000000000000000bcb3acba # `id` exited, so the device closed its half; we closed ours. The id pair # (1, 92) is now free for reuse. # # IMPORTANT and easy to get wrong: this A_CLSE can arrive AFTER you have # already sent the A_OPEN for your next stream. A client that assumes # "the next message answers the request I just made" will read a stale # close as a refusal. That was a real bug in this chapter's installer # before it was fixed. Demultiplex on arg1, always. ================================================================================ WHAT IS MISSING FROM THIS TRANSCRIPT, AND WHY ================================================================================ There is no A_AUTH exchange above. That is not an omission in the capture -- it is a property of the target. emulator: ro.adb.secure = 0 -> no authentication at all test phone: ro.adb.secure = 1 -> A_AUTH(TOKEN) after every A_CNXN The emulator answered A_CNXN immediately, with no key, no signature and no dialog. Anyone who can reach 127.0.0.1:5555 on that machine has a shell. That is exactly the configuration that shipped on the TV boxes and development boards that self-propagating ADB malware colonised from 2018 onward, so it is worth seeing rather than skipping. On a device with ro.adb.secure=1 the sequence between step 1 and step 2 would be: << A_AUTH arg0=1 (ADB_AUTH_TOKEN) len=20 >> A_AUTH arg0=2 (ADB_AUTH_SIGNATURE) len=256 << A_CNXN ... (if it verified) and, if the signature did not verify: << A_AUTH arg0=1 (ADB_AUTH_TOKEN) len=20 <- a NEW token. This is the only "auth failed" signal the protocol has. >> A_AUTH arg0=3 (ADB_AUTH_RSAPUBLICKEY) len=709 ... the device shows "Allow USB debugging?" and nothing proceeds until a human taps it. The A_AUTH branch was NOT captured against the physical test device in this chapter, and the reason is stated plainly rather than papered over: that device is a daily driver, it only listens on USB, and reaching its adbd over TCP would have required running `adb tcpip` on it. That is a real, persistent exposure to create for a demonstration, so it was not done. The signing code path was instead verified offline -- a signature produced by the same routine, checked against the matching public key -- and the client's A_AUTH handling is exercised by the code, not asserted. If you want to see A_AUTH on the wire, use a spare device you own, run `adb tcpip 5555`, capture, and then run `adb usb` and reboot it. ================================================================================ EXERCISES ================================================================================ 1. Decode the step-3 header by hand, on paper, then check it: python3 -B decode_header.py --explain 4f50454e0100000000000000090000001f030000b0afbab1 2. Compute the step-1 checksum yourself. The payload is "host::features=cmd,stat_v2,ls_v2" plus one NUL byte. Sum the byte values. You should get 3075 = 0x0c03. Then confirm from step 2 that the device does not bother. 3. Work out what the header for A_WRTE on stream (1, 92) carrying the four bytes "test" would be, in hex, and check it with decode_header.py. 4. Re-run the capture with --service "shell,v2,raw:id" and identify the packet id, the length field, and the second packet carrying the exit code. 5. Re-run it with --service "nosuchservice:" and find the A_CLSE with arg0 = 0. Confirm there is no error text anywhere.