1 local getopt
= require('getopt')
2 local ansicolors
= require('ansicolors')
8 This script tries to decode Mifare Classic Access bytes
11 1. script run data_mf_accessdecode -a 7F0F0869
14 script run data_mf_accessdecode [-h] [-a <access bytes>]
18 -a <access bytes> : 4 bytes ACCESS CONDITIONS
22 local bxor
= bit32
.bxor
23 local band
= bit32
.band
24 local rshift
= bit32
.rshift
27 -- A debug printout-function
28 local function dbg(args
)
29 if not DEBUG
then return end
30 if type(args
) == 'table' then
41 -- This is only meant to be used when errors occur
42 local function oops(err
)
44 core
.clearCommandBuffer()
54 print(ansicolors
.cyan
..'Usage'..ansicolors
.reset
)
56 print(ansicolors
.cyan
..'Arguments'..ansicolors
.reset
)
58 print(ansicolors
.cyan
..'Example usage'..ansicolors
.reset
)
62 local access_condition_sector_trailer
= {}
63 access_condition_sector_trailer
[0x0] = {'never','key A','key A','never','key A','key A'}
64 access_condition_sector_trailer
[0x2] = {'never','never','key A','never','key A','never'}
65 access_condition_sector_trailer
[0x4] = {'never','key B','key A|B','never','never','key B'}
66 access_condition_sector_trailer
[0x6] = {'never','never','key A|B','never','never','never'}
67 access_condition_sector_trailer
[0x1] = {'never','key A','key A','key A','key A','key A'}
68 access_condition_sector_trailer
[0x3] = {'never','key B','key A|B','key B','never','key B'}
69 access_condition_sector_trailer
[0x5] = {'never','never','key A|B','key B','never','never'}
70 access_condition_sector_trailer
[0x7] = {'never','never','key A|B','never','never','never'}
72 local access_condition_data_block
= {}
73 access_condition_data_block
[0x0] = {'key A|B','key A|B','key A|B','key A|B'}
74 access_condition_data_block
[0x2] = {'key A|B','never','never','never'}
75 access_condition_data_block
[0x4] = {'key A|B','key B','never','never'}
76 access_condition_data_block
[0x6] = {'key A|B','key B','key B','key A|B'}
77 access_condition_data_block
[0x1] = {'key A|B','never','never','key A|B'}
78 access_condition_data_block
[0x3] = {'key B','key B','never','never'}
79 access_condition_data_block
[0x5] = {'key B','never','never','never'}
80 access_condition_data_block
[0x7] = {'never','never','never','never'}
82 local function main(args
)
84 print( string.rep('--',20) )
85 print( string.rep('--',20) )
90 -- Read the parameters
91 for o
, a
in getopt
.getopt(args
, 'ha:') do
92 if o
== 'h' then return help() end
93 if o
== 'a' then access
= a
end
96 if access
== nil then return oops('empty ACCESS CONDITIONS') end
97 if #access
== 0 then return oops('empty ACCESS CONDITIONS') end
98 if #access
~= 8 then return oops('Wrong length. Should be 4 hex bytes ACCESS CONDITIONS (e.g. 7F0F0869)') end
100 local c2_b
= tonumber(string.sub(access
, 1, 1), 16)
101 local c1_b
= tonumber(string.sub(access
, 2, 2), 16)
102 local c1
= tonumber(string.sub(access
, 3, 3), 16)
103 local c3_b
= tonumber(string.sub(access
, 4, 4), 16)
104 local c3
= tonumber(string.sub(access
, 5, 5), 16)
105 local c2
= tonumber(string.sub(access
, 6, 6), 16)
106 local gpb
= string.sub(access
, 7, 8)
108 if bxor(c1
, c1_b
) ~= 0xF then print('!!! bitflip in c1') end
109 if bxor(c2
, c2_b
) ~= 0xF then print('!!! bitflip in c2') end
110 if bxor(c3
, c3_b
) ~= 0xF then print('!!! bitflip in c3') end
112 local ab
= c1
* 256 + c2
* 16 + c3
115 print('--> block '..block
)
116 -- mask bits for block
117 local abi
= band(rshift(ab
, block
), 0x111)
119 abi
= band(abi
+ rshift(abi
, 3) + rshift(abi
, 6),7)
122 print(' KEYSECXA read: '..access_condition_sector_trailer
[abi
][1])
123 print(' KEYSECXA write: '..access_condition_sector_trailer
[abi
][2])
124 print(' ACCESS COND. read: '..access_condition_sector_trailer
[abi
][3])
125 print('ACCESS COND. write: '..access_condition_sector_trailer
[abi
][4])
126 print(' KEYSECXB read: '..access_condition_sector_trailer
[abi
][5])
127 print(' KEYSECXB write: '..access_condition_sector_trailer
[abi
][6])
129 print(' read: '..access_condition_data_block
[abi
][1])
130 print(' write: '..access_condition_data_block
[abi
][2])
131 print(' inc: '..access_condition_data_block
[abi
][3])
132 print('decr, transfer, restore: '..access_condition_data_block
[abi
][4])