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 //-----------------------------------------------------------------------------
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"},
30 int CmdHFLegic(const char *Cmd
)
32 CmdsParse(CommandTable
, Cmd
);
36 int CmdHelp(const char *Cmd
)
38 CmdsHelp(CommandTable
);
43 * Output BigBuf and deobfuscate LEGIC RF tag data.
44 * This is based on information given in the talk held
45 * by Henryk Ploetz and Karsten Nohl at 26c3
47 int CmdLegicDecode(const char *Cmd
)
56 int data_buf
[1032]; // receiver buffer
57 char out_string
[3076]; // just use big buffer - bad practice
63 // copy data from proxmark into buffer
64 for (i
= 0; i
< 256; i
+= 12, h
+= 48) {
65 UsbCommand c
= {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
, {i
, 0, 0}};
67 WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
);
69 for (j
= 0; j
< 48; j
+= 8) {
70 for (k
= 0; k
< 8; k
++) {
71 data_buf
[h
+j
+k
] = sample_buf
[j
+k
];
74 if (delivered
>= 1024)
79 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
81 PrintAndLog("\nCDF: System Area");
83 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
93 switch (data_buf
[5]&0x7f) {
95 strncpy(token_type
, "IAM",sizeof(token_type
));
98 strcpy(token_type
, "SAM");
101 strcpy(token_type
, "GAM");
104 strcpy(token_type
, "???");
108 stamp_len
= 0xfc - data_buf
[6];
110 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
114 (data_buf
[5]&0x80)>>7,
118 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
120 (data_buf
[7]&0x70)>>4,
121 (data_buf
[7]&0x80)>>7,
126 PrintAndLog("Remaining Header Area");
128 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
144 PrintAndLog("\nADF: User Area");
147 for (n
=0; n
<64; n
++) {
148 segment_len
= ((data_buf
[i
+1]^crc
)&0x0f) * 256 + (data_buf
[i
]^crc
);
149 segment_flag
= ((data_buf
[i
+1]^crc
)&0xf0)>>4;
151 wrp
= (data_buf
[i
+2]^crc
);
152 wrc
= ((data_buf
[i
+3]^crc
)&0x70)>>4;
154 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",
161 (segment_flag
&0x4)>>2,
162 (segment_flag
&0x8)>>3,
166 ((data_buf
[i
+3]^crc
)&0x80)>>7,
173 PrintAndLog("WRC protected area:");
174 for (k
=0, j
=0; k
< wrc
&& j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
175 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
176 out_string
[j
+2] = ' ';
179 out_string
[j
] = '\0';
181 PrintAndLog("%s", out_string
);
185 PrintAndLog("Remaining write protected area:");
187 for (k
=0, j
=0; k
< (wrp
-wrc
) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
188 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
189 out_string
[j
+2] = ' ';
192 out_string
[j
] = '\0';
194 PrintAndLog("%s", out_string
);
196 sprintf(out_string
,"Card ID: %2X%02X%02X",data_buf
[i
-4]^crc
,data_buf
[i
-3]^crc
,data_buf
[i
-2]^crc
);
197 PrintAndLog("%s", out_string
);
201 PrintAndLog("Remaining segment payload:");
202 for (k
=0, j
=0; k
< (segment_len
- wrp
- 5) && j
<(sizeof(out_string
)-3); k
++, i
++, j
+= 3) {
203 sprintf(&out_string
[j
], "%02x", (data_buf
[i
]^crc
));
204 out_string
[j
+2] = ' ';
207 out_string
[j
] = '\0';
209 PrintAndLog("%s", out_string
);
211 // end with last segment
212 if (segment_flag
& 0x8)
218 int CmdLegicRFRead(const char *Cmd
)
220 int byte_count
=0,offset
=0;
221 sscanf(Cmd
, "%i %i", &offset
, &byte_count
);
222 if(byte_count
== 0) byte_count
= -1;
223 if(byte_count
+ offset
> 1024) byte_count
= 1024 - offset
;
224 UsbCommand c
={CMD_READER_LEGIC_RF
, {offset
, byte_count
, 0}};