-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardDumper.cs
496 lines (390 loc) · 15 KB
/
CardDumper.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using PCSC;
using PCSC.Iso7816;
using System.IO;
using System.Collections.Specialized;
namespace MonitorReaderEvents
{
class CardDumper
{
SCardContext m_card_context = null;
SCardMonitor m_monitor = null;
Dictionary<string, byte[]> m_keys = new Dictionary<string, byte[]>();
public CardDumper()
{
//LoadKeysFromFile(CardDumper.GetConfigurationKey("MfocLog"));
LoadKeysFromDumpFile(CardDumper.GetConfigurationKey("MfocDump"));
}
void Log(string message)
{
Console.WriteLine(message);
System.IO.File.AppendAllText("nespresso.log", message + "\n");
}
void LoadKeysFromDumpFile(string filename)
{
try
{
List<MifareClassicKey> keys = MifareClassic.GetKeysFromDump(System.IO.File.ReadAllBytes(filename));
m_keys.Clear();
for (int i = 0; i < 64; i++)
{
m_keys["A" + i] = keys[i / 4].KeyA;
m_keys["B" + i] = keys[i / 4].KeyB;
}
}
catch (Exception)
{
Log("Unable to load keys from: " + filename);
}
}
void LoadKeysFromFile(string filename)
{
m_keys.Clear();
string[] lines = null;
try
{
lines = File.ReadAllLines(filename);
}
catch (Exception) { }
if (lines == null)
{
Log("Unable to load keys from: " + filename);
return;
}
foreach (var line in lines)
{
if (!line.StartsWith("Block")) continue;
string[] segments = line.Split(',');
if ((segments == null) || (segments.Length != 3)) continue;
segments[0] = segments[0].Replace("Block", "").Trim();
int block = 0;
if (!int.TryParse(segments[0], out block)) continue;
string type = "";
type = segments[1].Replace("type", "").Trim();
segments = segments[2].Split(':');
if ((segments == null) || (segments.Length != 2)) continue;
string key = segments[0].Replace("key", "").Trim();
byte[] binary = FromHex(key);
// Log("> " + block + " " + type + " " + key + " " + ToHex(binary));
m_keys[type + block] = binary;
}
}
public void Start()
{
m_card_context = new SCardContext();
m_card_context.Establish(SCardScope.System);
string[] readernames = m_card_context.GetReaders();
// Create a monitor object with its own PC/SC context.
m_monitor = new SCardMonitor(new SCardContext(), SCardScope.System);
m_monitor.CardInserted += new CardInsertedEvent(CardInserted);
m_monitor.CardRemoved += new CardRemovedEvent(CardRemoved);
m_monitor.Initialized += new CardInitializedEvent(Initialized);
m_monitor.StatusChanged += new StatusChangeEvent(StatusChanged);
m_monitor.MonitorException += new MonitorExceptionEvent(MonitorException);
foreach (string reader in readernames)
Log("Start monitoring for reader " + reader + ".");
m_monitor.Start(readernames);
// Let the program run until the user presses a key
ConsoleKeyInfo keyinfo = Console.ReadKey();
GC.KeepAlive(keyinfo);
}
public void Stop()
{
if (m_monitor != null)
{
m_monitor.Cancel();
m_monitor = null;
}
if (m_card_context != null)
{
m_card_context.Release();
m_card_context = null;
}
}
static Dictionary<string, byte> m_hexindex = null;
static byte[] FromHex(string str)
{
if ((str.Length % 2) != 0) return null;
if (m_hexindex == null)
{
m_hexindex = new Dictionary<string, byte>();
for (byte i = 0; i < 255; i++)
m_hexindex.Add(i.ToString("X2"), i);
}
str = str.ToUpper();
List<byte> hexres = new List<byte>();
for (int i = 0; i < str.Length; i += 2)
hexres.Add(m_hexindex[str.Substring(i, 2)]);
return hexres.ToArray();
}
static string ToHex(byte[] atr)
{
if (atr == null)
return null;
StringBuilder sb = new StringBuilder();
foreach (byte b in atr)
sb.AppendFormat("{0:X2}", b);
return sb.ToString();
}
void DumpStatus(SCardReader RFIDReader)
{
string[] names; // contains the reader name(s)
SCardProtocol proto;// contains the currently used communication protocol
SCardState state;// contains the current state (flags)
byte[] atr; // contains the card ATR
SCardError rc = RFIDReader.Status(out names, out state, out proto, out atr);
if (rc == SCardError.Success)
{
Log("Connected with protocol " + proto + " in state " + state);
if (atr != null && atr.Length > 0)
Log("Card ATR: " + ToHex(atr));
}
}
bool LoadKey(IsoCard card, byte[] key)
{
CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);
apdu.CLA = 0xFF;
apdu.Instruction = InstructionCode.ExternalAuthenticate;
apdu.P1 = 0x20;
apdu.P2 = 0x00;
apdu.Data = key;
Response response = card.Transmit(apdu);
if (response.SW1 == (byte)SW1Code.Normal)
return true;
return false;
}
bool AuthBlock(IsoCard card, int block, bool isAKey = true)
{
byte type = 0x60;
if (!isAKey)
type = 0x61;
CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);
apdu.CLA = 0xFF;
apdu.INS = 0x86;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Data = new byte[] { 0x01, //version
0x00,
(byte)block,
type, //Key type 0x60 TYPE_A, 0x61 TYPE_B
0x00 }; //Key number 0x00 ~ 0x1F
Response response = card.Transmit(apdu);
if (response.SW1 == (byte)SW1Code.Normal)
return true;
return false;
}
string ReadBlock(IsoCard card, int block)
{
string ouput = "";
CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case2Short);
apdu.CLA = 0xFF;
apdu.Instruction = InstructionCode.ReadBinary;
apdu.P1 = 0x00;
apdu.P2 = (byte)block;
apdu.Le = 16;
Response response = card.Transmit(apdu);
if (response.SW1 == (byte)SW1Code.Normal)
{
byte[] data = response.GetData();
if (data != null)
ouput = ToHex(data);
}
return ouput;
}
bool WriteBlock(IsoCard card, int block, byte[] data)
{
CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);
apdu.CLA = 0xFF;
apdu.Instruction = InstructionCode.UpdateBinary;
apdu.P1 = 0x00;
apdu.P2 = (byte)block;
apdu.Data = data;
Response response = card.Transmit(apdu);
if (response.SW1 == (byte)SW1Code.Normal)
return true;
return false;
}
string Read(IsoCard card, int block)
{
string ouput = "";
bool ok = false;
byte[] key = null;
if (m_keys.TryGetValue("A" + block, out key))
{
if (LoadKey(card, key))
{
if (AuthBlock(card, block, true))
{
ouput = ReadBlock(card, block);
ok = !string.IsNullOrEmpty(ouput);
}
}
}
if (!ok && m_keys.TryGetValue("B" + block, out key))
{
// Log("--" + ToHex(key));
if (LoadKey(card, key))
{
if (AuthBlock(card, block, false))
{
ouput = ReadBlock(card, block);
ok = !string.IsNullOrEmpty(ouput);
}
}
}
return ouput;
}
bool Write(IsoCard card, int block, byte[] data)
{
bool ok = false;
byte[] key = null;
if (m_keys.TryGetValue("A" + block, out key))
if (LoadKey(card, key) && AuthBlock(card, block, true))
ok = WriteBlock(card, block, data);
if (!ok && m_keys.TryGetValue("B" + block, out key))
if (LoadKey(card, key) && AuthBlock(card, block, false))
ok = WriteBlock(card, block, data);
return ok;
}
string ReadUID(IsoCard card)
{
string ouput = "";
CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case2Short);
apdu.CLA = 0xFF;
apdu.Instruction = InstructionCode.GetData;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Le = 0x00;
Response response = card.Transmit(apdu);
if (response.SW1 == (byte)SW1Code.Normal)
{
byte[] data = response.GetData();
if (data != null)
ouput = ToHex(data);
}
return ouput;
}
void DumpCard(IsoCard card)
{
string uid = ReadUID(card);
Log("UID: " + uid);
Log("[----------Start of Memory Dump----------]");
int blockSize = 4;
for (int sector = 0; sector != 16; ++sector)
{
Log("----------------Sector " + string.Format("{0:D2}", sector) + "-----------------");
for (int block = 0; block != blockSize; ++block)
{
int cardBlock = sector * blockSize + block;
string data = Read(card, cardBlock);
Log("Block " + string.Format("{0:D2}", cardBlock) + ": " + data);
}
}
Log("[-----------End of Memory Dump-----------]");
}
string DumpMoney(IsoCard card)
{
string uid = ReadUID(card);
string data = Read(card, 45);
if (string.IsNullOrEmpty(data)) return "";
string money = data.Substring(18, 4);
byte[] binary = FromHex(money);
int value = (((int)binary[0]) << 8) | ((int)binary[1]);
Log("UID: " + uid + " " + value + " cents");
return data;
}
void UpdateMoney(IsoCard card)
{
string cfg = CardDumper.GetConfigurationKey("UpdateMoney");
int money = 0;
if (!int.TryParse(cfg, out money)) return;
string data = DumpMoney(card);
if (string.IsNullOrEmpty(data)) return;
int first = money & 0x0000FF;
int second = (money >> 8) & 0x0000FF;
//9
// 10
byte[] bytes = FromHex(data);
bytes[9] = (byte)second;
bytes[10] = (byte)first;
if (!Write(card, 45, bytes))
{
Log("Write failed!");
}
DumpMoney(card);
}
void CardInserted(object sender, CardStatusEventArgs args)
{
SCardMonitor monitor = (SCardMonitor)sender;
Log("CardInserted Event for reader: " + args.ReaderName);
try
{
SCardReader reader = new SCardReader(m_card_context);
if (reader.Connect(args.ReaderName, SCardShareMode.Shared, SCardProtocol.Any) == SCardError.Success)
{
DumpStatus(reader);
IsoCard card = new IsoCard(reader);
if (CardDumper.IsKeyEnabled("DumpCard"))
DumpCard(card);
if (CardDumper.IsKeyEnabled("DumpMoney"))
DumpMoney(card);
if (!string.IsNullOrEmpty(CardDumper.GetConfigurationKey("UpdateMoney")))
UpdateMoney(card);
}
reader.Disconnect(SCardReaderDisposition.Reset);
}
catch (Exception ex)
{
Log("Exception: " + ex.ToString());
}
}
void CardRemoved(object sender, CardStatusEventArgs args)
{
SCardMonitor monitor = (SCardMonitor)sender;
Log("CardRemoved Event for reader: " + args.ReaderName);
//Console.WriteLine(" ATR: " + StringAtr(args.Atr));
//Console.WriteLine(" State: " + args.State + "\n");
}
void Initialized(object sender, CardStatusEventArgs args)
{
SCardMonitor monitor = (SCardMonitor)sender;
//Console.WriteLine(">> Initialized Event for reader: " + args.ReaderName);
//Console.WriteLine(" ATR: " + StringAtr(args.Atr));
//Console.WriteLine(" State: " + args.State + "\n");
}
void StatusChanged(object sender, StatusChangeEventArgs args)
{
SCardMonitor monitor = (SCardMonitor)sender;
//Console.WriteLine(">> StatusChanged Event for reader: " + args.ReaderName);
//Console.WriteLine(" ATR: " + StringAtr(args.ATR));
//Console.WriteLine(" Last state: " + args.LastState + "\n New state: " + args.NewState + "\n");
}
void MonitorException(object sender, PCSCException ex)
{
Log("Monitor exited due an error:");
Log(SCardHelper.StringifyError(ex.SCardError));
}
public static bool IsKeyEnabled(string key)
{
return (GetConfigurationKey(key) == "1") || (GetConfigurationKey(key).ToUpper() == "TRUE");
}
public static string GetConfigurationKey(string key)
{
string output = "";
NameValueCollection settings = null;
try
{
settings = System.Configuration.ConfigurationManager.AppSettings;
if (settings != null)
{
output = settings[key];
}
}
catch (System.Configuration.ConfigurationErrorsException ex)
{ }
return output;
}
}
}