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 "proxusb.h"
14 #include "proxmark3.h"
17 #include "cmdparser.h"
18 #include "cmdhflegic.h"
21 static int CmdHelp(const char *Cmd
);
23 static command_t CommandTable
[] =
25 {"help", CmdHelp
, 1, "This help"},
26 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
27 {"reader", CmdLegicRFRead
, 0, "[offset [length]] -- read bytes from a LEGIC card"},
28 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
29 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
30 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
31 {"write", CmdLegicRfWrite
,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
32 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
36 int CmdHFLegic(const char *Cmd
)
38 CmdsParse(CommandTable
, Cmd
);
42 int CmdHelp(const char *Cmd
)
44 CmdsHelp(CommandTable
);
49 * Output BigBuf and deobfuscate LEGIC RF tag data.
50 * This is based on information given in the talk held
51 * by Henryk Ploetz and Karsten Nohl at 26c3
53 int CmdLegicDecode(const char *Cmd
)
62 uint8_t data_buf
[1024]; // receiver buffer
63 char out_string
[3076]; // just use big buffer - bad practice
66 // copy data from proxmark into buffer
67 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
68 WaitForResponse(CMD_ACK
,NULL
);
70 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
72 PrintAndLog("\nCDF: System Area");
74 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
84 switch (data_buf
[5]&0x7f) {
86 strncpy(token_type
, "IAM",sizeof(token_type
));
89 strcpy(token_type
, "SAM");
92 strcpy(token_type
, "GAM");
95 strcpy(token_type
, "???");
99 stamp_len
= 0xfc - data_buf
[6];
101 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
105 (data_buf
[5]&0x80)>>7,
109 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
111 (data_buf
[7]&0x70)>>4,
112 (data_buf
[7]&0x80)>>7,
117 PrintAndLog("Remaining Header Area");
119 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
135 PrintAndLog("\nADF: User Area");
138 for (n
=0; n
<64; n
++) {
139 segment_len
= ((data_buf
[i
+1]^crc
)&0x0f) * 256 + (data_buf
[i
]^crc
);
140 segment_flag
= ((data_buf
[i
+1]^crc
)&0xf0)>>4;
142 wrp
= (data_buf
[i
+2]^crc
);
143 wrc
= ((data_buf
[i
+3]^crc
)&0x70)>>4;
145 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",
152 (segment_flag
&0x4)>>2,
153 (segment_flag
&0x8)>>3,
157 ((data_buf
[i
+3]^crc
)&0x80)>>7,
164 PrintAndLog("WRC protected area:");
165 for (k
=0, j
=0; k
< wrc
&& j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
166 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
167 out_string
[j
+2] = ' ';
170 out_string
[j
] = '\0';
172 PrintAndLog("%s", out_string
);
176 PrintAndLog("Remaining write protected area:");
178 for (k
=0, j
=0; k
< (wrp
-wrc
) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
179 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
180 out_string
[j
+2] = ' ';
183 out_string
[j
] = '\0';
185 PrintAndLog("%s", out_string
);
187 sprintf(out_string
,"Card ID: %2X%02X%02X",data_buf
[i
-4]^crc
,data_buf
[i
-3]^crc
,data_buf
[i
-2]^crc
);
188 PrintAndLog("%s", out_string
);
192 PrintAndLog("Remaining segment payload:");
193 for (k
=0, j
=0; k
< (segment_len
- wrp
- 5) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
194 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
195 out_string
[j
+2] = ' ';
198 out_string
[j
] = '\0';
200 PrintAndLog("%s", out_string
);
202 // end with last segment
203 if (segment_flag
& 0x8)
209 int CmdLegicRFRead(const char *Cmd
)
211 int byte_count
=0,offset
=0;
212 sscanf(Cmd
, "%i %i", &offset
, &byte_count
);
213 if(byte_count
== 0) byte_count
= -1;
214 if(byte_count
+ offset
> 1024) byte_count
= 1024 - offset
;
215 UsbCommand c
={CMD_READER_LEGIC_RF
, {offset
, byte_count
, 0}};
220 int CmdLegicLoad(const char *Cmd
)
222 FILE *f
= fopen(Cmd
, "r");
224 PrintAndLog("couldn't open '%s'", Cmd
);
227 char line
[80]; int offset
= 0; unsigned int data
[8];
228 while(fgets(line
, sizeof(line
), f
)) {
229 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
230 &data
[0], &data
[1], &data
[2], &data
[3],
231 &data
[4], &data
[5], &data
[6], &data
[7]);
233 PrintAndLog("Error: could not read samples");
237 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
238 int j
; for(j
= 0; j
< 8; j
++) {
239 c
.d
.asBytes
[j
] = data
[j
];
242 WaitForResponse(CMD_ACK
, NULL
);
246 PrintAndLog("loaded %u samples", offset
);
250 int CmdLegicSave(const char *Cmd
)
252 int requested
= 1024;
258 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
260 /* If no length given save entire legic read buffer */
261 /* round up to nearest 8 bytes so the saved data can be used with legicload */
262 if (requested
== 0) {
265 if (requested
% 8 != 0) {
266 int remainder
= requested
% 8;
267 requested
= requested
+ 8 - remainder
;
270 if (offset
+ requested
> sizeof(got
)) {
271 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
275 FILE *f
= fopen(filename
, "w");
277 PrintAndLog("couldn't open '%s'", Cmd
+1);
281 GetFromBigBuf(got
,requested
,offset
);
282 WaitForResponse(CMD_ACK
,NULL
);
284 for (int j
= 0; j
< requested
; j
+= 8) {
285 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
296 if (delivered
>= requested
)
301 PrintAndLog("saved %u samples", delivered
);
305 int CmdLegicRfSim(const char *Cmd
)
307 UsbCommand c
={CMD_SIMULATE_TAG_LEGIC_RF
};
311 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
316 int CmdLegicRfWrite(const char *Cmd
)
318 UsbCommand c
={CMD_WRITER_LEGIC_RF
};
319 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
, &c
.arg
[0], &c
.arg
[1]);
321 PrintAndLog("Please specify the offset and length as two hex strings");
328 int CmdLegicRfFill(const char *Cmd
)
330 UsbCommand cmd
={CMD_WRITER_LEGIC_RF
};
331 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
333 PrintAndLog("Please specify the offset, length and value as two hex strings");
338 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
339 for(i
= 0; i
< 48; i
++) {
340 c
.d
.asBytes
[i
] = cmd
.arg
[2];
342 for(i
= 0; i
< 22; i
++) {
345 WaitForResponse(CMD_ACK
,NULL
);