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"
20 static int CmdHelp(const char *Cmd
);
23 * Output BigBuf and deobfuscate LEGIC RF tag data.
24 * This is based on information given in the talk held
25 * by Henryk Ploetz and Karsten Nohl at 26c3
27 int CmdLegicDecode(const char *Cmd
)
36 uint8_t data_buf
[3076]; // receiver buffer
37 char out_string
[3076]; // just use big buffer - bad practice
40 // copy data from proxmark into buffer
41 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
42 WaitForResponse(CMD_ACK
,NULL
);
44 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
46 PrintAndLog("\nCDF: System Area");
48 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
58 switch (data_buf
[5]&0x7f) {
60 strncpy(token_type
, "IAM",sizeof(token_type
));
63 strcpy(token_type
, "SAM");
66 strcpy(token_type
, "GAM");
69 strcpy(token_type
, "???");
73 stamp_len
= 0xfc - data_buf
[6];
75 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
79 (data_buf
[5]&0x80)>>7,
83 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
85 (data_buf
[7]&0x70)>>4,
86 (data_buf
[7]&0x80)>>7,
91 PrintAndLog("Remaining Header Area");
92 PrintAndLog("%s", sprint_hex(data_buf
+9, 13));
93 PrintAndLog("\nADF: User Area");
96 for ( n
=0; n
<64; n
++ ) {
97 segment_len
= ((data_buf
[i
+1]^crc
)&0x0f) * 256 + (data_buf
[i
]^crc
);
98 segment_flag
= ((data_buf
[i
+1]^crc
)&0xf0)>>4;
100 wrp
= (data_buf
[i
+2]^crc
);
101 wrc
= ((data_buf
[i
+3]^crc
)&0x70)>>4;
103 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",
110 (segment_flag
&0x4)>>2,
111 (segment_flag
&0x8)>>3,
115 ((data_buf
[i
+3]^crc
)&0x80)>>7,
122 PrintAndLog("WRC protected area:");
123 for ( k
=0, j
=0; k
< wrc
&& j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
124 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
125 out_string
[j
+2] = ' ';
128 out_string
[j
] = '\0';
130 PrintAndLog("%s", out_string
);
134 PrintAndLog("Remaining write protected area:");
136 for (k
=0, j
=0; k
< (wrp
-wrc
) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
137 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
138 out_string
[j
+2] = ' ';
141 out_string
[j
] = '\0';
143 PrintAndLog("%s", out_string
);
144 if( (wrp
-wrc
) == 8 ) {
145 sprintf(out_string
, "Card ID: %2X%02X%02X", data_buf
[i
-4]^crc
, data_buf
[i
-3]^crc
, data_buf
[i
-2]^crc
);
146 PrintAndLog("%s", out_string
);
150 PrintAndLog("Remaining segment payload:");
151 for ( k
=0, j
=0; k
< (segment_len
- wrp
- 5) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
152 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
153 out_string
[j
+2] = ' ';
156 out_string
[j
] = '\0';
158 PrintAndLog("%s", out_string
);
160 // end with last segment
161 if (segment_flag
& 0x8) return 0;
167 int CmdLegicRFRead(const char *Cmd
) {
168 int byte_count
=0, offset
=0;
169 sscanf(Cmd
, "%i %i", &offset
, &byte_count
);
170 if(byte_count
== 0) byte_count
= -1;
171 if(byte_count
+ offset
> 1024) byte_count
= 1024 - offset
;
173 UsbCommand c
= {CMD_READER_LEGIC_RF
, {offset
, byte_count
, 0}};
174 clearCommandBuffer();
179 int CmdLegicLoad(const char *Cmd
) {
180 char filename
[FILE_PATH_SIZE
] = {0x00};
183 char cmdp
= param_getchar(Cmd
, 0);
184 if ( cmdp
== 'H' || cmdp
== 'h' || cmdp
== 0x00) {
185 PrintAndLog("It loads datasamples from the file `filename`");
186 PrintAndLog("Usage: hf legic load <file name>");
187 PrintAndLog(" sample: hf legic load filename");
192 if (len
> FILE_PATH_SIZE
) {
193 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len
, FILE_PATH_SIZE
);
196 memcpy(filename
, Cmd
, len
);
198 FILE *f
= fopen(filename
, "r");
200 PrintAndLog("couldn't open '%s'", Cmd
);
206 uint32_t data
[8] = {0x00};
208 while ( fgets(line
, sizeof(line
), f
) ) {
209 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
210 &data
[0], &data
[1], &data
[2], &data
[3],
211 &data
[4], &data
[5], &data
[6], &data
[7]);
213 PrintAndLog("Error: could not read samples");
217 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
219 for( j
= 0; j
< 8; j
++) {
220 c
.d
.asBytes
[j
] = data
[j
];
224 WaitForResponse(CMD_ACK
, NULL
);
228 PrintAndLog("loaded %u samples", offset
);
232 int CmdLegicSave(const char *Cmd
) {
233 int requested
= 1024;
236 char filename
[FILE_PATH_SIZE
];
237 uint8_t got
[1024] = {0x00};
239 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
241 /* If no length given save entire legic read buffer */
242 /* round up to nearest 8 bytes so the saved data can be used with legicload */
243 if (requested
== 0) {
247 if (requested
% 8 != 0) {
248 int remainder
= requested
% 8;
249 requested
= requested
+ 8 - remainder
;
252 if (offset
+ requested
> sizeof(got
)) {
253 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
257 FILE *f
= fopen(filename
, "w");
259 PrintAndLog("couldn't open '%s'", Cmd
+1);
263 GetFromBigBuf(got
,requested
,offset
);
264 WaitForResponse(CMD_ACK
,NULL
);
266 for (int j
= 0; j
< requested
; j
+= 8) {
267 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
278 if (delivered
>= requested
) break;
282 PrintAndLog("saved %u samples", delivered
);
286 int CmdLegicRfSim(const char *Cmd
) {
287 UsbCommand c
= {CMD_SIMULATE_TAG_LEGIC_RF
, {6,3,0}};
288 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
289 clearCommandBuffer();
294 int CmdLegicRfWrite(const char *Cmd
) {
295 UsbCommand c
= {CMD_WRITER_LEGIC_RF
};
296 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
, &c
.arg
[0], &c
.arg
[1]);
298 PrintAndLog("Please specify the offset and length as two hex strings");
301 clearCommandBuffer();
306 int CmdLegicRfFill(const char *Cmd
) {
307 UsbCommand cmd
= {CMD_WRITER_LEGIC_RF
};
308 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
310 PrintAndLog("Please specify the offset, length and value as two hex strings");
315 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
316 for(i
= 0; i
< 48; i
++) {
317 c
.d
.asBytes
[i
] = cmd
.arg
[2];
320 for(i
= 0; i
< 22; i
++) {
323 WaitForResponse(CMD_ACK
,NULL
);
325 clearCommandBuffer();
330 static command_t CommandTable
[] = {
331 {"help", CmdHelp
, 1, "This help"},
332 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
333 {"reader", CmdLegicRFRead
, 0, "[offset [length]] -- read bytes from a LEGIC card"},
334 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
335 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
336 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
337 {"write", CmdLegicRfWrite
,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
338 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
339 {NULL
, NULL
, 0, NULL
}
342 int CmdHFLegic(const char *Cmd
) {
343 clearCommandBuffer();
344 CmdsParse(CommandTable
, Cmd
);
348 int CmdHelp(const char *Cmd
) {
349 CmdsHelp(CommandTable
);