| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2 |
|
| 3 |
<section xml:id="reverseEngineeringCodeplug" |
| 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 of the code plug format</title></info> |
| 9 |
|
| 10 |
<para> |
| 11 |
Once the communication protocol is known, it is time to focus on the code plug format. Depending |
| 12 |
on the code plug complexity, this can be a very cumbersome task. Moreover, depending on the |
| 13 |
device, it can be pretty hard to access the binary representation of the configuration (code plug) |
| 14 |
that is actually written onto the device. In general, one cannot expect, that the binary file, |
| 15 |
created by the manufacturer, does relate to the binary code plug written onto the device at all. |
| 16 |
</para> |
| 17 |
<para> |
| 18 |
To figure the relationship out, between the code plug file and the actual code plug written onto |
| 19 |
the device, the code plug must first be extracted from the Wireshark capture. As we already know |
| 20 |
the protocol, this can be done easily using Python and pyshark. When doing so, be careful |
| 21 |
with the addresses, data is written to or read from. The binary code plug might not be written |
| 22 |
sequentially and with gaps. A best practice is to extract the data read or written along with |
| 23 |
its addresses and sort it with respect to the address. Then dump everything as a hex-dump. |
| 24 |
</para> |
| 25 |
<para> |
| 26 |
Once the code plug is extracted from the Wireshark capture, compare it to the corresponding |
| 27 |
code plug file created by the CPS. If you are lucky (usually for very cheap DMR radios), the CPS |
| 28 |
code plug file is simply the binary code plug written to the device (e.g., for GD77, RD5R). Then |
| 29 |
it is very easy to reverse engineer the code plug format, as you can study changes to the CPS code |
| 30 |
plug file. |
| 31 |
</para> |
| 32 |
<para> |
| 33 |
If they are not identical, you will need to somehow extract the binary code plug written to the |
| 34 |
device from the manufacturer CPS. If the protocol is based on Serial-over-USB, you may be able |
| 35 |
to write an emulator for the device, knowing the communication protocol. This then allows to |
| 36 |
trick the CPS to talk to the emulator instead of the device and dump the code plug. If not, you |
| 37 |
are likely stuck with writing a lot of code plugs to the real device and extracting the code plug |
| 38 |
from the Wireshark captures. This can be considered the worst-case scenario. |
| 39 |
</para> |
| 40 |
|
| 41 |
<section> |
| 42 |
<info><title>Differential analysis</title></info> |
| 43 |
<para> |
| 44 |
Irrespective on how you gain access to the binary code plug written onto the device, the analysis |
| 45 |
of this code plug is always the same. This is called differential analysis. The idea is, to change |
| 46 |
a single option in the CPS and compare the resulting binary code plugs bit by bit. Ideally, |
| 47 |
only a single place in the binary code plug will change too and this change then reflects the |
| 48 |
encoding of the particular setting touched. This is then repeated for all possible settings in |
| 49 |
the CPS until the entire code plug structure is known. |
| 50 |
</para> |
| 51 |
<note> |
| 52 |
<para> |
| 53 |
There will be some bits and bytes in the code plug that will never changed. These are usually |
| 54 |
reserved bits/bytes. There may also be some undocumented and hidden options in the code plug, |
| 55 |
that cannot be set via the (normal) CPS. |
| 56 |
</para> |
| 57 |
</note> |
| 58 |
<para> |
| 59 |
Before you can start reverse engineering the code plug, you need to create a <quote>base</quote> |
| 60 |
code plug. That is, one small simple code plug, that touches every feature of the radio. It should |
| 61 |
not be overly complex with hundreds of channels, but should contain everything the radio provides. |
| 62 |
For example, it should contain the APRS settings, if the radio has GPS or a roaming zone if the |
| 63 |
radio supports roaming. You should also create two of every kind. That is, at least two channels, |
| 64 |
contacts, group lists, zones and scan lists. Otherwise, it will be hard to figure the index |
| 65 |
scheme out. |
| 66 |
</para> |
| 67 |
<para> |
| 68 |
Once you created the base-code plug, get its binary representation (e.g., from file, emulation or |
| 69 |
Wireshark capture). Change something simple. For example the name of the first contact. Then get |
| 70 |
the binary representation of this modified code plug and compare the two binary code plugs. |
| 71 |
</para> |
| 72 |
<para> |
| 73 |
As an example, consider the following difference between two hex-dumps of the binary code plugs |
| 74 |
for a BTech DMR-6X2UV Pro. Here, the name of the first contact was changed. |
| 75 |
</para> |
| 76 |
<example> |
| 77 |
<title>Difference between two binary code plugs for a DMR-6X2UV Pro. Only the name of the first |
| 78 |
contact was changed.</title> |
| 79 |
<programlisting><![CDATA[< 02680000 : 01 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1...... |
| 80 |
< 02680010 : 00 00 00 55 6e 6b 6e 6f 77 6e 00 00 00 00 00 00 | ...Unknown...... |
| 81 |
> 02680000 : 01 56 65 72 79 20 6c 6f 6e 67 20 6e 61 6d 65 2e | .Very long name. |
| 82 |
> 02680010 : 2e 00 00 55 6e 6b 6e 6f 77 6e 00 00 00 00 00 00 | ...Unknown......]]></programlisting> |
| 83 |
</example> |
| 84 |
<para> |
| 85 |
This difference already shows some very important information about the code plug encoding. First, |
| 86 |
the list of contacts appears to start at address 2680000h. This is very important for the coarse |
| 87 |
structure of the binary code plug. We also learn about the structure of the encoded contacts, that |
| 88 |
the name starts at offset 01h. So, the first byte is still unknown. The length of the name is |
| 89 |
limited to 16 bytes and is terminated/padded with 0-bytes. However, we still don't know how large |
| 90 |
the encoded contact element in the code plug actually is. |
| 91 |
</para> |
| 92 |
<para> |
| 93 |
To figure out the size of the contact element, one can use the fact, that all elements very |
| 94 |
likely have identical sizes. Hence the offset from one name to the name of the next contact will |
| 95 |
be the size of the contact element. We already know, that the contact list starts at the address |
| 96 |
2680000h. Hence, we can look in the code plug directly at that address. |
| 97 |
</para> |
| 98 |
<example> |
| 99 |
<title>Codeplug hex-dump at the contact-list address.</title> |
| 100 |
<programlisting><![CDATA[02680000 : 01 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1...... |
| 101 |
02680010 : 00 00 00 55 6e 6b 6e 6f 77 6e 00 00 00 00 00 00 | ...Unknown...... |
| 102 |
02680020 : 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 | ................ |
| 103 |
02680030 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 104 |
02680040 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 105 |
02680050 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 106 |
02680060 : 00 00 00 00 01 43 6f 6e 74 61 63 74 20 32 00 00 | .....Contact 2.. |
| 107 |
02680070 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 108 |
02680080 : 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 | ................ |
| 109 |
02680090 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 110 |
026800A0 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 111 |
026800B0 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ |
| 112 |
026800C0 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................]]></programlisting> |
| 113 |
</example> |
| 114 |
</section> |
| 115 |
<para> |
| 116 |
The offset between the two names <quote>Contact 1</quote> and <quote>Contact 2</quote> is exactly |
| 117 |
100 bytes (64h). With this, we know the exact size of the contact element. However, wo only know |
| 118 |
what 16 of these 100 bytes mean. So there is a lot to figure out. The differential analysis, |
| 119 |
however, makes it quiet easy. First we may change the call type of the first contact. From |
| 120 |
<quote>group call</quote> to <quote>private call</quote> and study the differences in the |
| 121 |
code plug. |
| 122 |
</para> |
| 123 |
<example> |
| 124 |
<title></title> |
| 125 |
<programlisting><![CDATA[< 02680000 : 01 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1...... |
| 126 |
> 02680000 : 00 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1......]]></programlisting> |
| 127 |
</example> |
| 128 |
<para> |
| 129 |
We see, that only one byte has changed. The one at offset 0. It changed from 01h to 00h. We may |
| 130 |
conclude that this byte encodes the contact type, where 00h means private call and 01h means |
| 131 |
group call. Analogously, we find out that 03h means all-call. |
| 132 |
</para> |
| 133 |
<para> |
| 134 |
The next step is to find the encoding and offset of the contact DMR ID. To find it, we reload the |
| 135 |
base code plug and change the DMR ID of the first contact from <quote>1</quote> to something more |
| 136 |
complex. E.g., to <quote>1234567</quote> and compare the binary . |
| 137 |
</para> |
| 138 |
<example> |
| 139 |
<title></title> |
| 140 |
<programlisting><![CDATA[< 02680020 : 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 | ................ |
| 141 |
> 02680020 : 00 00 00 01 23 45 67 00 00 00 00 00 00 00 00 00 | ....#Eg.........]]></programlisting> |
| 142 |
</example> |
| 143 |
<para> |
| 144 |
Here we find some changes at the offset 23h. The content changed from 00000001h to 01234567h. |
| 145 |
This is somewhat weird, as the binary representation of 1234567 in hex is actually 12d687h. |
| 146 |
However, for DMR code plugs, it is quiet common to store integers in so-called BCD. Here each |
| 147 |
digit is stored in 4bits. This has the consequence, that the decimal digits are readable in |
| 148 |
hex. But this storage is inefficient, as the DMR ID is actually a 24bit number, that can be |
| 149 |
stored in 3 bytes. Its BCD encoding requires 4 bytes. |
| 150 |
</para> |
| 151 |
<para> |
| 152 |
The last setting for contacts remaining in the CPS is the <quote>call-alert</quote>. This setting |
| 153 |
is only valid for private calls. Changing the contact 1 from group to private call, changing |
| 154 |
the call alert from <quote>None</quote> to <quote>Ring</quote> and comparing the code plugs, one |
| 155 |
gets: |
| 156 |
</para> |
| 157 |
<example> |
| 158 |
<title></title> |
| 159 |
<programlisting><![CDATA[< 02680000 : 01 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1...... |
| 160 |
> 02680000 : 00 43 6f 6e 74 61 63 74 20 31 00 00 00 00 00 00 | .Contact 1...... |
| 161 |
< 02680020 : 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 | ................ |
| 162 |
> 02680020 : 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 | ................]]></programlisting> |
| 163 |
</example> |
| 164 |
<para> |
| 165 |
Of cause, we find two changes. The change of the first byte, indicating the change from group to |
| 166 |
private call, as well as one byte changing at offset 27h from 00h to 01h. This is the only |
| 167 |
unexplained change and thus must reflect the alert-type setting. Consequently, the alert type |
| 168 |
<quote>None</quote> is represented by 00h and <quote>Ring</quote> by 01h. Analogously, we find |
| 169 |
that alert-type <quote>Online</quote> is encoded as 02h. |
| 170 |
</para> |
| 171 |
<para> |
| 172 |
Obviously, we still don't know the majority of the 100 bytes of the contact element. Lets have a |
| 173 |
look at the structure of the contact element. |
| 174 |
</para> |
| 175 |
<programlisting><![CDATA[ |
| 176 |
0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 177 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 178 |
00 |TYP| Name, 16 x ASCII, 0-pad ... |
| 179 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 180 |
10 |Pad| Unknown 16 bytes, ASCII? ... |
| 181 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 182 |
20 ... |Pad|DMR ID, BCD, BE|Rng| Unused, filled with 00h ... |
| 183 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 184 |
60 | |
| 185 |
+---+---+---+---+]]> |
| 186 |
</programlisting> |
| 187 |
<para> |
| 188 |
There are some additional Pad bytes (00h) that terminate ASCII strings. This is nothing, we found |
| 189 |
by the differential analysis, but it is some common scheme, found in many binary code plugs. |
| 190 |
Obviously, the firmware developer did not knew the <function>strnlen</function> C function. |
| 191 |
</para> |
| 192 |
|
| 193 |
<para> |
| 194 |
The remaining bytes of the contact element appears to be empty, unused space. This is also quiet |
| 195 |
frequent in code plugs. They contain a lot of unused bytes that might be reserved for future uses. |
| 196 |
This allows the firmware developer to extend the code plug, while maintaining backward |
| 197 |
compatibility with older versions of the CPS. |
| 198 |
</para> |
| 199 |
|
| 200 |
<para> |
| 201 |
Having reverse engineered the contacts, does not mean that you are done. There are a lot more |
| 202 |
elements to decipher. Did I mentioned that it is a lot of work? But the general scheme of things |
| 203 |
remain the same. Change a single setting in the CPS and compare the resulting binary code plugs. |
| 204 |
Document your findings. Step by step, you will figure out the entire code-plug structure. |
| 205 |
</para> |
| 206 |
</section> |