From bd20f7cc81c9fa4a250518c7ece80812d2bd56a2 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 29 Aug 2026 16:31:01 +0200 Subject: [PATCH] OLD/MIX convert to NG: Phase D. reworked all lua scripts to handle and behavie same way. Thanks Claude --- client/lualibs/commands.lua | 174 +++++++++++-------- client/lualibs/read14a.lua | 31 ++-- client/lualibs/read15.lua | 45 ++--- client/luascripts/hf_14a_aztek.lua | 14 +- client/luascripts/hf_14a_protectimus_nfc.lua | 34 ++-- client/luascripts/hf_14a_raw.lua | 22 ++- client/luascripts/hf_14a_read_ltocm.lua | 19 +- client/luascripts/hf_i2c_plus_2k_utils.lua | 13 +- client/luascripts/hf_mf_tnp3_clone.lua | 11 +- client/luascripts/hf_mf_tnp3_sim.lua | 7 +- client/luascripts/hf_mf_ultimatecard.lua | 13 +- client/luascripts/hf_mf_uscuid_prog.lua | 17 +- client/luascripts/hf_mfp_raw.lua | 15 +- client/luascripts/hf_mfu_magicwrite.lua | 14 +- client/luascripts/hf_mfu_ultra.lua | 17 +- client/luascripts/hf_ndef_dump.lua | 21 ++- client/luascripts/ntag_getsig.lua | 15 +- client/luascripts/ntag_hammertime.lua | 15 +- 18 files changed, 249 insertions(+), 248 deletions(-) diff --git a/client/lualibs/commands.lua b/client/lualibs/commands.lua index bf5f4f6b5..1c6bdfffe 100644 --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@ -50,29 +50,6 @@ Command = { o.data = data return o end, - newMIX = function(self, o) - - local o = o or {} -- create object if user does not provide one - setmetatable(o, self) -- DIY inheritance a'la javascript - self.__index = self - - o.cmd = o.cmd or _commands.CMD_UNKNOWN - o.arg1 = o.arg1 or 0 - o.arg2 = o.arg2 or 0 - o.arg3 = o.arg3 or 0 - local data = o.data or '' - - if (type(data) == 'string') then - if (#data > 1024) then - -- OOps, a bit too much data here - print( ( "WARNING: data size too large, was %s chars, will be truncated "):format( #data) ) - -- - data = data:sub(1, 1024) - end - end - o.data = data - return o - end, newNG = function(self, o) local o = o or {} -- create object if user does not provide one @@ -93,12 +70,6 @@ Command = { o.data = data return o end, - parse = function(packet) - local count, cmd, arg1, arg2, arg3, data = bin.unpack('LLLL', packet) - local length = #packet - count + 1 - count, data = bin.unpack('H'..length, packet, count) - return Command:new{cmd = cmd, arg1 = arg1, arg2 = arg2, arg3 = arg3, data = data} - end } -- commented out, not used. function Command:__tostring() @@ -111,55 +82,108 @@ function Command:__tostring() return output end -function Command:getBytes() - --If a hex-string has been used - local data = self.data - local cmd = self.cmd - local arg1, arg2, arg3 = self.arg1, self.arg2, self.arg3 - return bin.pack("LLLLH",cmd, arg1, arg2, arg3, data); + + + + + + + +--- Raw command support, uniform across card technologies. +-- +-- The device takes a different struct per technology, but the Lua side is the +-- same for all of them. Build with Command:newRaw{} using these named fields: +-- +-- tech '14a' | '14b' | '15' which technology +-- flags transport flag bitmask (ISO14A_* / ISO14B_* / ISO15_*) +-- data payload as a hex string optional, defaults to empty +-- timeout device-side timeout 14a and 14b only, ignored for 15 +-- wait_us pre-transmit delay 14a only, ignored elsewhere +-- lenbits send this many bits 14a only, ignored elsewhere +-- +-- The payload length is always derived from `data`, never passed separately. +-- +-- On the wire these become, little endian: +-- 14a iso14a_raw_cmd_t u32 flags, u32 timeout, u32 wait_us, u16 len, u16 lenbits, u8 data[] +-- 14b iso14b_raw_cmd_t u16 flags, u32 timeout, u16 rawlen, u8 raw[] +-- 15 iso15_raw_cmd_t u8 flags, u16 rawlen, u8 raw[] + +--- hex string -> raw byte string +function fromhex(hexstr) + return (hexstr:gsub('%x%x', function(cc) return string.char(tonumber(cc, 16)) end)) end ---- Sends a packet to the device --- @param command - the usb packet to send --- @param ignoreresponse - if set to true, we don't read the device answer packet --- which is usually recipe for fail. If not sent, the host will wait 2s for a --- response of type CMD_ACK or like NG use the CMD as ack.. --- @return packet,nil if successful --- nil, errormessage if unsuccessful -function Command:sendMIX( ignore_response, timeout, use_cmd_ack) - if timeout == nil then timeout = TIMEOUT end - local data = self.data - local cmd = self.cmd - local arg1, arg2, arg3 = self.arg1, self.arg2, self.arg3 - - local err, msg = core.SendCommandMIX(cmd, arg1, arg2, arg3, data) - if err == nil then return err, msg end - if ignore_response then return true, nil end - - local ack = _commands.CMD_ACK - if use_cmd_ack then - ack = cmd - end - - local response, msg = core.WaitForResponseTimeout(ack, timeout) - if response == nil then - return nil, 'Error, waiting for response timed out :: '..msg - end - -- lets digest - data = nil - cmd = nil - arg1 = nil - arg2 = nil - arg3 = nil - local count, length, magic, status, reason, crc, ng - --- S S I c c S L L L - count, cmd, length, magic, status, reason, crc, arg1, arg2, arg3 = bin.unpack('SSIccSLLL', response) - count, data, ng = bin.unpack('H'..length..'C', response, count) - - local packed = bin.pack("LLLLH", cmd, arg1, arg2, arg3, data) - return packed, nil; +--- raw byte string -> uppercase hex string +function tohex(raw) + return (raw:gsub('.', function(c) return string.format('%02X', string.byte(c)) end)) end + +-- little endian hex for a value of n bytes +local function le(value, nbytes) + local out = '' + local v = value or 0 + for _ = 1, nbytes do + out = out .. string.format('%02X', v % 256) + v = math.floor(v / 256) + end + return out +end + +local RAW_CMD = { + ['14a'] = _commands.CMD_HF_ISO14443A_READER, + ['14b'] = _commands.CMD_HF_ISO14443B_COMMAND, + ['15'] = _commands.CMD_HF_ISO15693_COMMAND, +} + +--- Build a raw command for any of 14a / 14b / 15. +-- @return a Command ready for :sendNG() +function Command:newRaw(o) + o = o or {} + local tech = o.tech or '14a' + local cmd = RAW_CMD[tech] + if cmd == nil then + error('Command:newRaw - unknown tech "'..tostring(tech)..'", expected 14a, 14b or 15') + end + + local data = o.data or '' + local len = #data / 2 + + local hdr + if tech == '14a' then + hdr = le(o.flags, 4) .. le(o.timeout, 4) .. le(o.wait_us, 4) .. le(len, 2) .. le(o.lenbits, 2) + elseif tech == '14b' then + hdr = le(o.flags, 2) .. le(o.timeout, 4) .. le(len, 2) + else + hdr = le(o.flags, 1) .. le(len, 2) + end + + local c = Command:newNG{ cmd = cmd, data = hdr .. data } + c.tech = tech + return c +end + +--- Unpack the reply to a raw command, uniform across technologies. +-- @param tech '14a' | '14b' | '15' +-- @param resp the table returned by :sendNG() +-- @return len, extra, data +-- len number of payload bytes +-- extra 14a: the select status on a CONNECT, otherwise nil +-- data payload as a raw byte string (use tohex() to print it) +function parseRaw(tech, resp) + if resp == nil or type(resp) ~= 'table' or resp.Data == nil then + return nil, nil, nil + end + if tech == '14a' then + -- iso14a_raw_resp_t: u16 len, u8 sel, u8 rfu, u8 data[] + if #resp.Data < 8 then return nil, nil, nil end + local len = tonumber(resp.Data:sub(1, 2), 16) + (tonumber(resp.Data:sub(3, 4), 16) * 256) + local sel = tonumber(resp.Data:sub(5, 6), 16) + return len, sel, fromhex(resp.Data:sub(9, 8 + (len * 2))) + end + -- 14b and 15 answer with the payload only + return resp.Length, nil, fromhex(resp.Data:sub(1, resp.Length * 2)) +end + function Command:sendNG( ignore_response, timeout ) if timeout == nil then timeout = TIMEOUT end local data = self.data diff --git a/client/lualibs/read14a.lua b/client/lualibs/read14a.lua index f1b5b1634..09382bf5a 100644 --- a/client/lualibs/read14a.lua +++ b/client/lualibs/read14a.lua @@ -87,26 +87,27 @@ end local function read14443a(dont_disconnect, no_rats) local command, result, info, err, data - command = Command:newMIX{ - cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = ISO14A_COMMAND.ISO14A_CONNECT + ISO14A_COMMAND.ISO14A_CLEARTRACE - } - + local flags = ISO14A_COMMAND.ISO14A_CONNECT + ISO14A_COMMAND.ISO14A_CLEARTRACE if dont_disconnect then - command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT + flags = flags + ISO14A_COMMAND.ISO14A_NO_DISCONNECT end if no_rats then - command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_RATS + flags = flags + ISO14A_COMMAND.ISO14A_NO_RATS end - local result, err = command:sendMIX() + command = Command:newRaw{ tech = '14a', flags = flags } + + local result, err = command:sendNG() if result then - local count, cmd, arg1, arg2, arg3 = bin.unpack('LLLL',result) - if arg1 == 0 then + -- on a CONNECT the extra value is the select status + local rlen, sel, carddata = parseRaw('14a', result) + if rlen == nil then + return nil, 'malformed response from device' + end + if sel == 0 then return nil, 'iso14443a card select failed' end - data = string.sub(result, count) - info = parse14443a(data) + info = parse14443a(carddata) else err = 'No response from card' end @@ -134,10 +135,10 @@ end -- Sends an instruction to do nothing, only disconnect local function disconnect14443a() - local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER} - -- We can ignore the response here, no ACK is returned for this command + -- We can ignore the response here, no reply is returned for this command -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details - return c:sendMIX(true) + local c = Command:newRaw{ tech = '14a' } + return c:sendNG(true) end local library = { diff --git a/client/lualibs/read15.lua b/client/lualibs/read15.lua index 9d4ca589d..b8ef0e796 100644 --- a/client/lualibs/read15.lua +++ b/client/lualibs/read15.lua @@ -23,6 +23,17 @@ local ISO15_COMMAND = { ISO15_REQ_NONINVENTORY = 0, } +-- pm3 transport flags, iso15_command_t in include/iso15.h +local PM3_ISO15 = { + CONNECT = 1, + NO_DISCONNECT = 2, + RAW = 4, + APPEND_CRC = 8, + HIGH_SPEED = 16, + READ_RESPONSE = 32, + LONG_WAIT = 64, +} + local function errorString15693(number) local errors = {} errors[0x01] = "The command is not supported" @@ -94,28 +105,23 @@ local function read15693(slow, dont_readresponse) data = utils.Crc15("260100") - command = Command:newMIX{ - cmd = cmds.CMD_HF_ISO15693_COMMAND, - arg1 = #data / 2, - arg2 = 1, - arg3 = 1, - data = data - } - - if slow then - command.arg2 = 0 + local flags = PM3_ISO15.CONNECT + PM3_ISO15.RAW + if not slow then + flags = flags + PM3_ISO15.HIGH_SPEED end - if dont_readresponse then - command.arg3 = 0 + if not dont_readresponse then + flags = flags + PM3_ISO15.READ_RESPONSE end - local result, err = command:sendMIX() + command = Command:newRaw{ tech = '15', flags = flags, data = data } + + local result, err = command:sendNG() if result then - local count, cmd, len, arg2, arg3 = bin.unpack('LLLL', result) - if len == 0 then + local len, _, raw = parseRaw('15', result) + if len == nil or len == 0 then return nil, 'iso15693 card select failed' end - data = string.sub(result, count, count+len-1) + data = raw info, err = parse15693(data) else err = 'No response from card' @@ -144,10 +150,9 @@ end -- Sends an instruction to do nothing, only disconnect local function disconnect15693() - local c = Command:newMIX{cmd = cmds.CMD_HF_ISO15693_COMMAND} - -- We can ignore the response here, no ACK is returned for this command - -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details - return c:sendMIX(true) + -- We can ignore the response here, no reply is returned for this command + local c = Command:newRaw{ tech = '15' } + return c:sendNG(true) end local library = { diff --git a/client/luascripts/hf_14a_aztek.lua b/client/luascripts/hf_14a_aztek.lua index c63a9f85c..18ff12f79 100644 --- a/client/luascripts/hf_14a_aztek.lua +++ b/client/luascripts/hf_14a_aztek.lua @@ -100,14 +100,9 @@ function sendRaw(rawdata, options) + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC + lib14a.ISO14A_COMMAND.ISO14A_NO_RATS - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - 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} + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } - return command:sendMIX(options.ignore_response) + return command:sendNG(options.ignore_response) end --- -- The main entry point @@ -146,8 +141,9 @@ function main(args) return oops(err) end - local cmd_response = Command.parse(res) - local len = tonumber(cmd_response.arg1) * 2 + local _rlen, _sel, _raw = parseRaw('14a', res) + local cmd_response = { arg1 = _rlen or 0, data = tohex(_raw or '') } + local len = (_rlen or 0) * 2 local data = string.sub(tostring(cmd_response.data), 0, len-4) showdata(block, data) diff --git a/client/luascripts/hf_14a_protectimus_nfc.lua b/client/luascripts/hf_14a_protectimus_nfc.lua index 2ff3fec06..c31432985 100644 --- a/client/luascripts/hf_14a_protectimus_nfc.lua +++ b/client/luascripts/hf_14a_protectimus_nfc.lua @@ -106,19 +106,9 @@ function sendRaw(rawdata, options) + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC - local command = Command:newMIX{ - cmd = cmds.CMD_HF_ISO14443A_READER, + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } - -- arg1 is the defined flags for sending "raw" ISO 14443A package - arg1 = flags, - - -- arg2 contains the length, which is half the length of the ASCII - -- string data - arg2 = string.len(rawdata) / 2, - data = rawdata - } - - return command:sendMIX(options.ignore_response) + return command:sendNG(options.ignore_response) end -- Read the current one-time password (OTP) @@ -139,8 +129,9 @@ function readOTP(show_output) end -- parse the response - local cmd_response = Command.parse(res) - local len = tonumber(cmd_response.arg1) * 2 + local _rlen, _sel, _raw = parseRaw('14a', res) + local cmd_response = { arg1 = _rlen or 0, data = tohex(_raw or '') } + local len = (_rlen or 0) * 2 local data = string.sub(tostring(cmd_response.data), 0, len - 4) -- check the response @@ -185,8 +176,9 @@ function readInfo(show_output) end -- parse the response - local cmd_response = Command.parse(res) - local len = tonumber(cmd_response.arg1) * 2 + local _rlen, _sel, _raw = parseRaw('14a', res) + local cmd_response = { arg1 = _rlen or 0, data = tohex(_raw or '') } + local len = (_rlen or 0) * 2 local data = string.sub(tostring(cmd_response.data), 0, len - 4) -- check the response @@ -263,8 +255,9 @@ function bruteforceCommands() end -- parse the response - local cmd_response = Command.parse(res) - local len = tonumber(cmd_response.arg1) * 2 + local _rlen, _sel, _raw = parseRaw('14a', res) + local cmd_response = { arg1 = _rlen or 0, data = tohex(_raw or '') } + local len = (_rlen or 0) * 2 local data = string.sub(tostring(cmd_response.data), 0, len - 4) -- check the response @@ -308,8 +301,9 @@ function setTime(time, otp_interval) end -- parse the response - local cmd_response = Command.parse(res) - local len = tonumber(cmd_response.arg1) * 2 + local _rlen, _sel, _raw = parseRaw('14a', res) + local cmd_response = { arg1 = _rlen or 0, data = tohex(_raw or '') } + local len = (_rlen or 0) * 2 local data = string.sub(tostring(cmd_response.data), 0, len - 4) end diff --git a/client/luascripts/hf_14a_raw.lua b/client/luascripts/hf_14a_raw.lua index 79bf47ad4..7576a33ce 100644 --- a/client/luascripts/hf_14a_raw.lua +++ b/client/luascripts/hf_14a_raw.lua @@ -151,11 +151,14 @@ end -- and displays the data -- @param usbpacket the data received from the device function showdata(usbpacket) - 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) + -- iso14a_raw_resp_t: u16 len, u8 sel, u8 rfu, u8 data[] + local rlen, sel, raw = parseRaw('14a', usbpacket) + if rlen == nil then + print("<< ", "malformed response") + return + end + local hex = tohex(raw) + print("<< ", hex) end function sendRaw(rawdata, options) @@ -170,13 +173,8 @@ function sendRaw(rawdata, options) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - 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} - return command:sendMIX(options.ignore_response) + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } + return command:sendNG(options.ignore_response) end diff --git a/client/luascripts/hf_14a_read_ltocm.lua b/client/luascripts/hf_14a_read_ltocm.lua index 626080065..1560062de 100644 --- a/client/luascripts/hf_14a_read_ltocm.lua +++ b/client/luascripts/hf_14a_read_ltocm.lua @@ -83,26 +83,21 @@ local function sendRaw(rawdata, options) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC end - local arg2 = #rawdata / 2 + local lenbits = 0 if options.bits7 then - arg2 = arg2 + tonumber(lshift(7, 16)) + lenbits = 7 end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, -- Send raw - -- arg2 contains the length, which is half the length - -- of the ASCII-string rawdata - arg2 = arg2, - data = rawdata} - return command:sendMIX(options.ignore_response) + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata, lenbits = lenbits } + return command:sendNG(options.ignore_response) end --- -- get hex data from response local function getdata(usbpacket) - local cmd_response = Command.parse(usbpacket) - local len = tonumber(cmd_response.arg1) * 2 - return string.sub(tostring(cmd_response.data), 0, len) + local _rlen, _sel, _raw = parseRaw('14a', usbpacket) + if _rlen == nil then return nil end + return tohex(_raw) end --- diff --git a/client/luascripts/hf_i2c_plus_2k_utils.lua b/client/luascripts/hf_i2c_plus_2k_utils.lua index 5e7652316..60f542619 100644 --- a/client/luascripts/hf_i2c_plus_2k_utils.lua +++ b/client/luascripts/hf_i2c_plus_2k_utils.lua @@ -114,12 +114,9 @@ local function sendRaw(rawdata, options) + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC - local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, - arg2 = string.len(rawdata)/2, - data = rawdata} + local c = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } - return c:sendMIX(options.ignore_response) + return c:sendNG(options.ignore_response) end --- @@ -152,9 +149,9 @@ end --- Function to get response data local function getResponseData(usbpacket) - local resp = Command.parse(usbpacket) - local len = tonumber(resp.arg1) * 2 - return string.sub(tostring(resp.data), 0, len); + local _rlen, _sel, _raw = parseRaw('14a', usbpacket) + if _rlen == nil then return nil end + return tohex(_raw) end --- diff --git a/client/luascripts/hf_mf_tnp3_clone.lua b/client/luascripts/hf_mf_tnp3_clone.lua index 22c626e80..a589d1bdb 100644 --- a/client/luascripts/hf_mf_tnp3_clone.lua +++ b/client/luascripts/hf_mf_tnp3_clone.lua @@ -91,12 +91,11 @@ end local function readmagicblock( blocknum ) -- Read block N local CSETBLOCK_SINGLE_OPERATION = 0x1F - local c = Command:newMIX{ - cmd = cmds.CMD_HF_MIFARE_CGETBL - , arg1 = CSETBLOCK_SINGLE_OPERATION - , arg3 = blocknum - } - local b, err = getblockdata(c:sendMIX()) + -- mf_chinese_blk_t { u8 params, u8 blockno, u8 data[] } + -- mf_chinese_blk_t { u8 params, u8 blockno, u8 data[] } + local payload = ('%02X%02X'):format(CSETBLOCK_SINGLE_OPERATION, blocknum) + local c = Command:newNG{ cmd = cmds.CMD_HF_MIFARE_CGETBL, data = payload } + local b, err = getblockdata(c:sendNG()) if not b then return oops(err) end return b end diff --git a/client/luascripts/hf_mf_tnp3_sim.lua b/client/luascripts/hf_mf_tnp3_sim.lua index f04c8d7fa..3ccfc854d 100644 --- a/client/luascripts/hf_mf_tnp3_sim.lua +++ b/client/luascripts/hf_mf_tnp3_sim.lua @@ -244,8 +244,11 @@ local function LoadEmulator(uid, blocks) io.write( _..',') io.flush() core.clearCommandBuffer() - cmd = Command:newMIX{cmd = cmds.CMD_HF_MIFARE_EML_MEMSET, arg1 = _ ,arg2 = 1,arg3 = 16, data = blockdata} - local err, msg = cmd:sendMIX(true) + -- payload: u16 blockno, u8 blockcnt, u8 blockwidth, u8 data[] + -- payload: u16 blockno, u8 blockcnt, u8 blockwidth, u8 data[] + local eml_payload = ('%02X%02X%02X%02X'):format(_ % 256, math.floor(_ / 256) % 256, 1, 16) .. blockdata + cmd = Command:newNG{cmd = cmds.CMD_HF_MIFARE_EML_MEMSET, data = eml_payload} + local err, msg = cmd:sendNG(true) if err == nil then return err, msg end end io.write('\n') diff --git a/client/luascripts/hf_mf_ultimatecard.lua b/client/luascripts/hf_mf_ultimatecard.lua index ca8d2609a..f9e159c79 100644 --- a/client/luascripts/hf_mf_ultimatecard.lua +++ b/client/luascripts/hf_mf_ultimatecard.lua @@ -144,20 +144,17 @@ end -- reads the number of bytes specified in arg1 (arg0 in c-struct) -- @param usbpacket the data received from the device local function getResponseData(usbpacket) - local resp = Command.parse(usbpacket) - local len = tonumber(resp.arg1) * 2 - return string.sub(tostring(resp.data), 0, len); + local _rlen, _sel, _raw = parseRaw('14a', usbpacket) + if _rlen == nil then return nil end + return tohex(_raw) end --- local function sendRaw(rawdata, options) local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC - local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, - arg2 = string.len(rawdata)/2, - data = rawdata} - return c:sendMIX(options.ignore_response) + local c = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } + return c:sendNG(options.ignore_response) end --- local function send(payload) diff --git a/client/luascripts/hf_mf_uscuid_prog.lua b/client/luascripts/hf_mf_uscuid_prog.lua index d0afe351e..742ce8de9 100644 --- a/client/luascripts/hf_mf_uscuid_prog.lua +++ b/client/luascripts/hf_mf_uscuid_prog.lua @@ -198,19 +198,14 @@ end local function sendRaw(rawdata, keep) flags = lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC if keep == true then flags = flags + lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, -- Send raw - arg2 = string.len(rawdata) / 2, -- arg2 contains the length, which is half the length of the ASCII-string rawdata - data = rawdata - } + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } local ignore_response = false - local result, err = command:sendMIX(ignore_response) + local result, err = command:sendNG(ignore_response) if result then - --local count,cmd,arg1,arg2,arg3,data = bin.unpack('LLLLH512',result) - local p = command.parse(result) - arg1 = p["arg1"] - data = p["data"] - returned_bytes = string.sub(data, 1, arg1 * 2) + local rlen, sel, raw = parseRaw('14a', result) + if rlen == nil then return nil end + local data = tohex(raw) + returned_bytes = data if #returned_bytes > 0 then return returned_bytes else return nil end end end diff --git a/client/luascripts/hf_mfp_raw.lua b/client/luascripts/hf_mfp_raw.lua index 28fec1474..27238bb65 100644 --- a/client/luascripts/hf_mfp_raw.lua +++ b/client/luascripts/hf_mfp_raw.lua @@ -76,18 +76,17 @@ local function sendRaw(rawdata, crc, power) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, -- Send raw - arg2 = string.len(rawdata) / 2, -- arg2 contains the length, which is half the length of the ASCII-string rawdata - data = rawdata - } + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } local ignore_response = false - local result, err = command:sendMIX(ignore_response) + local result, err = command:sendNG(ignore_response) if result then --unpack the first 4 parts of the result as longs, and the last as an extremely long string to later be cut down based on arg1, the number of bytes returned - local count,cmd,arg1,arg2,arg3,data = bin.unpack('LLLLH512',result) + -- iso14a_raw_resp_t: u16 len, u8 sel, u8 rfu, u8 data[] + local rlen, sel, raw = parseRaw('14a', result) + if rlen == nil then return nil end + local data = tohex(raw) - returned_bytes = string.sub(data, 1, arg1 * 2) + returned_bytes = data if #returned_bytes > 0 then print((": %s"):format(returned_bytes)) -- need to multiply by 2 because the hex digits are actually two bytes when they are strings return returned_bytes diff --git a/client/luascripts/hf_mfu_magicwrite.lua b/client/luascripts/hf_mfu_magicwrite.lua index 1fed9b934..97ef8fb9c 100644 --- a/client/luascripts/hf_mfu_magicwrite.lua +++ b/client/luascripts/hf_mfu_magicwrite.lua @@ -115,9 +115,9 @@ end -- reads the number of bytes specified in arg1 (arg0 in c-struct) -- @param usbpacket the data received from the device local function getResponseData(usbpacket) - local resp = Command.parse(usbpacket) - local len = tonumber(resp.arg1) * 2 - return string.sub(tostring(resp.data), 0, len); + local _rlen, _sel, _raw = parseRaw('14a', usbpacket) + if _rlen == nil then return nil end + return tohex(_raw) end --- -- @@ -127,13 +127,9 @@ local function sendRaw(rawdata, options) + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC - local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, - -- arg2 contains the length, which is half the length of the ASCII-string rawdata - arg2 = string.len(rawdata)/2, - data = rawdata} + local c = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } - return c:sendMIX(options.ignore_response) + return c:sendNG(options.ignore_response) end --- -- diff --git a/client/luascripts/hf_mfu_ultra.lua b/client/luascripts/hf_mfu_ultra.lua index 93dd5bcec..9c78ba3d3 100644 --- a/client/luascripts/hf_mfu_ultra.lua +++ b/client/luascripts/hf_mfu_ultra.lua @@ -59,9 +59,9 @@ end --- Parses response data local function parseResponse(rawResponse) - local resp = Command.parse(rawResponse) - local len = tonumber(resp.arg1) * 2 - return string.sub(tostring(resp.data), 0, len); + local _rlen, _sel, _raw = parseRaw('14a', rawResponse) + if _rlen == nil then return nil end + return tohex(_raw) end --- @@ -86,16 +86,13 @@ local function sendRaw(rawdata, options) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC end - local arg2 = #rawdata / 2 + local lenbits = 0 if options.bits7 then - arg2 = arg2 | tonumber(bit32.lshift(7, 16)) + lenbits = 7 end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, - arg2 = arg2, - data = rawdata} - return command:sendMIX(options.ignore_response) + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata, lenbits = lenbits } + return command:sendNG(options.ignore_response) end --- diff --git a/client/luascripts/hf_ndef_dump.lua b/client/luascripts/hf_ndef_dump.lua index ae0718617..ee2f347a3 100644 --- a/client/luascripts/hf_ndef_dump.lua +++ b/client/luascripts/hf_ndef_dump.lua @@ -74,10 +74,10 @@ end -- -- Sends an instruction to do nothing, only disconnect function disconnect() - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, arg1 = 0,} + local command = Command:newRaw{ tech = '14a', flags = 0 } -- We can ignore the response here, no ACK is returned for this command -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details - return command:sendMIX(true) + return command:sendNG(true) end --- -- @@ -86,9 +86,11 @@ local function getblockdata(response) return nil, 'No response from device' end - local count, cmd, arg0, arg1, arg2, data = bin.unpack('LLLLH40', response) - if arg0 == 1 then - return data:sub(1, 32) + if response == nil or type(response) ~= 'table' then + return nil, "malformed response from device" + end + if response.Status == 0 and response.Length >= 16 then + return response.Data:sub(1, 32) end return nil, "Couldn't read block" @@ -98,8 +100,13 @@ end -- @return nil, errormessage if unsuccessful local function getBlock(blockno) local block, err - local c = Command:newMIX{cmd = cmds.CMD_HF_MIFAREU_READBL, arg1 = blockno, data = 0} - block, err = getblockdata(c:sendMIX(false)) + -- mful_readblock_t { bool use_schann, u8 block_no, u8 num_of_blocks, + -- u8 keytype, u8 keylen, u8 key[16] } + -- mful_readblock_t { bool use_schann, u8 block_no, u8 num_of_blocks, + -- u8 keytype, u8 keylen, u8 key[16] } + local rb_payload = ('%02X%02X%02X%02X%02X'):format(0, blockno, 1, 0, 0) .. string.rep('00', 16) + local c = Command:newNG{cmd = cmds.CMD_HF_MIFAREU_READBL, data = rb_payload} + block, err = getblockdata(c:sendNG(false)) if not block then return oops(err) end if #block < 32 then diff --git a/client/luascripts/ntag_getsig.lua b/client/luascripts/ntag_getsig.lua index a70cb8623..c2be1d98f 100644 --- a/client/luascripts/ntag_getsig.lua +++ b/client/luascripts/ntag_getsig.lua @@ -44,18 +44,17 @@ local function sendRaw(rawdata, crc, power) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, -- Send raw - arg2 = string.len(rawdata) / 2, -- arg2 contains the length, which is half the length of the ASCII-string rawdata - data = rawdata - } + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } local ignore_response = false - local result, err = command:sendMIX(ignore_response) + local result, err = command:sendNG(ignore_response) if result then --unpack the first 4 parts of the result as longs, and the last as an extremely long string to later be cut down based on arg1, the number of bytes returned - local count,cmd,arg1,arg2,arg3,data = bin.unpack('LLLLH512',result) + -- iso14a_raw_resp_t: u16 len, u8 sel, u8 rfu, u8 data[] + local rlen, sel, raw = parseRaw('14a', result) + if rlen == nil then return nil end + local data = tohex(raw) - returned_bytes = string.sub(data, 1, arg1 * 2) + returned_bytes = data if #returned_bytes > 0 then -- print((": %s"):format(returned_bytes)) -- need to multiply by 2 because the hex digits are actually two bytes when they are strings return returned_bytes diff --git a/client/luascripts/ntag_hammertime.lua b/client/luascripts/ntag_hammertime.lua index fc9168ae5..18e535069 100644 --- a/client/luascripts/ntag_hammertime.lua +++ b/client/luascripts/ntag_hammertime.lua @@ -64,18 +64,17 @@ local function sendRaw(rawdata, crc, power) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT end - local command = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER, - arg1 = flags, -- Send raw - arg2 = string.len(rawdata) / 2, -- arg2 contains the length, which is half the length of the ASCII-string rawdata - data = rawdata - } + local command = Command:newRaw{ tech = '14a', flags = flags, data = rawdata } local ignore_response = false - local result, err = command:sendMIX(ignore_response) + local result, err = command:sendNG(ignore_response) if result then --unpack the first 4 parts of the result as longs, and the last as an extremely long string to later be cut down based on arg1, the number of bytes returned - local count,cmd,arg1,arg2,arg3,data = bin.unpack('LLLLH512',result) + -- iso14a_raw_resp_t: u16 len, u8 sel, u8 rfu, u8 data[] + local rlen, sel, raw = parseRaw('14a', result) + if rlen == nil then return nil end + local data = tohex(raw) - returned_bytes = string.sub(data, 1, arg1 * 2) + returned_bytes = data if #returned_bytes > 0 then -- print((": %s"):format(returned_bytes)) -- need to multiply by 2 because the hex digits are actually two bytes when they are strings return returned_bytes