| 1 |
# How to reverse engineer the AnyTone code-plugs |
| 2 |
Unfortunately, AnyTone CPSs store the code-plug in a completely different format on the disc |
| 3 |
compared to the content being written to the device. This requires to observe the communication |
| 4 |
between the host running the original CPS and the device. Moreover, a complete code-plug must |
| 5 |
be programmed to the device for every option in the CPS, to be able to reverse engineer the |
| 6 |
format. |
| 7 |
|
| 8 |
So to reverse engineer the code-plug format on the device, you first need to get the original |
| 9 |
CPS running in a virtual machine on a Linux host (I use VirtualBox for that, but any other will |
| 10 |
do too). Then the *usbmon* kernel module must be loaded to be able to grab all traffic between |
| 11 |
the host and the radio. So call |
| 12 |
``` |
| 13 |
$ sudo modprobe usbmon |
| 14 |
``` |
| 15 |
|
| 16 |
Then fire up wireshark |
| 17 |
``` |
| 18 |
$ sudo wireshark |
| 19 |
``` |
| 20 |
and start monitoring on the *usbmon* device. |
| 21 |
|
| 22 |
* Then start to upload a base code-plug to the device. Wireshark will capture everything that |
| 23 |
gets sent to the device. |
| 24 |
* Once the code-plug is written to the device, stop capturing in wireshark and save the data |
| 25 |
under a recognizable filename, e.g. *base.pcapng*. |
| 26 |
* Change a single thing within the CPS, e.g., enable GPS. Remember, we are doing a differential |
| 27 |
analysis, so change only one setting at a time. |
| 28 |
* Restart capturing in wireshark and upload the modified code-plug to the device. |
| 29 |
* Once the code-plug is written, stop capturing and save the data in another file, e.g. |
| 30 |
*gps_enabled.pcapng*. |
| 31 |
* Extract the data written to the device using the *extract.py* script with |
| 32 |
`python3 extract.py PCAPNG_FILE > HEX_FILE` for both captured code-plugs. |
| 33 |
* Using *diff* you can now compare the two hex files. As only one feature of the code-plug was |
| 34 |
changed, the difference between the two hex files should be tiny. In fact if only a single |
| 35 |
setting is changed, frequently only a single bit/byte will change in the code-plug. |
| 36 |
|
| 37 |
Repeat the steps above over and over again to find the memory location of each option, setting |
| 38 |
and feature of the original CPS. Over time, you will be able to reverse engineer the entire |
| 39 |
code-plug. |
| 40 |
|
| 41 |
But keep in mind, that you will not be able to reverse engineer every single bit of the code-plug. |
| 42 |
At the end, there will be some reserved, fixed or hidden options left. |
| 43 |
|
| 44 |
## An example |
| 45 |
In this directory, you will find two examples. The captured and extracted base code-plug |
| 46 |
(*capture_base.pcapng* and *d878uv_base.hex*) as well as a captured and extracted |
| 47 |
code-plug, where only a single setting was changed (*capture_set_gps_on.pcapng* and |
| 48 |
*d878uv_set_gps_on.hex*). In this example, the GPS was enabled in the CPS. |
| 49 |
|
| 50 |
The *diff* of the two hex files reveals |
| 51 |
``` |
| 52 |
3029c3029 |
| 53 |
< 02500020 : 01 00 00 00 00 00 04 01 00 01 01 00 00 01 02 01 | ................ |
| 54 |
--- |
| 55 |
> 02500020 : 01 00 00 00 00 00 04 01 01 01 01 00 00 01 02 01 | ................ |
| 56 |
``` |
| 57 |
This tells me, that at the address 0x02500020 + 8, a single byte changed from 00 to 01. The entire |
| 58 |
remaining code-plug (about 100kB) stayed the same. Comparing the address 0x02500028 with the |
| 59 |
[coarse memory layout](https://dm3mat.darc.de/qdmr/libdmrconf/class_d878_u_v_codeplug.html) |
| 60 |
of the code-plug tells me, that byte 40 (hex 0x28) of the general settings section of the |
| 61 |
code-plug enables or disables the GPS. |