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