| 1 | local bin = require('bin') |
| 2 | local getopt = require('getopt') |
| 3 | local utils = require('utils') |
| 4 | |
| 5 | local bxor=bit32.bxor |
| 6 | |
| 7 | example =[[ |
| 8 | script run calc_mizip |
| 9 | script run calc_mizip -u 11223344 |
| 10 | ]] |
| 11 | author = "Iceman" |
| 12 | usage = "script run calc_mizip -u <uid>" |
| 13 | desc =[[ |
| 14 | This script calculates mifare keys based on uid diversification for mizip. |
| 15 | Algo not found by me. |
| 16 | Arguments: |
| 17 | -h : this help |
| 18 | -u <UID> : UID |
| 19 | ]] |
| 20 | --- |
| 21 | -- A debug printout-function |
| 22 | function dbg(args) |
| 23 | if type(args) == "table" then |
| 24 | local i = 1 |
| 25 | while args[i] do |
| 26 | dbg(args[i]) |
| 27 | i = i+1 |
| 28 | end |
| 29 | else |
| 30 | print("###", args) |
| 31 | end |
| 32 | end |
| 33 | --- |
| 34 | -- This is only meant to be used when errors occur |
| 35 | function oops(err) |
| 36 | print("ERROR: ",err) |
| 37 | return nil,err |
| 38 | end |
| 39 | --- |
| 40 | -- Usage help |
| 41 | function help() |
| 42 | print(desc) |
| 43 | print("Example usage") |
| 44 | print(example) |
| 45 | end |
| 46 | -- |
| 47 | -- Exit message |
| 48 | function exitMsg(msg) |
| 49 | print( string.rep('--',20) ) |
| 50 | print( string.rep('--',20) ) |
| 51 | print(msg) |
| 52 | print() |
| 53 | end |
| 54 | |
| 55 | local _xortable = { |
| 56 | --[[ sector key A/B, 6byte xor |
| 57 | --]] |
| 58 | {"001","09125a2589e5","F12C8453D821"}, |
| 59 | {"002","AB75C937922F","73E799FE3241"}, |
| 60 | {"003","E27241AF2C09","AA4D137656AE"}, |
| 61 | {"004","317AB72F4490","B01327272DFD"}, |
| 62 | } |
| 63 | local function printRow(sector, keyA, keyB) |
| 64 | print('|'..sector..'| '..keyA..' | '..keyB..' |' ) |
| 65 | end |
| 66 | local function keyStr(p1, p2, p3, p4, p5, p6) |
| 67 | return string.format('%02X%02X%02X%02X%02X%02X',p1, p2, p3, p4, p5, p6) |
| 68 | end |
| 69 | local function calckey(uid, xorkey, keytype) |
| 70 | local p1,p2,p3,p4,p5,p6 |
| 71 | if keytype == 'A' then |
| 72 | p1 = bxor( uid[1], xorkey[1]) |
| 73 | p2 = bxor( uid[2], xorkey[2]) |
| 74 | p3 = bxor( uid[3], xorkey[3]) |
| 75 | p4 = bxor( uid[4], xorkey[4]) |
| 76 | p5 = bxor( uid[1], xorkey[5]) |
| 77 | p6 = bxor( uid[2], xorkey[6]) |
| 78 | else |
| 79 | p1 = bxor( uid[3], xorkey[1]) |
| 80 | p2 = bxor( uid[4], xorkey[2]) |
| 81 | p3 = bxor( uid[1], xorkey[3]) |
| 82 | p4 = bxor( uid[2], xorkey[4]) |
| 83 | p5 = bxor( uid[3], xorkey[5]) |
| 84 | p6 = bxor( uid[4], xorkey[6]) |
| 85 | end |
| 86 | return keyStr(p1,p2,p3,p4,p5,p6) |
| 87 | end |
| 88 | local function main(args) |
| 89 | |
| 90 | print( string.rep('==', 30) ) |
| 91 | print() |
| 92 | |
| 93 | local i,j, pwd |
| 94 | local uid = '11223344' |
| 95 | |
| 96 | -- Arguments for the script |
| 97 | for o, a in getopt.getopt(args, 'hu:') do |
| 98 | if o == "h" then return help() end |
| 99 | if o == "u" then uid = a end |
| 100 | end |
| 101 | |
| 102 | -- uid string checks |
| 103 | if uid == nil then return oops('empty uid string') end |
| 104 | if #uid == 0 then return oops('empty uid string') end |
| 105 | if #uid ~= 8 then return oops('uid wrong length. Should be 4 hex bytes') end |
| 106 | |
| 107 | local uidbytes = utils.ConvertHexToBytes(uid) |
| 108 | |
| 109 | print('|UID|', uid) |
| 110 | print('|---|----------------|----------------|') |
| 111 | print('|sec|key A |key B |') |
| 112 | print('|---|----------------|----------------|') |
| 113 | printRow('000', keyStr(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5), keyStr(0xB4,0xC1,0x32,0x43,0x9e,0xef) ) |
| 114 | |
| 115 | for k, v in pairs(_xortable) do |
| 116 | local keyA = calckey(uidbytes, utils.ConvertHexToBytes(v[2]), 'A') |
| 117 | local keyB = calckey(uidbytes, utils.ConvertHexToBytes(v[3]), 'B') |
| 118 | printRow(v[1], keyA, keyB ) |
| 119 | end |
| 120 | print('|---|----------------|----------------|') |
| 121 | end |
| 122 | |
| 123 | main(args) |