mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-03-21 20:52:45 +08:00
CHG - some lua functions in utils.lua
This commit is contained in:
parent
47cbb2d418
commit
f91f0ebb35
3 changed files with 101 additions and 64 deletions
client
|
@ -117,10 +117,10 @@ local _commands = {
|
||||||
|
|
||||||
|
|
||||||
local _reverse_lookup,k,v = {}
|
local _reverse_lookup,k,v = {}
|
||||||
for k, v in pairs(_commands) do
|
for k, v in pairs(_commands) do
|
||||||
_reverse_lookup[v] = k
|
_reverse_lookup[v] = k
|
||||||
end
|
end
|
||||||
_commands.tostring = function(command)
|
_commands.tostring = function(command)
|
||||||
if(type(command) == 'number') then
|
if(type(command) == 'number') then
|
||||||
return ("%s (%d)"):format(_reverse_lookup[command]or "ERROR UNDEFINED!", command)
|
return ("%s (%d)"):format(_reverse_lookup[command]or "ERROR UNDEFINED!", command)
|
||||||
end
|
end
|
||||||
|
@ -182,7 +182,6 @@ function Command:getBytes()
|
||||||
local cmd = self.cmd
|
local cmd = self.cmd
|
||||||
local arg1, arg2, arg3 = self.arg1, self.arg2, self.arg3
|
local arg1, arg2, arg3 = self.arg1, self.arg2, self.arg3
|
||||||
|
|
||||||
|
|
||||||
return bin.pack("LLLLH",cmd, arg1, arg2, arg3,data);
|
return bin.pack("LLLLH",cmd, arg1, arg2, arg3,data);
|
||||||
end
|
end
|
||||||
return _commands
|
return _commands
|
|
@ -33,9 +33,86 @@ local Utils =
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
------------ FILE READING
|
||||||
|
ReadDumpFile = function (filename)
|
||||||
|
|
||||||
|
if filename == nil then
|
||||||
|
return nil, 'Filename is empty'
|
||||||
|
end
|
||||||
|
if #filename == 0 then
|
||||||
|
return nil, 'Filename length is zero'
|
||||||
|
end
|
||||||
|
|
||||||
|
infile = io.open(filename, "rb")
|
||||||
|
if infile == nil then
|
||||||
|
return nil, string.format("Could not read file %s",filename)
|
||||||
|
end
|
||||||
|
local t = infile:read("*all")
|
||||||
|
len = string.len(t)
|
||||||
|
local _,hex = bin.unpack(("H%d"):format(len),t)
|
||||||
|
io.close(infile)
|
||||||
|
return hex
|
||||||
|
end,
|
||||||
|
|
||||||
|
------------ string split function
|
||||||
|
Split = function( inSplitPattern, outResults )
|
||||||
|
if not outResults then
|
||||||
|
outResults = {}
|
||||||
|
end
|
||||||
|
local start = 1
|
||||||
|
local splitStart, splitEnd = string.find( self, inSplitPattern, start )
|
||||||
|
while splitStart do
|
||||||
|
table.insert( outResults, string.sub( self, start, splitStart-1 ) )
|
||||||
|
start = splitEnd + 1
|
||||||
|
splitStart, splitEnd = string.find( self, inSplitPattern, start )
|
||||||
|
end
|
||||||
|
table.insert( outResults, string.sub( self, start ) )
|
||||||
|
return outResults
|
||||||
|
end,
|
||||||
|
|
||||||
|
------------ CRC-16 ccitt checksums
|
||||||
|
|
||||||
|
-- Takes a hex string and calculates a crc16
|
||||||
|
Crc16 = function(s)
|
||||||
|
if s == nil then return nil end
|
||||||
|
if #s == 0 then return nil end
|
||||||
|
if type(s) == 'string' then
|
||||||
|
local utils = require('utils')
|
||||||
|
local asc = utils.ConvertHexToAscii(s)
|
||||||
|
local hash = core.crc16(asc)
|
||||||
|
return hash
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- input parameter is a string
|
||||||
|
-- Swaps the endianess and returns a number,
|
||||||
|
-- IE: 'cd7a' -> '7acd' -> 0x7acd
|
||||||
|
SwapEndianness = function(s, len)
|
||||||
|
if s == nil then return nil end
|
||||||
|
if #s == 0 then return '' end
|
||||||
|
if type(s) ~= 'string' then return nil end
|
||||||
|
|
||||||
|
local retval = 0
|
||||||
|
if len == 16 then
|
||||||
|
local t = s:sub(3,4)..s:sub(1,2)
|
||||||
|
retval = tonumber(t,16)
|
||||||
|
elseif len == 24 then
|
||||||
|
local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
|
||||||
|
retval = tonumber(t,16)
|
||||||
|
elseif len == 32 then
|
||||||
|
local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
|
||||||
|
retval = tonumber(t,16)
|
||||||
|
end
|
||||||
|
return retval
|
||||||
|
end,
|
||||||
|
|
||||||
|
------------ CONVERSIONS
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Converts DECIMAL to HEX
|
-- Converts DECIMAL to HEX
|
||||||
ConvertDec2Hex = function(IN)
|
ConvertDecToHex = function(IN)
|
||||||
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
|
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
|
||||||
while IN>0 do
|
while IN>0 do
|
||||||
I=I+1
|
I=I+1
|
||||||
|
@ -46,7 +123,7 @@ local Utils =
|
||||||
end,
|
end,
|
||||||
---
|
---
|
||||||
-- Convert Byte array to string of hex
|
-- Convert Byte array to string of hex
|
||||||
ConvertBytes2HexString = function(bytes)
|
ConvertBytesToHex = function(bytes)
|
||||||
if #bytes == 0 then
|
if #bytes == 0 then
|
||||||
return ''
|
return ''
|
||||||
end
|
end
|
||||||
|
@ -57,7 +134,7 @@ local Utils =
|
||||||
return table.concat(s)
|
return table.concat(s)
|
||||||
end,
|
end,
|
||||||
-- Convert byte array to string with ascii
|
-- Convert byte array to string with ascii
|
||||||
ConvertBytesToAsciiString = function(bytes)
|
ConvertBytesToAscii = function(bytes)
|
||||||
if #bytes == 0 then
|
if #bytes == 0 then
|
||||||
return ''
|
return ''
|
||||||
end
|
end
|
||||||
|
@ -67,7 +144,7 @@ local Utils =
|
||||||
end
|
end
|
||||||
return table.concat(s)
|
return table.concat(s)
|
||||||
end,
|
end,
|
||||||
ConvertHexStringToBytes = function(s)
|
ConvertHexToBytes = function(s)
|
||||||
local t={}
|
local t={}
|
||||||
if s == nil then return t end
|
if s == nil then return t end
|
||||||
if #s == 0 then return t end
|
if #s == 0 then return t end
|
||||||
|
@ -76,7 +153,7 @@ local Utils =
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end,
|
end,
|
||||||
ConvertAsciiStringToBytes = function(s)
|
ConvertAsciiToBytes = function(s)
|
||||||
local t={}
|
local t={}
|
||||||
if s == nil then return t end
|
if s == nil then return t end
|
||||||
if #s == 0 then return t end
|
if #s == 0 then return t end
|
||||||
|
|
|
@ -28,10 +28,10 @@ Arguments:
|
||||||
-o : filename for the saved dumps
|
-o : filename for the saved dumps
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local hashconstant = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
|
local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
|
||||||
|
|
||||||
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
|
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
|
||||||
local DEBUG = true -- the debug flag
|
local DEBUG = false -- the debug flag
|
||||||
local numBlocks = 64
|
local numBlocks = 64
|
||||||
local numSectors = 16
|
local numSectors = 16
|
||||||
---
|
---
|
||||||
|
@ -154,13 +154,16 @@ local function main(args)
|
||||||
core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
|
core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
core.clearCommandBuffer()
|
||||||
|
|
||||||
-- Loading keyfile
|
-- Loading keyfile
|
||||||
print('Loading dumpkeys.bin')
|
print('Loading dumpkeys.bin')
|
||||||
local infile = io.open(input, "rb")
|
local hex, err = utils.ReadDumpFile(input)
|
||||||
if infile == nil then
|
if not hex then
|
||||||
return oops('Could not read file ', input)
|
return oops(err)
|
||||||
end
|
end
|
||||||
local akeys = readdumpkeys(infile):sub(0,12*16)
|
|
||||||
|
local akeys = hex:sub(0,12*16)
|
||||||
|
|
||||||
-- Read block 0
|
-- Read block 0
|
||||||
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
|
cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
|
||||||
|
@ -206,13 +209,13 @@ local function main(args)
|
||||||
-- Block 0-7 not encrypted
|
-- Block 0-7 not encrypted
|
||||||
blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
|
blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
|
||||||
else
|
else
|
||||||
local base = ('%s%s%02x%s'):format(block0, block1, blockNo, hashconstant)
|
local base = ('%s%s%02x%s'):format(block0, block1, blockNo, HASHCONSTANT)
|
||||||
local baseStr = utils.ConvertHexToAscii(base)
|
local baseStr = utils.ConvertHexToAscii(base)
|
||||||
local md5hash = md5.sumhexa(baseStr)
|
local md5hash = md5.sumhexa(baseStr)
|
||||||
local aestest = core.aes(md5hash, blockdata)
|
local aestest = core.aes(md5hash, blockdata)
|
||||||
|
|
||||||
local hex = utils.ConvertAsciiStringToBytes(aestest)
|
local hex = utils.ConvertAsciiToBytes(aestest)
|
||||||
hex = utils.ConvertBytes2HexString(hex)
|
hex = utils.ConvertBytesToHex(hex)
|
||||||
--local _,hex = bin.unpack(("H%d"):format(16),aestest)
|
--local _,hex = bin.unpack(("H%d"):format(16),aestest)
|
||||||
|
|
||||||
-- blocks with zero not encrypted.
|
-- blocks with zero not encrypted.
|
||||||
|
@ -238,8 +241,8 @@ local function main(args)
|
||||||
|
|
||||||
for _,s in pairs(blocks) do
|
for _,s in pairs(blocks) do
|
||||||
local slice = s:sub(8,#s)
|
local slice = s:sub(8,#s)
|
||||||
local str = utils.ConvertBytesToAsciiString(
|
local str = utils.ConvertBytesToAscii(
|
||||||
utils.ConvertHexStringToBytes(slice)
|
utils.ConvertHexToBytes(slice)
|
||||||
)
|
)
|
||||||
emldata = emldata..slice..'\n'
|
emldata = emldata..slice..'\n'
|
||||||
for c in (str):gmatch('.') do
|
for c in (str):gmatch('.') do
|
||||||
|
@ -266,47 +269,5 @@ local function main(args)
|
||||||
print( (' CARDID : 0x%s'):format(cardid ) )
|
print( (' CARDID : 0x%s'):format(cardid ) )
|
||||||
print( string.rep('--',20) )
|
print( string.rep('--',20) )
|
||||||
|
|
||||||
print('Validating checksums')
|
|
||||||
-- Checksum Typ 0
|
|
||||||
local test1 = ('%s%s'):format(block0, block1:sub(1,28))
|
|
||||||
local crc = block1:sub(29,32)
|
|
||||||
local revcrc = reverseCrcBytes(crc)
|
|
||||||
|
|
||||||
io.write( ('BLOCK 0-1 : %04x = %04x \n'):format(revcrc,computeCrc16(test1)))
|
|
||||||
|
|
||||||
-- Checksum Typ 1 BLOCK 9
|
|
||||||
local block9 = blocks[9]:sub(8,35)
|
|
||||||
test1 = ('%s0500'):format(block9)
|
|
||||||
crc = blocks[9]:sub(36,39)
|
|
||||||
revcrc = reverseCrcBytes(crc)
|
|
||||||
io.write( ('BLOCK 8 : %04x = %04x \n'):format(revcrc,computeCrc16(test1)))
|
|
||||||
|
|
||||||
-- Checksum Typ 1 BLOCK 37
|
|
||||||
local block37 = blocks[37]:sub(8,35)
|
|
||||||
test1 = ('%s0500'):format(block37)
|
|
||||||
crc = blocks[37]:sub(36,39)
|
|
||||||
revcrc = reverseCrcBytes(crc)
|
|
||||||
io.write( ('BLOCK 36 : %04x = %04x \n'):format(revcrc,computeCrc16(test1)))
|
|
||||||
|
|
||||||
-- Checksum Typ 2
|
|
||||||
-- 10,11,13
|
|
||||||
test1 = blocks[10]:sub(8,39)..
|
|
||||||
blocks[11]:sub(8,39)..
|
|
||||||
blocks[13]:sub(8,39)
|
|
||||||
|
|
||||||
crc = blocks[9]:sub(32,35)
|
|
||||||
revcrc = reverseCrcBytes(crc)
|
|
||||||
io.write( ('BLOCK 10-11-13 :%04x = %04x \n'):format(revcrc,computeCrc16(test1)))
|
|
||||||
-- Checksum Typ 3
|
|
||||||
-- 15,17,18,19
|
|
||||||
crc = blocks[9]:sub(28,31)
|
|
||||||
revcrc = reverseCrcBytes(crc)
|
|
||||||
test1 = blocks[14]:sub(8,39)..
|
|
||||||
blocks[15]:sub(8,39)..
|
|
||||||
blocks[17]:sub(8,39)
|
|
||||||
|
|
||||||
local tohash = test1..string.rep('00',0xe0)
|
|
||||||
local hashed = computeCrc16(tohash)
|
|
||||||
io.write( ('BLOCK 14-15-17 %04x = %04x \n'):format(revcrc,hashed))
|
|
||||||
end
|
end
|
||||||
main(args)
|
main(args)
|
Loading…
Add table
Reference in a new issue