proxmark3/client/lualibs/read14b.lua

136 lines
3.4 KiB
Lua
Raw Normal View History

--[[
2019-03-09 17:40:45 +08:00
This is a library to read 14443b tags. It can be used something like this
2019-03-09 17:40:45 +08:00
local reader = require('read14b')
2019-05-08 04:33:26 +08:00
result, err = reader.read14443b()
2019-03-09 17:40:45 +08:00
if not result then
print(err)
return
end
print(result.name)
--]]
-- Loads the commands-library
local cmds = require('commands')
local utils = require('utils')
2019-05-08 04:33:26 +08:00
-- Shouldn't take longer than 2.5 seconds
2020-09-30 16:10:57 +08:00
local TIMEOUT = 2000
2019-05-08 04:33:26 +08:00
local ISO14B_COMMAND = {
2020-09-30 16:10:57 +08:00
ISO14B_CONNECT = 0x1,
ISO14B_DISCONNECT = 0x2,
ISO14B_APDU = 0x4,
ISO14B_RAW = 0x8,
2019-03-09 17:40:45 +08:00
ISO14B_REQUEST_TRIGGER = 0x10,
ISO14B_APPEND_CRC = 0x20,
ISO14B_SELECT_STD = 0x40,
ISO14B_SELECT_SR = 0x80,
2020-09-30 16:10:57 +08:00
ISO14B_SET_TIMEOUT = 0x100,
}
2019-08-04 18:58:36 +08:00
local function parse14443b(data)
2019-03-09 17:40:45 +08:00
--[[
Based on this struct :
typedef struct {
2019-03-21 22:19:18 +08:00
uint8_t uid[10];
uint8_t uidlen;
uint8_t atqb[7];
uint8_t chipid;
uint8_t cid;
} PACKED iso14b_card_select_t;
2019-03-09 17:40:45 +08:00
--]]
local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
2019-05-08 04:33:26 +08:00
uid = uid:sub(1, 2 * uidlen)
2019-05-08 07:35:51 +08:00
return {
2019-05-08 04:33:26 +08:00
uid = uid,
uidlen = uidlen,
atqb = atqb,
chipid = chipid,
cid = cid
}
end
-- This function does a connect and retrieves some info
-- @return if successful: an table containing card info
-- @return if unsuccessful : nil, error
local function read14443b(disconnect)
2019-03-09 17:40:45 +08:00
local command, result, info, err, data
local flags = ISO14B_COMMAND.ISO14B_CONNECT +
ISO14B_COMMAND.ISO14B_SELECT_STD
if disconnect then
print('DISCONNECT')
flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT
end
2019-05-08 04:33:26 +08:00
command = Command:newMIX{
cmd = cmds.CMD_HF_ISO14443B_COMMAND,
2019-05-08 04:33:26 +08:00
arg1 = flags
}
2019-05-08 07:35:51 +08:00
2020-09-30 16:10:57 +08:00
info = nil
local result, err = command:sendMIX(false, TIMEOUT, true)
2019-03-09 17:40:45 +08:00
if result then
2020-09-30 16:10:57 +08:00
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL', result)
2019-03-09 17:40:45 +08:00
if arg0 == 0 then
data = string.sub(result, count)
2019-08-04 18:58:36 +08:00
info, err = parse14443b(data)
2019-03-09 17:40:45 +08:00
else
2019-05-08 04:33:26 +08:00
err = 'iso14443b card select failed'
2019-03-09 17:40:45 +08:00
end
else
2019-05-08 04:33:26 +08:00
err = 'No response from card'
2019-03-09 17:40:45 +08:00
end
if err then
return nil, err
end
2020-09-30 16:10:57 +08:00
return info, nil
end
---
2019-03-09 17:40:45 +08:00
-- Waits for a mifare card to be placed within the vicinity of the reader.
-- @return if successful: an table containing card info
-- @return if unsuccessful : nil, error
local function waitFor14443b()
print('Waiting for card... press Enter to quit')
while not core.kbd_enter_pressed() do
2019-03-09 17:40:45 +08:00
res, err = read14443b(false)
2020-09-30 16:10:57 +08:00
if res then return res, err end
2019-03-09 17:40:45 +08:00
-- err means that there was no response from card
end
2019-05-08 04:33:26 +08:00
return nil, 'Aborted by user'
end
2019-05-08 04:59:29 +08:00
---
-- turns on the HF field.
local function connect14443b()
local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443B_COMMAND, arg1 = ISO14B_COMMAND.ISO14B_CONNECT}
return c:sendMIX(true)
2019-05-08 04:59:29 +08:00
end
---
2019-05-08 04:33:26 +08:00
-- Sends an instruction to do nothing, only disconnect
local function disconnect14443b()
local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443B_COMMAND, arg1 = ISO14B_COMMAND.ISO14B_DISCONNECT}
2019-05-08 04:33:26 +08:00
-- We can ignore the response here, no ACK is returned for this command
-- Check /armsrc/iso14443b.c, ReaderIso14443b() for details
return c:sendMIX(true)
end
local library = {
2019-03-09 17:40:45 +08:00
read = read14443b,
waitFor14443b = waitFor14443b,
2019-08-04 18:58:36 +08:00
parse14443b = parse14443b,
2019-05-08 04:59:29 +08:00
connect = connect14443b,
2019-05-08 04:33:26 +08:00
disconnect = disconnect14443b,
2019-03-09 17:40:45 +08:00
ISO14B_COMMAND = ISO14B_COMMAND,
}
2019-03-12 07:12:26 +08:00
return library