]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/scripts/calypso.lua
ADD; first try at reading calypso tags
[proxmark3-svn] / client / scripts / calypso.lua
1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local lib14b = require('read14b')
4 local utils = require('utils')
5
6 example = "script runs 14b raw commands to query a CAPLYPSO tag"
7 author = "Iceman, 2016"
8 desc =
9 [[
10 This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
11
12 Arguments:
13 -b 123
14 Examples :
15 script run f -b 11223344
16 script run f
17
18 Examples :
19
20 # 1. Connect and don't disconnect
21 script run f
22 # 2. Send mf auth, read response
23 script run f
24 # 3. disconnect
25 script run f
26
27 ]]
28
29 --[[
30 This script communicates with /armsrc/iso14443b.c,
31 Check there for details about data format and how commands are interpreted on the
32 device-side.
33 ]]
34
35 ---
36 --
37 local function calypso_switch_on_field()
38 local flags = lib14b.ISO14B_COMMAND.ISO14B_CONNECT
39 local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags}
40 return lib14b.sendToDevice(c, true)
41 end
42 ---
43 -- Disconnect (poweroff) the antenna forcing a disconnect of a 14b tag.
44 local function calypso_switch_off_field()
45 local flags = lib14b.ISO14B_COMMAND.ISO14B_DISCONNECT
46 local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags}
47 return lib14b.sendToDevice(c, true)
48 end
49
50 local function calypso_parse(result)
51 local r = Command.parse(result)
52 local len = r.arg2 * 2
53 r.data = string.sub(r.data, 0, len);
54 if r.arg1 == 0 then
55 return r, nil
56 end
57 return nil,nil
58 end
59 ---
60 -- A debug printout-function
61 local function dbg(args)
62 if DEBUG then
63 print("###", args)
64 end
65 end
66 ---
67 -- This is only meant to be used when errors occur
68 local function oops(err)
69 print("ERROR: ",err)
70 calypso_switch_off_field()
71 return nil,err
72 end
73 ---
74 -- Usage help
75 local function help()
76 print(desc)
77 print("Example usage")
78 print(example)
79 end
80 --
81 -- helper function, give current count of items in lua-table.
82 local function tablelen(T)
83 local count = 0
84 for _ in pairs(T) do count = count + 1 end
85 return count
86 end
87 ---
88 -- helper function, gives a sorted table from table t,
89 -- order can be a seperate sorting-order function.
90 local function spairs(t, order)
91 -- collect the keys
92 local keys = {}
93 for k in pairs(t) do keys[#keys+1] = k end
94
95 -- if order function given, sort by it by passing the table and keys a, b,
96 -- otherwise just sort the keys
97 if order then
98 table.sort(keys, function(a,b) return order(t, a, b) end)
99 else
100 table.sort(keys)
101 end
102
103 -- return the iterator function
104 local i = 0
105 return function()
106 i = i + 1
107 if keys[i] then
108 return keys[i], t[keys[i]]
109 end
110 end
111 end
112 ---
113 -- Sends a usbpackage , "hf 14b raw"
114 -- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length.
115 local function calypso_send_cmd_raw(data, ignoreresponse )
116
117 local command, flags, result, err
118 flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
119 lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
120
121 data = data or "00"
122
123 command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND,
124 arg1 = flags,
125 arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string
126 arg3 = 0,
127 data = data} -- data bytes (commands etc)
128 result, err = lib14b.sendToDevice(command, false)
129
130 if ignoreresponse then return response, err end
131
132 if result then
133 local r = calypso_parse(result)
134 return r, nil
135 end
136 return respone, err
137 end
138 ---
139 -- calypso_card_num : Reads card number from ATR and
140 -- writes it in the tree in decimal format.
141 local function calypso_card_num(card)
142 if not card then return end
143 local card_num = tonumber( card.uid:sub(1,8),16 )
144 print('Card UID', card.uid)
145 print('Card Number', card_num)
146 end
147 ---
148 -- analyse CALYPSO apdu status bytes.
149 local function calypso_apdu_status(apdu)
150 -- last two is CRC
151 -- next two is APDU status bytes.
152 local sw = apdu:sub( #apdu-7, #apdu-4)
153 print ('SW', sw )
154 -- if 0x9000 OK
155 if sw == '9000' then return 1 end
156 return 0
157 end
158
159 local _calypso_cmds = {
160 ["1.Select ICC file"] = "02 94 a4 08 00 04 3f 00 00 02",
161 ["2.ICC"] = "02 94 b2 01 04 1d",
162 ["3.Select EnvHol file"]= '02 94 a4 08 00 04 20 00 20 01',
163 ["4.EnvHol1"] = '02 94 b2 01 04 1d',
164 ["5.Select EvLog file"] = '02 94 a4 08 00 04 20 00 20 10',
165 ["6.EvLog1"] = '06 00b2 0104 1d',
166 ["7.EvLog2"] = '06 00b2 0204 1d',
167 ["8.EvLog3"] = '06 00b2 0304 1d',
168 -- ["Select ConList file"]= '42 01 04 0a 00a4 0800 04 2000 2050',
169 -- ["ConList"] = '42 01 06 06 00b2 0104 1d',
170 -- ["Select Contra file"]= '42 01 08 0a 00a4 0800 04 2000 2020',
171 -- ["Contra1"] = '42 01 0a 06 00b2 0104 1d',
172 -- ["Contra2"] = '42 01 0c 06 00b2 0204 1d',
173 -- ["Contra3"] = '42 01 0e 06 00b2 0304 1d',
174 -- ["Contra4"] = '42 01 00 06 00b2 0404 1d',
175 -- ["Select Counter file"]= '42 01 02 0a 00a4 0800 04 2000 2069',
176 -- ["Counter"] = '42 01 04 06 00b2 0104 1d',
177 -- ["Select SpecEv file"]= '42 01 06 0a 00a4 08 0004 2000 2040',
178 -- ["SpecEv1"] = '42 01 08 06 00b2 0104 1d',
179 }
180
181 ---
182 -- The main entry point
183 function main(args)
184
185 local data, apdu, flags, uid, cid, result, err, card
186 -- Read the parameters
187 for o, a in getopt.getopt(args, 'h') do
188 if o == "h" then return help() end
189 end
190
191 calypso_switch_on_field()
192
193 -- Select 14b tag.
194 card, err = lib14b.waitFor14443b()
195 if not card then return oops(err) end
196
197 calypso_card_num(card)
198 cid = card.cid
199
200 --[[
201 NAME VALUE APDU_POS
202 PCB 0x0A 0
203 CID 0x00 1
204 CLA 0x94 2
205 SELECT FILE 0xA4 3
206 READ FILE 0xB2 3
207 P1 4
208 P2 5
209 LEN_
210 0 1 2 3 4 5 6 7
211 apdu = '0a 00 94 a4 08 00 04 3f 00 00 02' --select ICC file
212 DF_NAME = "1TIC.ICA"
213 --]]
214 --for i = 1,10 do
215 --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
216 for i, apdu in spairs(_calypso_cmds) do
217 apdu = apdu:gsub("%s+","")
218 result, err = calypso_send_cmd_raw(apdu , false)
219 if result then
220 calypso_apdu_status(result.data)
221 print( result.data )
222 end
223 end
224 calypso_switch_off_field()
225 end
226 ---
227 -- a simple selftest function, tries to convert
228 function selftest()
229 DEBUG = true
230 dbg("Performing test")
231 dbg("Tests done")
232 end
233 -- Flip the switch here to perform a sanity check.
234 -- It read a nonce in two different ways, as specified in the usage-section
235 if "--test"==args then
236 selftest()
237 else
238 -- Call the main
239 main(args)
240 end
Impressum, Datenschutz