hf_mf_ultimatecard: '-w 0' now formats whole Mifare classic 4k

This commit is contained in:
DidierA 2022-11-25 10:16:24 +01:00
commit c54db094cd

View file

@ -228,7 +228,6 @@ local function read_config()
if ulmode == '03' then versionstr = 'Ultralight' if ulmode == '03' then versionstr = 'Ultralight'
elseif ulmode == '02' then versionstr = 'Ultralight-C' elseif ulmode == '02' then versionstr = 'Ultralight-C'
elseif cversion == '0004030101000B03' then versionstr = 'UL EV1 48b' elseif cversion == '0004030101000B03' then versionstr = 'UL EV1 48b'
elseif cversion == '0004030101000B03' then versionstr = 'UL EV1 48b'
elseif cversion == '0004030101000E03' then versionstr = 'UL EV1 128b' elseif cversion == '0004030101000E03' then versionstr = 'UL EV1 128b'
elseif cversion == '0004040101000B03' then versionstr = 'NTAG 210' elseif cversion == '0004040101000B03' then versionstr = 'NTAG 210'
elseif cversion == '0004040101000E03' then versionstr = 'NTAG 212' elseif cversion == '0004040101000E03' then versionstr = 'NTAG 212'
@ -845,86 +844,101 @@ local function set_type(tagtype)
end end
end end
--- ---
-- returns true if b is the index of a sector trailer
local function mfIsSectorTrailer(b)
n=b+1
if (n < 32*4 ) then
if (n % 4 == 0) then return true
else return false
end
end
if (n % 16 == 0) then return true
end
return false
end
---
-- wipe tag -- wipe tag
local function wipe(wtype) local function wipe(wtype)
local info = connect() local info = connect()
if not info then return false, "Can't select card" end if not info then return false, "Can't select card" end
if wtype == '0' then if wtype == '0' then
print('Starting Mifare Wipe') print('Starting Mifare Wipe')
send("CF".._key.."F000000000000002000978009102DABC19101011121314151604000800") send("CF".._key.."F000000000000002000978009102DABC19101011121314151604000800")
send("CF".._key.."CD000102030404080400000000000000BEAF") send("CF".._key.."CD000102030404080400000000000000BEAF")
local err, msg, resp local err, msg, resp
local cmd_empty = 'CF'.._key..'CD%02X00000000000000000000000000000000' local cmd_empty = 'CF'.._key..'CD%02X00000000000000000000000000000000'
local cmd_cfg1 = 'CF'.._key..'CD%02XFFFFFFFFFFFFFF078069FFFFFFFFFFFF' local cmd_trail = 'CF'.._key..'CD%02XFFFFFFFFFFFFFF078069FFFFFFFFFFFF'
for b = 1, 0xFB do for b = 1, 0xFF do
if b == 0x03 or b == 0x07 or b == 0x0B or b == 0x0F or b == 0x13 or b == 0x17 or b == 0x1B or b == 0x1F or b == 0x23 or b == 0x27 or b == 0x2B or b == 0x2F or b == 0x33 or b == 0x37 or b == 0x3B or b == 0x3F then if mfIsSectorTrailer(b) then
local cmd = (cmd_cfg1):format(b) local cmd = (cmd_trail):format(b)
resp = send(cmd) resp = send(cmd)
else else
local cmd = (cmd_empty):format(b) local cmd = (cmd_empty):format(b)
resp = send(cmd) resp = send(cmd)
end end
if resp == nil then if resp == nil then
io.write('\nwrote block '..b, ' failed\n') io.write('\nwrote block '..b, ' failed\n')
err = true err = true
else else
io.write('.') io.write('.')
end end
io.flush() io.flush()
end end
print('\n') print('\n')
err, msg = set_type(3) err, msg = set_type(3)
if err == nil then return err, msg end if err == nil then return err, msg end
lib14a.disconnect() lib14a.disconnect()
return true, 'Ok' return true, 'Ok'
elseif wtype == '1' then elseif wtype == '1' then
print('Starting Ultralight Wipe') print('Starting Ultralight Wipe')
local err, msg, resp local err, msg, resp
local cmd_empty = 'A2%02X00000000' local cmd_empty = 'A2%02X00000000'
local cmd_cfg1 = 'A2%02X000000FF' local cmd_cfg1 = 'A2%02X000000FF'
local cmd_cfg2 = 'A2%02X00050000' local cmd_cfg2 = 'A2%02X00050000'
print('Wiping tag') print('Wiping tag')
local info = connect() local info = connect()
if not info then return false, "Can't select card" end if not info then return false, "Can't select card" end
send("CF".._key.."F001010000000003000978009102DABC19101011121314151644000001") send("CF".._key.."F001010000000003000978009102DABC19101011121314151644000001")
for b = 3, 0xFB do for b = 3, 0xFB do
--configuration block 0 --configuration block 0
if b == 0x29 or b == 0x83 or b == 0xe3 then if b == 0x29 or b == 0x83 or b == 0xe3 then
local cmd = (cmd_cfg1):format(b) local cmd = (cmd_cfg1):format(b)
resp = send(cmd) resp = send(cmd)
--configuration block 1 --configuration block 1
elseif b == 0x2a or b == 0x84 or b == 0xe4 then elseif b == 0x2a or b == 0x84 or b == 0xe4 then
local cmd = (cmd_cfg2):format(b) local cmd = (cmd_cfg2):format(b)
resp = send(cmd) resp = send(cmd)
else else
resp = send(cmd_empty:format(b)) resp = send(cmd_empty:format(b))
end end
if resp == '04' or #resp == 0 then if resp == '04' or #resp == 0 then
io.write('\nwrote block '..b, ' failed\n') io.write('\nwrote block '..b, ' failed\n')
err = true err = true
else else
io.write('.') io.write('.')
end end
io.flush() io.flush()
end end
io.write('\r\n') io.write('\r\n')
lib14a.disconnect() lib14a.disconnect()
print('\n') print('\n')
if err then return nil, "Tag locked down, "..err_lock end if err then return nil, "Tag locked down, "..err_lock end
-- set NTAG213 default values -- set NTAG213 default values
err, msg = set_type(14) err, msg = set_type(14)
if err == nil then return err, msg end if err == nil then return err, msg end
--set UID --set UID
err, msg = write_uid('04112233445566') err, msg = write_uid('04112233445566')
if err == nil then return err, msg end if err == nil then return err, msg end
--set NTAG pwd --set NTAG pwd
err, msg = write_ntagpwd('FFFFFFFF') err, msg = write_ntagpwd('FFFFFFFF')
if err == nil then return err, msg end if err == nil then return err, msg end
--set pack --set pack
err, msg = write_pack('0000') err, msg = write_pack('0000')
if err == nil then return err, msg end if err == nil then return err, msg end
lib14a.disconnect() lib14a.disconnect()
return true, 'Ok' return true, 'Ok'
else oops('Use 0 for Mifare wipe or 1 for Ultralight wipe') else oops('Use 0 for Mifare wipe or 1 for Ultralight wipe')
end end
end end