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
local TIMEOUT = 2500
2019-05-08 04:33:26 +08:00
local ISO14B_COMMAND = {
2019-03-09 17:40:45 +08:00
ISO14B_CONNECT = 1,
ISO14B_DISCONNECT = 2,
ISO14B_APDU = 4,
ISO14B_RAW = 8,
ISO14B_REQUEST_TRIGGER = 0x10,
ISO14B_APPEND_CRC = 0x20,
ISO14B_SELECT_STD = 0x40,
ISO14B_SELECT_SR = 0x80,
}
local function parse1443b(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;
2019-03-09 17:40:45 +08:00
} __attribute__((__packed__)) iso14b_card_select_t;
--]]
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 successfull: an table containing card info
-- @return if unsuccessfull : 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_ISO_14443B_COMMAND,
arg1 = flags
}
2019-05-08 07:35:51 +08:00
2019-05-08 04:33:26 +08:00
local result, err = command:sendMIX()
2019-03-09 17:40:45 +08:00
if result then
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result)
if arg0 == 0 then
data = string.sub(result, count)
info, err = parse1443b(data)
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
print(err)
return nil, err
end
return info
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 successfull: an table containing card info
-- @return if unsuccessfull : nil, error
local function waitFor14443b()
2019-05-08 04:33:26 +08:00
print('Waiting for card... press any key to quit')
2019-03-09 17:40:45 +08:00
while not core.ukbhit() do
res, err = read14443b(false)
if res then return res end
-- 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_ISO_14443B_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()
2019-05-08 04:59:29 +08:00
local c = Command:newMIX{cmd = cmds.CMD_ISO_14443B_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,
parse1443b = parse1443b,
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