-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMagSpoofPI.c
302 lines (261 loc) · 6.72 KB
/
MagSpoofPI.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
300
301
302
/*
* 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
*
*----------------------------------------------------------
*
* MagSpoofPI - code integration by Salvador Mendoza
*
* Main idea: be able to compile MagSpoof with variable tracks, and use MagSpoof
* without arduino dependency, and be able to use it without removing it from
* the raspberry pi gpio.
* -
* Special code for Raspberry Pi implementing avr-gcc without arduino.
* -
* Main modifications from the original MagSpoof in functions like digitalWrite(),
* setup() and loop().
*
* Example of configuration:
* https://netxing.wordpress.com/2016/08/27/magspoofpi/
*/
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#define interrupts() sei()
#define noInterrupts() cli()
#define PIN_A PB0
#define PIN_B PB1
#define BUTTON_PIN PB4
#define ENABLE_PIN PB3 // also green LED
#define SWAP_PIN PB2 // unused
#define CLOCK_US 200
#define HIGH 1
#define LOW 0
#define _BV(bit) (1 << (bit))
#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[] = {
"%4059557240058686^22111015312020010591?;4059557240058686^22111015312020010591?\0", // Track 1
";4059557240058686=22111015312020010591?\0" // Track 2
};
char revTrack[41];
const int sublen[] = {
32, 48, 48 };
const int bitlen[] = {
7, 5, 5 };
unsigned int curTrack = 0;
void digitalWrite(int a, int b){
if (b == 1) {
PORTB |= (1<<a); // high
}else if (b == 0){
PORTB &= ~(1<<a); // low
}else {
PORTB &= ~(b<<a); // Anything else
}
}
void blink(int pin, int times)
{
int i;
for (i = 0; i < times; i++)
{
//PORTB = 0b00001000;
digitalWrite(pin, HIGH);
_delay_ms(200);
digitalWrite(pin, LOW);
_delay_ms(200);
//PORTB = 0b00000000;
}
}
// stores track for reverse usage later
void storeRevTrack(int track)
{
int i, j, tmp, crc, lrc = 0;
track--; // index 0
int dir = 0;
for (i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
tmp = tracks[track][i] - sublen[track];
for (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 (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 setup()
{
//DDRB = 0b00001111;
DDRB |= (1 << PIN_A); // output
DDRB |= (1 << PIN_B); // output
DDRB |= (1 << ENABLE_PIN); // output
DDRB &=~(1 << BUTTON_PIN); // input
PORTB &= ~(1<<BUTTON_PIN);
// blink to show we started up
blink(ENABLE_PIN, 3);
//store reverse track 2 to play later
storeRevTrack(2);
}
// send a single bit out
int dir;
void playBit(int sendBit)
{
dir ^= 1;
digitalWrite(PIN_A, dir);
digitalWrite(PIN_B, !dir);
_delay_us(CLOCK_US);
if (sendBit)
{
dir ^= 1;
digitalWrite(PIN_A, dir);
digitalWrite(PIN_B, !dir);
}
_delay_us(CLOCK_US);
}
// when reversing
void reverseTrack(int track)
{
int i = 0;
track--; // index 0
dir = 0;
int j;
while (revTrack[i++] != '\0');
i--;
while (i--)
for (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 = 0;
track--; // index 0
dir = 0;
// enable H-bridge and LED
digitalWrite(ENABLE_PIN, HIGH);
// First put out a bunch of leading zeros.
int i, j;
for (i = 0; i < 25; i++)
playBit(0);
// Check for the end of the track
for (i = 0; tracks[track][i] != '\0'; i++)
{
crc = 1;
tmp = tracks[track][i] - sublen[track];
for (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 (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 (i = 0; i < BETWEEN_ZERO; i++)
playBit(0);
// send second track in reverse
reverseTrack(2);
}
// finish with 0's
for (i = 0; i < 5 * 5; i++)
playBit(0);
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, LOW);
digitalWrite(ENABLE_PIN, LOW);
}
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()
{
sleep();
noInterrupts();
while ((PINB & (1<<BUTTON_PIN)) != 0);
_delay_ms(50);
while ((PINB & (1<<BUTTON_PIN)) != 0);
playTrack(1 + (curTrack++ % 2));
_delay_ms(400);
interrupts();
//playTrack(1 + (curTrack++ % 2));
}
int main(void){
setup();
while(1){
loop();
}
return 1;
}