1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 #include "cmdhfthinfilm.h"
23 #include "cliparser.h"
24 #include "cmdparser.h" // command_t
29 #include "cmdhf14a.h" // manufacture
31 static int CmdHelp(const char *Cmd
);
33 // Printing function based upon the code in libnfc
35 // https://github.com/nfc-tools/libnfc/blob/master/utils/nfc-barcode.c
36 static int print_barcode(uint8_t *barcode
, const size_t barcode_len
, bool verbose
) {
38 PrintAndLogEx(NORMAL
, "");
40 uint8_t mb
= barcode
[0] & ~0x80;
41 PrintAndLogEx(SUCCESS
, " Manufacturer : "_YELLOW_("%s") "[0x%02X]", getTagInfo(mb
), mb
);
44 PrintAndLogEx(SUCCESS
, " Data format : "_YELLOW_("%02X"), barcode
[1]);
45 if (barcode_len
> 2) {
46 uint8_t b1
= 0, b2
= 0;
47 compute_crc(CRC_14443_A
, barcode
, barcode_len
- 2, &b1
, &b2
);
48 bool isok
= (barcode
[barcode_len
- 1] == b1
&& barcode
[barcode_len
- 2] == b2
);
50 PrintAndLogEx(SUCCESS
, " Checksum : "_YELLOW_("%02X %02X")" ( %s )", b2
, b1
, (isok
) ? _GREEN_("ok") : _RED_("fail"));
52 PrintAndLogEx(SUCCESS
, " Checksum : "_YELLOW_("too few data for checksum")" - " _RED_("fail"));
54 PrintAndLogEx(SUCCESS
, " Data len (bits) : "_YELLOW_("%zu")" ( %s )", barcode_len
* 8, (barcode_len
== 16 || barcode_len
== 32) ? _GREEN_("ok") : _YELLOW_("warning"));
55 PrintAndLogEx(SUCCESS
, " Raw data : "_YELLOW_("%s"), sprint_hex(barcode
, barcode_len
));
56 if (barcode_len
< 4) // too few to go to next decoding stages
61 memset(s
, 0x00, sizeof(s
));
65 PrintAndLogEx(SUCCESS
, " Data format : Reserved for allocation by tag manufacturer");
68 snprintf(s
, sizeof(s
), "http://www.");
71 snprintf(s
, sizeof(s
), "https://www.");
74 snprintf(s
, sizeof(s
), "http://");
77 snprintf(s
, sizeof(s
), "https://");
80 if (barcode_len
< 16) {
81 PrintAndLogEx(WARNING
, "EPC: (partial data) %s", sprint_hex(barcode
+ 2, barcode_len
- 2));
84 PrintAndLogEx(SUCCESS
, "EPC: %s", sprint_hex(barcode
+ 2, 12));
87 PrintAndLogEx(SUCCESS
, " Data format : RFU Reserved for future use (%02X)", barcode
[1]);
89 PrintAndLogEx(SUCCESS
, "Raw data with CRC: "_YELLOW_("%s"), sprint_hex(barcode
, barcode_len
));
93 snprintf(s
+ strlen(s
), barcode_len
- 3, (const char *)&barcode
[2], barcode_len
- 4);
95 for (size_t i
= 0; i
< strlen(s
); i
++) {
98 if ((uint8_t) s
[i
] == 0xFE) {
103 PrintAndLogEx(SUCCESS
, " Decoded NFC URL : "_YELLOW_("%s"), s
);
107 int CmdHfThinFilmInfo(const char *Cmd
) {
108 CLIParserContext
*ctx
;
109 CLIParserInit(&ctx
, "hf thinfilm info",
110 "Get info from Thinfilm tags",
117 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
119 return infoThinFilm(true);
122 int infoThinFilm(bool verbose
) {
124 clearCommandBuffer();
125 SendCommandNG(CMD_HF_THINFILM_READ
, NULL
, 0);
127 PacketResponseNG resp
;
128 if (!WaitForResponseTimeout(CMD_HF_THINFILM_READ
, &resp
, 1500)) {
129 PrintAndLogEx(WARNING
, "timeout while waiting for reply.");
133 if (resp
.status
== PM3_SUCCESS
) {
134 if (resp
.length
== 16 || resp
.length
== 32) {
135 PrintAndLogEx(NORMAL
, "");
136 PrintAndLogEx(INFO
, "--- " _CYAN_("Tag Information") " ---------------------------");
137 print_barcode(resp
.data
.asBytes
, resp
.length
, verbose
);
140 PrintAndLogEx(WARNING
, "Response is wrong length. (%d)", resp
.length
);
149 int CmdHfThinFilmSim(const char *Cmd
) {
150 CLIParserContext
*ctx
;
151 CLIParserInit(&ctx
, "hf thinfilm sim",
152 "Simulate Thinfilm tag",
153 "hf thinfilm sim -d B70470726f786d61726b2e636f6d");
157 arg_str1("d", "data", "<hex>", "bytes to send"),
158 arg_lit0(NULL
, "raw", "raw, provided bytes should include CRC"),
161 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
164 uint8_t data
[512] = {0};
165 CLIGetHexWithReturn(ctx
, 1, data
, &data_len
);
169 if (arg_get_lit(ctx
, 2)) {
175 if (addcrc
&& data_len
<= 510) {
176 uint8_t b1
= 0, b2
= 0;
177 compute_crc(CRC_14443_A
, data
, data_len
, &b1
, &b2
);
178 data
[data_len
++] = b2
;
179 data
[data_len
++] = b1
;
182 clearCommandBuffer();
183 SendCommandNG(CMD_HF_THINFILM_SIMULATE
, (uint8_t *)&data
, data_len
);
184 PacketResponseNG resp
;
185 PrintAndLogEx(SUCCESS
, "press " _GREEN_("pm3 button") " to abort simulation");
188 while (!(ret
= kbd_enter_pressed())) {
189 if (WaitForResponseTimeout(CMD_HF_THINFILM_SIMULATE
, &resp
, 500) == 0) continue;
190 if (resp
.status
!= PM3_SUCCESS
) break;
193 PrintAndLogEx(INFO
, "Client side interrupted");
194 PrintAndLogEx(WARNING
, "Simulation still running on Proxmark3 till next command or button press");
196 PrintAndLogEx(INFO
, "Done!");
201 static int CmdHfThinFilmList(const char *Cmd
) {
202 return CmdTraceListAlias(Cmd
, "hf thinfilm", "thinfilm");
205 static command_t CommandTable
[] = {
206 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
207 {"info", CmdHfThinFilmInfo
, IfPm3NfcBarcode
, "Tag information"},
208 {"list", CmdHfThinFilmList
, AlwaysAvailable
, "List NFC Barcode / Thinfilm history - not correct"},
209 {"sim", CmdHfThinFilmSim
, IfPm3NfcBarcode
, "Fake Thinfilm tag"},
210 {NULL
, NULL
, NULL
, NULL
}
213 static int CmdHelp(const char *Cmd
) {
214 (void)Cmd
; // Cmd is not used so far
215 CmdsHelp(CommandTable
);
219 int CmdHFThinfilm(const char *Cmd
) {
220 clearCommandBuffer();
221 return CmdsParse(CommandTable
, Cmd
);