| 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_ev1_it |
| 9 | script run calc_ev1_it -u 11223344556677 |
| 10 | ]] |
| 11 | author = "Iceman" |
| 12 | usage = "script run calc_ev1_it -u <uid> " |
| 13 | desc =[[ |
| 14 | Arguments: |
| 15 | -h : this help |
| 16 | -u <UID> : UID |
| 17 | ]] |
| 18 | --- |
| 19 | -- A debug printout-function |
| 20 | function dbg(args) |
| 21 | if type(args) == "table" then |
| 22 | local i = 1 |
| 23 | while args[i] do |
| 24 | dbg(args[i]) |
| 25 | i = i+1 |
| 26 | end |
| 27 | else |
| 28 | print("###", args) |
| 29 | end |
| 30 | end |
| 31 | --- |
| 32 | -- This is only meant to be used when errors occur |
| 33 | function oops(err) |
| 34 | print("ERROR: ",err) |
| 35 | return nil,err |
| 36 | end |
| 37 | --- |
| 38 | -- Usage help |
| 39 | function help() |
| 40 | print(desc) |
| 41 | print("Example usage") |
| 42 | print(example) |
| 43 | end |
| 44 | -- |
| 45 | -- Exit message |
| 46 | function exitMsg(msg) |
| 47 | print( string.rep('--',20) ) |
| 48 | print( string.rep('--',20) ) |
| 49 | print(msg) |
| 50 | print() |
| 51 | end |
| 52 | |
| 53 | local _xortable = { |
| 54 | --[[ position, 4byte xor |
| 55 | --]] |
| 56 | {"00","4f2711c1"}, |
| 57 | {"01","07D7BB83"}, |
| 58 | {"02","9636EF07"}, |
| 59 | {"03","B5F4460E"}, |
| 60 | {"04","F271141C"}, |
| 61 | {"05","7D7BB038"}, |
| 62 | {"06","636EF871"}, |
| 63 | {"07","5F4468E3"}, |
| 64 | {"08","271149C7"}, |
| 65 | {"09","D7BB0B8F"}, |
| 66 | {"0A","36EF8F1E"}, |
| 67 | {"0B","F446863D"}, |
| 68 | {"0C","7114947A"}, |
| 69 | {"0D","7BB0B0F5"}, |
| 70 | {"0E","6EF8F9EB"}, |
| 71 | {"0F","44686BD7"}, |
| 72 | {"10","11494fAF"}, |
| 73 | {"11","BB0B075F"}, |
| 74 | {"12","EF8F96BE"}, |
| 75 | {"13","4686B57C"}, |
| 76 | {"14","1494F2F9"}, |
| 77 | {"15","B0B07DF3"}, |
| 78 | {"16","F8F963E6"}, |
| 79 | {"17","686B5FCC"}, |
| 80 | {"18","494F2799"}, |
| 81 | {"19","0B07D733"}, |
| 82 | {"1A","8F963667"}, |
| 83 | {"1B","86B5F4CE"}, |
| 84 | {"1C","94F2719C"}, |
| 85 | {"1D","B07D7B38"}, |
| 86 | {"1E","F9636E70"}, |
| 87 | {"1F","6B5F44E0"}, |
| 88 | } |
| 89 | |
| 90 | local function findEntryByUid( uid ) |
| 91 | |
| 92 | -- xor UID4,UID5,UID6,UID7 |
| 93 | -- mod 0x20 (dec 32) |
| 94 | local pos = (bxor(bxor(bxor(uid[4],uid[5]), uid[6]),uid[7])) % 32 |
| 95 | |
| 96 | -- convert to hexstring |
| 97 | pos = string.format('%02X', pos) |
| 98 | |
| 99 | for k, v in pairs(_xortable) do |
| 100 | if ( v[1] == pos ) then |
| 101 | return utils.ConvertHexToBytes(v[2]) |
| 102 | end |
| 103 | end |
| 104 | return nil |
| 105 | end |
| 106 | |
| 107 | local function main(args) |
| 108 | |
| 109 | print( string.rep('--',20) ) |
| 110 | print( string.rep('--',20) ) |
| 111 | print() |
| 112 | |
| 113 | local i,j, pwd |
| 114 | local uid = '04111211121110' |
| 115 | |
| 116 | -- Arguments for the script |
| 117 | for o, a in getopt.getopt(args, 'hu:') do |
| 118 | if o == "h" then return help() end |
| 119 | if o == "u" then uid = a end |
| 120 | end |
| 121 | |
| 122 | -- uid string checks |
| 123 | if uid == nil then return oops('empty uid string') end |
| 124 | if #uid == 0 then return oops('empty uid string') end |
| 125 | if #uid ~= 14 then return oops('uid wrong length. Should be 7 hex bytes') end |
| 126 | |
| 127 | local uidbytes = utils.ConvertHexToBytes(uid) |
| 128 | |
| 129 | local entry = findEntryByUid(uidbytes) |
| 130 | if entry == nil then return oops("Can't find a xor entry") end |
| 131 | |
| 132 | -- PWD CALC |
| 133 | -- PWD0 = T0 xor B xor C xor D |
| 134 | -- PWD1 = T1 xor A xor C xor E |
| 135 | -- PWD2 = T2 xor A xor B xor F |
| 136 | -- PWD3 = T3 xor G |
| 137 | |
| 138 | local pwd0 = bxor( bxor( bxor( entry[1], uidbytes[2]), uidbytes[3]), uidbytes[4]) |
| 139 | local pwd1 = bxor( bxor( bxor( entry[2], uidbytes[1]), uidbytes[3]), uidbytes[5]) |
| 140 | local pwd2 = bxor( bxor( bxor( entry[3], uidbytes[1]), uidbytes[2]), uidbytes[6]) |
| 141 | local pwd3 = bxor( entry[4], uidbytes[7]) |
| 142 | |
| 143 | print('UID | '..uid) |
| 144 | print(string.format('PWD | %02X%02X%02X%02X', pwd0, pwd1, pwd2, pwd3)) |
| 145 | end |
| 146 | |
| 147 | main(args) |