]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/read14a.lua
Generic tracing; removed iso14a_XX-functions, removed traceLen as global varible
[proxmark3-svn] / client / lualibs / read14a.lua
1 --[[
2 This is a library to read 14443a tags. It can be used something like this
3
4 local reader = require('read14a')
5 result, err = reader.read1443a()
6 if not result then
7 print(err)
8 return
9 end
10 print(result.name)
11
12 --]]
13 -- Loads the commands-library
14 local cmds = require('commands')
15 local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
16 local ISO14A_COMMAND = {
17 ISO14A_CONNECT = 1,
18 ISO14A_NO_DISCONNECT = 2,
19 ISO14A_APDU = 4,
20 ISO14A_RAW = 8,
21 ISO14A_REQUEST_TRIGGER = 0x10,
22 ISO14A_APPEND_CRC = 0x20,
23 ISO14A_SET_TIMEOUT = 0x40
24 }
25
26 local ISO14443a_TYPES = {}
27 ISO14443a_TYPES[0x00] = "NXP MIFARE Ultralight | Ultralight C"
28 ISO14443a_TYPES[0x01] = "NXP MIFARE TNP3xxx Activision Game Appliance"
29 ISO14443a_TYPES[0x04] = "NXP MIFARE (various !DESFire !DESFire EV1)"
30 ISO14443a_TYPES[0x08] = "NXP MIFARE CLASSIC 1k | Plus 2k"
31 ISO14443a_TYPES[0x09] = "NXP MIFARE Mini 0.3k"
32 ISO14443a_TYPES[0x10] = "NXP MIFARE Plus 2k"
33 ISO14443a_TYPES[0x11] = "NXP MIFARE Plus 4k"
34 ISO14443a_TYPES[0x18] = "NXP MIFARE Classic 4k | Plus 4k"
35 ISO14443a_TYPES[0x20] = "NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"
36 ISO14443a_TYPES[0x24] = "NXP MIFARE DESFire | DESFire EV1"
37 ISO14443a_TYPES[0x28] = "JCOP31 or JCOP41 v2.3.1"
38 ISO14443a_TYPES[0x38] = "Nokia 6212 or 6131 MIFARE CLASSIC 4K"
39 ISO14443a_TYPES[0x88] = "Infineon MIFARE CLASSIC 1K"
40 ISO14443a_TYPES[0x98] = "Gemplus MPCOS"
41
42
43 local function tostring_1443a(sak)
44 return ISO14443a_TYPES[sak] or ("Unknown (SAK=%x)"):format(sak)
45 end
46
47 local function parse1443a(data)
48 --[[
49
50 Based on this struct :
51
52 typedef struct {
53 byte_t uid[10];
54 byte_t uidlen;
55 byte_t atqa[2];
56 byte_t sak;
57 byte_t ats_len;
58 byte_t ats[256];
59 } __attribute__((__packed__)) iso14a_card_select_t;
60
61 --]]
62
63 local count,uid,uidlen, atqa, sak, ats_len, ats= bin.unpack('H10CH2CC',data)
64 uid = uid:sub(1,2*uidlen)
65 --print("uid, atqa, sak: ",uid, atqa, sak)
66 --print("TYPE: ", tostring_1443a(sak))
67 return { uid = uid, atqa = atqa, sak = sak, name = tostring_1443a(sak)}
68 end
69
70 --- Sends a USBpacket to the device
71 -- @param command - the usb packet to send
72 -- @param ignoreresponse - if set to true, we don't read the device answer packet
73 -- which is usually recipe for fail. If not sent, the host will wait 2s for a
74 -- response of type CMD_ACK
75 -- @return packet,nil if successfull
76 -- nil, errormessage if unsuccessfull
77
78 local function sendToDevice(command, ignoreresponse)
79 core.clearCommandBuffer()
80 local err = core.SendCommand(command:getBytes())
81 if err then
82 print(err)
83 return nil, err
84 end
85 if ignoreresponse then return nil,nil end
86
87 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
88 return response,nil
89 end
90
91 -- This function does a connect and retrieves som einfo
92 -- @param dont_disconnect - if true, does not disable the field
93 -- @return if successfull: an table containing card info
94 -- @return if unsuccessfull : nil, error
95 local function read14443a(dont_disconnect)
96 local command, result, info, err, data
97
98 command = Command:new{cmd = cmds.CMD_READER_ISO_14443a,
99 arg1 = ISO14A_COMMAND.ISO14A_CONNECT}
100 if dont_disconnect then
101 command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT
102 end
103 local result,err = sendToDevice(command)
104 if result then
105 local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result)
106 if arg0 == 0 then
107 return nil, "iso14443a card select failed"
108 end
109 data = string.sub(result,count)
110 info, err = parse1443a(data)
111 else
112 err ="No response from card"
113 end
114
115 if err then
116 print(err)
117 return nil, err
118 end
119 return info
120 end
121
122 ---
123 -- Waits for a mifare card to be placed within the vicinity of the reader.
124 -- @return if successfull: an table containing card info
125 -- @return if unsuccessfull : nil, error
126 local function waitFor14443a()
127 print("Waiting for card... press any key to quit")
128 while not core.ukbhit() do
129 res, err = read14443a()
130 if res then return res end
131 -- err means that there was no response from card
132 end
133 return nil, "Aborted by user"
134 end
135 local library = {
136
137 read1443a = read14443a,
138 read = read14443a,
139 waitFor14443a = waitFor14443a,
140 parse1443a = parse1443a,
141 sendToDevice = sendToDevice,
142 ISO14A_COMMAND = ISO14A_COMMAND,
143 }
144
145 return library
Impressum, Datenschutz