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
);
22 static command_t CommandTable
[] =
24 {"help", CmdHelp
, 1, "This help"},
25 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
26 {"reader", CmdLegicRFRead
, 0, "[offset [length]] -- read bytes from a LEGIC card"},
27 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
28 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
29 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
30 {"write", CmdLegicRfWrite
,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
31 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
35 int CmdHFLegic(const char *Cmd
)
37 CmdsParse(CommandTable
, Cmd
);
41 int CmdHelp(const char *Cmd
)
43 CmdsHelp(CommandTable
);
48 * Output BigBuf and deobfuscate LEGIC RF tag data.
49 * This is based on information given in the talk held
50 * by Henryk Ploetz and Karsten Nohl at 26c3
52 int CmdLegicDecode(const char *Cmd
)
61 uint8_t data_buf
[1024]; // receiver buffer
62 char out_string
[3076]; // just use big buffer - bad practice
65 // copy data from proxmark into buffer
66 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
67 WaitForResponse(CMD_ACK
,NULL
);
69 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
71 PrintAndLog("\nCDF: System Area");
73 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
83 switch (data_buf
[5]&0x7f) {
85 strncpy(token_type
, "IAM",sizeof(token_type
));
88 strcpy(token_type
, "SAM");
91 strcpy(token_type
, "GAM");
94 strcpy(token_type
, "???");
98 stamp_len
= 0xfc - data_buf
[6];
100 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
104 (data_buf
[5]&0x80)>>7,
108 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
110 (data_buf
[7]&0x70)>>4,
111 (data_buf
[7]&0x80)>>7,
116 PrintAndLog("Remaining Header Area");
118 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
134 PrintAndLog("\nADF: User Area");
137 for (n
=0; n
<64; n
++) {
138 segment_len
= ((data_buf
[i
+1]^crc
)&0x0f) * 256 + (data_buf
[i
]^crc
);
139 segment_flag
= ((data_buf
[i
+1]^crc
)&0xf0)>>4;
141 wrp
= (data_buf
[i
+2]^crc
);
142 wrc
= ((data_buf
[i
+3]^crc
)&0x70)>>4;
144 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",
151 (segment_flag
&0x4)>>2,
152 (segment_flag
&0x8)>>3,
156 ((data_buf
[i
+3]^crc
)&0x80)>>7,
163 PrintAndLog("WRC protected area:");
164 for (k
=0, j
=0; k
< wrc
&& j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
165 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
166 out_string
[j
+2] = ' ';
169 out_string
[j
] = '\0';
171 PrintAndLog("%s", out_string
);
175 PrintAndLog("Remaining write protected area:");
177 for (k
=0, j
=0; k
< (wrp
-wrc
) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
178 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
179 out_string
[j
+2] = ' ';
182 out_string
[j
] = '\0';
184 PrintAndLog("%s", out_string
);
186 sprintf(out_string
,"Card ID: %2X%02X%02X",data_buf
[i
-4]^crc
,data_buf
[i
-3]^crc
,data_buf
[i
-2]^crc
);
187 PrintAndLog("%s", out_string
);
191 PrintAndLog("Remaining segment payload:");
192 for (k
=0, j
=0; k
< (segment_len
- wrp
- 5) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
193 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
194 out_string
[j
+2] = ' ';
197 out_string
[j
] = '\0';
199 PrintAndLog("%s", out_string
);
201 // end with last segment
202 if (segment_flag
& 0x8)
208 int CmdLegicRFRead(const char *Cmd
)
210 int byte_count
=0,offset
=0;
211 sscanf(Cmd
, "%i %i", &offset
, &byte_count
);
212 if(byte_count
== 0) byte_count
= -1;
213 if(byte_count
+ offset
> 1024) byte_count
= 1024 - offset
;
214 UsbCommand c
={CMD_READER_LEGIC_RF
, {offset
, byte_count
, 0}};
219 int CmdLegicLoad(const char *Cmd
)
221 FILE *f
= fopen(Cmd
, "r");
223 PrintAndLog("couldn't open '%s'", Cmd
);
226 char line
[80]; int offset
= 0; unsigned int data
[8];
227 while(fgets(line
, sizeof(line
), f
)) {
228 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
229 &data
[0], &data
[1], &data
[2], &data
[3],
230 &data
[4], &data
[5], &data
[6], &data
[7]);
232 PrintAndLog("Error: could not read samples");
236 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
237 int j
; for(j
= 0; j
< 8; j
++) {
238 c
.d
.asBytes
[j
] = data
[j
];
241 WaitForResponse(CMD_ACK
, NULL
);
245 PrintAndLog("loaded %u samples", offset
);
249 int CmdLegicSave(const char *Cmd
)
251 int requested
= 1024;
257 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
259 /* If no length given save entire legic read buffer */
260 /* round up to nearest 8 bytes so the saved data can be used with legicload */
261 if (requested
== 0) {
264 if (requested
% 8 != 0) {
265 int remainder
= requested
% 8;
266 requested
= requested
+ 8 - remainder
;
268 if (offset
+ requested
> sizeof(got
)) {
269 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
273 FILE *f
= fopen(filename
, "w");
275 PrintAndLog("couldn't open '%s'", Cmd
+1);
279 GetFromBigBuf(got
,requested
,offset
);
280 WaitForResponse(CMD_ACK
,NULL
);
282 for (int j
= 0; j
< requested
; j
+= 8) {
283 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
294 if (delivered
>= requested
)
299 PrintAndLog("saved %u samples", delivered
);
303 int CmdLegicRfSim(const char *Cmd
)
305 UsbCommand c
={CMD_SIMULATE_TAG_LEGIC_RF
};
309 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
314 int CmdLegicRfWrite(const char *Cmd
)
316 UsbCommand c
={CMD_WRITER_LEGIC_RF
};
317 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
, &c
.arg
[0], &c
.arg
[1]);
319 PrintAndLog("Please specify the offset and length as two hex strings");
326 int CmdLegicRfFill(const char *Cmd
)
328 UsbCommand cmd
={CMD_WRITER_LEGIC_RF
};
329 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
331 PrintAndLog("Please specify the offset, length and value as two hex strings");
336 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
337 for(i
= 0; i
< 48; i
++) {
338 c
.d
.asBytes
[i
] = cmd
.arg
[2];
340 for(i
= 0; i
< 22; i
++) {
343 WaitForResponse(CMD_ACK
,NULL
);