proxmark3/client/luascripts/hf_14b_calypso.lua

287 lines
8.2 KiB
Lua
Raw Normal View History

2016-03-21 03:22:35 +08:00
local cmds = require('commands')
local getopt = require('getopt')
local lib14b = require('read14b')
local utils = require('utils')
2016-04-05 10:46:25 +08:00
local iso7816 = require('7816_error')
2020-04-05 16:05:14 +08:00
local ansicolors = require('ansicolors')
2016-03-21 03:22:35 +08:00
2019-05-08 04:48:18 +08:00
copyright = ''
author = 'Iceman'
2021-04-22 00:36:21 +08:00
version = 'v1.0.5'
2019-05-08 04:48:18 +08:00
desc = [[
2016-03-21 03:22:35 +08:00
This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
2019-05-08 04:48:18 +08:00
]]
example = [[
script run hf_14b_calypso -b 11223344
2016-03-21 03:22:35 +08:00
2019-05-08 04:48:18 +08:00
]]
usage = [[
script run hf_14b_calypso -h -b
2020-04-05 16:05:14 +08:00
]]
arguments = [[
2019-05-08 04:48:18 +08:00
h this helptext
b raw bytes to send
2016-03-21 03:22:35 +08:00
]]
--[[
2019-03-09 17:34:43 +08:00
This script communicates with /armsrc/iso14443b.c,
Check there for details about data format and how commands are interpreted on the
device-side.
2016-03-21 03:22:35 +08:00
]]
local function calypso_parse(result)
2021-04-24 04:25:58 +08:00
if result.Oldarg0 >= 0 then
local len = result.Oldarg0 * 2
2020-09-30 20:27:19 +08:00
if len > 0 then
2021-04-24 04:25:58 +08:00
local d = string.sub(result.Data, 0, len);
return d, nil
2021-05-06 03:04:48 +08:00
end
2019-03-09 17:34:43 +08:00
end
2021-04-24 04:25:58 +08:00
return nil, "calypso_parse failed"
2016-03-21 03:22:35 +08:00
end
2019-03-09 17:34:43 +08:00
---
2016-03-21 03:22:35 +08:00
-- A debug printout-function
local function dbg(args)
2019-05-08 04:48:18 +08:00
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
2019-03-09 17:34:43 +08:00
end
end
---
2016-03-21 03:22:35 +08:00
-- This is only meant to be used when errors occur
local function oops(err)
2019-05-08 04:48:18 +08:00
print('ERROR: ', err)
2019-05-08 04:59:29 +08:00
lib14b.disconnect()
2019-03-09 17:34:43 +08:00
return nil, err
2016-03-21 03:22:35 +08:00
end
2019-03-09 17:34:43 +08:00
---
2016-03-21 03:22:35 +08:00
-- Usage help
local function help()
2019-05-08 04:48:18 +08:00
print(copyright)
print(author)
print(version)
2019-03-09 17:34:43 +08:00
print(desc)
2020-04-05 16:05:14 +08:00
print(ansicolors.cyan..'Usage'..ansicolors.reset)
print(usage)
2020-04-05 16:05:14 +08:00
print(ansicolors.cyan..'Arguments'..ansicolors.reset)
print(arguments)
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
2016-03-21 03:22:35 +08:00
end
--
-- helper function, give current count of items in lua-table.
local function tablelen(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
---
2019-03-09 17:34:43 +08:00
-- helper function, gives a sorted table from table t,
2021-10-10 07:35:38 +08:00
-- order can be a separate sorting-order function.
2016-03-21 03:22:35 +08:00
local function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
2019-03-09 17:34:43 +08:00
-- otherwise just sort the keys
2016-03-21 03:22:35 +08:00
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
---
2019-03-09 17:34:43 +08:00
-- Sends a usbpackage , "hf 14b raw"
2016-03-21 03:22:35 +08:00
-- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length.
local function calypso_send_cmd_raw(data, ignoreresponse )
2021-04-22 00:36:21 +08:00
local flags = lib14b.ISO14B_COMMAND.ISO14B_APDU
2020-10-04 06:05:37 +08:00
-- flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
-- lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
2021-04-24 04:25:58 +08:00
local flags = lib14b.ISO14B_COMMAND.ISO14B_APDU
2021-04-22 00:36:21 +08:00
data = data or ""
-- LEN of data, half the length of the ASCII-string hex string
-- 2 bytes flags
-- 4 bytes timeout
-- 2 bytes raw len
-- n bytes raw
2021-04-24 04:25:58 +08:00
local flags_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(flags), 16))
local time_str = ('%08x'):format(0)
local rawlen_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(( 8 + #data/2)), 16))
local senddata = ('%s%s%s%s'):format(flags_str, time_str, rawlen_str,data)
2021-04-22 00:36:21 +08:00
local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = senddata}
local result, err = c:sendNG(ignoreresponse, 2000)
2019-03-09 17:34:43 +08:00
if result then
2021-04-24 04:25:58 +08:00
if result.Oldarg0 >= 0 then
2020-09-30 16:10:57 +08:00
return calypso_parse(result)
2020-09-30 20:27:19 +08:00
else
2020-09-30 16:10:57 +08:00
err = 'card response failed'
end
else
err = 'No response from card'
2019-03-09 17:34:43 +08:00
end
2020-09-30 16:10:57 +08:00
return result, err
2016-03-21 03:22:35 +08:00
end
---
-- calypso_card_num : Reads card number from ATR and
-- writes it in the tree in decimal format.
local function calypso_card_num(card)
2019-03-09 17:34:43 +08:00
if not card then return end
local card_num = tonumber( card.uid:sub(1,8),16 )
2020-09-30 16:10:57 +08:00
print('')
print('Card UID ' ..ansicolors.green..card.uid:format('%x')..ansicolors.reset)
print('Card Number ' ..ansicolors.green..string.format('%u', card_num)..ansicolors.reset)
print('-----------------------')
2016-03-21 03:22:35 +08:00
end
---
-- analyse CALYPSO apdu status bytes.
local function calypso_apdu_status(apdu)
2019-03-09 17:34:43 +08:00
-- last two is CRC
-- next two is APDU status bytes.
local mess = 'FAIL'
local sw = apdu:sub( #apdu-7, #apdu-4)
desc, err = iso7816.tostring(sw)
2020-09-30 16:10:57 +08:00
--print ('SW', sw, desc, err )
local status = ( sw == '9000' )
return status, desc, err
2016-03-21 03:22:35 +08:00
end
2020-10-04 06:05:37 +08:00
local CLA = '94'
2016-03-21 03:22:35 +08:00
local _calypso_cmds = {
2016-08-05 03:57:40 +08:00
-- Break down of command bytes:
-- A4 = select
-- Master File 3F00
2019-03-09 17:34:43 +08:00
-- 0x3F = master file
2016-08-05 03:57:40 +08:00
-- 0x00 = master file id, is constant to 0x00.
-- DF Dedicated File 38nn
-- can be seen as directories
2019-03-09 17:34:43 +08:00
-- 0x38
2016-08-05 03:57:40 +08:00
-- 0xNN id
2019-03-09 17:34:43 +08:00
-- ["01.Select ICC file"] = '0294 a4 080004 3f00 0002',
2016-08-05 03:57:40 +08:00
2019-03-09 17:34:43 +08:00
-- EF Elementary File
2016-08-05 03:57:40 +08:00
-- EF1 Pin file
-- EF2 Key file
-- Grey Lock file
-- Electronic deposit file
-- Electronic Purse file
-- Electronic Transaction log file
2020-10-04 06:05:37 +08:00
['01.Select ICC file'] = CLA..'a4 080004 3f00 0002',
['02.ICC'] = CLA..'b2 01 041d',
['03.Select EnvHol file'] = CLA..'a4 080004 2000 2001',
['04.EnvHol1'] = CLA..'b2 01 041d',
['05.Select EvLog file'] = CLA..'a4 080004 2000 2010',
['06.EvLog1'] = CLA..'b2 01 041d',
['07.EvLog2'] = CLA..'b2 02 041d',
['08.EvLog3'] = CLA..'b2 03 041d',
['09.Select ConList file']= CLA..'a4 080004 2000 2050',
['10.ConList'] = CLA..'b2 01 041d',
['11.Select Contra file'] = CLA..'a4 080004 2000 2020',
['12.Contra1'] = CLA..'b2 01 041d',
['13.Contra2'] = CLA..'b2 02 041d',
['14.Contra3'] = CLA..'b2 03 041d',
['15.Contra4'] = CLA..'b2 04 041d',
['16.Select Counter file']= CLA..'a4 080004 2000 2069',
['17.Counter'] = CLA..'b2 01 041d',
['18.Select SpecEv file'] = CLA..'a4 080004 2000 2040',
['19.SpecEv1'] = CLA..'b2 01 041d',
2016-03-21 03:22:35 +08:00
}
2019-03-09 17:34:43 +08:00
---
2016-03-21 03:22:35 +08:00
-- The main entry point
function main(args)
2019-03-09 17:34:43 +08:00
print( string.rep('--',20) )
print( string.rep('--',20) )
print()
local data, apdu, flags, uid, cid, result, err, card
-- Read the parameters
for o, a in getopt.getopt(args, 'h') do
2019-05-08 04:48:18 +08:00
if o == 'h' then return help() end
if o == 'b' then bytes = a end
2019-03-09 17:34:43 +08:00
end
2016-03-21 03:22:35 +08:00
2020-09-30 16:10:57 +08:00
-- lib14b.connect()
2019-05-08 07:35:51 +08:00
2019-03-09 17:34:43 +08:00
-- Select 14b tag.
card, err = lib14b.waitFor14443b()
if not card then return oops(err) end
calypso_card_num(card)
cid = card.cid
--[[
NAME VALUE APDU_POS
PCB 0x0A 0
CID 0x00 1
CLA 0x94 2
SELECT FILE 0xA4 3
READ FILE 0xB2 3
P1 4
P2 5
LEN_
0 1 2 3 4 5 6 7
apdu = '02 94 a4 08 00 04 3f 00 00 02' --select ICC file
DF_NAME = "1TIC.ICA"
--]]
--for i = 1,10 do
--result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
for i, apdu in spairs(_calypso_cmds) do
2020-09-30 16:10:57 +08:00
print('>> '..ansicolors.yellow..i..ansicolors.reset)
2019-05-08 04:48:18 +08:00
apdu = apdu:gsub('%s+', '')
2021-04-24 04:25:58 +08:00
data, err = calypso_send_cmd_raw(apdu , false)
2020-09-30 16:10:57 +08:00
if err then
print('<< '..err)
2019-03-09 17:34:43 +08:00
else
2021-04-24 04:25:58 +08:00
if data then
local status, desc, err = calypso_apdu_status(data)
local d = data:sub(3, (#data - 8))
2020-09-30 16:10:57 +08:00
if status then
print('<< '..d..' ('..ansicolors.green..'ok'..ansicolors.reset..')')
else
print('<< '..d..' '..ansicolors.red..err..ansicolors.reset )
end
else
print('<< no answer')
end
2019-03-09 17:34:43 +08:00
end
end
2019-05-08 04:59:29 +08:00
lib14b.disconnect()
2016-03-21 03:22:35 +08:00
end
---
2019-03-09 17:34:43 +08:00
-- a simple selftest function, tries to convert
2016-03-21 03:22:35 +08:00
function selftest()
2019-03-09 17:34:43 +08:00
DEBUG = true
2019-05-08 04:48:18 +08:00
dbg('Performing test')
dbg('Tests done')
2016-03-21 03:22:35 +08:00
end
2019-03-09 17:34:43 +08:00
-- Flip the switch here to perform a sanity check.
2016-03-21 03:22:35 +08:00
-- It read a nonce in two different ways, as specified in the usage-section
2019-05-08 04:48:18 +08:00
if '--test'==args then
2019-03-09 17:34:43 +08:00
selftest()
else
-- Call the main
main(args)
2019-03-12 07:12:26 +08:00
end