This commit is contained in:
iceman1001 2019-04-29 01:51:00 +02:00
parent 47fbb557b4
commit 2b561ad732
2 changed files with 43 additions and 41 deletions

View file

@ -8,26 +8,22 @@
Copyright (C) 2013 m h swende <martin at swende.se> Copyright (C) 2013 m h swende <martin at swende.se>
--]] --]]
-- Loads the commands-library
local cmds = require('commands') local cmds = require('commands')
-- Load the default keys
local keylist = require('mf_default_keys') local keylist = require('mf_default_keys')
-- Ability to read what card is there
local lib14a = require('read14a') local lib14a = require('read14a')
local getopt = require('getopt') local getopt = require('getopt')
-- Asks the user for input
local utils = require('utils') local utils = require('utils')
example =[[ copyright = ''
script run mfkeys
]]
author = "Holiman" author = "Holiman"
usage = "script run mfkeys" version = 'v1.0.1'
desc = ("This script implements Mifare check keys.\ desc = ("This script implements Mifare check keys.\
It utilises a large list of default keys (currently %d keys).\ It utilises a large list of default keys (currently %d keys).\
If you want to add more, just put them inside /lualibs/mf_default_keys.lua\n"):format(#keylist) .. If you want to add more, just put them inside /lualibs/mf_default_keys.lua\n"):format(#keylist)
[[ example = [[
1. script run mfkeys
]]
usage = [[
Arguments: Arguments:
-h : this help -h : this help
-p : print keys -p : print keys
@ -37,36 +33,36 @@ local TIMEOUT = 10000 -- 10 seconds
--- ---
-- 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)
return nil,err 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
-- --
-- waits for answer from pm3 device -- waits for answer from pm3 device
local function checkCommand(command) local function checkCommand(response)
core.clearCommandBuffer() if not response then
local usb = command:getBytes()
core.SendCommand(usb)
local result = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT)
if result then
local count, cmd, arg0 = bin.unpack('LL',result)
if(arg0==1) then
local count, arg1, arg2, data = bin.unpack('LLH511',result,count)
key = data:sub(1,12)
return key
else
return nil
end
else
print("Timeout while waiting for response. Increase TIMEOUT in mfkeys.lua to wait longer") print("Timeout while waiting for response. Increase TIMEOUT in mfkeys.lua to wait longer")
return nil, "Timeout while waiting for device to respond" return nil, "Timeout while waiting for device to respond"
end end
local count, cmd, arg0, arg1, arg2, data = bin.unpack('LLLLH40',result)
if arg0 == 1 then
key = data:sub(1, 12)
return key
end
return nil
end end
local function checkBlock(blockno, testkeys, keytype) local function checkBlock(blockno, testkeys, keytype)
@ -85,13 +81,15 @@ local function checkBlock(blockno, testkeys, keytype)
local d1 = table.concat(testkeys, "", start, n) local d1 = table.concat(testkeys, "", start, n)
core.clearCommandBuffer()
print(("Testing block %d, keytype %d, with %d keys"):format(blockno, keytype, chunksize)) print(("Testing block %d, keytype %d, with %d keys"):format(blockno, keytype, chunksize))
local command = Command:new{cmd = cmds.CMD_MIFARE_CHKKEYS, local c = Command:newMIX{cmd = cmds.CMD_MIFARE_CHKKEYS,
arg1 = arg1, arg1 = arg1,
arg2 = 0,
arg3 = chunksize, arg3 = chunksize,
data = d1} data = d1}
local status = checkCommand(command) status, err = checkCommand(cmd:sendMIX(false, TIMEOUT))
if status then return status, blockno end if status then return status, blockno end
start = start + chunksize start = start + chunksize
remaining = remaining - chunksize remaining = remaining - chunksize
@ -101,7 +99,7 @@ local function checkBlock(blockno, testkeys, keytype)
end end
return nil return nil
end end
---
-- A function to display the results -- A function to display the results
local function display_results(keys) local function display_results(keys)
local sector, keyA, keyB, succA, succB local sector, keyA, keyB, succA, succB
@ -116,6 +114,7 @@ local function display_results(keys)
end end
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')
end end
---
-- A little helper to place an item first in the list -- A little helper to place an item first in the list
local function placeFirst(akey, list) local function placeFirst(akey, list)
akey = akey:lower() akey = akey:lower()
@ -179,12 +178,16 @@ local function dumptofile(keys)
file:close() file:close()
end end
end end
---
--
local function printkeys() local function printkeys()
for i=1, #keylist do for i=1, #keylist do
print(i, keylist[i]) print(i, keylist[i])
end end
print ('Number of keys: '..#keylist) print ('Number of keys: '..#keylist)
end end
---
--
local function perform_check(numsectors) local function perform_check(numsectors)
local keyType = 0 -- A=0, B=1 local keyType = 0 -- A=0, B=1
@ -256,8 +259,8 @@ local function main(args)
-- Arguments for the script -- Arguments for the script
for o, a in getopt.getopt(args, 'hp') do for o, a in getopt.getopt(args, 'hp') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == "p" then return printkeys() end if o == 'p' then return printkeys() end
end end
-- identify tag -- identify tag
tag, err = lib14a.read(false, true) tag, err = lib14a.read(false, true)

View file

@ -77,15 +77,14 @@ end
local function getblockdata(response) local function getblockdata(response)
if not response then if not response then
return nil, 'No response from device' return nil, 'No response from device'
end end
local count, cmd, arg0 = bin.unpack('LL', response) local count, cmd, arg0, arg1, arg2, data = bin.unpack('LLLLH40', response)
if arg0 == 1 then if arg0 == 1 then
local count, arg1, arg2, data = bin.unpack('LLH511', response, count)
return data:sub(1, 32) return data:sub(1, 32)
else end
return nil, "Couldn't read block"
end return nil, "Couldn't read block"
end end
---_ Gets data from a block ---_ Gets data from a block
-- @return {block, block+1, block+2, block+3} if successfull -- @return {block, block+1, block+2, block+3} if successfull