1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // High frequency Legic commands
9 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
16 #include "cmdparser.h"
17 #include "cmdhflegic.h"
21 static int CmdHelp(const char *Cmd
);
23 int usage_legic_calccrc8(void){
24 PrintAndLog("Calculates the legic crc8 on the input hexbytes.");
25 PrintAndLog("There must be an even number of hexsymbols as input.");
26 PrintAndLog("Usage: hf legic crc8 <hexbytes>");
27 PrintAndLog("Options :");
28 PrintAndLog(" <hexbytes> : hex bytes in a string");
30 PrintAndLog("Sample : hf legic crc8 deadbeef1122");
34 int usage_legic_load(void){
35 PrintAndLog("It loads datasamples from the file `filename` to device memory");
36 PrintAndLog("Usage: hf legic load <file name>");
37 PrintAndLog(" sample: hf legic load filename");
42 * Output BigBuf and deobfuscate LEGIC RF tag data.
43 * This is based on information given in the talk held
44 * by Henryk Ploetz and Karsten Nohl at 26c3
46 int CmdLegicDecode(const char *Cmd
) {
54 uint8_t data_buf
[1200]; // receiver buffer
55 //char out_string[3076]; // just use big buffer - bad practice
58 // copy data from proxmark into buffer
59 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
60 WaitForResponse(CMD_ACK
,NULL
);
62 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
64 uint32_t calc_crc
= CRC8Legic(data_buf
, 4);
66 PrintAndLog("\nCDF: System Area");
68 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
74 (calc_crc
== crc
) ? "OK":"Fail"
77 switch (data_buf
[5]&0x7f) {
79 strncpy(token_type
, "IAM",sizeof(token_type
));
82 strcpy(token_type
, "SAM");
85 strcpy(token_type
, "GAM");
88 strcpy(token_type
, "???");
92 stamp_len
= 0xfc - data_buf
[6];
94 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
98 (data_buf
[5]&0x80)>>7,
102 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
104 (data_buf
[7]&0x70)>>4,
105 (data_buf
[7]&0x80)>>7,
110 PrintAndLog("Remaining Header Area");
111 PrintAndLog("%s", sprint_hex(data_buf
+9, 13));
112 PrintAndLog("\nADF: User Area");
115 uint8_t segCrcBytes
[8] = {0x00};
116 uint32_t segCalcCRC
= 0;
119 for ( n
=0; n
<64; n
++ ) {
120 segment_len
= ((data_buf
[i
+1]^crc
)&0x0f) * 256 + (data_buf
[i
]^crc
);
121 segment_flag
= ((data_buf
[i
+1]^crc
)&0xf0)>>4;
123 wrp
= (data_buf
[i
+2]^crc
);
124 wrc
= ((data_buf
[i
+3]^crc
)&0x70)>>4;
126 /* validate segment-crc */
127 segCRC
= data_buf
[i
+4]^crc
;
129 segCrcBytes
[0]=data_buf
[0]; //uid0
130 segCrcBytes
[1]=data_buf
[1]; //uid1
131 segCrcBytes
[2]=data_buf
[2]; //uid2
132 segCrcBytes
[3]=data_buf
[3]; //uid3
133 segCrcBytes
[4]=(data_buf
[i
]^crc
); //hdr0
134 segCrcBytes
[5]=(data_buf
[i
+1]^crc
); //hdr1
135 segCrcBytes
[6]=(data_buf
[i
+2]^crc
); //hdr2
136 segCrcBytes
[7]=(data_buf
[i
+3]^crc
); //hdr3
137 segCalcCRC
= CRC8Legic(segCrcBytes
, 8);
139 PrintAndLog("Segment %02u: raw header=%02x %02x %02x %02x, flag=%01x (valid=%01u, last=%01u), len=%04u, WRP=%02u, WRC=%02u, RD=%01u, CRC=%02x (%s)",
146 (segment_flag
&0x4)>>2,
147 (segment_flag
&0x8)>>3,
151 ((data_buf
[i
+3]^crc
)&0x80)>>7,
153 ( segCRC
== segCalcCRC
) ? "OK" : "fail"
159 PrintAndLog("WRC protected area:");
161 for ( k
=i
; k
< wrc
; k
++)
164 for ( k
=i
; k
< wrc
; k
+= 8)
165 PrintAndLog("%s", sprint_hex( data_buf
+k
, 8) );
171 PrintAndLog("Remaining write protected area:");
173 if ( data_buf
[k
] > 0) {
174 for (k
=i
; k
< (wrp
-wrc
); k
++)
178 for (k
=i
; k
< (wrp
-wrc
); k
++)
179 PrintAndLog("%s", sprint_hex( data_buf
+k
, 16) );
184 PrintAndLog("Card ID: %2X%02X%02X", data_buf
[i
-4]^crc
, data_buf
[i
-3]^crc
, data_buf
[i
-2]^crc
);
187 PrintAndLog("Remaining segment payload:");
189 if ( data_buf
[k
] > 0 ) {
190 for ( k
=i
; k
< (segment_len
- wrp
- 5); k
++)
194 for ( k
=i
; k
< (segment_len
- wrp
- 5); k
++)
195 PrintAndLog("%s", sprint_hex( data_buf
+k
, 16) );
197 // end with last segment
198 if (segment_flag
& 0x8) return 0;
204 int CmdLegicRFRead(const char *Cmd
) {
205 int byte_count
=0, offset
=0;
206 sscanf(Cmd
, "%i %i", &offset
, &byte_count
);
207 if(byte_count
== 0) byte_count
= -1;
208 if(byte_count
+ offset
> 1024) byte_count
= 1024 - offset
;
210 UsbCommand c
= {CMD_READER_LEGIC_RF
, {offset
, byte_count
, 0}};
211 clearCommandBuffer();
216 int CmdLegicLoad(const char *Cmd
) {
218 char cmdp
= param_getchar(Cmd
, 0);
219 if ( cmdp
== 'H' || cmdp
== 'h' || cmdp
== 0x00) return usage_legic_load();
221 char filename
[FILE_PATH_SIZE
] = {0x00};
222 int len
= strlen(Cmd
);
224 if (len
> FILE_PATH_SIZE
) {
225 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len
, FILE_PATH_SIZE
);
228 memcpy(filename
, Cmd
, len
);
230 FILE *f
= fopen(filename
, "r");
232 PrintAndLog("couldn't open '%s'", Cmd
);
238 uint8_t data
[8] = {0x00};
240 while ( fgets(line
, sizeof(line
), f
) ) {
241 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
242 (unsigned int *)&data
[0], (unsigned int *)&data
[1], (unsigned int *)&data
[2], (unsigned int *)&data
[3],
243 (unsigned int *)&data
[4], (unsigned int *)&data
[5], (unsigned int *)&data
[6], (unsigned int *)&data
[7]);
246 PrintAndLog("Error: could not read samples");
251 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
252 memcpy(c
.d
.asBytes
, data
, 8);
253 clearCommandBuffer();
255 WaitForResponse(CMD_ACK
, NULL
);
259 PrintAndLog("loaded %u samples", offset
);
263 int CmdLegicSave(const char *Cmd
) {
264 int requested
= 1024;
267 char filename
[FILE_PATH_SIZE
];
268 uint8_t got
[1024] = {0x00};
270 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
272 /* If no length given save entire legic read buffer */
273 /* round up to nearest 8 bytes so the saved data can be used with legicload */
277 if (requested
% 8 != 0) {
278 int remainder
= requested
% 8;
279 requested
= requested
+ 8 - remainder
;
282 if (offset
+ requested
> sizeof(got
)) {
283 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
287 FILE *f
= fopen(filename
, "w");
289 PrintAndLog("couldn't open '%s'", Cmd
+1);
293 GetFromBigBuf(got
,requested
,offset
);
294 WaitForResponse(CMD_ACK
,NULL
);
296 for (int j
= 0; j
< requested
; j
+= 8) {
297 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
308 if (delivered
>= requested
) break;
312 PrintAndLog("saved %u samples", delivered
);
316 int CmdLegicRfSim(const char *Cmd
) {
317 UsbCommand c
= {CMD_SIMULATE_TAG_LEGIC_RF
, {6,3,0}};
318 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
319 clearCommandBuffer();
324 int CmdLegicRfWrite(const char *Cmd
) {
325 UsbCommand c
= {CMD_WRITER_LEGIC_RF
};
326 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
, &c
.arg
[0], &c
.arg
[1]);
328 PrintAndLog("Please specify the offset and length as two hex strings");
331 clearCommandBuffer();
336 int CmdLegicRfFill(const char *Cmd
) {
337 UsbCommand cmd
= {CMD_WRITER_LEGIC_RF
};
338 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
340 PrintAndLog("Please specify the offset, length and value as two hex strings");
345 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
346 for(i
= 0; i
< 48; i
++) {
347 c
.d
.asBytes
[i
] = cmd
.arg
[2];
350 for(i
= 0; i
< 22; i
++) {
353 WaitForResponse(CMD_ACK
,NULL
);
355 clearCommandBuffer();
360 int CmdLegicCalcCrc8(const char *Cmd
){
362 int len
= strlen(Cmd
);
363 if (len
& 1 ) return usage_legic_calccrc8();
365 uint8_t *data
= malloc(len
);
366 if ( data
== NULL
) return 1;
368 param_gethex(Cmd
, 0, data
, len
);
370 uint32_t checksum
= CRC8Legic(data
, len
/2);
371 PrintAndLog("Bytes: %s || CRC8: %X", sprint_hex(data
, len
/2), checksum
);
376 static command_t CommandTable
[] = {
377 {"help", CmdHelp
, 1, "This help"},
378 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
379 {"read", CmdLegicRFRead
, 0, "[offset][length] -- read bytes from a LEGIC card"},
380 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
381 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
382 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
383 {"write", CmdLegicRfWrite
,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
384 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
385 {"crc8", CmdLegicCalcCrc8
, 1, "Calculate Legic CRC8 over given hexbytes"},
386 {NULL
, NULL
, 0, NULL
}
389 int CmdHFLegic(const char *Cmd
) {
390 clearCommandBuffer();
391 CmdsParse(CommandTable
, Cmd
);
395 int CmdHelp(const char *Cmd
) {
396 CmdsHelp(CommandTable
);