]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfpyramid.c
ADD: "LF PYRAMID SIM"
[proxmark3-svn] / client / cmdlfpyramid.c
CommitLineData
71cb7328 1//-----------------------------------------------------------------------------
2//
3// This code is licensed to you under the terms of the GNU GPL, version 2 or,
4// at your option, any later version. See the LICENSE.txt file for the text of
5// the license.
6//-----------------------------------------------------------------------------
7// Low frequency Farpoint / Pyramid tag commands
8//-----------------------------------------------------------------------------
9#include <string.h>
10#include <inttypes.h>
11#include "cmdlfpyramid.h"
12static int CmdHelp(const char *Cmd);
13
14int usage_lf_pyramid_clone(void){
15 PrintAndLog("clone a Farepointe/Pyramid tag to a T55x7 tag.");
16 PrintAndLog("Per pyramid format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
17 PrintAndLog("");
18 PrintAndLog("Usage: lf pyramid clone <Facility-Code> <Card-Number>");
19 PrintAndLog("Options :");
20 PrintAndLog(" <Facility-Code> : 8-bit value facility code");
21 PrintAndLog(" <Card Number> : 16-bit value card number");
22 PrintAndLog("");
23 PrintAndLog("Sample : lf pyramid clone 123 11223");
24 return 0;
25}
26
27int usage_lf_pyramid_sim(void) {
28 PrintAndLog("Enables simulation of Farepointe/Pyramid card with specified card number.");
29 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
30 PrintAndLog("Per pyramid format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
31 PrintAndLog("");
32 PrintAndLog("Usage: lf pyramid sim <Card-Number>");
33 PrintAndLog("Options :");
34 PrintAndLog(" <Facility-Code> : 8-bit value facility code");
35 PrintAndLog(" <Card Number> : 16-bit value card number");
36 PrintAndLog("");
37 PrintAndLog("Sample : lf pyramid sim 123 11223");
38 return 0;
39}
40
71cb7328 41int GetPyramidBits(uint32_t fc, uint32_t cn, uint8_t *pyramidBits) {
42
43 uint8_t pre[128];
44 memset(pre, 0x00, sizeof(pre));
45
f087591d 46 // format start bit
581b31fb 47 pre[79] = 1;
f087591d 48
49 // Get 26 wiegand from FacilityCode, CardNumber
581b31fb 50 uint8_t wiegand[24];
51 memset(wiegand, 0x00, sizeof(wiegand));
71cb7328 52 num_to_bytebits(fc, 8, wiegand);
53 num_to_bytebits(cn, 16, wiegand+8);
54
f087591d 55 // add wiegand parity bits (dest, source, len)
581b31fb 56 wiegand_add_parity(pre+80, wiegand, 24);
57
f087591d 58 // add paritybits (bitsource, dest, sourcelen, paritylen, parityType (odd, even,)
59 addParity(pre+8, pyramidBits+8, 112, 8, 1);
71cb7328 60
61 // add checksum
f087591d 62 uint8_t csBuff[13];
63 for (uint8_t i = 0; i < 13; i++)
64 csBuff[i] = bytebits_to_byte(pyramidBits + 16 + (i*8), 8);
65
66 uint32_t crc = CRC8Maxim(csBuff, 13);
67 num_to_bytebits(crc, 8, pyramidBits+120);
581b31fb 68
71cb7328 69 return 1;
70}
71
72int CmdPyramidRead(const char *Cmd) {
73 // read lf silently
74 CmdLFRead("s");
75 // get samples silently
76 getSamples("30000",false);
77 // demod and output Pyramid ID
78 return CmdFSKdemodPyramid("");
79}
80
81int CmdPyramidClone(const char *Cmd) {
82
83 char cmdp = param_getchar(Cmd, 0);
84 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_clone();
85
f087591d 86 uint32_t facilitycode=0, cardnumber=0, fc = 0, cn = 0;
87
ce9a7cbf 88 uint8_t bs[128];
581b31fb 89 memset(bs, 0x00, sizeof(bs));
f087591d 90
71cb7328 91 //Pyramid - compat mode, FSK2a, data rate 50, 4 data blocks
92 uint32_t blocks[5] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 4<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0, 0};
93
94// if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
95// blocks[0] = T5555_MODULATION_FSK2 | 50<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT;
96
f087591d 97 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_clone();
71cb7328 98
f087591d 99 facilitycode = (fc & 0x000000FF);
100 cardnumber = (cn & 0x0000FFFF);
101
71cb7328 102 if ( !GetPyramidBits(facilitycode, cardnumber, bs)) {
103 PrintAndLog("Error with tag bitstream generation.");
104 return 1;
105 }
106
107 blocks[1] = bytebits_to_byte(bs,32);
108 blocks[2] = bytebits_to_byte(bs+32,32);
109 blocks[3] = bytebits_to_byte(bs+64,32);
110 blocks[4] = bytebits_to_byte(bs+96,32);
111
112 PrintAndLog("Preparing to clone Farepointe/Pyramid to T55x7 with Facility Code: %u, Card Number: %u", facilitycode, cardnumber);
113 PrintAndLog("Blk | Data ");
114 PrintAndLog("----+------------");
115 for ( uint8_t i=0; i<5; ++i )
f087591d 116 PrintAndLog(" %d | %08x",i , blocks[i]);
117
71cb7328 118 UsbCommand resp;
f087591d 119 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
71cb7328 120
121 for ( uint8_t i=0; i<5; ++i ) {
f087591d 122 c.arg[0] = blocks[i];
123 c.arg[1] = i;
71cb7328 124 clearCommandBuffer();
f087591d 125 SendCommand(&c);
71cb7328 126 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
127 PrintAndLog("Error occurred, device did not respond during write operation.");
128 return -1;
129 }
130 }
131 return 0;
132}
133
134int CmdPyramidSim(const char *Cmd) {
71cb7328 135
136 char cmdp = param_getchar(Cmd, 0);
137 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_sim();
138
ce9a7cbf 139 uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0;
140
141 uint8_t bs[128];
142 size_t size = sizeof(bs);
143 memset(bs, 0x00, size);
144
145 // Pyramid uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
146 uint64_t arg1, arg2;
147 arg1 = (10 << 8) + 8;
148 arg2 = 50 | 0;
71cb7328 149
ce9a7cbf 150 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_sim();
71cb7328 151
ce9a7cbf 152 facilitycode = (fc & 0x000000FF);
153 cardnumber = (cn & 0x0000FFFF);
154
155 if ( !GetPyramidBits(facilitycode, cardnumber, bs)) {
156 PrintAndLog("Error with tag bitstream generation.");
157 return 1;
158 }
71cb7328 159
ce9a7cbf 160 PrintAndLog("Simulating - Facility Code: %u, CardNumber: %u", facilitycode, cardnumber );
71cb7328 161
ce9a7cbf 162 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
163 memcpy(c.d.asBytes, bs, size);
164 clearCommandBuffer();
165 SendCommand(&c);
71cb7328 166 return 0;
167}
168
169static command_t CommandTable[] = {
170 {"help", CmdHelp, 1, "This help"},
171 {"read", CmdPyramidRead, 0, "Attempt to read and extract tag data"},
172 {"clone", CmdPyramidClone, 0, "<Facility-Code> <Card Number> clone pyramid tag"},
173 {"sim", CmdPyramidSim, 0, "<Facility-Code> <Card Number> simulate pyramid tag"},
174 {NULL, NULL, 0, NULL}
175};
176
177int CmdLFPyramid(const char *Cmd) {
178 clearCommandBuffer();
179 CmdsParse(CommandTable, Cmd);
180 return 0;
181}
182
183int CmdHelp(const char *Cmd) {
184 CmdsHelp(CommandTable);
185 return 0;
186}
Impressum, Datenschutz