2017-01-30 23:51:07 +08:00
|
|
|
local cmds = require('commands')
|
|
|
|
local getopt = require('getopt')
|
|
|
|
local lib14a = require('read14a')
|
|
|
|
local utils = require('utils')
|
2020-04-05 18:56:35 +08:00
|
|
|
local ansicolors = require('ansicolors')
|
2019-04-29 00:46:06 +08:00
|
|
|
|
|
|
|
copyright = ''
|
|
|
|
author = 'Iceman'
|
2020-04-05 18:56:35 +08:00
|
|
|
version = 'v1.0.2'
|
2019-05-08 05:21:56 +08:00
|
|
|
desc = [[
|
2019-12-18 17:13:41 +08:00
|
|
|
This is a script that reads AZTEK ISO14443a tags.
|
|
|
|
It starts from block 0 and ends at default block 20. Use 'b' to say different endblock.
|
2017-01-30 23:51:07 +08:00
|
|
|
xor: the first three block (0,1,2) is not XORED. The rest seems to be xored.
|
2019-04-29 00:46:06 +08:00
|
|
|
]]
|
|
|
|
example = [[
|
|
|
|
-- default
|
2020-08-14 00:41:31 +08:00
|
|
|
script run hf_14a_aztek
|
2019-04-29 04:54:00 +08:00
|
|
|
|
2019-04-29 00:46:06 +08:00
|
|
|
-- stop at block 10
|
2020-08-14 00:41:31 +08:00
|
|
|
script run hf_14a_aztek -b 10
|
2019-04-29 00:46:06 +08:00
|
|
|
]]
|
|
|
|
usage = [[
|
2020-08-14 00:41:31 +08:00
|
|
|
script run hf_14a_aztek [-h] [-b]
|
2020-04-05 18:56:35 +08:00
|
|
|
]]
|
|
|
|
arguments = [[
|
|
|
|
-h This help
|
|
|
|
-b endblock in decimal (1-255, default 20)
|
2017-01-30 23:51:07 +08:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- Some globals
|
|
|
|
local DEBUG = false -- the debug flag
|
2019-03-09 17:34:43 +08:00
|
|
|
---
|
2017-01-30 23:51:07 +08:00
|
|
|
-- A debug printout-function
|
2017-10-05 22:17:09 +08:00
|
|
|
local function dbg(args)
|
2019-04-29 00:46:06 +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
|
|
|
|
---
|
2017-01-30 23:51:07 +08:00
|
|
|
-- This is only meant to be used when errors occur
|
2017-10-05 22:17:09 +08:00
|
|
|
local function oops(err)
|
2019-04-29 00:46:06 +08:00
|
|
|
print('ERROR:', err)
|
2019-03-09 17:34:43 +08:00
|
|
|
core.clearCommandBuffer()
|
2019-04-29 04:54:00 +08:00
|
|
|
return nil, err
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
---
|
2017-01-30 23:51:07 +08:00
|
|
|
-- Usage help
|
2017-10-05 22:17:09 +08:00
|
|
|
local function help()
|
2019-04-29 00:46:06 +08:00
|
|
|
print(copyright)
|
|
|
|
print(author)
|
|
|
|
print(version)
|
2019-03-09 17:34:43 +08:00
|
|
|
print(desc)
|
2020-04-05 18:56:35 +08:00
|
|
|
print(ansicolors.cyan..'Usage'..ansicolors.reset)
|
2019-04-29 04:54:00 +08:00
|
|
|
print(usage)
|
2020-04-05 18:56:35 +08:00
|
|
|
print(ansicolors.cyan..'Arguments'..ansicolors.reset)
|
|
|
|
print(arguments)
|
|
|
|
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
|
|
|
|
print(example)
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
|
|
|
--
|
|
|
|
--- Picks out and displays the data read from a tag
|
|
|
|
-- Specifically, takes a usb packet, converts to a Command
|
2019-03-09 17:34:43 +08:00
|
|
|
-- (as in commands.lua), takes the data-array and
|
2017-01-30 23:51:07 +08:00
|
|
|
-- reads the number of bytes specified in arg1 (arg0 in c-struct)
|
|
|
|
-- and displays the data
|
|
|
|
-- @blockno just to print which block the data belong to
|
|
|
|
-- @param usbpacket the data received from the device
|
|
|
|
function showdata(blockno, data)
|
2019-03-09 17:34:43 +08:00
|
|
|
local xorkey = '55AA55AA55AA55AA6262'
|
|
|
|
local s = data.." | "
|
|
|
|
local dex = ''
|
|
|
|
local rs
|
|
|
|
for i = 1, 20-4, 4 do
|
|
|
|
local item = string.sub(data, i, i+3)
|
|
|
|
local xor = string.sub(xorkey, i, i+3)
|
|
|
|
|
|
|
|
if blockno > 2 then
|
|
|
|
rs = bit32.bxor(tonumber(item,16) , tonumber(xor,16))
|
|
|
|
else
|
|
|
|
rs = tonumber(item, 16)
|
|
|
|
end
|
|
|
|
dex = (dex..'%04X'):format(rs)
|
|
|
|
end
|
|
|
|
s = s..dex.." | "
|
|
|
|
print( (" %02d | %s"):format(blockno,s))
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
|
|
|
--
|
|
|
|
-- Send a "raw" iso14443a package, ie "hf 14a raw" command
|
|
|
|
function sendRaw(rawdata, options)
|
2019-04-29 00:46:06 +08:00
|
|
|
|
|
|
|
local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT
|
|
|
|
+ lib14a.ISO14A_COMMAND.ISO14A_RAW
|
|
|
|
+ lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC
|
|
|
|
+ lib14a.ISO14A_COMMAND.ISO14A_NO_RATS
|
|
|
|
|
2019-08-04 01:17:00 +08:00
|
|
|
local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER,
|
2019-03-09 17:34:43 +08:00
|
|
|
arg1 = flags, -- Send raw
|
|
|
|
-- arg2 contains the length, which is half the length
|
|
|
|
-- of the ASCII-string rawdata
|
|
|
|
arg2 = string.len(rawdata)/2,
|
|
|
|
data = rawdata}
|
2019-04-29 04:54:00 +08:00
|
|
|
|
2019-04-29 00:46:06 +08:00
|
|
|
return command:sendMIX(options.ignore_response)
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
---
|
2017-01-30 23:51:07 +08:00
|
|
|
-- The main entry point
|
|
|
|
function main(args)
|
|
|
|
|
2019-03-09 17:34:43 +08:00
|
|
|
local ignore_response = false
|
|
|
|
local endblock = 20
|
|
|
|
|
|
|
|
-- Read the parameters
|
|
|
|
for o, a in getopt.getopt(args, 'hb:') do
|
2019-04-29 00:46:06 +08:00
|
|
|
if o == 'h' then return help() end
|
|
|
|
if o == 'b' then endblock = a end
|
2019-03-09 17:34:43 +08:00
|
|
|
end
|
|
|
|
endblock = endblock or 20
|
|
|
|
|
|
|
|
-- First of all, connect
|
|
|
|
info, err = lib14a.read(true, true)
|
2019-05-08 07:35:51 +08:00
|
|
|
if err then
|
2019-05-08 05:21:56 +08:00
|
|
|
lib14a.disconnect()
|
|
|
|
return oops(err)
|
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
core.clearCommandBuffer()
|
|
|
|
|
|
|
|
local blockData = {}
|
|
|
|
|
|
|
|
-- Show tag info
|
2019-04-29 00:46:06 +08:00
|
|
|
print(('\nFound Card UID [%s]\n'):format(info.uid))
|
2019-03-09 17:34:43 +08:00
|
|
|
|
2019-04-29 00:46:06 +08:00
|
|
|
print('blk | data | xored')
|
|
|
|
print('----+------------------+-------------------')
|
2019-03-09 17:34:43 +08:00
|
|
|
for block = 00, endblock do
|
2019-04-29 00:46:06 +08:00
|
|
|
local cmd = string.format('10%02x00', block)
|
2019-03-09 17:34:43 +08:00
|
|
|
res, err = sendRaw(cmd , {ignore_response = ignore_response})
|
2019-05-08 07:35:51 +08:00
|
|
|
if err then
|
2019-05-08 05:21:56 +08:00
|
|
|
lib14a.disconnect()
|
|
|
|
return oops(err)
|
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
|
|
|
|
local cmd_response = Command.parse(res)
|
|
|
|
local len = tonumber(cmd_response.arg1) * 2
|
|
|
|
local data = string.sub(tostring(cmd_response.data), 0, len-4)
|
|
|
|
|
|
|
|
showdata(block, data)
|
|
|
|
table.insert(blockData, data)
|
|
|
|
end
|
|
|
|
print("----+------------------+-------------------")
|
2019-05-08 05:21:56 +08:00
|
|
|
lib14a.disconnect()
|
2019-03-09 17:34:43 +08:00
|
|
|
|
2019-04-29 05:43:55 +08:00
|
|
|
local filename, err = utils.WriteDumpFile(info.uid, blockData)
|
2019-03-09 17:34:43 +08:00
|
|
|
if err then return oops(err) end
|
|
|
|
|
2019-04-29 00:46:06 +08:00
|
|
|
print(string.format('\nDumped data into %s', filename))
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
|
2017-01-30 23:51:07 +08:00
|
|
|
-------------------------
|
2019-03-09 17:34:43 +08:00
|
|
|
-- Testing
|
2017-01-30 23:51:07 +08:00
|
|
|
-------------------------
|
|
|
|
function selftest()
|
2019-03-09 17:34:43 +08:00
|
|
|
DEBUG = true
|
2019-04-29 00:46:06 +08:00
|
|
|
dbg('Performing test')
|
2019-03-09 17:34:43 +08:00
|
|
|
main()
|
2019-04-29 00:46:06 +08:00
|
|
|
dbg('Tests done')
|
2017-01-30 23:51:07 +08:00
|
|
|
end
|
2019-03-09 17:34:43 +08:00
|
|
|
-- Flip the switch here to perform a sanity check.
|
2017-01-30 23:51:07 +08:00
|
|
|
-- It read a nonce in two different ways, as specified in the usage-section
|
2019-04-29 00:46:06 +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
|