proxmark3/client/luascripts/hf_14a_raw.lua

204 lines
5.4 KiB
Lua
Raw Normal View History

local cmds = require('commands')
local getopt = require('getopt')
local lib14a = require('read14a')
2020-04-05 16:05:14 +08:00
local ansicolors = require('ansicolors')
2019-05-08 05:12:58 +08:00
copyright = ''
author = "Martin Holst Swende"
2020-04-05 16:05:14 +08:00
version = 'v1.0.2'
2019-05-08 05:12:58 +08:00
desc = [[
2020-09-23 06:28:33 +08:00
This is a script to allow raw 14443a commands to be sent and received.
2019-05-08 05:12:58 +08:00
]]
example = [[
# 1. Connect and don't disconnect
2020-10-01 06:29:24 +08:00
script run hf_14a_raw -k
2019-05-08 07:35:51 +08:00
2019-05-08 05:12:58 +08:00
# 2. Send mf auth, read response (nonce)
2020-10-01 06:29:24 +08:00
script run hf_14a_raw -o -x 6000F57b -k
2019-05-08 07:35:51 +08:00
2019-05-08 05:12:58 +08:00
# 3. disconnect
script run hf_14a_raw -o
2019-05-08 05:12:58 +08:00
# All three steps in one go:
script run hf_14a_raw -x 6000F57b
2019-05-08 05:12:58 +08:00
]]
2019-05-08 07:35:51 +08:00
usage = [[
script run hf_14a_raw -x 6000F57b
2020-04-05 16:05:14 +08:00
]]
arguments = [[
2020-10-01 06:29:24 +08:00
-o do not connect - use this only if you previously used -k to stay connected
2019-03-09 17:34:43 +08:00
-r do not read response
-c calculate and append CRC
2020-10-01 06:29:24 +08:00
-k stay connected - dont inactivate the field
2019-03-09 17:34:43 +08:00
-x <payload> Data to send (NO SPACES!)
-d Debug flag
-t Topaz mode
-3 ISO14443-4 (use RATS)
]]
--[[
2019-03-09 17:34:43 +08:00
This script communicates with
/armsrc/iso14443a.c, specifically ReaderIso14443a() at around line 1779 and onwards.
2019-03-09 17:34:43 +08:00
Check there for details about data format and how commands are interpreted on the
device-side.
]]
-- Some globals
local DEBUG = false -- the debug flag
-------------------------------
2019-03-09 17:34:43 +08:00
-- Some utilities
-------------------------------
2019-03-09 17:34:43 +08:00
---
-- A debug printout-function
local function dbg(args)
2019-05-08 05:12:58 +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
---
-- This is only meant to be used when errors occur
local function oops(err)
2019-05-08 05:12:58 +08:00
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
2019-03-09 17:34:43 +08:00
---
-- Usage help
local function help()
2019-05-08 05:12:58 +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)
2019-05-08 05:12:58 +08:00
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)
end
2019-03-09 17:34:43 +08:00
---
-- The main entry point
function main(args)
2019-03-09 17:34:43 +08:00
if args == nil or #args == 0 then return help() end
local ignore_response = false
local append_crc = false
local stayconnected = false
local payload = nil
local doconnect = true
local topaz_mode = false
local no_rats = false
-- Read the parameters
for o, a in getopt.getopt(args, 'orcpx:dt3') do
2019-05-08 05:12:58 +08:00
if o == 'o' then doconnect = false end
if o == 'r' then ignore_response = true end
if o == 'c' then append_crc = true end
if o == 'p' then stayconnected = true end
if o == 'x' then payload = a end
if o == 'd' then DEBUG = true end
if o == 't' then topaz_mode = true end
if o == '3' then no_rats = true end
2019-03-09 17:34:43 +08:00
end
-- First of all, connect
if doconnect then
dbg("doconnect")
2019-05-08 05:12:58 +08:00
info, err = lib14a.read(true, no_rats)
if err then
lib14a.disconnect()
return oops(err)
end
print(('Connected to card, uid = %s'):format(info.uid))
2019-03-09 17:34:43 +08:00
end
-- The actual raw payload, if any
if payload then
2019-05-08 05:12:58 +08:00
res, err = sendRaw(payload,{ignore_response = ignore_response, topaz_mode = topaz_mode, append_crc = append_crc})
2019-05-08 07:35:51 +08:00
if err then
2019-05-08 05:12:58 +08:00
lib14a.disconnect()
return oops(err)
end
2019-03-09 17:34:43 +08:00
if not ignoreresponse then
-- Display the returned data
showdata(res)
end
end
-- And, perhaps disconnect?
if not stayconnected then
2019-05-08 05:12:58 +08:00
lib14a.disconnect()
2019-03-09 17:34:43 +08:00
end
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
-- reads the number of bytes specified in arg1 (arg0 in c-struct)
-- and displays the data
-- @param usbpacket the data received from the device
function showdata(usbpacket)
2019-03-09 17:34:43 +08:00
local cmd_response = Command.parse(usbpacket)
local len = tonumber(cmd_response.arg1) *2
--print("data length:",len)
local data = string.sub(tostring(cmd_response.data), 0, len);
print("<< ",data)
end
function sendRaw(rawdata, options)
2019-05-08 05:12:58 +08:00
print('>> ', rawdata)
2019-03-09 17:34:43 +08:00
local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW
if options.topaz_mode then
flags = flags + lib14a.ISO14A_COMMAND.ISO14A_TOPAZMODE
end
if options.append_crc then
flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC
end
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-05-08 05:12:58 +08:00
return command:sendMIX(options.ignore_response)
end
-------------------------
2019-03-09 17:34:43 +08:00
-- Testing
-------------------------
function selftest()
2019-03-09 17:34:43 +08:00
DEBUG = true
2019-05-08 05:12:58 +08:00
dbg('Performing test')
2019-03-09 17:34:43 +08:00
main()
2020-10-01 06:29:24 +08:00
main('-k')
main(' -o -x 6000F57b -k')
2019-05-08 05:12:58 +08:00
main('-o')
main('-x 6000F57b')
dbg('Tests done')
end
2019-03-09 17:34:43 +08:00
-- Flip the switch here to perform a sanity check.
-- It read a nonce in two different ways, as specified in the usage-section
2019-05-08 05:12:58 +08:00
if '--test'==args then
2019-03-09 17:34:43 +08:00
selftest()
else
-- Call the main
main(args)
end