]>
Commit | Line | Data |
---|---|---|
dbf6e824 CY |
1 | //----------------------------------------------------------------------------- |
2 | // Authored by Craig Young <cyoung@tripwire.com> based on cmdlfhid.c structure | |
3 | // | |
4 | // cmdlfhid.c is Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
5 | // | |
6 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
7 | // at your option, any later version. See the LICENSE.txt file for the text of | |
8 | // the license. | |
9 | //----------------------------------------------------------------------------- | |
10 | // Low frequency AWID26 commands | |
11 | //----------------------------------------------------------------------------- | |
12 | ||
13 | #include <stdio.h> // sscanf | |
14 | #include "proxmark3.h" // Definitions, USB controls, etc | |
15 | #include "ui.h" // PrintAndLog | |
16 | #include "cmdparser.h" // CmdsParse, CmdsHelp | |
17 | #include "cmdlfawid.h" // AWID function declarations | |
18 | #include "lfdemod.h" // parityTest | |
19 | ||
20 | static int CmdHelp(const char *Cmd); | |
21 | ||
22 | int CmdAWIDDemodFSK(const char *Cmd) | |
23 | { | |
24 | int findone=0; | |
25 | if(Cmd[0]=='1') findone=1; | |
26 | UsbCommand c={CMD_AWID_DEMOD_FSK}; | |
27 | c.arg[0]=findone; | |
28 | SendCommand(&c); | |
29 | return 0; | |
30 | } | |
31 | ||
32 | int getAWIDBits(unsigned int fc, unsigned int cn, uint8_t *AWIDBits) | |
33 | { | |
34 | int i; | |
35 | uint32_t fcode=(fc & 0x000000FF), cnum=(cn & 0x0000FFFF), uBits=0; | |
36 | if (fcode != fc) | |
37 | PrintAndLog("NOTE: Facility code truncated for AWID26 format (8-bit facility code)"); | |
38 | if (cnum!=cn) | |
39 | PrintAndLog("NOTE: Card number was truncated for AWID26 format (16-bit card number)"); | |
40 | ||
41 | AWIDBits[0] = 0x01; // 6-bit Preamble with 2 parity bits | |
42 | AWIDBits[1] = 0x1D; // First byte from card format (26-bit) plus parity bits | |
43 | AWIDBits[2] = 0x80; // Set the next two bits as 0b10 to finish card format | |
44 | uBits = (fcode<<4) + (cnum>>12); | |
45 | if (!parityTest(uBits,12,0)) | |
46 | AWIDBits[2] |= (1<<5); // If not already even parity, set bit to make even | |
47 | uBits = AWIDBits[2]>>5; | |
48 | if (!parityTest(uBits, 3, 1)) | |
49 | AWIDBits[2] |= (1<<4); | |
50 | uBits = fcode>>5; // first 3 bits of facility-code | |
51 | AWIDBits[2] += (uBits<<1); | |
52 | if (!parityTest(uBits, 3, 1)) | |
53 | AWIDBits[2]++; // Set parity bit to make odd parity | |
54 | uBits = (fcode & 0x1C)>>2; | |
55 | AWIDBits[3] = 0; | |
56 | if (!parityTest(uBits,3,1)) | |
57 | AWIDBits[3] |= (1<<4); | |
58 | AWIDBits[3] += (uBits<<5); | |
59 | uBits = ((fcode & 0x3)<<1) + ((cnum & 0x8000)>>15); // Grab/shift 2 LSBs from facility code and add shifted MSB from cardnum | |
60 | if (!parityTest(uBits,3,1)) | |
61 | AWIDBits[3]++; // Set LSB for parity | |
62 | AWIDBits[3]+= (uBits<<1); | |
63 | uBits = (cnum & 0x7000)>>12; | |
64 | AWIDBits[4] = uBits<<5; | |
65 | if (!parityTest(uBits,3,1)) | |
66 | AWIDBits[4] |= (1<<4); | |
67 | uBits = (cnum & 0x0E00)>>9; | |
68 | AWIDBits[4] += (uBits<<1); | |
69 | if (!parityTest(uBits,3,1)) | |
70 | AWIDBits[4]++; // Set LSB for parity | |
71 | uBits = (cnum & 0x1C0)>>6; // Next bits from card number | |
72 | AWIDBits[5]=(uBits<<5); | |
73 | if (!parityTest(uBits,3,1)) | |
74 | AWIDBits[5] |= (1<<4); // Set odd parity bit as needed | |
75 | uBits = (cnum & 0x38)>>3; | |
76 | AWIDBits[5]+= (uBits<<1); | |
77 | if (!parityTest(uBits,3,1)) | |
78 | AWIDBits[5]++; // Set odd parity bit as needed | |
79 | uBits = (cnum & 0x7); // Last three bits from card number! | |
80 | AWIDBits[6] = (uBits<<5); | |
81 | if (!parityTest(uBits,3,1)) | |
82 | AWIDBits[6] |= (1<<4); | |
83 | uBits = (cnum & 0x0FFF); | |
84 | if (!parityTest(uBits,12,1)) | |
85 | AWIDBits[6] |= (1<<3); | |
86 | else | |
87 | AWIDBits[6]++; | |
88 | for (i = 7; i<12; i++) | |
89 | AWIDBits[i]=0x11; | |
90 | return 1; | |
91 | } | |
92 | ||
93 | int CmdAWIDSim(const char *Cmd) | |
94 | { | |
95 | uint32_t fcode = 0, cnum = 0, fc=0, cn=0, i=0; | |
96 | uint8_t *BS, BitStream[12]; | |
97 | uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8 | |
98 | uint64_t arg2 = 50; // clk RF/50 invert=0 | |
99 | BS = BitStream; | |
100 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) { | |
101 | PrintAndLog("Usage: lf awid sim <facility-code> <card number>"); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | fcode=(fc & 0x000000FF); | |
106 | cnum=(cn & 0x0000FFFF); | |
107 | if (fc!=fcode) | |
108 | PrintAndLog("Facility-Code (%u) truncated to 8-bits: %u",fc,fcode); | |
109 | if (cn!=cnum) | |
110 | PrintAndLog("Card number (%u) truncated to 16-bits: %u",cn,cnum); | |
111 | PrintAndLog("Emulating AWID26 -- FC: %u; CN: %u\n",fcode,cnum); | |
112 | PrintAndLog("Press pm3-button to abort simulation or run another command"); | |
113 | // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0 | |
114 | if (getAWIDBits(fc, cn, BS)) { | |
115 | PrintAndLog("Running 'lf simfsk c 50 H 10 L 8 d %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'", | |
116 | BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6], | |
117 | BS[7],BS[8],BS[9],BS[10],BS[11]); | |
118 | } else | |
119 | PrintAndLog("Error with tag bitstream generation."); | |
120 | UsbCommand c; | |
121 | c.cmd = CMD_FSK_SIM_TAG; | |
122 | c.arg[0] = arg1; // fcHigh<<8 + fcLow | |
123 | c.arg[1] = arg2; // Inversion and clk setting | |
124 | c.arg[2] = 96; // Bitstream length: 96-bits == 12 bytes | |
125 | for (i=0; i < 96; i++) | |
126 | c.d.asBytes[i] = (BS[i/8] & (1<<(7-(i%8))))?1:0; | |
127 | SendCommand(&c); | |
128 | return 0; | |
129 | } | |
130 | ||
131 | int CmdAWIDClone(const char *Cmd) | |
132 | { | |
133 | uint32_t fc=0,cn=0,blocks[4] = {0x00107060, 0, 0, 0x11111111}, i=0; | |
134 | uint8_t BitStream[12]; | |
135 | uint8_t *BS=BitStream; | |
136 | UsbCommand c; | |
137 | ||
138 | ||
139 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) { | |
140 | PrintAndLog("Usage: lf awid clone <facility-code> <card number>"); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | if ((fc & 0xFF) != fc) { | |
145 | fc &= 0xFF; | |
146 | PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc); | |
147 | } | |
148 | if ((cn & 0xFFFF) != cn) { | |
149 | cn &= 0xFFFF; | |
150 | PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn); | |
151 | } | |
152 | if (getAWIDBits(fc,cn,BS)) { | |
153 | PrintAndLog("Preparing to clone AWID26 to T55x7 with FC: %u, CN: %u (Raw: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x)", | |
154 | fc,cn, BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6],BS[7],BS[8],BS[9],BS[10],BS[11]); | |
155 | blocks[1] = (BS[0]<<24) + (BS[1]<<16) + (BS[2]<<8) + (BS[3]); | |
156 | blocks[2] = (BS[4]<<24) + (BS[5]<<16) + (BS[6]<<8) + (BS[7]); | |
157 | PrintAndLog("Block 0: 0x%08x", blocks[0]); | |
158 | PrintAndLog("Block 1: 0x%08x", blocks[1]); | |
159 | PrintAndLog("Block 2: 0x%08x", blocks[2]); | |
160 | PrintAndLog("Block 3: 0x%08x", blocks[3]); | |
161 | for (i=0; i<4; i++) { | |
162 | c.cmd = CMD_T55XX_WRITE_BLOCK; | |
163 | c.arg[0] = blocks[i]; | |
164 | c.arg[1] = i; | |
165 | c.arg[2] = 0; | |
166 | SendCommand(&c); | |
167 | } | |
168 | } | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static command_t CommandTable[] = | |
173 | { | |
174 | {"help", CmdHelp, 1, "This help"}, | |
175 | {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"}, | |
176 | {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"}, | |
177 | {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> -- Clone AWID to T55x7 (tag must be in range of antenna)"}, | |
178 | {NULL, NULL, 0, NULL} | |
179 | }; | |
180 | ||
181 | int CmdLFAWID(const char *Cmd) | |
182 | { | |
183 | CmdsParse(CommandTable, Cmd); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | int CmdHelp(const char *Cmd) | |
188 | { | |
189 | CmdsHelp(CommandTable); | |
190 | return 0; | |
191 | } |