forked from samyk/magspoof
-
Notifications
You must be signed in to change notification settings - Fork 13
/
magspoof.c
302 lines (245 loc) · 6.29 KB
/
magspoof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* MagSpoof - "wireless" magnetic stripe/credit card emulator
*
* by Samy Kamkar
*
* http://samy.pl/magspoof/
*
* - Allows you to store all of your credit cards and magstripes in one device
* - Works on traditional magstripe readers wirelessly (no NFC/RFID required)
* - Can disable Chip-and-PIN
* - Correctly predicts Amex credit card numbers + expirations from previous card number (code not included)
* - Supports all three magnetic stripe tracks, and even supports Track 1+2 simultaneously
* - Easy to build using Arduino or ATtiny
*
*/
#include <avr/sleep.h>
#include <avr/interrupt.h>
#define PIN_A 0
#define PIN_B 1
#define ENABLE_PIN 3 // also green LED
#define SWAP_PIN 4 // unused
#define BUTTON_PIN 2
#define CLOCK_US 200
#define BETWEEN_ZERO 53 // 53 zeros between track1 & 2
#define TRACKS 2
// consts get stored in flash as we don't adjust them
const char* tracks[] = {
"%B123456781234567^LASTNAME/FIRST^YYMMSSSDDDDDDDDDDDDDDDDDDDDDDDDD?\0", // Track 1
";123456781234567=YYMMSSSDDDDDDDDDDDDDD?\0" // Track 2
};
// service code to make requirements more lax and disable chip-and-PIN (third digit)
const char* sc_rep = { "101" };
char revTrack[41];
const int sublen[] = {
32, 48, 48 };
const int bitlen[] = {
7, 5, 5 };
unsigned int curTrack = 0;
int dir;
void setup()
{
pinMode(PIN_A, OUTPUT);
pinMode(PIN_B, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// blink to show we started up
blink(ENABLE_PIN, 200, 3);
// store reverse track 2 to play later
storeRevTrack(2);
}
void blink(int pin, int msdelay, int times)
{
for (int i = 0; i < times; i++)
{
digitalWrite(pin, HIGH);
delay(msdelay);
digitalWrite(pin, LOW);
delay(msdelay);
}
}
// send a single bit out
void playBit(int sendBit)
{
dir ^= 1;
digitalWrite(PIN_A, dir);
digitalWrite(PIN_B, !dir);
delayMicroseconds(CLOCK_US);
if (sendBit)
{
dir ^= 1;
digitalWrite(PIN_A, dir);
digitalWrite(PIN_B, !dir);
}
delayMicroseconds(CLOCK_US);
}
// when reversing
void reverseTrack(int track)
{
int i = 0;
track--; // index 0
dir = 0;
while (revTrack[i++] != '\0');
i--;
while (i--)
for (int j = bitlen[track]-1; j >= 0; j--)
playBit((revTrack[i] >> j) & 1);
}
// plays out a full track, calculating CRCs and LRC
void playTrack(int track)
{
int tmp, crc, lrc, cnt = 0;
track--; // index 0
dir = 0;
// enable H-bridge and LED
digitalWrite(ENABLE_PIN, HIGH);
// First put out a bunch of leading zeros.
for (int i = 0; i < 25; i++)
playBit(0);
//
for (int i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
if(cnt<0)
cnt--;
else { // look for FS
if(tracks[track][i]=='^')
cnt++;
if(tracks[track][i]=='=')
cnt+=2;
if(cnt==2)
cnt=-1;
}
if (cnt<-5 && cnt>-9) // SS is located 4 chars after the last FS
tmp = sc_rep[8+cnt] - sublen[track];
else
tmp = tracks[track][i] - sublen[track];
for (int j = 0; j < bitlen[track]-1; j++)
{
crc ^= tmp & 1;
lrc ^= (tmp & 1) << j;
playBit(tmp & 1);
tmp >>= 1;
}
playBit(crc);
}
// finish calculating and send last "byte" (LRC)
tmp = lrc;
crc = 1;
for (int j = 0; j < bitlen[track]-1; j++)
{
crc ^= tmp & 1;
playBit(tmp & 1);
tmp >>= 1;
}
playBit(crc);
// if track 1, play 2nd track in reverse (like swiping back?)
if (track == 0)
{
// if track 1, also play track 2 in reverse
// zeros in between
for (int i = 0; i < BETWEEN_ZERO; i++)
playBit(0);
// send second track in reverse
reverseTrack(2);
}
// finish with 0's
for (int i = 0; i < 5 * 5; i++)
playBit(0);
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, LOW);
digitalWrite(ENABLE_PIN, LOW);
}
// stores track for reverse usage later
void storeRevTrack(int track)
{
int i, tmp, crc, lrc, cnt = 0;
track--; // index 0
dir = 0;
for (i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
if(cnt<0)
cnt--;
else { // look for FS
if(tracks[track][i]=='^')
cnt++;
if(tracks[track][i]=='=')
cnt+=2;
if(cnt==2)
cnt=-1;
}
if (cnt<-5 && cnt>-9) // SS is located 4 chars after the last FS
tmp = sc_rep[8+cnt] - sublen[track];
else
tmp = tracks[track][i] - sublen[track];
for (int j = 0; j < bitlen[track]-1; j++)
{
crc ^= tmp & 1;
lrc ^= (tmp & 1) << j;
tmp & 1 ?
(revTrack[i] |= 1 << j) :
(revTrack[i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTrack[i] |= 1 << 4) :
(revTrack[i] &= ~(1 << 4));
}
// finish calculating and send last "byte" (LRC)
tmp = lrc;
crc = 1;
for (int j = 0; j < bitlen[track]-1; j++)
{
crc ^= tmp & 1;
tmp & 1 ?
(revTrack[i] |= 1 << j) :
(revTrack[i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTrack[i] |= 1 << 4) :
(revTrack[i] &= ~(1 << 4));
i++;
revTrack[i] = '\0';
}
void sleep()
{
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT2); // Use PB3 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
MCUCR &= ~_BV(ISC01);
MCUCR &= ~_BV(ISC00); // Interrupt on rising edge
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT2); // Turn off PB3 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
// XXX move playtrack in here?
ISR(PCINT0_vect) {
/* noInterrupts();
while (digitalRead(BUTTON_PIN) == LOW);
delay(50);
while (digitalRead(BUTTON_PIN) == LOW);
playTrack(1 + (curTrack++ % 2));
delay(400);
interrupts();*/
}
void loop()
{
//for(int i=0;i<10;i++){playTrack(1+(curTrack++%2));delay(3000);}
sleep();
noInterrupts();
while (digitalRead(BUTTON_PIN) == LOW);
delay(50);
while (digitalRead(BUTTON_PIN) == LOW);
playTrack(1 + (curTrack++ % 2));
delay(400);
interrupts();
//playTrack(1 + (curTrack++ % 2));
}