1 local bin = require('bin')
2 local getopt = require('getopt')
3 local utils = require('utils')
9 script run calc_mizip -u 11223344
12 usage = "script run calc_mizip -u <uid>"
14 This script calculates mifare keys based on uid diversification for mizip.
21 -- A debug printout-function
23 if type(args) == "table" then
34 -- This is only meant to be used when errors occur
43 print("Example usage")
49 print( string.rep('--',20) )
50 print( string.rep('--',20) )
56 --[[ sector key A/B, 6byte xor
58 {"001","09125a2589e5","F12C8453D821"},
59 {"002","AB75C937922F","73E799FE3241"},
60 {"003","E27241AF2C09","AA4D137656AE"},
61 {"004","317AB72F4490","B01327272DFD"},
63 local function printRow(sector, keyA, keyB)
64 print('|'..sector..'| '..keyA..' | '..keyB..' |' )
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)
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])
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])
86 return keyStr(p1,p2,p3,p4,p5,p6)
88 local function main(args)
90 print( string.rep('==', 30) )
94 local uid = '11223344'
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
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
107 local uidbytes = utils.ConvertHexToBytes(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) )
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 )
120 print('|---|----------------|----------------|')