| 1 |
<?xml version="1.0"?> |
| 2 |
|
| 3 |
<section xml:id="reverseEngineeringProtocol" |
| 4 |
xmlns="http://docbook.org/ns/docbook" |
| 5 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 6 |
xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 7 |
|
| 8 |
<info><title>Reverse engineering communication protocols</title></info> |
| 9 |
|
| 10 |
<para> |
| 11 |
There is no one ultimate way to successfully reverse engineer a communication protocol. But some |
| 12 |
methods are very common. First, one needs to get some captures of the communication between the |
| 13 |
manufacturer codeplug programming software (<acronym>CPS</acronym>) and the radio of interest. |
| 14 |
The majority of these radios will communicate via USB. Hence, some means are needed to capture |
| 15 |
USB traffic. |
| 16 |
</para> |
| 17 |
<para> |
| 18 |
There is an application, called <application>Wireshark</application> |
| 19 |
(<ulink url="https://www.wireshark.org/"/>). This application is usually used to capture and |
| 20 |
analyze network traffic. It can, however, also capture traffic from USB. How this is done, |
| 21 |
depends a bit on the operating system you are using. If you want to capture and analyze the |
| 22 |
communication under <trademark>Windows</trademark>, read the instructions at |
| 23 |
<ulink url="https://wiki.wireshark.org/CaptureSetup/USB"/>. This makes sense, as you need a |
| 24 |
windows installation, to run the manufacturer CPS. |
| 25 |
</para> |
| 26 |
<para> |
| 27 |
I personally prefer to work under Linux, to this end, I've installed Windows in a virtual |
| 28 |
machine, but capture the USB traffic under the host Linux system. To do that, the |
| 29 |
<command>usbmon</command> kernel module must be loaded. If loaded, Wireshark will show USB |
| 30 |
as a capture source. |
| 31 |
</para> |
| 32 |
<para> |
| 33 |
Once, capturing USB traffic works, connect the radio to the host. You may need to allow the |
| 34 |
guest system to have access to the USB device. Then fire up the manufacturer CPS, start |
| 35 |
capturing in Wireshark and start a codeplug read. Stop capturing once the read is complete. |
| 36 |
Save the captured data and restart the capturing. Then write the codeplug back to the device, |
| 37 |
stop the capture and save it in another file. |
| 38 |
</para> |
| 39 |
<para> |
| 40 |
These files now contain all packets send and received by the PC over USB. In a next step, |
| 41 |
the packets must be filtered and inspected. To do that, I personally prefer to write short |
| 42 |
Python scripts. The <productname>pyshark</productname> package provides means to read |
| 43 |
packages from the capture file and access their content. This allows to filter those |
| 44 |
packages to and from the device, that contain the actual payload to and from the device. |
| 45 |
</para> |
| 46 |
<para> |
| 47 |
Please note, that may radios may misuse some already existing protocols over USB. Some |
| 48 |
Radioddity and Baofeng devices use the HID specification to transfer data to and from |
| 49 |
the device. Others may use the DFU specification (TyT, Retevis), mass storage (CSi) or |
| 50 |
simply serial-over-USB (AnyTone). Irrespective of the underlying specification used to |
| 51 |
send data to the device, the payload of these packages must be extracted. |
| 52 |
</para> |
| 53 |
<para> |
| 54 |
To do that, use Wireshark to inspect these packages by hand. Search for a packet, that |
| 55 |
was definitely send to or by the device and note the USB (URB) device address. This is |
| 56 |
the best way to filter all packets to and from the device. |
| 57 |
</para> |
| 58 |
<example> |
| 59 |
<title>Some simple example to filter packages by device address and destination/source.</title> |
| 60 |
<programlisting language="python"><![CDATA[ |
| 61 |
import pyshark |
| 62 |
|
| 63 |
packages = pyshark.FileCapture(FILENAME, include_raw=True, use_json=True) |
| 64 |
device_address = '9' # just an example |
| 65 |
|
| 66 |
for package in packages: |
| 67 |
if device_address != package.usb.device_address: |
| 68 |
continue |
| 69 |
if "host" == package.usb.src: |
| 70 |
# From host to device. |
| 71 |
else if "host" == package.usb.dst: |
| 72 |
# From device to host.]]> |
| 73 |
</programlisting> |
| 74 |
</example> |
| 75 |
<para> |
| 76 |
This example shows how to filter only those packets to and from a specific device and also |
| 77 |
dispatch depending on the destination and source of the packet. That is, if the packet was |
| 78 |
send by the host or the device. |
| 79 |
</para> |
| 80 |
<para> |
| 81 |
In a next step, the captured payload must be inspected. If the packet contains any additional |
| 82 |
payload data, the <varname>usb.data_length</varname> field will be set. Then, there is a field |
| 83 |
called <varname>usb.capdata_raw</varname>. Unfortunately, that field name contains a dot, so you |
| 84 |
need to access it differently. That is |
| 85 |
</para> |
| 86 |
<programlisting language="python"><![CDATA[ |
| 87 |
# [...] |
| 88 |
if int(package.usb.data_length): |
| 89 |
data = package["usb.capdata_raw"].value |
| 90 |
# [...]]]> |
| 91 |
</programlisting> |
| 92 |
<para> |
| 93 |
The extracted data is hex string. Now comes the difficult part. Staring at these packets and |
| 94 |
making sense of them. Usually, the communication is performed in a strict command-response |
| 95 |
structure. That is, the host sends a request packet and the device responses with a single |
| 96 |
packet. This eases the analysis a lot, as the communication is always initiated by the host and |
| 97 |
the association between the request and the response packets are trivial. |
| 98 |
</para> |
| 99 |
|
| 100 |
<section> |
| 101 |
<title>Identifying the communication structure</title> |
| 102 |
<para> |
| 103 |
A typical communication with the device is performed in three steps: First, the |
| 104 |
radio is identified and brought into a programming mode. Several packets might be needed to |
| 105 |
perform this task. In that state, the radio usually displays some message on the screen or |
| 106 |
blinks an LED. |
| 107 |
</para> |
| 108 |
<para> |
| 109 |
The second step consists of the actual codeplug transfer. There, a large |
| 110 |
amount of packets are send. This step is usually easy to find within the captured packets, as a |
| 111 |
large number of equally sized packets are exchanged. Sometimes, a relatively large amount of |
| 112 |
data is read or written at once. This can be spotted easily, as basic control commands of |
| 113 |
the first step are usually pretty short. |
| 114 |
</para> |
| 115 |
<para> |
| 116 |
Frequently, these packets contain an address field or sequence number used to specify where the |
| 117 |
codeplug data chunk is written to or read from. These fields are of upmost importance and need to |
| 118 |
be identified. They are, however, easy to spot, as this field is likely constantly increasing, |
| 119 |
with a fixed increment from packet to packet. |
| 120 |
</para> |
| 121 |
<para> |
| 122 |
The final step usually is to reboot the radio or leaving the programming mode. This is commonly |
| 123 |
performed by a single packet. Sometimes, the device reboots immediately and no response to the |
| 124 |
command is received by the host. |
| 125 |
</para> |
| 126 |
<para> |
| 127 |
Once the protocol structure is identified, that is, the purpose of the single packets is known, |
| 128 |
the actual work of identifying the packet structure can start. |
| 129 |
</para> |
| 130 |
</section> |
| 131 |
|
| 132 |
<section> |
| 133 |
<title>Identifying the packet format</title> |
| 134 |
<para> |
| 135 |
The actual packet format will be much harder to reverse engineer. I cannot give any general |
| 136 |
suggestions on how to figure out the meaning of each byte in a packet. However, I can give you |
| 137 |
some examples. This might help in reverse engineering new protocols as the structure of the |
| 138 |
packets will be different, but the concepts remain the same. For example, read and write commands |
| 139 |
must somehow specify an address to read from and write to as well as the amount of data to read |
| 140 |
or write. This might be helpful |
| 141 |
</para> |
| 142 |
<para> |
| 143 |
Let us inspect some packets send and received from an AnyTone device. |
| 144 |
</para> |
| 145 |
<example xml:id="revEngD578"> |
| 146 |
<title>Capture of a codeplug read from an AnyTone AT-D578UV</title> |
| 147 |
<programlisting><![CDATA[> 50 52 4f 47 52 41 4d | PROGRAM |
| 148 |
< 51 58 06 | QX. |
| 149 |
|
| 150 |
> 02 | . |
| 151 |
< 49 44 35 37 38 55 56 00 12 56 31 31 30 00 00 06 | ID578UV..V110... |
| 152 |
|
| 153 |
> 52 02 64 00 00 10 | R.d... |
| 154 |
< 57 02 64 00 00 10 fe ff ff ff ff ff ff ff ff ff | W.d............. |
| 155 |
| ff ff ff ff ff ff 65 06 | ......e. |
| 156 |
|
| 157 |
> 52 02 64 00 10 10 | R.d... |
| 158 |
< 57 02 64 00 10 10 ff ff ff ff ff ff ff ff ff ff | W.d............. |
| 159 |
| ff ff ff ff ff ff 76 06 | ......v. |
| 160 |
|
| 161 |
> 52 02 64 00 20 10 | R.d. . |
| 162 |
< 57 02 64 00 20 10 ff ff ff ff ff ff ff ff ff ff | W.d. ........... |
| 163 |
| ff ff ff ff ff ff 86 06 | ........ |
| 164 |
|
| 165 |
... |
| 166 |
|
| 167 |
> 52 01 64 08 80 10 | R.d... |
| 168 |
< 57 01 64 08 80 10 00 ff ff ff ff ff ff ff ff ff | W.d............. |
| 169 |
| ff ff ff ff ff ff ee 06 | ........ |
| 170 |
|
| 171 |
> 52 02 48 02 00 10 | R.H... |
| 172 |
< 57 02 48 02 00 10 01 08 00 00 ff ff ff ff ff ff | W.H............. |
| 173 |
| ff ff ff ff ff ff 59 06 | ......Y. |
| 174 |
|
| 175 |
... ]]></programlisting> |
| 176 |
</example> |
| 177 |
<para> |
| 178 |
The <xref linkend="revEngD578"/> shows a capture of the data exchanged between the host |
| 179 |
and an AnyTone AT-D578UV during a codeplug read. Everything starting with <token>></token> is |
| 180 |
sent from the host to the device, while everything starting with <token><</token> is sent by |
| 181 |
the device to the host. |
| 182 |
</para> |
| 183 |
<para> |
| 184 |
The first packet send, is the ASCII string <token>PROGRAM</token>. This causes the device to enter |
| 185 |
the programming mode. A message will be shown on the screen of the device. This is then |
| 186 |
acknowledged by the device with a short message <token>QX</token> followed by a single |
| 187 |
<token>06h</token> byte. The last byte appears to be weird, but it will always be present on any |
| 188 |
response from the device. This might be seen as an end-of-packet byte. |
| 189 |
</para> |
| 190 |
<para> |
| 191 |
The next command send by the host is pretty simple. It consists of a single <token>02h</token> |
| 192 |
byte. The device responds with a 15 bytes long ID string, identifying the device name and hardware |
| 193 |
version followed by the 06h end-of-packet byte. |
| 194 |
</para> |
| 195 |
<para> |
| 196 |
After this, the actual codeplug read appears to start. The host sends a series of equally size |
| 197 |
commands, each starting with an <token>R</token> followed by 5 bytes of payload. As mentioned |
| 198 |
above, this payload must contain some sort of memory address and length field, to specify how |
| 199 |
much to read and from where. To figure that out, one may study subsequent read requests. |
| 200 |
</para> |
| 201 |
<para> |
| 202 |
The payloads of the first tree read read requests are |
| 203 |
<programlisting><![CDATA[02 64 00 00 10 |
| 204 |
02 64 00 10 10 |
| 205 |
02 64 00 20 10 |
| 206 |
]]></programlisting> |
| 207 |
The only difference of these requests is the fourth byte. Hence, one may assume, that this byte is |
| 208 |
part of the address field. However, we do not know which bytes are part of the address as well. |
| 209 |
</para> |
| 210 |
<para> |
| 211 |
The responses to these requests also contain these bytes as well as 18 more bytes of payload. It is |
| 212 |
reasonable that the transfer size is a power of two. So the request likely reads 16 bytes from the |
| 213 |
device. 16 in hex is <token>10h</token>, hence one may assume that the last byte specifies the |
| 214 |
amount of data to read. Hence we identified the size field as the fifth byte of the read request |
| 215 |
payload data. |
| 216 |
</para> |
| 217 |
<para> |
| 218 |
As the address increases by 10h from request to request, one may infer, that the read request |
| 219 |
specifies the address to read from, not the sequence or block number of a read from. As the |
| 220 |
entire Codeplug will not fit into 256 bytes, the address field must be larger. Even 64kb will |
| 221 |
be too small, to hold the entire codeplug, the address is likely a 32bit integer. |
| 222 |
</para> |
| 223 |
<para> |
| 224 |
This does not need to be true. Some radios will implement special commands to select the memory |
| 225 |
bank beforehand. Then each 64kB bank can be accessed with a 16bit address. |
| 226 |
</para> |
| 227 |
<para> |
| 228 |
To verify our assumption, we will study some read requests, that appear much later and check if |
| 229 |
the first bytes change as well. And indeed, much later, we observe read request payloads like |
| 230 |
<programlisting><![CDATA[01 64 08 80 10 |
| 231 |
02 48 02 00 10 |
| 232 |
]]></programlisting> |
| 233 |
Obviously, these bytes change too. Consequently, we may assume that the address is stored as a |
| 234 |
32bit unsigned integer in big-endian byte-order. Finally, we may summarize the read request |
| 235 |
format as |
| 236 |
<programlisting><![CDATA[+---+---+---+---+---+---+ |
| 237 |
|'R'| Address | N | |
| 238 |
+---+---+---+---+---+---+]]></programlisting> |
| 239 |
Where <token>N</token> is the number of bytes to read. |
| 240 |
</para> |
| 241 |
<para> |
| 242 |
Now, it is time to study the structure of the read response. As we already reverse engineered the |
| 243 |
read request, the read response is then easy to understand. Each of these responses start with a |
| 244 |
<token>W</token> char, followed by the same address and length as send by the request. That is |
| 245 |
<programlisting><![CDATA[> 52 02 64 00 00 10 |
| 246 |
< 57 02 64 00 00 10 fe ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 65 06]]></programlisting> |
| 247 |
Obviously the 16 byte data read, follows the length byte. The last byte is the common 06h |
| 248 |
end-of-packet byte, we have already seen. So a single unknown 65h byte is still unknown. |
| 249 |
</para> |
| 250 |
<para> |
| 251 |
This byte might be some sort of checksum over the payload. Checksums are notoriously difficult to |
| 252 |
figure out. Frequently, common techniques like CRC16 are used. Here, a single checksum byte is |
| 253 |
used, hence one of the common checksums is unlikely. Now starts some guesswork. We may compare |
| 254 |
some very similar read responses to get an idea, how this checksum may work. For example |
| 255 |
<programlisting><![CDATA[57 02 64 00 10 10 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 76 06 |
| 256 |
57 02 64 00 20 10 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 86 06]]></programlisting> |
| 257 |
The only difference between these two responses is one byte in the address, the remaining payload |
| 258 |
is identical. The address also only differs by 10h like the checksum. So one may assume, that the |
| 259 |
checksum is simply the sum over the payload bytes. Summing all bytes of the latter up to the |
| 260 |
checksum gets 10ddh. Of cause, this is not the checksum directly. The checksum is a single byte. |
| 261 |
But the least significant byte of the sum (ddh), however, does not match. |
| 262 |
</para> |
| 263 |
<para> |
| 264 |
Maybe, the sum is not taken over the entire payload, but over the part that actually matters. That |
| 265 |
is, the address, length and data fields. This time, the sum is 1086h and this time, the least |
| 266 |
significant byte (86h) matches the checksum. Now, we can summarize the read response format as |
| 267 |
<programlisting><![CDATA[+---+---+---+---+---+---+---+...+---+---+---+ |
| 268 |
|'W'| Address | N | Data (N) |CRC|06h| |
| 269 |
+---+---+---+---+---+---+---+...+---+---+---+]]></programlisting> |
| 270 |
where the CRC is the least significant byte of the sum over the address, length and data payload. |
| 271 |
</para> |
| 272 |
</section> |
| 273 |
|
| 274 |
</section> |