ec710b2c24d55d0a220d01809a7247b09c3832d0
[proxmark3-svn] / client / cmdhflist.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Merlok - 2017
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Command: hf mf list. It shows data from arm buffer.
9 //-----------------------------------------------------------------------------
10
11 #include "cmdhflist.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include "util.h"
19 #include "data.h"
20 #include "ui.h"
21 #include "iso14443crc.h"
22 #include "parity.h"
23 #include "protocols.h"
24
25
26 enum MifareAuthSeq {
27 masNone,
28 masNt,
29 masNrAr,
30 masAt,
31 masFirstData,
32 masData,
33 masDataNested,
34 masError,
35 };
36 static enum MifareAuthSeq MifareAuthState;
37 static TAuthData AuthData;
38
39 void ClearAuthData() {
40 AuthData.uid = 0;
41 AuthData.nt = 0;
42 AuthData.first_auth = false;
43 }
44
45 /**
46 * @brief iso14443A_CRC_check Checks CRC in command or response
47 * @param isResponse
48 * @param data
49 * @param len
50 * @return 0 : CRC-command, CRC not ok
51 * 1 : CRC-command, CRC ok
52 * 2 : Not crc-command
53 */
54 uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
55 {
56 uint8_t b1,b2;
57
58 if(len <= 2) return 2;
59
60 if(isResponse & (len < 6)) return 2;
61
62 ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2);
63 if (b1 != data[len-2] || b2 != data[len-1]) {
64 return 0;
65 } else {
66 return 1;
67 }
68 }
69
70 uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
71 {
72 switch(MifareAuthState) {
73 case masNone:
74 case masFirstData:
75 case masData:
76 case masDataNested:
77 case masError:
78 return iso14443A_CRC_check(isResponse, data, len);
79 default:
80 return 2;
81 }
82 }
83
84 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
85 {
86 switch(cmd[0])
87 {
88 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
89 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
90 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
91 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
92 if(cmd[1] == 0x70)
93 {
94 snprintf(exp,size,"SELECT_UID"); break;
95 }else
96 {
97 snprintf(exp,size,"ANTICOLL"); break;
98 }
99 }
100 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
101 //95 20 = Anticollision of cascade level2
102 //95 70 = Select of cascade level2
103 if(cmd[2] == 0x70)
104 {
105 snprintf(exp,size,"SELECT_UID-2"); break;
106 }else
107 {
108 snprintf(exp,size,"ANTICOLL-2"); break;
109 }
110 }
111 case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break;
112 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
113 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
114 case ISO14443A_CMD_HALT:
115 snprintf(exp,size,"HALT");
116 MifareAuthState = masNone;
117 break;
118 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
119 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
120 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
121 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
122 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
123 case MIFARE_AUTH_KEYA:
124 if ( cmdsize > 3) {
125 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
126 MifareAuthState = masNt;
127 } else {
128 // case MIFARE_ULEV1_VERSION : both 0x60.
129 snprintf(exp,size,"EV1 VERSION");
130 }
131 break;
132 case MIFARE_AUTH_KEYB:
133 MifareAuthState = masNt;
134 snprintf(exp,size,"AUTH-B(%d)",cmd[1]);
135 break;
136 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
137 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
138 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
139 case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break;
140 case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break;
141 case MIFARE_ULEV1_AUTH:
142 if ( cmdsize == 7 )
143 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
144 else
145 snprintf(exp,size,"PWD-AUTH");
146 break;
147 case MIFARE_ULEV1_FASTREAD:{
148 if ( cmdsize >=3 && cmd[2] <= 0xE6)
149 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
150 else
151 snprintf(exp,size,"?");
152 break;
153 }
154 case MIFARE_ULC_WRITE:{
155 if ( cmd[1] < 0x21 )
156 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
157 else
158 snprintf(exp,size,"?");
159 break;
160 }
161 case MIFARE_ULEV1_READ_CNT:{
162 if ( cmd[1] < 5 )
163 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
164 else
165 snprintf(exp,size,"?");
166 break;
167 }
168 case MIFARE_ULEV1_INCR_CNT:{
169 if ( cmd[1] < 5 )
170 snprintf(exp,size,"INCR(%d)",cmd[1]);
171 else
172 snprintf(exp,size,"?");
173 break;
174 }
175 case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break;
176 case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
177 case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break;
178 default: snprintf(exp,size,"?"); break;
179 }
180 return;
181 }
182
183 void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, bool isResponse) {
184 // get UID
185 if (MifareAuthState == masNone) {
186 if (cmdsize == 7 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
187 ClearAuthData();
188 AuthData.uid = bytes_to_num(&cmd[2], 4);
189 }
190 if (cmdsize == 7 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
191 ClearAuthData();
192 AuthData.uid = bytes_to_num(&cmd[2], 4);
193 }
194 }
195
196 switch(MifareAuthState) {
197 case masNt:
198 if (cmdsize == 4 && isResponse) {
199 snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)");
200 MifareAuthState = masNrAr;
201 if (AuthData.first_auth)
202 AuthData.nt = bytes_to_num(cmd, cmdsize);
203 else
204 AuthData.nt_enc = bytes_to_num(cmd, cmdsize);
205 return;
206 } else {
207 MifareAuthState = masError;
208 }
209 break;
210 case masNrAr:
211 if (cmdsize == 8 && !isResponse) {
212 snprintf(exp,size,"AUTH: nr ar (enc)");
213 MifareAuthState = masAt;
214 AuthData.nr_enc = bytes_to_num(cmd, cmdsize);
215 AuthData.ar_enc = bytes_to_num(&cmd[3], cmdsize);
216 return;
217 } else {
218 MifareAuthState = masError;
219 }
220 break;
221 case masAt:
222 if (cmdsize == 4 && isResponse) {
223 snprintf(exp,size,"AUTH: at (enc)");
224 MifareAuthState = masFirstData;
225 AuthData.at_enc = bytes_to_num(cmd, cmdsize);
226 return;
227 } else {
228 MifareAuthState = masError;
229 }
230 break;
231 default:
232 break;
233 }
234
235 if (!isResponse)
236 annotateIso14443a(exp, size, cmd, cmdsize);
237
238 }
Impressum, Datenschutz