1 local bin
= require('bin')
2 local getopt
= require('getopt')
3 local lib14a
= require('read14a')
4 local utils
= require('utils')
5 local cl
= require('ansicolors')
11 This script calculates mifare Ultralight-EV1 pwd based on uid diversification for an Italian ticketsystem
14 ]].. "You can also look at the native pm3 command `" .. cl
.yellow
.. "hf mfu pwdgen -h" .. cl
.reset
.. "`\n"
17 -- if called without, it reads tag uid
18 script run hf_mfu_pwdgen_italy
21 script run hf_mfu_pwdgen_italy -u 11223344556677
24 script run hf_mfu_uidkeycalc_italy -h -u <uid> "
32 local bxor
= bit32
.bxor
34 -- A debug printout-function
35 local function dbg(args
)
36 if not DEBUG
then return end
37 if type(args
) == 'table' then
48 -- This is only meant to be used when errors occur
49 local function oops(err
)
51 core
.clearCommandBuffer()
61 print(cl
.cyan
..'Usage'..cl
.reset
)
63 print(cl
.cyan
..'Arguments'..cl
.reset
)
65 print(cl
.cyan
..'Example usage'..cl
.reset
)
70 local function exitMsg(msg
)
71 print( string.rep('--',20) )
72 print( string.rep('--',20) )
78 --[[ position, 4byte xor
114 local function findEntryByUid( uid
)
116 -- xor UID4,UID5,UID6,UID7
118 local pos
= (bxor(uid
[4], uid
[5], uid
[6], uid
[7])) % 32
120 -- convert to hexstring
121 pos
= string.format('%02X', pos
)
123 for k
, v
in pairs(_xortable
) do
124 if ( v
[1] == pos
) then
125 return utils
.ConvertHexToBytes(v
[2])
132 local function pwdgen(uid
)
134 -- PWD0 = T0 xor B xor C xor D
135 -- PWD1 = T1 xor A xor C xor E
136 -- PWD2 = T2 xor A xor B xor F
138 local uidbytes
= utils
.ConvertHexToBytes(uid
)
139 local entry
= findEntryByUid(uidbytes
)
140 if entry
== nil then return nil, "Can't find a xor entry" end
142 local pwd0
= bxor( entry
[1], uidbytes
[2], uidbytes
[3], uidbytes
[4])
143 local pwd1
= bxor( entry
[2], uidbytes
[1], uidbytes
[3], uidbytes
[5])
144 local pwd2
= bxor( entry
[3], uidbytes
[1], uidbytes
[2], uidbytes
[6])
145 local pwd3
= bxor( entry
[4], uidbytes
[7])
146 return string.format('%02X%02X%02X%02X', pwd0
, pwd1
, pwd2
, pwd3
)
151 local function main(args
)
153 print( string.rep('--',20) )
154 print( string.rep('--',20) )
157 local uid
= '04111211121110'
160 -- Arguments for the script
161 for o
, a
in getopt
.getopt(args
, 'hu:') do
162 if o
== 'h' then return help() end
163 if o
== 'u' then uid
= a
; useUID
= true end
168 if uid
== nil then return oops('empty uid string') end
169 if #uid
== 0 then return oops('empty uid string') end
170 if #uid
~= 14 then return oops('uid wrong length. Should be 7 hex bytes') end
173 local tag, err
= lib14a
.read(false, true)
174 if not tag then return oops(err
) end
175 core
.clearCommandBuffer()
180 local pwd
, err
= pwdgen(uid
)
181 if not pwd
then return ooops(err
) end
183 print(string.format('PWD | %s', pwd
))