From c180c8df205d4c42411c783b0b9ef7f2f128a47a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 21:35:22 +0200 Subject: [PATCH 01/18] chg: hw ping - shouldnt be converted yet.. --- client/cmdhw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cmdhw.c b/client/cmdhw.c index 4eda2dc66..f1f011509 100644 --- a/client/cmdhw.c +++ b/client/cmdhw.c @@ -468,7 +468,7 @@ static int CmdPing(const char *Cmd) { (void)Cmd; // Cmd is not used so far clearCommandBuffer(); PacketResponseNG resp; - SendCommandNG(CMD_PING, NULL, 0); + SendCommandMIX(CMD_PING, 0, 0, 0, NULL, 0); if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) PrintAndLogEx(SUCCESS, "Ping " _GREEN_("successful")); else From df555be792d99cc5a184d36b1742183f0ffb83d9 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 21:49:45 +0200 Subject: [PATCH 02/18] chg: script run didump - use MIX --- client/scripts/didump.lua | 118 ++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 50 deletions(-) diff --git a/client/scripts/didump.lua b/client/scripts/didump.lua index b6bc03a11..990291b57 100644 --- a/client/scripts/didump.lua +++ b/client/scripts/didump.lua @@ -7,29 +7,36 @@ local lib14a = require('read14a') local json = require('dkjson') local toys = require('default_toys_di') -example =[[ +copyright = '' +author = 'Iceman' +version = 'v1.0.1' +desc = [[ +This is a script to dump and decrypt the data of a specific type of Mifare Mini token. +The dump is decrypted. If a raw dump is wanted, use the -r parameter +]] +example = [[ script run didump script run didump -t script run didump -r ]] -author = "Iceman" -usage = "script run didump -h -t" -desc = [[ -This is a script to dump and decrypt the data of a specific type of Mifare Mini token. -The dump is decrypted. If a raw dump is wanted, use the -r parameter +usage = [[ +script run didump -h -t -r + Arguments: - -h : this help - -r : raw - -t : selftest + h this helptext + r raw + t selftest ]] -local band=bit32.band -local bor=bit32.bor -local bnot=bit32.bnot -local bxor=bit32.bxor -local lsh=bit32.lshift -local rsh=bit32.rshift +-- Some shortcuts +local band = bit32.band +local bor = bit32.bor +local bnot = bit32.bnot +local bxor = bit32.bxor +local lsh = bit32.lshift +local rsh = bit32.rshift +-- Some globals local FOO = 'AF62D2EC0491968CC52A1A7165F865FE' local BAR = '286329204469736E65792032303133' local MIS = '0A14FD0507FF4BCD026BA83F0A3B89A9' @@ -44,29 +51,33 @@ local CHECKSUM_OFFSET = 12; -- +1??? -- A debug printout-function local function dbg(args) if not DEBUG then return end - if type(args) == "table" then + if type(args) == 'table' then local i = 1 while args[i] do - print("###", args[i]) + print('###', args[i]) i = i+1 end else - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ",err) + print('ERROR: ', err) core.clearCommandBuffer() - return false + return nil, err end --- -- Usage help local function help() + print(copyright) + print(author) + print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage) end --- -- @@ -370,21 +381,7 @@ local function updateChecksum(data) return string.format("%s%X", part, chksum) end --- --- receives the answer from deviceside, used with a readblock command -local function waitCmd() - local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT) - if response then - local count,cmd,arg0 = bin.unpack('LL',response) - if(arg0==1) then - local count,arg1,arg2,data = bin.unpack('LLH511',response,count) - return data:sub(1,32) - else - return nil, "Couldn't read block.." - end - end - return nil, "No response from device" -end - +-- local function keygen(uid) local data = MIS..uid..BAR local hash = utils.ConvertAsciiToBytes(utils.Sha1Hex(data)) @@ -397,7 +394,6 @@ local function keygen(uid) hash[6+1] ) end - --- encode 'table' into a json formatted string -- local function convert_to_json( obj ) @@ -449,6 +445,29 @@ local function create_key(uid) key = key..utils.SwapEndiannessStr( sha:sub(25,32), 32 ) return key end +--- +-- decode response and get the blockdata from a normal mifare read command +local function getblockdata(response) + if not response then + return nil, 'No response from device' + end + + local count, cmd, arg0 = bin.unpack('LL', response) + if arg0 == 1 then + local count, arg1, arg2, data = bin.unpack('LLH511', response, count) + return data:sub(1, 32) + else + return nil, "Couldn't read block.. ["..arg0.."]" + end +end + +local function readblock( blocknum, key ) + -- Read block N + local c = Command:newMIX{cmd = cmds.CMD_MIFARE_READBL, arg1 = blocknum, data = key} + local b, err = getblockdata(c:sendMIX()) + if not b then return oops(err) end + return b +end --- reads all blocks from tag -- local function readtag(mfkey, aeskey ) @@ -463,11 +482,8 @@ local function readtag(mfkey, aeskey ) end -- read block from tag. - cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = mfkey} - local err = core.SendCommand(cmd:getBytes()) - if err then return oops(err) end - local blockdata, err = waitCmd() - if err then return oops(err) end + local blockdata = readblock(blockNo, mfkey) + if not blockdata then return oops('[!] failed reading block') end -- rules: -- the following blocks is NOT encrypted @@ -488,7 +504,6 @@ local function readtag(mfkey, aeskey ) else -- Sectorblocks, not encrypted, but we add our known key to it since it is normally zeros. blockdata = mfkey..blockdata:sub(13,20)..mfkey - --dbg(blockdata:sub(13,20)) end table.insert(tagdata, blockdata) end @@ -549,12 +564,12 @@ function main(args) -- Read the parameters for o, a in getopt.getopt(args, 'htdevi:') do - if o == "h" then help() return end - if o == "t" then return selftest() end - if o == "d" then shall_dec = true end - if o == "e" then shall_enc = true end - if o == "v" then shall_validate = true end - if o == "i" then input = load_json(a) end + if o == 'h' then help() return end + if o == 't' then return selftest() end + if o == 'd' then shall_dec = true end + if o == 'e' then shall_enc = true end + if o == 'v' then shall_validate = true end + if o == 'i' then input = load_json(a) end end -- Turn off Debug @@ -562,7 +577,10 @@ function main(args) -- GET TAG UID tag, err = lib14a.read(false, true) - if not tag then return oops(err) end + if err then + lib14a.disconnect() + return oops(err) + end core.clearCommandBuffer() -- simple tag check From c99845b3d190c75f0ec1df2842f796e9b6b0aa6d Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 21:56:05 +0200 Subject: [PATCH 03/18] fix: rename to match the new file --- client/lualibs/commands.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/lualibs/commands.lua b/client/lualibs/commands.lua index ca959291f..59825d2ca 100644 --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@ -1,8 +1,8 @@ --[[ -Handle Proxmark USB Commands +Handle Proxmark Communication Commands --]] -local _commands = require('usb_cmd') +local _commands = require('pm3_cmd') local util = require('utils') local TIMEOUT = 2000 From 07b6535f7e4d138ccfa556c59b23f519c8e34833 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:03:11 +0200 Subject: [PATCH 04/18] chg: script run didump - helptexts --- client/scripts/didump.lua | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/client/scripts/didump.lua b/client/scripts/didump.lua index 990291b57..a3601102c 100644 --- a/client/scripts/didump.lua +++ b/client/scripts/didump.lua @@ -16,16 +16,28 @@ The dump is decrypted. If a raw dump is wanted, use the -r parameter ]] example = [[ script run didump + + -- selftest script run didump -t + + -- Generate raw dump, into json. script run didump -r + + -- load file + script run didump -i dumpdata.json ]] usage = [[ -script run didump -h -t -r +script run didump -h -t -r -d -e -v -i dumpdata.json Arguments: - h this helptext - r raw - t selftest + h this helptext + r raw + t selftest + d decrypt data + e encrypt data + v validate data + i dumpdata.json load json dump file + end ]] -- Some shortcuts From 6cb6f8739eff48aec6e718cc19eaa7f8096650b8 Mon Sep 17 00:00:00 2001 From: Iceman Date: Tue, 7 May 2019 22:04:45 +0200 Subject: [PATCH 05/18] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 38058230d..60b048377 100644 --- a/.gitignore +++ b/.gitignore @@ -61,5 +61,6 @@ ppls patches/* client/lualibs/mf_default_keys.lua client/lualibs/usb_cmd.lua +client/lualibs/pm3_cmd.lua # recompiled fpga_version_info.c From 75325aef8630e8054408baa1822250c29b19b45f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:08:46 +0200 Subject: [PATCH 06/18] textual --- client/scripts/calc_di.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/client/scripts/calc_di.lua b/client/scripts/calc_di.lua index 9a8c051a4..7b8c304db 100644 --- a/client/scripts/calc_di.lua +++ b/client/scripts/calc_di.lua @@ -33,21 +33,22 @@ local bxor = bit32.bxor -- A debug printout-function local function dbg(args) if not DEBUG then return end - if type(args) == "table" then + if type(args) == 'table' then local i = 1 while args[i] do dbg(args[i]) i = i+1 end else - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ",err) - return nil,err + print('ERROR: ', err) + core.clearCommandBuffer() + return nil, err end --- -- Usage help @@ -58,6 +59,7 @@ local function help() print(desc) print('Example usage') print(example) + print(usage) end --- -- Exit message @@ -142,8 +144,8 @@ local function main(args) -- Arguments for the script for o, a in getopt.getopt(args, 'hu:') do - if o == "h" then return help() end - if o == "u" then uid = a; useUID = true end + if o == 'h' then return help() end + if o == 'u' then uid = a; useUID = true end end if useUID then @@ -160,7 +162,7 @@ local function main(args) -- simple tag check if 0x09 ~= tag.sak then if 0x4400 ~= tag.atqa then - return oops(('[fail] found tag %s :: looking for Mifare Mini 0.3k'):format(tag.name)) + return oops(('[!] found tag %s :: looking for Mifare Mini 0.3k'):format(tag.name)) end end uid = tag.uid From 2af7255f629d247437c7afc7d625ab568dd7bcf8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:12:18 +0200 Subject: [PATCH 07/18] textual --- client/scripts/calc_ev1_it.lua | 18 ++++++++++-------- client/scripts/tnp3clone.lua | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/client/scripts/calc_ev1_it.lua b/client/scripts/calc_ev1_it.lua index b4e995e96..89e778023 100644 --- a/client/scripts/calc_ev1_it.lua +++ b/client/scripts/calc_ev1_it.lua @@ -31,22 +31,22 @@ local bxor = bit32.bxor -- A debug printout-function local function dbg(args) if not DEBUG then return end - - if type(args) == "table" then + if type(args) == 'table' then local i = 1 while args[i] do dbg(args[i]) i = i+1 end else - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ",err) - return nil,err + print('ERROR: ', err) + core.clearCommandBuffer() + return nil, err end --- -- Usage help @@ -57,10 +57,11 @@ local function help() print(desc) print("Example usage") print(example) + print(usage) end -- -- Exit message -function exitMsg(msg) +local function exitMsg(msg) print( string.rep('--',20) ) print( string.rep('--',20) ) print(msg) @@ -138,6 +139,7 @@ local function pwdgen(uid) local pwd3 = bxor( entry[4], uidbytes[7]) return string.format('%02X%02X%02X%02X', pwd0, pwd1, pwd2, pwd3) end + -- -- main local function main(args) @@ -151,8 +153,8 @@ local function main(args) -- Arguments for the script for o, a in getopt.getopt(args, 'hu:') do - if o == "h" then return help() end - if o == "u" then uid = a; useUID = true end + if o == 'h' then return help() end + if o == 'u' then uid = a; useUID = true end end if useUID then diff --git a/client/scripts/tnp3clone.lua b/client/scripts/tnp3clone.lua index 1e3545a1b..b168c9d1a 100644 --- a/client/scripts/tnp3clone.lua +++ b/client/scripts/tnp3clone.lua @@ -135,11 +135,11 @@ local function main(args) --13-14 -- find tag - result, err = lib14a.read(false, true) - if not result then return oops(err) end + local card, err = lib14a.read(false, true) + if not card then return oops(err) end -- load keys - local akeys = pre.GetAll(result.uid) + local akeys = pre.GetAll(card.uid) local keyA = akeys:sub(1, 12 ) local b0 = readblock(0, keyA) @@ -154,7 +154,7 @@ local function main(args) core.clearCommandBuffer() -- wipe card. - local cmd = (csetuid..'%s %s %s w'):format(result.uid, atqa, sak) + local cmd = (csetuid..'%s %s %s w'):format(card.uid, atqa, sak) core.console(cmd) core.clearCommandBuffer() From c81bce6bf2410a66019e3d318db4e075641364df Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:15:26 +0200 Subject: [PATCH 08/18] textual --- client/scripts/calc_mizip.lua | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/client/scripts/calc_mizip.lua b/client/scripts/calc_mizip.lua index 8844f6d66..4ca7cb496 100644 --- a/client/scripts/calc_mizip.lua +++ b/client/scripts/calc_mizip.lua @@ -3,8 +3,9 @@ local getopt = require('getopt') local lib14a = require('read14a') local utils = require('utils') +copyright = '' author = 'Iceman' -version = 'v1.0.0' +version = 'v1.0.1' desc = [[ This script calculates mifare keys based on uid diversification for mizip. Algo not found by me. @@ -28,30 +29,31 @@ local bxor = bit32.bxor local _xortable = { --[[ sector key A/B, 6byte xor --]] - {1, "09125a2589e5", "F12C8453D821"}, - {2, "AB75C937922F", "73E799FE3241"}, - {3, "E27241AF2C09", "AA4D137656AE"}, - {4, "317AB72F4490", "B01327272DFD"}, + {1, '09125a2589e5', 'F12C8453D821'}, + {2, 'AB75C937922F', '73E799FE3241'}, + {3, 'E27241AF2C09', 'AA4D137656AE'}, + {4, '317AB72F4490', 'B01327272DFD'}, } --- -- A debug printout-function local function dbg(args) if not DEBUG then return end - if type(args) == "table" then + if type(args) == 'table' then local i = 1 while args[i] do dbg(args[i]) i = i+1 end else - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ",err) - return nil,err + print('ERROR: ', err) + core.clearCommandBuffer() + return nil, err end --- -- Usage help @@ -62,6 +64,7 @@ local function help() print(desc) print("Example usage") print(example) + print(usage) end -- -- Exit message @@ -163,8 +166,8 @@ local function main(args) -- Arguments for the script for o, a in getopt.getopt(args, 'hu:') do - if o == "h" then return help() end - if o == "u" then uid = a ; useUID = true end + if o == 'h' then return help() end + if o == 'u' then uid = a ; useUID = true end end if useUID then @@ -181,7 +184,7 @@ local function main(args) -- simple tag check if 0x09 ~= tag.sak then if 0x4400 ~= tag.atqa then - return oops(('[fail] found tag %s :: looking for Mifare Mini 0.3k'):format(tag.name)) + return oops(('[!] found tag %s :: looking for Mifare Mini 0.3k'):format(tag.name)) end end uid = tag.uid From b73146533a0d98de9a38b171628ab5de9c7a32cb Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:33:26 +0200 Subject: [PATCH 09/18] using MIX --- client/lualibs/read14a.lua | 4 +- client/lualibs/read14b.lua | 93 +++++++++++++------------------------- 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/client/lualibs/read14a.lua b/client/lualibs/read14a.lua index 37f13decb..de4535c2f 100644 --- a/client/lualibs/read14a.lua +++ b/client/lualibs/read14a.lua @@ -120,13 +120,13 @@ end -- @return if successfull: an table containing card info -- @return if unsuccessfull : nil, error local function waitFor14443a() - print("Waiting for card... press any key to quit") + print('Waiting for card... press any key to quit') while not core.ukbhit() do res, err = read14443a() if res then return res end -- err means that there was no response from card end - return nil, "Aborted by user" + return nil, 'Aborted by user' end -- Sends an instruction to do nothing, only disconnect diff --git a/client/lualibs/read14b.lua b/client/lualibs/read14b.lua index 59e451ba4..52b2903bd 100644 --- a/client/lualibs/read14b.lua +++ b/client/lualibs/read14b.lua @@ -2,7 +2,7 @@ This is a library to read 14443b tags. It can be used something like this local reader = require('read14b') - result, err = reader.select1443b() + result, err = reader.read14443b() if not result then print(err) return @@ -13,7 +13,10 @@ -- Loads the commands-library local cmds = require('commands') local utils = require('utils') + +-- Shouldn't take longer than 2.5 seconds local TIMEOUT = 2500 + local ISO14B_COMMAND = { ISO14B_CONNECT = 1, ISO14B_DISCONNECT = 2, @@ -41,45 +44,16 @@ local function parse1443b(data) --]] local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data) - uid = uid:sub(1, 2*uidlen) - return { uid = uid, uidlen = uidlen, atqb = atqb, chipid = chipid, cid = cid } + uid = uid:sub(1, 2 * uidlen) + return { + uid = uid, + uidlen = uidlen, + atqb = atqb, + chipid = chipid, + cid = cid + } end ---- Sends a USBpacket 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 --- @return packet,nil if successfull --- nil, errormessage if unsuccessfull -local function sendToDevice(cmd, ignoreresponse) - --core.clearCommandBuffer() - local bytes = cmd:getBytes() - local count,c,arg0,arg1,arg2 = bin.unpack('LLLL',bytes) - local err = core.SendCommand(cmd:getBytes()) - if err then - print('ERROR',err) - return nil, err - end - if ignoreresponse then return nil,nil end - - local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT) - return response,nil -end ---- Picks out and displays the data read from a tag --- Specifically, takes a usb packet, converts to a Command --- (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 -local function showData(usbpacket) - local response = Command.parse(usbpacket) - local len = response.arg2 * 2 - local data = string.sub(response.data, 0, len); - print("<< ",data) -end - - -- This function does a connect and retrieves some info -- @return if successfull: an table containing card info -- @return if unsuccessfull : nil, error @@ -95,18 +69,22 @@ local function read14443b(disconnect) flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT end - command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} - local result, err = sendToDevice(command, false) + command = Command:newMIX{ + cmd = cmds.CMD_ISO_14443B_COMMAND, + arg1 = flags + } + + local result, err = command:sendMIX() 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 - err = "iso14443b card select failed" + err = 'iso14443b card select failed' end else - err = "No response from card" + err = 'No response from card' end if err then @@ -115,43 +93,34 @@ local function read14443b(disconnect) end return info end ---PING / PONG - Custom Anticollison for Navigo. --- AA / BB ?!? --- local ping = ('BA00') --- result, err = sendRaw(ping, 1, 1) --- if result then - -- resp = Command.parse( result ) - -- if arg1 == 0 then - -- return nil, "iso14443b card - PING/PONG failed" - -- end - -- showData(result) --- else - -- err = "No response from card" - -- print(err) - -- return nil, err --- end - --- -- 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() - print("Waiting for card... press any key to quit") + print('Waiting for card... press any key to quit') 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 - return nil, "Aborted by user" + return nil, 'Aborted by user' +end + +-- Sends an instruction to do nothing, only disconnect +local function disconnect14443b() + local c = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443b} + -- 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 = { read = read14443b, waitFor14443b = waitFor14443b, parse1443b = parse1443b, - sendToDevice = sendToDevice, - showData = showData, + disconnect = disconnect14443b, ISO14B_COMMAND = ISO14B_COMMAND, } From b52a4a53b085806e278b0f42518a756829841eb8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:33:51 +0200 Subject: [PATCH 10/18] textual --- client/lualibs/taglib.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/lualibs/taglib.lua b/client/lualibs/taglib.lua index 0612ed2d3..8ba9272d1 100644 --- a/client/lualibs/taglib.lua +++ b/client/lualibs/taglib.lua @@ -115,10 +115,10 @@ return { lookupManufacturer = function (value) if type(value) == 'string' then local v = tonumber(value, 16) - print(string.format("WARNING: lookupManufacturer expects numeric value, converted %s into %x", value,v)) + print(string.format('WARNING: lookupManufacturer expects numeric value, converted %s into %x', value,v)) value = v end - return m[value] or "no tag-info available" + return m[value] or 'no tag-info available' end, } From 6fbd45aaedb72ddaaf08801c29f4760e15725024 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:46:00 +0200 Subject: [PATCH 11/18] 15 - use NG and refactoring --- client/lualibs/read14a.lua | 2 +- client/lualibs/read15.lua | 88 ++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/client/lualibs/read14a.lua b/client/lualibs/read14a.lua index de4535c2f..6fe7f8bd1 100644 --- a/client/lualibs/read14a.lua +++ b/client/lualibs/read14a.lua @@ -96,7 +96,7 @@ local function read14443a(dont_disconnect, no_rats) command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_RATS end - local result,err = command:sendMIX() + local result, err = command:sendMIX() if result then local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) if arg0 == 0 then diff --git a/client/lualibs/read15.lua b/client/lualibs/read15.lua index 5d231ad52..e045cccad 100644 --- a/client/lualibs/read15.lua +++ b/client/lualibs/read15.lua @@ -13,27 +13,15 @@ -- Loads the commands-library local cmds = require('commands') local utils = require('utils') -local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds + -- Shouldn't take longer than 2 seconds +local TIMEOUT = 2000 ---- Sends a USBpacket 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 --- @return packet,nil if successfull --- nil, errormessage if unsuccessfull -local function sendToDevice(command, ignoreresponse) - local err = core.SendCommand(command:getBytes()) - if err then - print(err) - return nil, err - end - if ignoreresponse then return nil, nil end - - local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT) - return response, nil -end +local ISO15_COMMAND = { + ISO15_REQ_SUBCARRIER_SINGLE = 0, + ISO15_REQ_DATARATE_HIGH = 2, + ISO15_REQ_NONINVENTORY = 0, +} local function errorString15693(number) local errors = {} @@ -50,7 +38,6 @@ local function errorString15693(number) return errors[number] or "Reserved for Future Use or Custom command error." end - local function parse15693(data) local bytes = utils.ConvertAsciiToBytes(data) local tmp = utils.ConvertAsciiToHex(data) @@ -59,14 +46,14 @@ local function parse15693(data) local crcStr = utils.Crc15(tmp, #tmp) if string.sub(crcStr, #crcStr - 3) ~= '470F' then - print("CRC", crc ) - return nil, "CRC failed" + print('CRC', crc ) + return nil, 'CRC failed' end if bytes[1] % 2 == 1 then -- Above is a poor-mans bit check: -- recv[0] & ISO15_RES_ERROR //(0x01) - local err = "Tag returned error %i: %s" + local err = 'Tag returned error %i: %s' err = string.format(err, bytes[1], errorString15693(bytes[1])) return nil, err end @@ -82,7 +69,6 @@ end local function read15693(slow, dont_readresponse) --[[ - We start by trying this command: MANDATORY (present in ALL iso15693 tags) command (the example below is sent to a tag different from the above one): @@ -108,11 +94,13 @@ local function read15693(slow, dont_readresponse) data = utils.Crc15("260100") - command = Command:new{cmd = cmds.CMD_ISO_15693_COMMAND, - arg1 = #data / 2, - arg2 = 1, - arg3 = 1, - data = data} + command = Command:newMIX{ + cmd = cmds.CMD_ISO_15693_COMMAND, + arg1 = #data / 2, + arg2 = 1, + arg3 = 1, + data = data + } if slow then command.arg2 = 0 @@ -121,24 +109,23 @@ local function read15693(slow, dont_readresponse) command.arg3 = 0 end - local result, err = sendToDevice(command, dont_readresponse) - if not result then - print(err) - return nil, "15693 identify: no answer" - end - - local count, cmd, len, arg2, arg3 = bin.unpack('LLLL', result) - if len > 0 then + local result, err = command:sendMIX() + if result then + local count, cmd, len, arg2, arg3 = bin.unpack('LLLL', result) + if len == 0 then + return nil, 'iso15693 card select failed' + end data = string.sub(result, count, count+len-1) info, err = parse15693(data) - if err then - print(err) - return nil, err - end - return info else - return nil, "Failed to get response" + err = 'No response from card' end + + if err then + print(err) + return nil, err + end + return info end --- @@ -146,19 +133,28 @@ end -- @return if successfull: an table containing card info -- @return if unsuccessfull : nil, error local function waitFor15693() - print("Waiting for card... press any key to quit") + print('Waiting for card... press any key to quit') while not core.ukbhit() do res, err = read15693() if res then return res end -- err means that there was no response from card end - return nil, "Aborted by user" + return nil, 'Aborted by user' end + +-- Sends an instruction to do nothing, only disconnect +local function disconnect15693() + local c = Command:newMIX{cmd = cmds.CMD_ISO_15693_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) +end + local library = { read = read15693, waitFor15693 = waitFor15693, parse15693 = parse15693, - sendToDevice = sendToDevice, + disconnect = disconnect15693, } return library From 9bf961cb8a9a9fd32f96252889c9794775a54ffd Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:48:18 +0200 Subject: [PATCH 12/18] refactoring --- client/scripts/calypso.lua | 101 ++++++++++++++++++++----------------- client/scripts/cmdline.lua | 8 ++- 2 files changed, 60 insertions(+), 49 deletions(-) diff --git a/client/scripts/calypso.lua b/client/scripts/calypso.lua index 74de3cb9a..6b0a95151 100644 --- a/client/scripts/calypso.lua +++ b/client/scripts/calypso.lua @@ -4,27 +4,22 @@ local lib14b = require('read14b') local utils = require('utils') local iso7816 = require('7816_error') -example = "script runs 14b raw commands to query a CAPLYPSO tag" -author = "Iceman, 2016" -desc = -[[ +copyright = '' +author = 'Iceman' +version = 'v1.0.1' +desc = [[ This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands +]] +example = [[ + script run calypso -b 11223344 + +]] +usage = [[ +script run calypso -h -b Arguments: - -b 123 -Examples : - script run f -b 11223344 - script run f - -Examples : - -# 1. Connect and don't disconnect -script run f -# 2. Send mf auth, read response -script run f -# 3. disconnect -script run f - + h this helptext + b raw bytes to send ]] --[[ @@ -61,23 +56,34 @@ end --- -- A debug printout-function local function dbg(args) - if DEBUG then - print("###", args) + 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) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ", err) + print('ERROR: ', err) calypso_switch_off_field() return nil, err end --- -- Usage help local function help() + print(copyright) + print(author) + print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage)) end -- -- helper function, give current count of items in lua-table. @@ -185,26 +191,26 @@ local _calypso_cmds = { -- Electronic Transaction log file - --["01.Select ICC file"] = '0294 a4 00 0002 3f00', - ["01.Select ICC file"] = '0294 a4 080004 3f00 0002', - ["02.ICC"] = '0394 b2 01 041d', - ["03.Select EnvHol file"] = '0294 a4 080004 2000 2001', - ["04.EnvHol1"] = '0394 b2 01 041d', - ["05.Select EvLog file"] = '0294 a4 080004 2000 2010', - ["06.EvLog1"] = '0394 b2 01 041d', - ["07.EvLog2"] = '0294 b2 02 041d', - ["08.EvLog3"] = '0394 b2 03 041d', - ["09.Select ConList file"]= '0294 a4 080004 2000 2050', - ["10.ConList"] = '0394 b2 01 041d', - ["11.Select Contra file"] = '0294 a4 080004 2000 2020', - ["12.Contra1"] = '0394 b2 01 041d', - ["13.Contra2"] = '0294 b2 02 041d', - ["14.Contra3"] = '0394 b2 03 041d', - ["15.Contra4"] = '0294 b2 04 041d', - ["16.Select Counter file"]= '0394 a4 080004 2000 2069', - ["17.Counter"] = '0294 b2 01 041d', - ["18.Select SpecEv file"] = '0394 a4 080004 2000 2040', - ["19.SpecEv1"] = '0294 b2 01 041d', + --['01.Select ICC file'] = '0294 a4 00 0002 3f00', + ['01.Select ICC file'] = '0294 a4 080004 3f00 0002', + ['02.ICC'] = '0394 b2 01 041d', + ['03.Select EnvHol file'] = '0294 a4 080004 2000 2001', + ['04.EnvHol1'] = '0394 b2 01 041d', + ['05.Select EvLog file'] = '0294 a4 080004 2000 2010', + ['06.EvLog1'] = '0394 b2 01 041d', + ['07.EvLog2'] = '0294 b2 02 041d', + ['08.EvLog3'] = '0394 b2 03 041d', + ['09.Select ConList file']= '0294 a4 080004 2000 2050', + ['10.ConList'] = '0394 b2 01 041d', + ['11.Select Contra file'] = '0294 a4 080004 2000 2020', + ['12.Contra1'] = '0394 b2 01 041d', + ['13.Contra2'] = '0294 b2 02 041d', + ['14.Contra3'] = '0394 b2 03 041d', + ['15.Contra4'] = '0294 b2 04 041d', + ['16.Select Counter file']= '0394 a4 080004 2000 2069', + ['17.Counter'] = '0294 b2 01 041d', + ['18.Select SpecEv file'] = '0394 a4 080004 2000 2040', + ['19.SpecEv1'] = '0294 b2 01 041d', } --- @@ -218,7 +224,8 @@ function main(args) local data, apdu, flags, uid, cid, result, err, card -- Read the parameters for o, a in getopt.getopt(args, 'h') do - if o == "h" then return help() end + if o == 'h' then return help() end + if o == 'b' then bytes = a end end calypso_switch_on_field() @@ -248,7 +255,7 @@ function main(args) --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file for i, apdu in spairs(_calypso_cmds) do print('>>', i ) - apdu = apdu:gsub("%s+","") + apdu = apdu:gsub('%s+', '') result, err = calypso_send_cmd_raw(apdu , false) if result then calypso_apdu_status(result.data) @@ -263,12 +270,12 @@ end -- a simple selftest function, tries to convert function selftest() DEBUG = true - dbg("Performing test") - dbg("Tests done") + dbg('Performing test') + dbg('Tests done') end -- Flip the switch here to perform a sanity check. -- It read a nonce in two different ways, as specified in the usage-section -if "--test"==args then +if '--test'==args then selftest() else -- Call the main diff --git a/client/scripts/cmdline.lua b/client/scripts/cmdline.lua index 29627afc7..dc1e752c1 100644 --- a/client/scripts/cmdline.lua +++ b/client/scripts/cmdline.lua @@ -1,12 +1,16 @@ +--[[ +A sampe script file on how to implement at cmd line inteface. +--]] + print("This is how a cmd-line interface could be implemented\nPrint 'exit' to exit.\n") local answer repeat io.write("$>") io.flush() - answer=io.read() + answer = io.read() if answer ~= 'exit' then local func = assert(loadstring("return " .. answer)) io.write("\n"..tostring(func() or "").."\n"); end--]] -until answer=="exit" +until answer == "exit" print("Bye\n"); From 3c533db308d46d462a3f8687d66cb69b785be02e Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 22:59:29 +0200 Subject: [PATCH 13/18] refactoring NG --- client/lualibs/read14b.lua | 10 +++++++++- client/scripts/calypso.lua | 39 +++++++++++--------------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/client/lualibs/read14b.lua b/client/lualibs/read14b.lua index 52b2903bd..8d28408f1 100644 --- a/client/lualibs/read14b.lua +++ b/client/lualibs/read14b.lua @@ -108,9 +108,16 @@ local function waitFor14443b() return nil, 'Aborted by user' end +--- +-- 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) +end +--- -- Sends an instruction to do nothing, only disconnect local function disconnect14443b() - local c = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443b} + local c = Command:newMIX{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = ISO14B_COMMAND.ISO14B_DISCONNECT} -- We can ignore the response here, no ACK is returned for this command -- Check /armsrc/iso14443b.c, ReaderIso14443b() for details return c.sendMIX(true) @@ -120,6 +127,7 @@ local library = { read = read14443b, waitFor14443b = waitFor14443b, parse1443b = parse1443b, + connect = connect14443b, disconnect = disconnect14443b, ISO14B_COMMAND = ISO14B_COMMAND, } diff --git a/client/scripts/calypso.lua b/client/scripts/calypso.lua index 6b0a95151..a5e35b82a 100644 --- a/client/scripts/calypso.lua +++ b/client/scripts/calypso.lua @@ -28,21 +28,6 @@ Check there for details about data format and how commands are interpreted on th device-side. ]] ---- --- -local function calypso_switch_on_field() - local flags = lib14b.ISO14B_COMMAND.ISO14B_CONNECT - local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} - return lib14b.sendToDevice(c, true) -end ---- --- Disconnect (poweroff) the antenna forcing a disconnect of a 14b tag. -local function calypso_switch_off_field() - local flags = lib14b.ISO14B_COMMAND.ISO14B_DISCONNECT - local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} - return lib14b.sendToDevice(c, true) -end - local function calypso_parse(result) local r = Command.parse(result) local len = r.arg2 * 2 @@ -71,7 +56,7 @@ end -- This is only meant to be used when errors occur local function oops(err) print('ERROR: ', err) - calypso_switch_off_field() + lib14b.disconnect() return nil, err end --- @@ -128,15 +113,13 @@ local function calypso_send_cmd_raw(data, ignoreresponse ) data = data or "00" - command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, - arg1 = flags, - arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string - arg3 = 0, - data = data} -- data bytes (commands etc) - result, err = lib14b.sendToDevice(command, false) - - if ignoreresponse then return response, err end - + command = Command:newMIX{ + cmd = cmds.CMD_ISO_14443B_COMMAND, + arg1 = flags, + arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string + data = data} -- data bytes (commands etc) + + result, err = command:sendMIX(ignoreresponse) if result then local r = calypso_parse(result) return r, nil @@ -228,8 +211,8 @@ function main(args) if o == 'b' then bytes = a end end - calypso_switch_on_field() - + lib14b.connect() + -- Select 14b tag. card, err = lib14b.waitFor14443b() if not card then return oops(err) end @@ -264,7 +247,7 @@ function main(args) print('<< no answer') end end - calypso_switch_off_field() + lib14b.disconnect() end --- -- a simple selftest function, tries to convert From 6b472f6c56878881d2017dc090499c40ffe03200 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 23:04:29 +0200 Subject: [PATCH 14/18] textual --- client/scripts/formatMifare.lua | 90 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/client/scripts/formatMifare.lua b/client/scripts/formatMifare.lua index f53a05eff..e200d92cb 100644 --- a/client/scripts/formatMifare.lua +++ b/client/scripts/formatMifare.lua @@ -4,6 +4,19 @@ local bin = require('bin') local lib14a = require('read14a') local utils = require('utils') +copyright = '' +author = 'Iceman' +version = 'v1.0.1' +desc = [[ +This script will generate 'hf mf wrbl' commands for each block to format a Mifare card. + +Alla datablocks gets 0x00 +As default the script sets the keys A/B to 0xFFFFFFFFFFFF +and the access bytes will become 0x78,0x77,0x88 +The GDB will become 0x00 + +The script will skip the manufactoring block 0. +]] example = [[ -- generate commands 1. script run formatMifare @@ -14,22 +27,8 @@ example = [[ -- generate commands and execute them against card. 3. script run formatMifare -x ]] -copyright = '' -version = '' -author = 'Iceman' usage = [[ - script run formatMifare -k -n -a -x -]] -desc = [[ -This script will generate 'hf mf wrbl' commands for each block to format a Mifare card. - -Alla datablocks gets 0x00 -As default the script sets the keys A/B to 0xFFFFFFFFFFFF -and the access bytes will become 0x78,0x77,0x88 -The GDB will become 0x00 - -The script will skip the manufactoring block 0. - +script run formatMifare -k -n -a -x Arguments: -h - this help @@ -38,6 +37,7 @@ Arguments: -a - the new access bytes that will be written to the card -x - execute the commands aswell. ]] + local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds local DEBUG = true -- the debug flag local CmdString = 'hf mf wrbl %d B %s %s' @@ -45,39 +45,39 @@ local numBlocks = 64 local numSectors = 16 --- -- A debug printout-function -function dbg(args) - if not DEBUG then - return - end - - if type(args) == "table" then +local function dbg(args) + if not DEBUG then return end + if type(args) == 'table' then local i = 1 - while result[i] do - dbg(result[i]) + while args[i] do + dbg(args[i]) i = i+1 end else - print("###", args) + print('###', args) end end --- -- This is only meant to be used when errors occur -function oops(err) - print("ERROR: ",err) +local function oops(err) + print('ERROR:', err) + core.clearCommandBuffer() + return nil, err end --- -- Usage help -function help() +local function help() print(copyright) print(author) print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage) end -- -- Exit message -function ExitMsg(msg) +local function ExitMsg(msg) print( string.rep('--',20) ) print( string.rep('--',20) ) print(msg) @@ -91,7 +91,7 @@ function GetCardInfo() print(err) return end - print(("Found: %s"):format(result.name)) + print(('Found: %s'):format(result.name)) core.clearCommandBuffer() @@ -138,11 +138,11 @@ local function main(args) -- Arguments for the script for o, a in getopt.getopt(args, 'hk:n:a:x') do - if o == "h" then return help() end - if o == "k" then OldKey = a end - if o == "n" then NewKey = a end - if o == "a" then Accessbytes = a end - if o == "x" then x = true end + if o == 'h' then return help() end + if o == 'k' then OldKey = a end + if o == 'n' then NewKey = a end + if o == 'a' then Accessbytes = a end + if o == 'x' then x = true end end -- validate input args. @@ -169,29 +169,29 @@ local function main(args) print( string.format('Old key: %s', OldKey)) print( string.format('New key: %s', NewKey)) print( string.format('New Access: %s', Accessbytes)) - print( string.rep('--',20) ) + print( string.rep('--', 20) ) - -- Set new block data - local EMPTY_BL = string.rep('00',16) - local EMPTY_SECTORTRAIL = string.format('%s%s%s%s',NewKey,Accessbytes,'00',NewKey) + -- Set new block data + local EMPTY_BL = string.rep('00', 16) + local EMPTY_SECTORTRAIL = string.format('%s%s%s%s', NewKey, Accessbytes, '00', NewKey) - dbg( string.format('New sector-trailer : %s',EMPTY_SECTORTRAIL)) - dbg( string.format('New emptyblock: %s',EMPTY_BL)) + dbg( string.format('New sector-trailer : %s', EMPTY_SECTORTRAIL)) + dbg( string.format('New emptyblock: %s', EMPTY_BL)) dbg('') if x then print('[Warning] you have used the EXECUTE parameter, which means this will run these commands against card.') end -- Ask - local dialogResult = utils.confirm("Do you want to erase this card") + local dialogResult = utils.confirm('Do you want to erase this card') if dialogResult == false then return ExitMsg('Quiting it is then. Your wish is my command...') end - print( string.rep('--',20) ) + print( string.rep('--', 20) ) -- main loop - for block=0,numBlocks,1 do + for block = 0, numBlocks, 1 do local reminder = (block+1) % 4 local cmd @@ -207,7 +207,7 @@ local function main(args) end if core.ukbhit() then - print("aborted by user") + print('aborted by user') break end end From e799717880482a1a5c62709c5473c2b3e13dfd95 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 23:12:58 +0200 Subject: [PATCH 15/18] refactored NG --- client/scripts/14araw.lua | 120 +++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/client/scripts/14araw.lua b/client/scripts/14araw.lua index e0448d9db..776ff754e 100644 --- a/client/scripts/14araw.lua +++ b/client/scripts/14araw.lua @@ -2,11 +2,27 @@ local cmds = require('commands') local getopt = require('getopt') local lib14a = require('read14a') -example = "script run 14araw -x 6000F57b" +copyright = '' author = "Martin Holst Swende" -desc = -[[ +version = 'v1.0.1' +desc = [[ This is a script to allow raw 1444a commands to be sent and received. +]] +example = [[ + # 1. Connect and don't disconnect + script run 14araw -p + + # 2. Send mf auth, read response (nonce) + script run 14araw -o -x 6000F57b -p + + # 3. disconnect + script run 14araw -o + + # All three steps in one go: + script run 14araw -x 6000F57b +]] +usage = [[ +script run 14araw -x 6000F57b Arguments: -o do not connect - use this only if you previously used -p to stay connected @@ -17,18 +33,6 @@ Arguments: -d Debug flag -t Topaz mode -3 ISO14443-4 (use RATS) - -Examples : - -# 1. Connect and don't disconnect -script run 14araw -p -# 2. Send mf auth, read response (nonce) -script run 14araw -o -x 6000F57b -p -# 3. disconnect -script run 14araw -o - -# All three steps in one go: -script run 14araw -x 6000F57b ]] --[[ @@ -41,7 +45,6 @@ device-side. ]] -- Some globals -local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds local DEBUG = false -- the debug flag ------------------------------- @@ -51,21 +54,34 @@ local DEBUG = false -- the debug flag --- -- A debug printout-function local function dbg(args) - if DEBUG then - print("###", args) + 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) end end --- -- This is only meant to be used when errors occur local function oops(err) - print("ERROR: ",err) + print('ERROR:', err) + core.clearCommandBuffer() + return nil, err end --- -- Usage help local function help() + print(copyright) + print(author) + print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage) end --- -- The main entry point @@ -83,31 +99,35 @@ function main(args) -- Read the parameters for o, a in getopt.getopt(args, 'orcpx:dt3') do - 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 + 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 end -- First of all, connect if doconnect then dbg("doconnect") - -- We reuse the connect functionality from a - -- common library - info, err = lib14a.read(true, no_rats) - if err then return oops(err) end - print(("Connected to card, uid = %s"):format(info.uid)) + 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)) end -- The actual raw payload, if any if payload then - res,err = sendRaw(payload,{ignore_response = ignore_response, topaz_mode = topaz_mode, append_crc = append_crc}) - if err then return oops(err) end + res, err = sendRaw(payload,{ignore_response = ignore_response, topaz_mode = topaz_mode, append_crc = append_crc}) + if err then + lib14a.disconnect() + return oops(err) + end if not ignoreresponse then -- Display the returned data @@ -116,7 +136,7 @@ function main(args) end -- And, perhaps disconnect? if not stayconnected then - disconnect() + lib14a.disconnect() end end @@ -132,11 +152,10 @@ function showdata(usbpacket) --print("data length:",len) local data = string.sub(tostring(cmd_response.data), 0, len); print("<< ",data) - --print("----------------") end function sendRaw(rawdata, options) - print(">> ", rawdata) + print('>> ', rawdata) local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW @@ -147,39 +166,32 @@ function sendRaw(rawdata, options) flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC end - local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, + local command = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443a, 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 lib14a.sendToDevice(command, options.ignore_response) + return command:sendMIX(options.ignore_response) end --- Sends an instruction to do nothing, only disconnect -function disconnect() - local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, arg1 = 0,} - -- We can ignore the response here, no ACK is returned for this command - -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details - return lib14a.sendToDevice(command,true) -end ------------------------- -- Testing ------------------------- function selftest() DEBUG = true - dbg("Performing test") + dbg('Performing test') main() - main("-p") - main(" -o -x 6000F57b -p") - main("-o") - main("-x 6000F57b") - dbg("Tests done") + main('-p') + main(' -o -x 6000F57b -p') + main('-o') + main('-x 6000F57b') + dbg('Tests done') end -- Flip the switch here to perform a sanity check. -- It read a nonce in two different ways, as specified in the usage-section -if "--test"==args then +if '--test'==args then selftest() else -- Call the main From 7ccff2db4adfc0bbf39d5f513d4403695681d818 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 23:19:22 +0200 Subject: [PATCH 16/18] textual --- client/scripts/emul2dump.lua | 49 +++++++++++++++++++---------- client/scripts/emul2html.lua | 61 ++++++++++++++++++++++-------------- 2 files changed, 71 insertions(+), 39 deletions(-) diff --git a/client/scripts/emul2dump.lua b/client/scripts/emul2dump.lua index 0cbc3a4c2..2ce229e7f 100644 --- a/client/scripts/emul2dump.lua +++ b/client/scripts/emul2dump.lua @@ -2,37 +2,54 @@ local getopt = require('getopt') local bin = require('bin') local dumplib = require('html_dumplib') +copyright = '' +author = 'Iceman' +version = 'v1.0.1' +desc =[[ +This script takes an dumpfile on EML (ASCII) format and converts it to the PM3 dumpbin file to be used with `hf mf restore` +]] example =[[ 1. script run emul2dump 2. script run emul2dump -i myfile.eml 3. script run emul2dump -i myfile.eml -o myfile.bin ]] -author = "Iceman" -usage = "script run emul2dump [-i ] [-o ]" -desc =[[ -This script takes an dumpfile on EML (ASCII) format and converts it to the PM3 dumpbin file to be used with "hf mf restore" +usage = [[ +script run emul2dump [-i ] [-o ] Arguments: -h This help -i Specifies the dump-file (input). If omitted, 'dumpdata.eml' is used -o Specifies the output file. If omitted, .bin is used. -]] +]] --- -- This is only meant to be used when errors occur -function oops(err) - print("ERROR: ",err) +local function oops(err) + 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) + end end --- -- Usage help -function help() +local function help() + print(copyright) + print(author) + print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage) end -- -- Exit message -function ExitMsg(msg) +local function ExitMsg(msg) print( string.rep('--',20) ) print( string.rep('--',20) ) print(msg) @@ -41,20 +58,20 @@ end local function main(args) - local input = "dumpdata.eml" - local output = os.date("%Y-%m-%d_%H%M%S.bin"); + local input = 'dumpdata.eml' + local output = os.date('%Y-%m-%d_%H%M%S.bin'); -- Arguments for the script for o, a in getopt.getopt(args, 'hi:o:') do - if o == "h" then return help() end - if o == "i" then input = a end - if o == "o" then output = a end + if o == 'h' then return help() end + if o == 'i' then input = a end + if o == 'o' then output = a end end local filename, err = dumplib.convert_eml_to_bin(input,output) if err then return oops(err) end - ExitMsg(("Wrote a BIN dump to the file %s"):format(filename)) + ExitMsg(('Wrote a BIN dump to the file %s'):format(filename)) end main(args) diff --git a/client/scripts/emul2html.lua b/client/scripts/emul2html.lua index 36230f8af..879250077 100644 --- a/client/scripts/emul2html.lua +++ b/client/scripts/emul2html.lua @@ -4,12 +4,18 @@ getopt = require('getopt') bin = require('bin') dumplib = require('html_dumplib') -example = "script run emul2html -o dumpdata.eml " -author = "Martin Holst Swende" -usage = "script run htmldump [-i ] [-o ]" -desc =[[ +copyright = '' +author = 'Martin Holst Swende' +version = 'v1.0.1' +desc = [[ This script takes a dumpfile on EML (ASCII) format and produces a html based dump, which is a bit more easily analyzed. +]] +example = [[ + script run emul2html -o dumpdata.eml +]] +usage = [[ +script run htmldump [-i ] [-o ] Arguments: -h This help @@ -18,45 +24,54 @@ Arguments: ]] -------------------------------- --- Some utilities -------------------------------- - +-- Some globals +local DEBUG = false -- the debug flag --- -- A debug printout-function -function dbg(args) - if DEBUG then - print("###", args) +local function dbg(args) + 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) end end --- -- This is only meant to be used when errors occur -function oops(err) - print("ERROR: ",err) +local function oops(err) + print('ERROR:', err) + core.clearCommandBuffer() + return nil, err end - - --- -- Usage help -function help() +local function help() + print(copyright) + print(author) + print(version) print(desc) - print("Example usage") + print('Example usage') print(example) + print(usage) end local function main(args) - local input = "dumpdata.eml" - local output = os.date("%Y-%m-%d_%H%M%S.html"); + local input = 'dumpdata.eml' + local output = os.date('%Y-%m-%d_%H%M%S.html'); for o, a in getopt.getopt(args, 'i:o:h') do - if o == "h" then return help() end - if o == "i" then input = a end - if o == "o" then output = a end + if o == 'h' then return help() end + if o == 'i' then input = a end + if o == 'o' then output = a end end local filename, err = dumplib.convert_eml_to_html(input,output) if err then return oops(err) end - print(("Wrote a HTML dump to the file %s"):format(filename)) + print(('Wrote a HTML dump to the file %s'):format(filename)) end --[[ From e1bb05b35eb4d83c0ec4e554d6313d58bfacdbfa Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 23:21:56 +0200 Subject: [PATCH 17/18] refactor --- client/scripts/ufodump.lua | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/client/scripts/ufodump.lua b/client/scripts/ufodump.lua index 655d6e7ed..12f7cb9b2 100644 --- a/client/scripts/ufodump.lua +++ b/client/scripts/ufodump.lua @@ -6,8 +6,7 @@ local utils = require('utils') copyright = '' author = 'Iceman' version = 'v1.0.1' -desc = -[[ +desc = [[ 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. xor: the first three block (0,1,2) is not XORED. The rest seems to be xored. @@ -106,14 +105,6 @@ function sendRaw(rawdata, options) return command:sendMIX(options.ignore_response) end --- --- Sends an instruction to do nothing, only disconnect -function disconnect() - local command = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443a, arg1 = 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) -end --- -- The main entry point function main(args) @@ -130,7 +121,10 @@ function main(args) -- First of all, connect info, err = lib14a.read(true, true) - if err then disconnect() return oops(err) end + if err then + lib14a.disconnect() + return oops(err) + end core.clearCommandBuffer() local blockData = {} @@ -143,7 +137,10 @@ function main(args) for block = 00, endblock do local cmd = string.format('10%02x00', block) res, err = sendRaw(cmd , {ignore_response = ignore_response}) - if err then disconnect() return oops(err) end + if err then + lib14a.disconnect() + return oops(err) + end local cmd_response = Command.parse(res) local len = tonumber(cmd_response.arg1) * 2 @@ -153,7 +150,7 @@ function main(args) table.insert(blockData, data) end print("----+------------------+-------------------") - disconnect() + lib14a.disconnect() local filename, err = utils.WriteDumpFile(info.uid, blockData) if err then return oops(err) end From a59f2a1b60b30983ae646ddd018576ea26bde35b Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 23:27:16 +0200 Subject: [PATCH 18/18] refactor NG --- client/scripts/amiibo.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/client/scripts/amiibo.lua b/client/scripts/amiibo.lua index 2bcd2a07e..3d8e026dd 100644 --- a/client/scripts/amiibo.lua +++ b/client/scripts/amiibo.lua @@ -15,9 +15,13 @@ local luamiibo = luamiibo_open() local function nfc_read_amiibo () - local command = Command:new{cmd = cmds.CMD_MIFAREU_READCARD, arg1 = 0, arg2 = 135} + local command = Command:newMIX{ + cmd = cmds.CMD_MIFAREU_READCARD, + arg1 = 0, + arg2 = 135 + } - local result, err = reader.sendToDevice(command) + local result, err = command.sendMIX() if result then -- Do Mifare Ultralight read local count, cmd, arg0, data_len, offset = bin.unpack('LLLL', result) @@ -85,8 +89,13 @@ local function emulate_amiibo (amiibo_data) print(string.format('Simulating with UID: 0x%04x 0x%04x', uid_first, uid_second)) -- Begin simulating NTAG215 - local simCmd = Command:new{cmd = cmds.CMD_SIMULATE_TAG_ISO_14443a, arg1 = 7, arg2 = uid_first, arg3 = uid_second} - local _, err = reader.sendToDevice(simCmd) + local simCmd = Command:newMIX{ + cmd = cmds.CMD_SIMULATE_TAG_ISO_14443a, + arg1 = 7, + arg2 = uid_first, + arg3 = uid_second + } + local _, err = simCmd.sendMIX() if err then print('Failed to start simulator', err) return