forked from InteliSecureLabs/MSR605
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmsr605.cpp
More file actions
634 lines (523 loc) · 17.3 KB
/
libmsr605.cpp
File metadata and controls
634 lines (523 loc) · 17.3 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* MSR605.cpp
* msr605-lib
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "msr605.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <iostream>
#include <signal.h>
//#define DEBUG
using namespace std;
//VERSION ALPHA
static inline uint8_t swap_bits(uint8_t n) {
n = ((n&0xF0) >>4 ) | ( (n&0x0F) <<4);
n = ((n&0xCC) >>2 ) | ( (n&0x33) <<2);
n = ((n&0xAA) >>1 ) | ( (n&0x55) <<1);
return n;
};
void catch_alarm(int sig_num)
{
printf("MSR605 Timed out! Exiting...\n\n");
exit(0);
}
MSR605::MSR605()
{
fd = -1;
}
/*-------------------------------------------------------------------------------------*/
MSR605::~MSR605()
{
}
/*-------------------------------------------------------------------------------------*/
bool MSR605::isConnected()
{
if(this->fd > 0) return true;
return false;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::init()
{
if(!isConnected()) throw "Unable to initialize: Not connected to device.";
this->setRedLEDOn();
sleep(1);
this->setYellowLEDOn();
sleep(1);
this->setGreenLEDOn();
sleep(1);
this->setAllLEDOff();
if(!this->commTest()) throw "Unable to initialize: Communications Test failed.";
this->sendReset();
}
/*-------------------------------------------------------------------------------------*/
int MSR605::write_bytes(char *buf, int num)
{
int ret = 0;
if(!isConnected()) throw "Unable to send data: Not connected to device.";
ret = write(this->fd, buf, num);
if(ret <= 0) throw "Unable to send data: write() failed";
#ifdef DEBUG
printf("Sent Data(%d bytes): ", num);
for(int x = 0; x < num; x++) printf("%02x ", buf[x]);
printf("\n");
#endif
return ret;
}
/*-------------------------------------------------------------------------------------*/
int MSR605::read_bytes(unsigned char *buf, int num)
{
int temp = 0;
int ret;
if(buf == NULL) return -1;
while(temp != num) {
ret = read(this->fd, buf+temp, num - temp);
if(ret < 0) return -1;
if(ret > 0) temp += ret;
}
#ifdef DEBUG
printf("Received Data(%d bytes): ", num);
for(int x = 0; x < num; x++) printf("%02x ", buf[x]);
printf("\n");
#endif
return num;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::readTrack1(unsigned char * &outBuf, unsigned int &outLen, char trackOptions)
{
unsigned char buf[256];
unsigned char buf2[256];
unsigned int len=0;
/* track header */
read_bytes((unsigned char*)&buf, 1);
if(memcmp(buf, "\x1b", 1) != 1) {
read_bytes((unsigned char*)&buf, 1);
printf("Track Header %02x\n",buf[0]);
for(int i=0;i<254;i++){
read_bytes((unsigned char*)&buf, 1);
if(memcmp(buf,"\x1b", 1) == 0) {
break;
}else{
buf2[i]=buf[0];
len=i;
//do nothing
}
}
}
len++;
#ifdef DEBUG
printf("\nTrying to read data: %d\n",len);
#endif
//read_bytes((unsigned char*)&buf2, len);
decode_8bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_7BIT) decode_7bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_5BIT) decode_5bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_8BIT) decode_8bit((unsigned char*)&buf2, len, outBuf, outLen);
#ifdef DEBUG
printf("\nTrack complete\n");
#endif
}
void MSR605::readTrack23(unsigned char * &outBuf, unsigned int &outLen, char trackOptions)
{
unsigned char buf[256];
unsigned char buf2[256];
unsigned int len=0;
/* track header */
read_bytes((unsigned char*)&buf, 1);
printf("Track Header %02x\n",buf[0]);
for(int i=0;i<254;i++){
read_bytes((unsigned char*)&buf, 1);
if(memcmp(buf,"\x1b", 1) == 0) {
break;
}else{
buf2[i]=buf[0];
len=i;
//do nothing
}
}
len++;
#ifdef DEBUG
printf("\nTrying to read data: %d\n",len);
#endif
//read_bytes((unsigned char*)&buf2, len);
decode_8bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_7BIT) decode_7bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_5BIT) decode_5bit((unsigned char*)&buf2, len, outBuf, outLen);
//if(trackOptions == TRACK_8BIT) decode_8bit((unsigned char*)&buf2, len, outBuf, outLen);
#ifdef DEBUG
printf("\nTrack complete\n");
#endif
}
void MSR605::readTrack_raw(unsigned char * &outBuf, unsigned int &outLen, char trackOptions)
{
unsigned char buf[512];
unsigned int len;
/* track header */
read_bytes((unsigned char*)&buf, 2);
if(memcmp(buf, "\x1b", 1) != 0) {
throw "Unable to read data: Expected Track Header";
}
printf("Track Header %02x %02x\n",buf[0],buf[1]);
/* track length */
if(read_bytes((unsigned char*)&buf, 1) != 1) {
throw "Unable to read data: Expected Track Length";
}
printf("Track Length %02x \n",buf[0]);
if(buf[0] > 254) {
throw "Unable to read data: Invalid length received";
}
else if(buf[0] == 0) { /* no data for this track */
outBuf = NULL;
outLen = 0;
return;
}
len = buf[0];
/* track data */
read_bytes((unsigned char*)&buf, len);
if(trackOptions == TRACK_7BIT) decode_7bit((unsigned char*)&buf, len, outBuf, outLen);
if(trackOptions == TRACK_5BIT) decode_5bit((unsigned char*)&buf, len, outBuf, outLen);
if(trackOptions == TRACK_8BIT) decode_8bit((unsigned char*)&buf, len, outBuf, outLen);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::free_ms_data(magnetic_stripe_t *ms_data)
{
if(ms_data == NULL) return;
if(ms_data->track1 != NULL) free(ms_data->track1);
if(ms_data->track2 != NULL) free(ms_data->track2);
if(ms_data->track3 != NULL) free(ms_data->track3);
free(ms_data);
}
/*-------------------------------------------------------------------------------------*/
magnetic_stripe_t *MSR605::readCard_raw(char track1_format, char track2_format, char track3_format)
{
unsigned char buf[512];
unsigned char end[3];
magnetic_stripe_t *ms_data = (magnetic_stripe_t*)malloc(sizeof(magnetic_stripe_t));
if(!isConnected()) {
free_ms_data(ms_data);
throw "Unable to read card: Not connected to device.";
}
/* set track format */
this->setBPC(track1_format, track2_format, track3_format);
write_bytes(MSR_READ_RAW, 2);
/* check for ack */
read_bytes((unsigned char*)&buf, 2);
if(memcmp(buf, MSR_READ_ACK, 2) != 0) {
free_ms_data(ms_data);
throw "Unable to read data: Invalid Response";
}
readTrack_raw(ms_data->track1, ms_data->t1_len, track1_format);
readTrack_raw(ms_data->track2, ms_data->t2_len, track2_format);
readTrack_raw(ms_data->track3, ms_data->t3_len, track3_format);
/* read end */
read_bytes((unsigned char*)&end, 3);
if(memcmp(end, MSR_END_READ, 3) != 0) {
free_ms_data(ms_data);
throw "Unable to read data: Invalid end response";
}
/* read status byte */
read_bytes((unsigned char*)&end, 1);
if(end[0] == MSR_STATUS_OK) {
return ms_data;
}
else { /* error messages */
free_ms_data(ms_data);
switch(end[0])
{
case MSR_STATUS_WRITE_READ_ERROR:
throw "Unable to read card: Write or read error";
case MSR_STATUS_COMMAND_FORMAT_ERROR:
throw "Unable to read card: Command format error";
case MSR_STATUS_INVALID_COMMAND:
throw "Unable to read card: Invalid command";
case MSR_STATUS_INVALID_SWIPE_WRITE_MODE:
throw "Unable to read card: Invalid card swipe when in write mode";
default:
throw "Unable to read card: Unknown error code";
}
}
return ms_data;
}
magnetic_stripe_t *MSR605::readCard_iso(char track1_format, char track2_format, char track3_format)
{
unsigned char buf[1024];
unsigned char end[3];
magnetic_stripe_t *ms_data = (magnetic_stripe_t*)malloc(sizeof(magnetic_stripe_t));
if(!isConnected()) {
free_ms_data(ms_data);
throw "Unable to read card: Not connected to device.";
}
/* set track format */
this->setBPC(track1_format, track2_format, track3_format);
write_bytes(MSR_READ_ISO, 2);
/* check for ack */
read_bytes((unsigned char*)&buf, 2);
if(memcmp(buf, MSR_READ_ACK, 2) != 0) {
free_ms_data(ms_data);
throw "Unable to read data: Invalid Response";
}
readTrack1(ms_data->track1, ms_data->t1_len, track1_format);
readTrack23(ms_data->track2, ms_data->t2_len, track2_format);
readTrack23(ms_data->track3, ms_data->t3_len, track3_format);
/* read end */
read_bytes((unsigned char*)&end, 1);
read_bytes((unsigned char*)&end, 3);
if(memcmp(end, MSR_END_READ, 3) != 0) {
free_ms_data(ms_data);
throw "Unable to read data: Invalid end response";
}
/* read status byte */
read_bytes((unsigned char*)&end, 1);
if(end[0] == MSR_STATUS_OK) {
printf("READ OK!\n");
//return ms_data;
}
else { /* error messages */
free_ms_data(ms_data);
switch(end[0])
{
case MSR_STATUS_WRITE_READ_ERROR:
throw "Unable to read card: Write or read error";
case MSR_STATUS_COMMAND_FORMAT_ERROR:
throw "Unable to read card: Command format error";
case MSR_STATUS_INVALID_COMMAND:
throw "Unable to read card: Invalid command";
case MSR_STATUS_INVALID_SWIPE_WRITE_MODE:
throw "Unable to read card: Invalid card swipe when in write mode";
default:
throw "Unable to read card: Unknown error code";
}
}
return ms_data;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::getLeadingZeros(leading_zeros_t *zeros)
{
unsigned char buf[3];
if(!isConnected()) throw "Unable to check leading zeros: Not connected to device.";
if(zeros == NULL) throw "Invalid leading zeros structure passed";
write_bytes(MSR_CHECK_ZEROS, 2);
if(read_bytes((unsigned char*)&buf, 3) != 3) throw "Unable to check leading zeros: invalid response";
if(memcmp(buf, "\x1b", 1) != 0) throw "Unable to check leading zeros: bad resposne";
zeros->t1t3 = buf[1];
zeros->t2 = buf[2];
}
/*-------------------------------------------------------------------------------------*/
void MSR605::decode_7bit(unsigned char *buf, unsigned int len, unsigned char * &outBuf, unsigned int &outLen)
{
unsigned int bytes = (len * 8) / 7;
outBuf = (unsigned char*)malloc(bytes);
unsigned int tempLen = 0;
char *test;
test = (char*)outBuf;
for(int y = 0; y < (len/7); y++) {
if(*buf == 0x00 || buf[1] == 0x00) break;
test[0] = swap_bits(buf[0] & 0xfc) + 0x20;
test[1] = swap_bits(((buf[1] & 0xf8) >> 1) | ((buf[0] & 1) << 7)) + 0x20;
test[2] = swap_bits(((buf[2] & 0xf0) >> 2) | ((buf[1] & 3) << 6)) + 0x20;
test[3] = swap_bits(((buf[3] & 0xe0) >> 3) | ((buf[2] & 7) << 5)) + 0x20;
test[4] = swap_bits(((buf[4] & 0xc0) >> 4) | ((buf[3] & 0xf) << 4)) + 0x20;
test[5] = swap_bits(((buf[5] & 0x80) >> 5) | ((buf[4] & 0x1f) << 3)) + 0x20;
test[6] = swap_bits((buf[5] & 0x3f) << 2) + 0x20;
test[7] = swap_bits((buf[6] & 0x7e) << 1) + 0x20;
tempLen += 8;
buf += 7;
test += 8;
}
outLen = tempLen;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::decode_5bit(unsigned char *buf, unsigned int len, unsigned char * &outBuf, unsigned int &outLen)
{
unsigned int bytes = (len * 8) / 5;
outBuf = (unsigned char*)malloc(bytes);
unsigned int tempLen = 0;
char *test;
test = (char*)outBuf;
for(int y = 0; y < (len/5); y++) {
if(*buf == 0x00 || buf[1] == 0x00) break;
test[0] = swap_bits(buf[0] & 0xf0) + 0x30; //f0 was f8 corrected due to parity errors when buf[0]=1!
test[1] = swap_bits(((buf[1] & 0x80) >> 3) | ((buf[0] & 7) << 5)) + 0x30;
test[2] = swap_bits((buf[1] & 0x3c) << 2) + 0x30;
test[3] = swap_bits(((buf[2] & 0xe0) >> 1) | ((buf[1] & 1) << 7)) + 0x30;
test[4] = swap_bits((buf[2] & 0xf) << 4) + 0x30;
test[5] = swap_bits((buf[3] & 0x78) << 1) + 0x30;
test[6] = swap_bits((buf[4] & 0xc0) >> 2 | ((buf[3] & 3) << 6)) + 0x30;
test[7] = swap_bits((buf[4] & 0x1e) << 3) + 0x30;
tempLen += 8;
buf += 5;
test += 8;
}
outLen = tempLen;
}
void MSR605::decode_8bit(unsigned char *buf, unsigned int len, unsigned char * &outBuf, unsigned int &outLen)
{
//unsigned int bytes = len;
unsigned int bytes = (len * 8) /8;
outBuf = (unsigned char *)malloc(len);
unsigned int tempLen = 0;
//char *test;
//unsigned int *len_ptr=len;
char *test;
test=(char*)outBuf;
for(int y = 0; y < (len/8); y++) {
if(*buf == 0x00 || buf[1] == 0x00) break;
test[0] =buf[0];
test[1] =buf[1];
test[2] = buf[2];
test[3] = buf[3];
test[4] = buf[4];
test[5] = buf[5];
test[6] = buf[6];
test[7] = buf[7];
tempLen += 8;
buf += 8;
test += 8;
}
outLen=len;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::disconnect()
{
if(!isConnected()) throw "Unable to close connection: not connected";
sendReset();
close(this->fd);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::print_bytes(unsigned char *bytes, int len)
{
printf("Bytes: ");
for(int x = 0; x < len; x++) printf("%02x ", bytes[x]);
printf("\n");
}
/*-------------------------------------------------------------------------------------*/
bool MSR605::setBPC(char track1, char track2, char track3)
{
char ack[5];
char bpc_str[6];
if(!isConnected()) throw "Unable to set bpc: not connected";
memcpy(bpc_str, (void*)MSR_SET_BPC, 2);
memcpy(&bpc_str[2], (void*)&track1, 1);
memcpy(&bpc_str[3], (void*)&track2, 1);
memcpy(&bpc_str[4], (void*)&track3, 1);
write_bytes(bpc_str, 5);
if(read_bytes((unsigned char*)&ack, 5) != 5) throw "Unable to set BPC: invalid response";
if(memcmp(ack, MSR_SET_BPC_ACK, 5) == 0) return true;
return false;
}
/*-------------------------------------------------------------------------------------*/
bool MSR605::commTest()
{
char ack[2];
signal(SIGALRM, catch_alarm);
if(!isConnected()) throw "Unable to perform comm test: not connected";
write_bytes(MSR_COMM_TEST, 2);
printf("Comm Test Sent...\n");
//alarm(6);
if(read_bytes((unsigned char*)&ack, 2) != 2) throw "Comm test failed: invalid response";
//alarm(6);
printf("Receiving Response.\n");
if(memcmp(ack, MSR_COMM_TEST_ACK, 2) == 0) return true;
return false;
}
/*-------------------------------------------------------------------------------------*/
void MSR605::setRedLEDOn()
{
if(!isConnected()) throw "Unable to turn Red LED on: not connected";
write_bytes(MSR_RED_LED_ON, 2);
}
void MSR605::setGreenLEDOn()
{
if(!isConnected()) throw "Unable to turn Green LED on: not connected";
write_bytes(MSR_GREEN_LED_ON, 2);
}
void MSR605::setYellowLEDOn()
{
if(!isConnected()) throw "Unable to turn Yellow LED on: not connected";
write_bytes(MSR_YELLOW_LED_ON, 2);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::setAllLEDOn()
{
if(!isConnected()) throw "Unable to turn all LEDs on: not connected";
write_bytes(MSR_ALL_LIGHTS_ON, 2);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::setAllLEDOff()
{
if(!isConnected()) throw "Unable to turn all LEDs off: not connected";
write_bytes(MSR_ALL_LIGHTS_OFF, 2);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::sendReset()
{
if(!isConnected()) throw "Unable to send reset: not connected";
write_bytes(MSR_RESET, 2);
}
/*-------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------*/
void MSR605::connect(char *devName)
{
struct termios options;
if(devName == NULL) throw "Invalid device name specified.";
/* open connection to device */
this->fd = open(devName, O_RDWR | O_NOCTTY);
if(this->fd < 0) throw "Unable to open connection to device.";
/* get options for terminal session */
tcgetattr(this->fd, &options);
/* set options */
options.c_cflag = CS8 | CREAD | CLOCAL;
options.c_oflag = 0;
options.c_iflag = 0;
/* set baud rate */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
/* push options */
tcsetattr(this->fd, TCSANOW, &options);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::getModel()
{
char *model = (char *)malloc(sizeof(char *) * 3);
write_bytes("\x1b\x74", 2);
read_bytes((unsigned char*)model, 3);
model[2]='\0';
memmove (model, model+1, strlen (model));
printf("Model: %s\n",model);
free(model);
}
/*-------------------------------------------------------------------------------------*/
void MSR605::getFirmware()
{
char *firmware= (char *)malloc(sizeof(char *) * 9);
write_bytes("\x1b\x76", 2);
read_bytes((unsigned char*)firmware, 9);
firmware[9]='\0';
memmove (firmware, firmware+1, strlen (firmware));
printf("Firmware: %s\n",firmware);
free(firmware);
}