forked from samyk/magspoof
-
Notifications
You must be signed in to change notification settings - Fork 1
/
magspoof.c
275 lines (231 loc) · 5.94 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
/*
* 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 (code not included)
* - 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 PADDING 25 // number of 0's before and after tracks
#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
};
char revTrack1[80];
char revTrack2[41];
char* revTracks[] = { revTrack1, revTrack2 };
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);
storeRevTrack(1);
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);
}
//plays tracks in different ways for maximum compatibility among magstripe readers
void playTracks()
{
curTrack++;
if((curTrack % 4)==1)
{
//forward then reverse swipe
playTrack(1);
reverseTrack(2);
} else if ((curTrack % 4)==2){
//forward consecutive swipes
playTrack(1);
playTrack(2);
} else if ((curTrack % 4)==3) {
//reverse then forward swipe
reverseTrack(1);
playTrack(2);
} else {
//reverse consecutive swipes
reverseTrack(1);
reverseTrack(2);
}
}
// plays out a full track, calculating CRCs and LRC
void playTrack(int track)
{
int tmp, crc, lrc = 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 < PADDING; i++)
playBit(0);
for (int i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
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);
// finish with 0's
for (int i = 0; i < PADDING; i++)
playBit(0);
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, LOW);
digitalWrite(ENABLE_PIN, LOW);
}
// when reversing
void reverseTrack(int track)
{
int i = 0;
track--; // index 0
dir = 0;
digitalWrite(ENABLE_PIN, HIGH);
// First put out a bunch of leading zeros.
for (int i = 0; i < PADDING; i++)
playBit(0);
while (revTracks[track][i++] != '\0');
i--;
while (i--)
for (int j = bitlen[track]-1; j >= 0; j--)
playBit((revTracks[track][i] >> j) & 1);
// finish with 0's
for (int i = 0; i < PADDING; 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 = 0;
track--; // index 0
dir = 0;
for (i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
tmp = tracks[track][i] - sublen[track];
for (int j = 0; j < bitlen[track]-1; j++)
{
crc ^= tmp & 1;
lrc ^= (tmp & 1) << j;
tmp & 1 ?
(revTracks[track][i] |= 1 << j) :
(revTracks[track][i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTracks[track][i] |= 1 << 4) :
(revTracks[track][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 ?
(revTracks[track][i] |= 1 << j) :
(revTracks[track][i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTracks[track][i] |= 1 << 4) :
(revTracks[track][i] &= ~(1 << 4));
i++;
revTracks[track][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
}
ISR(PCINT0_vect) {
}
void loop()
{
sleep();
noInterrupts();
while (digitalRead(BUTTON_PIN) == LOW);
delay(100);
while (digitalRead(BUTTON_PIN) == LOW);
playTracks();
delay(400);
interrupts();
}