first batch of updated scripts to use sendMIX commands instead.

This commit is contained in:
iceman1001 2019-04-28 18:46:06 +02:00
commit 819896acf6
7 changed files with 223 additions and 194 deletions

View file

@ -4,6 +4,7 @@ Handle Proxmark USB Commands
local _commands = require('usb_cmd') local _commands = require('usb_cmd')
local util = require('utils') local util = require('utils')
local TIMEOUT = 2000
local _reverse_lookup,k,v = {} local _reverse_lookup,k,v = {}
for k, v in pairs(_commands) do for k, v in pairs(_commands) do
@ -131,6 +132,14 @@ function Command:__responsetostring()
print('package ::', self.resp_response) print('package ::', self.resp_response)
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
-- @return packet,nil if successfull
-- nil, errormessage if unsuccessfull
function Command:sendMIX( ignore_response, timeout ) function Command:sendMIX( ignore_response, timeout )
local data = self.data local data = self.data
local cmd = self.cmd local cmd = self.cmd
@ -139,7 +148,9 @@ function Command:sendMIX( ignore_response, timeout )
local err, msg = core.SendCommandMIX(cmd, arg1, arg2, arg3, data) local err, msg = core.SendCommandMIX(cmd, arg1, arg2, arg3, data)
if err == nil then return err, msg end if err == nil then return err, msg end
if ignoreresponse then return true, nil end if ignore_response then return true, nil end
if timeout == nil then timeout = TIMEOUT end
local response, msg = core.WaitForResponseTimeout(_commands.CMD_ACK, timeout) local response, msg = core.WaitForResponseTimeout(_commands.CMD_ACK, timeout)
if response == nil then if response == nil then
@ -174,8 +185,10 @@ function Command:sendNG( ignore_response, timeout )
local err, msg = core.SendCommandNG(cmd, data) local err, msg = core.SendCommandNG(cmd, data)
if err == nil then return err, msg end if err == nil then return err, msg end
if ignoreresponse then return true, nil end if ignore_response then return true, nil end
if timeout == nil then timeout = TIMEOUT end
local response, msg = core.WaitForResponseTimeout(cmd, timeout) local response, msg = core.WaitForResponseTimeout(cmd, timeout)
if response == nil then if response == nil then
return nil, 'Error, waiting for response timed out :: '..msg return nil, 'Error, waiting for response timed out :: '..msg

View file

@ -71,26 +71,6 @@ local function parse14443a(data)
return { uid = uid, atqa = atqa, sak = sak, name = tostring_14443a(sak), data = data} return { uid = uid, atqa = atqa, sak = sak, name = tostring_14443a(sak), data = data}
end 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(command, ignoreresponse)
--core.clearCommandBuffer()
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
-- This function does a connect and retrieves som einfo -- This function does a connect and retrieves som einfo
-- @param dont_disconnect - if true, does not disable the field -- @param dont_disconnect - if true, does not disable the field
-- @return if successfull: an table containing card info -- @return if successfull: an table containing card info
@ -98,7 +78,10 @@ end
local function read14443a(dont_disconnect, no_rats) local function read14443a(dont_disconnect, no_rats)
local command, result, info, err, data local command, result, info, err, data
command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, arg1 = ISO14A_COMMAND.ISO14A_CONNECT } command = Command:newMIX{
cmd = cmds.CMD_READER_ISO_14443a,
arg1 = ISO14A_COMMAND.ISO14A_CONNECT
}
if dont_disconnect then if dont_disconnect then
command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT
@ -106,17 +89,17 @@ local function read14443a(dont_disconnect, no_rats)
if no_rats then if no_rats then
command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_RATS command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_RATS
end end
local result,err = sendToDevice(command) local result,err = command:sendMIX()
if result then if result then
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result)
if arg0 == 0 then if arg0 == 0 then
return nil, "iso14443a card select failed" return nil, 'iso14443a card select failed'
end end
data = string.sub(result,count) data = string.sub(result,count)
info, err = parse14443a(data) info, err = parse14443a(data)
else else
err ="No response from card" err = 'No response from card'
end end
if err then if err then
@ -143,7 +126,6 @@ local library = {
read = read14443a, read = read14443a,
waitFor14443a = waitFor14443a, waitFor14443a = waitFor14443a,
parse14443a = parse14443a, parse14443a = parse14443a,
sendToDevice = sendToDevice,
ISO14A_COMMAND = ISO14A_COMMAND, ISO14A_COMMAND = ISO14A_COMMAND,
} }

View file

@ -7,7 +7,14 @@ local md5 = require('md5')
local dumplib = require('html_dumplib') local dumplib = require('html_dumplib')
local toys = require('default_toys') local toys = require('default_toys')
example =[[ copyright = ''
author = 'Iceman'
version = 'v1.0.1'
desc =[[
This script will try to dump the contents of a Mifare TNP3xxx card.
It will need a valid KeyA in order to find the other keys and decode the card.
]]
example = [[
script run tnp3dump script run tnp3dump
script run tnp3dump -n script run tnp3dump -n
script run tnp3dump -p script run tnp3dump -p
@ -18,11 +25,9 @@ example =[[
script run tnp3dump -p -o myfile script run tnp3dump -p -o myfile
script run tnp3dump -k aabbccddeeff -n -o myfile script run tnp3dump -k aabbccddeeff -n -o myfile
]] ]]
author = "Iceman" usage = [[
usage = "script run tnp3dump -k <key> -n -p -o <filename>" script run tnp3dump -k <key> -n -p -o <filename>
desc =[[
This script will try to dump the contents of a Mifare TNP3xxx card.
It will need a valid KeyA in order to find the other keys and decode the card.
Arguments: Arguments:
-h : this help -h : this help
-k <key> : Sector 0 Key A. -k <key> : Sector 0 Key A.
@ -31,41 +36,44 @@ Arguments:
-o : filename for the saved dumps -o : filename for the saved dumps
]] ]]
local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20' local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
local TIMEOUT = 2500 -- Shouldn't take longer than 2 seconds
local DEBUG = false -- the debug flag local DEBUG = false -- the debug flag
local numBlocks = 64 local numBlocks = 64
local numSectors = 16 local numSectors = 16
--- ---
-- A debug printout-function -- A debug printout-function
function dbg(args) local function dbg(args)
if not DEBUG then return end if not DEBUG then return end
if type(args) == 'table' then
if type(args) == "table" then
local i = 1 local i = 1
while result[i] do while result[i] do
dbg(result[i]) dbg(result[i])
i = i+1 i = i+1
end end
else else
print("###", args) print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
return nil,err core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
function help() local function help()
print(copyright)
print(author)
print(version)
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
print(usage)
end end
-- --
-- Exit message -- Exit message
function ExitMsg(msg) local function ExitMsg(msg)
print( string.rep('--',20) ) print( string.rep('--',20) )
print( string.rep('--',20) ) print( string.rep('--',20) )
print(msg) print(msg)
@ -79,18 +87,18 @@ local function readdumpkeys(infile)
return hex return hex
end end
local function waitCmd() local function getblockdata(response)
local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT) if not response then
if response then return nil, 'No response from device'
local count, cmd, arg0 = bin.unpack('LL',response) end
if(arg0==1) then
local count,arg1,arg2,data = bin.unpack('LLH511',response,count) local count, cmd, arg0 = bin.unpack('LL', response)
return data:sub(1,32) if arg0 == 1 then
else local count, arg1, arg2, data = bin.unpack('LLH511', response, count)
return nil, "Couldn't read block.. ["..arg0.."]" return data:sub(1, 32)
end else
return nil, "Couldn't read block.. ["..arg0.."]"
end end
return nil, 'No response from device'
end end
local function main(args) local function main(args)
@ -98,9 +106,7 @@ local function main(args)
print( string.rep('--',20) ) print( string.rep('--',20) )
print( string.rep('--',20) ) print( string.rep('--',20) )
local keyA local keyA, cmd, err
local cmd
local err
local useNested = false local useNested = false
local usePreCalc = false local usePreCalc = false
local cmdReadBlockString = 'hf mf rdbl %d A %s' local cmdReadBlockString = 'hf mf rdbl %d A %s'
@ -127,9 +133,7 @@ local function main(args)
core.console( cmdSetDbgOff) core.console( cmdSetDbgOff)
result, err = lib14a.read(false, true) result, err = lib14a.read(false, true)
if not result then if not result then return oops(err) end
return oops(err)
end
core.clearCommandBuffer() core.clearCommandBuffer()
@ -159,23 +163,20 @@ local function main(args)
akeys = hex:sub(0,12*16) akeys = hex:sub(0,12*16)
end end
local block0, block1
-- Read block 0 -- Read block 0
dbg('Reading block 0') dbg('Reading block 0')
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0, arg2 = 0, arg3 = 0, data = keyA} cmd = Command:newMIX{cmd = cmds.CMD_MIFARE_READBL, data = keyA}
err = core.SendCommand(cmd:getBytes()) block0, err = getblockdata(cmd:sendMIX(false))
if err then return oops(err) end if not block0 then return oops(err) end
local block0, err = waitCmd()
if err then return oops(err) end
core.clearCommandBuffer() core.clearCommandBuffer()
-- Read block 1 -- Read block 1
dbg('Reading block 1') dbg('Reading block 1')
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1, arg2 = 0, arg3 = 0, data = keyA} cmd = Command:newMIX{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1, data = keyA}
err = core.SendCommand(cmd:getBytes()) block1, err = getblockdata(cmd:sendMIX(false))
if err then return oops(err) end if not block1 then return oops(err) end
local block1, err = waitCmd()
if err then return oops(err) end
core.clearCommandBuffer() core.clearCommandBuffer()
@ -201,12 +202,9 @@ local function main(args)
pos = (math.floor( blockNo / 4 ) * 12)+1 pos = (math.floor( blockNo / 4 ) * 12)+1
key = akeys:sub(pos, pos + 11 ) key = akeys:sub(pos, pos + 11 )
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key} cmd = Command:newMIX{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo, data = key}
local err = core.SendCommand(cmd:getBytes()) local blockdata, err = getblockdata(cmd:sendMIX(false))
if err then return oops(err) end if not blockdata then return oops(err) end
local blockdata, err = waitCmd()
if err then return oops(err) end
if blockNo%4 ~= 3 then if blockNo%4 ~= 3 then

View file

@ -7,27 +7,29 @@ local md5 = require('md5')
local toys = require('default_toys') local toys = require('default_toys')
local pre = require('precalc') local pre = require('precalc')
example =[[ copyright = ''
1. script run tnp3sim author = 'Iceman'
2. script run tnp3sim -m version = 'v1.0.1'
3. script run tnp3sim -m -i myfile
]]
author = "Iceman"
usage = "script run tnp3sim -h -m -i <filename>"
desc =[[ desc =[[
This script will try to load a binary datadump of a Mifare TNP3xxx card. This script will try to load a binary datadump of a Mifare TNP3xxx card.
It vill try to validate all checksums and view some information stored in the dump It vill try to validate all checksums and view some information stored in the dump
For an experimental mode, it tries to manipulate some data. For an experimental mode, it tries to manipulate some data.
At last it sends all data to the PM3 device memory where it can be used in the command "hf mf sim" At last it sends all data to the PM3 device memory where it can be used in the command "hf mf sim"
]]
example =[[
1. script run tnp3sim
2. script run tnp3sim -m
3. script run tnp3sim -m -i myfile
]]
usage = [[
script run tnp3sim -h -m -i <filename>
Arguments: Arguments:
-h : this help -h : this help
-m : Maxed out items (experimental) -m : Maxed out items (experimental)
-i : filename for the datadump to read (bin) -i : filename for the datadump to read (bin)
]]
]]
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
local DEBUG = true -- the debug flag local DEBUG = true -- the debug flag
local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20' local RANDOM = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
@ -42,35 +44,39 @@ local format = string.format
--- ---
-- A debug printout-function -- A debug printout-function
function dbg(args) local function dbg(args)
if not DEBUG then return end if not DEBUG then return end
if type(args) == 'table' then
if type(args) == "table" then
local i = 1 local i = 1
while result[i] do while result[i] do
dbg(result[i]) dbg(result[i])
i = i+1 i = i+1
end end
else else
print("###", args) print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
return nil,err core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
function help() local function help()
print(copyright)
print(author)
print(version)
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
print(usage)
end end
-- --
-- Exit message -- Exit message
function ExitMsg(msg) local function ExitMsg(msg)
print( string.rep('--',20) ) print( string.rep('--',20) )
print( string.rep('--',20) ) print( string.rep('--',20) )
print(msg) print(msg)
@ -78,9 +84,9 @@ function ExitMsg(msg)
end end
local function writedumpfile(infile) local function writedumpfile(infile)
t = infile:read("*all") t = infile:read('*all')
len = string.len(t) len = string.len(t)
local len,hex = bin.unpack(("H%d"):format(len),t) local len,hex = bin.unpack(('H%d'):format(len),t)
return hex return hex
end end
-- blocks with data -- blocks with data
@ -238,9 +244,9 @@ local function LoadEmulator(uid, blocks)
end end
end end
core.clearCommandBuffer() core.clearCommandBuffer()
cmd = Command:new{cmd = cmds.CMD_MIFARE_EML_MEMSET, arg1 = _ ,arg2 = 1,arg3 = 16, data = blockdata} cmd = Command:newMIX{cmd = cmds.CMD_MIFARE_EML_MEMSET, arg1 = _ ,arg2 = 1,arg3 = 16, data = blockdata}
local err = core.SendCommand(cmd:getBytes()) local err, msg = cmd:sendMIX(true)
if err then return err end if err == nil then return err, msg end
end end
io.write('\n') io.write('\n')
end end
@ -348,30 +354,30 @@ local function main(args)
local result, err, hex local result, err, hex
local maxed = false local maxed = false
local inputTemplate = "dumpdata.bin" local inputTemplate = 'dumpdata.bin'
local outputTemplate = os.date("toydump_%Y-%m-%d_%H%M"); local outputTemplate = os.date('toydump_%Y-%m-%d_%H%M');
-- Arguments for the script -- Arguments for the script
for o, a in getopt.getopt(args, 'hmi:o:') do for o, a in getopt.getopt(args, 'hmi:o:') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "m" then maxed = true end if o == 'm' then maxed = true end
if o == "o" then outputTemplate = a end if o == 'o' then outputTemplate = a end
if o == "i" then inputTemplate = a end if o == 'i' then inputTemplate = a end
end end
-- Turn off Debug -- Turn off Debug
local cmdSetDbgOff = "hf mf dbg 0" local cmdSetDbgOff = 'hf mf dbg 0'
core.console( cmdSetDbgOff) core.console( cmdSetDbgOff)
-- Load dump.bin file -- Load dump.bin file
print( (' Load data from %s'):format(inputTemplate)) print( ('Load data from %s'):format(inputTemplate))
hex, err = utils.ReadDumpFile(inputTemplate) hex, err = utils.ReadDumpFile(inputTemplate)
if not hex then return oops(err) end if not hex then return oops(err) end
local blocks = {} local blocks = {}
local blockindex = 0 local blockindex = 0
for i = 1, #hex, 32 do for i = 1, #hex, 32 do
blocks[blockindex] = hex:sub(i,i+31) blocks[blockindex] = hex:sub(i, i+31)
blockindex = blockindex + 1 blockindex = blockindex + 1
end end
@ -397,8 +403,8 @@ local function main(args)
print( (' ITEM TYPE : 0x%s 0x%s'):format(toytype, subtype) ) print( (' ITEM TYPE : 0x%s 0x%s'):format(toytype, subtype) )
end end
print( (' UID : 0x%s'):format(uid) ) print( (' UID : %s'):format(uid) )
print( (' CARDID : 0x%s %s [%s]'):format( print( (' CARDID : %s %s [%s]'):format(
cardidMsw,cardidLsw, cardidMsw,cardidLsw,
--Num2Card(cardidMsw, cardidLsw)) --Num2Card(cardidMsw, cardidLsw))
'') '')

View file

@ -4,11 +4,9 @@ local bin = require('bin')
local utils = require('utils') local utils = require('utils')
local dumplib = require('html_dumplib') local dumplib = require('html_dumplib')
example =[[ copyright = ''
1. script run tracetest author = 'Iceman'
]] version = 'v1.0.1'
author = "Iceman"
usage = "script run tracetest"
desc =[[ desc =[[
This script will load several traces files in ../traces/ folder and do This script will load several traces files in ../traces/ folder and do
"data load" "data load"
@ -17,45 +15,52 @@ This script will load several traces files in ../traces/ folder and do
The following tracefiles will be loaded: The following tracefiles will be loaded:
em*.pm3 em*.pm3
m*.pm3 m*.pm3
]]
example =[[
script run tracetest
]]
usage = [[
script run tracetest -h
Arguments: Arguments:
-h : this help -h : this help
]] ]]
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
local DEBUG = true -- the debug flag local DEBUG = true -- the debug flag
--- ---
-- A debug printout-function -- A debug printout-function
function dbg(args) local function dbg(args)
if not DEBUG then if not DEBUG then return end
return if type(args) == 'table' then
end
if type(args) == "table" then
local i = 1 local i = 1
while result[i] do while result[i] do
dbg(result[i]) dbg(result[i])
i = i+1 i = i+1
end end
else else
print("###", args) print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
function help() local function help()
print(copyright)
print(author)
print(version)
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
print(usage)
end end
-- --
-- Exit message -- Exit message
function ExitMsg(msg) local function ExitMsg(msg)
print( string.rep('--',20) ) print( string.rep('--',20) )
print( string.rep('--',20) ) print( string.rep('--',20) )
print(msg) print(msg)
@ -73,11 +78,11 @@ local function main(args)
local tracesMOD = "find '../traces/' -iname 'm*.pm3' -type f" local tracesMOD = "find '../traces/' -iname 'm*.pm3' -type f"
local write2File = false local write2File = false
local outputTemplate = os.date("testtest_%Y-%m-%d_%H%M%S") local outputTemplate = os.date('testtest_%Y-%m-%d_%H%M%S')
-- Arguments for the script -- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do for o, arg in getopt.getopt(args, 'h') do
if o == "h" then return help() end if o == 'h' then return help() end
end end
core.clearCommandBuffer() core.clearCommandBuffer()
@ -98,13 +103,13 @@ local function main(args)
end end
p.close(); p.close();
local cmdLFSEARCH = "lf search 1 u" local cmdLFSEARCH = 'lf search 1 u'
-- main loop -- main loop
io.write('Starting to test traces > ') io.write('Starting to test traces > ')
for _,file in pairs(files) do for _,file in pairs(files) do
local x = "data load "..file local x = 'data load '..file
dbg(x) dbg(x)
core.console(x) core.console(x)
@ -114,7 +119,7 @@ local function main(args)
core.clearCommandBuffer() core.clearCommandBuffer()
if core.ukbhit() then if core.ukbhit() then
print("aborted by user") print('aborted by user')
break break
end end
end end

View file

@ -2,17 +2,25 @@ local cmds = require('commands')
local getopt = require('getopt') local getopt = require('getopt')
local lib14a = require('read14a') local lib14a = require('read14a')
local utils = require('utils') local utils = require('utils')
example = [[
script run ufodump copyright = ''
script run ufodump -b 10 author = 'Iceman'
]] version = 'v1.0.1'
author = "Iceman"
desc = desc =
[[ [[
This is a script that reads AZTEK iso14443a tags. 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. 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. xor: the first three block (0,1,2) is not XORED. The rest seems to be xored.
]]
example = [[
-- default
script run ufodump
-- stop at block 10
script run ufodump -b 10
]]
usage = [[
script run ufudump -h -b
Arguments: Arguments:
h this helptext h this helptext
@ -20,43 +28,54 @@ Arguments:
]] ]]
-- Some globals -- Some globals
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
local DEBUG = false -- the debug flag local DEBUG = false -- the debug flag
--- ---
-- A debug printout-function -- A debug printout-function
local function dbg(args) local function dbg(args)
if DEBUG then if not DEBUG then return end
print("###", args) if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
local function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
core.clearCommandBuffer() core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
local function help() local function help()
print(copyright)
print(author)
print(version)
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
print(usage)
end end
-- --
-- writes data to ascii textfile. -- writes data to ascii textfile.
function writeDumpFile(uid, blockData) function writeDumpFile(uid, blockData)
local destination = string.format("%s.eml", uid) local destination = string.format('%s.eml', uid)
local file = io.open(destination, "w") local file = io.open(destination, 'w')
if file == nil then if file == nil then
return nil, string.format("Could not write to file %s", destination) return nil, string.format('Could not write to file %s', destination)
end end
local rowlen = string.len(blockData[1]) local rowlen = string.len(blockData[1])
for i,block in ipairs(blockData) do for i,block in ipairs(blockData) do
if rowlen ~= string.len(block) then if rowlen ~= string.len(block) then
print(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i)) print(string.format('WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1',i))
end end
file:write(block.."\n") file:write(block..'\n')
end end
file:close() file:close()
return destination return destination
@ -91,24 +110,28 @@ end
-- --
-- Send a "raw" iso14443a package, ie "hf 14a raw" command -- Send a "raw" iso14443a package, ie "hf 14a raw" command
function sendRaw(rawdata, options) function sendRaw(rawdata, options)
--print(">> ", rawdata)
local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC + lib14a.ISO14A_COMMAND.ISO14A_NO_RATS local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT
local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, + lib14a.ISO14A_COMMAND.ISO14A_RAW
+ lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC
+ lib14a.ISO14A_COMMAND.ISO14A_NO_RATS
local command = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443a,
arg1 = flags, -- Send raw arg1 = flags, -- Send raw
-- arg2 contains the length, which is half the length -- arg2 contains the length, which is half the length
-- of the ASCII-string rawdata -- of the ASCII-string rawdata
arg2 = string.len(rawdata)/2, arg2 = string.len(rawdata)/2,
data = rawdata} data = rawdata}
return lib14a.sendToDevice(command, options.ignore_response)
return command:sendMIX(options.ignore_response)
end end
-- --
-- Sends an instruction to do nothing, only disconnect -- Sends an instruction to do nothing, only disconnect
function disconnect() function disconnect()
local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, arg1 = 0,} 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 -- We can ignore the response here, no ACK is returned for this command
-- Check /armsrc/iso14443a.c, ReaderIso14443a() for details -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details
return lib14a.sendToDevice(command, true) return command:sendMIX(true)
--core.console("hf 14a raw -r")
end end
--- ---
-- The main entry point -- The main entry point
@ -119,8 +142,8 @@ function main(args)
-- Read the parameters -- Read the parameters
for o, a in getopt.getopt(args, 'hb:') do for o, a in getopt.getopt(args, 'hb:') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "b" then endblock = a end if o == 'b' then endblock = a end
end end
endblock = endblock or 20 endblock = endblock or 20
@ -132,12 +155,12 @@ function main(args)
local blockData = {} local blockData = {}
-- Show tag info -- Show tag info
print(("\nFound Card UID [%s]\n"):format(info.uid)) print(('\nFound Card UID [%s]\n'):format(info.uid))
print("blk | data | xored") print('blk | data | xored')
print("----+------------------+-------------------") print('----+------------------+-------------------')
for block = 00, endblock do for block = 00, endblock do
local cmd = string.format("10%02x00", block) local cmd = string.format('10%02x00', block)
res, err = sendRaw(cmd , {ignore_response = ignore_response}) res, err = sendRaw(cmd , {ignore_response = ignore_response})
if err then disconnect() return oops(err) end if err then disconnect() return oops(err) end
@ -154,7 +177,7 @@ function main(args)
local filename, err = writeDumpFile(info.uid, blockData) local filename, err = writeDumpFile(info.uid, blockData)
if err then return oops(err) end if err then return oops(err) end
print(string.format("\nDumped data into %s", filename)) print(string.format('\nDumped data into %s', filename))
end end
------------------------- -------------------------
@ -162,13 +185,13 @@ end
------------------------- -------------------------
function selftest() function selftest()
DEBUG = true DEBUG = true
dbg("Performing test") dbg('Performing test')
main() main()
dbg("Tests done") dbg('Tests done')
end end
-- Flip the switch here to perform a sanity check. -- Flip the switch here to perform a sanity check.
-- It read a nonce in two different ways, as specified in the usage-section -- It read a nonce in two different ways, as specified in the usage-section
if "--test"==args then if '--test' == args then
selftest() selftest()
else else
-- Call the main -- Call the main

View file

@ -3,7 +3,7 @@ local utils = require('utils')
copyright = '' copyright = ''
author = "Iceman" author = "Iceman"
version = 'v1.0.0' version = 'v1.0.1'
desc = [[ desc = [[
This script tries to set UID on a mifare Ultralight magic card which either This script tries to set UID on a mifare Ultralight magic card which either
- answers to chinese backdoor commands - answers to chinese backdoor commands
@ -31,20 +31,21 @@ local bxor = bit32.bxor
-- A debug printout-function -- A debug printout-function
local function dbg(args) local function dbg(args)
if not DEBUG then return end if not DEBUG then return end
if type(args) == "table" then if type(args) == 'table' then
local i = 1 local i = 1
while args[i] do while args[i] do
dbg(args[i]) dbg(args[i])
i = i+1 i = i+1
end end
else else
print("###", args) print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
local function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
core.clearCommandBuffer()
return nil, err return nil, err
end end
--- ---
@ -56,6 +57,7 @@ local function help()
print(desc) print(desc)
print('Example usage') print('Example usage')
print(example) print(example)
print(usage)
end end
-- --
--- Set UID on magic command enabled --- Set UID on magic command enabled
@ -64,19 +66,19 @@ function magicUID(b0, b1, b2)
print('Using backdoor Magic tag function') print('Using backdoor Magic tag function')
-- write block 0 -- write block 0
core.console("hf 14a raw -p -a -b 7 40") core.console('hf 14a raw -p -a -b 7 40')
core.console("hf 14a raw -p -a 43") core.console('hf 14a raw -p -a 43')
core.console("hf 14a raw -c -a A200"..b0) core.console('hf 14a raw -c -a A200'..b0)
-- write block 1 -- write block 1
core.console("hf 14a raw -p -a -b 7 40") core.console('hf 14a raw -p -a -b 7 40')
core.console("hf 14a raw -p -a 43") core.console('hf 14a raw -p -a 43')
core.console("hf 14a raw -c -a A201"..b1) core.console('hf 14a raw -c -a A201'..b1)
-- write block 2 -- write block 2
core.console("hf 14a raw -p -a -b 7 40") core.console('hf 14a raw -p -a -b 7 40')
core.console("hf 14a raw -p -a 43") core.console('hf 14a raw -p -a 43')
core.console("hf 14a raw -c -a A202"..b2) core.console('hf 14a raw -c -a A202'..b2)
end end
-- --
--- Set UID on magic but brickable --- Set UID on magic but brickable
@ -84,16 +86,16 @@ function brickableUID(b0, b1, b2)
print('Using BRICKABLE Magic tag function') print('Using BRICKABLE Magic tag function')
core.console("hf 14a raw -p -s -3") core.console('hf 14a raw -p -s -3')
-- write block 0 -- write block 0
core.console("hf 14a raw -p -c A200"..b0) core.console('hf 14a raw -p -c A200'..b0)
-- write block 1 -- write block 1
core.console("hf 14a raw -p -c A201"..b1) core.console('hf 14a raw -p -c A201'..b1)
-- write block 2 -- write block 2
core.console("hf 14a raw -p -c A202"..b2) core.console('hf 14a raw -p -c A202'..b2)
end end
--- ---
-- The main entry point -- The main entry point
@ -108,9 +110,9 @@ function main(args)
-- Read the parameters -- Read the parameters
for o, a in getopt.getopt(args, 'hu:b') do for o, a in getopt.getopt(args, 'hu:b') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "u" then uid = a end if o == 'u' then uid = a end
if o == "b" then tagtype = 2 end if o == 'b' then tagtype = 2 end
end end
-- uid string checks -- uid string checks
@ -138,7 +140,7 @@ function main(args)
end end
--halt --halt
core.console("hf 14a raw -c -a 5000") core.console('hf 14a raw -c -a 5000')
end end
main(args) main(args)