From f91f0ebb35c1c131e5e255457212a0784c81dcd6 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 13 Nov 2014 20:14:14 +0100 Subject: [PATCH] CHG - some lua functions in utils.lua --- client/lualibs/commands.lua | 9 ++-- client/lualibs/utils.lua | 87 ++++++++++++++++++++++++++++++++++--- client/scripts/tnp3.lua | 69 +++++++---------------------- 3 files changed, 101 insertions(+), 64 deletions(-) diff --git a/client/lualibs/commands.lua b/client/lualibs/commands.lua index bf2a8a1f..f88eeae2 100644 --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@ -117,10 +117,10 @@ local _commands = { local _reverse_lookup,k,v = {} -for k, v in pairs(_commands) do - _reverse_lookup[v] = k -end -_commands.tostring = function(command) + for k, v in pairs(_commands) do + _reverse_lookup[v] = k + end + _commands.tostring = function(command) if(type(command) == 'number') then return ("%s (%d)"):format(_reverse_lookup[command]or "ERROR UNDEFINED!", command) end @@ -182,7 +182,6 @@ function Command:getBytes() local cmd = self.cmd local arg1, arg2, arg3 = self.arg1, self.arg2, self.arg3 - return bin.pack("LLLLH",cmd, arg1, arg2, arg3,data); end return _commands \ No newline at end of file diff --git a/client/lualibs/utils.lua b/client/lualibs/utils.lua index 15b96ee5..9b36dfc8 100644 --- a/client/lualibs/utils.lua +++ b/client/lualibs/utils.lua @@ -33,9 +33,86 @@ local Utils = return answer 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 - ConvertDec2Hex = function(IN) + ConvertDecToHex = function(IN) local B,K,OUT,I,D=16,"0123456789ABCDEF","",0 while IN>0 do I=I+1 @@ -46,7 +123,7 @@ local Utils = end, --- -- Convert Byte array to string of hex - ConvertBytes2HexString = function(bytes) + ConvertBytesToHex = function(bytes) if #bytes == 0 then return '' end @@ -57,7 +134,7 @@ local Utils = return table.concat(s) end, -- Convert byte array to string with ascii - ConvertBytesToAsciiString = function(bytes) + ConvertBytesToAscii = function(bytes) if #bytes == 0 then return '' end @@ -67,7 +144,7 @@ local Utils = end return table.concat(s) end, - ConvertHexStringToBytes = function(s) + ConvertHexToBytes = function(s) local t={} if s == nil then return t end if #s == 0 then return t end @@ -76,7 +153,7 @@ local Utils = end return t end, - ConvertAsciiStringToBytes = function(s) + ConvertAsciiToBytes = function(s) local t={} if s == nil then return t end if #s == 0 then return t end diff --git a/client/scripts/tnp3.lua b/client/scripts/tnp3.lua index 006e5a5d..77de766f 100644 --- a/client/scripts/tnp3.lua +++ b/client/scripts/tnp3.lua @@ -28,10 +28,10 @@ Arguments: -o : filename for the saved dumps ]] -local hashconstant = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20' +local HASHCONSTANT = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20' 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 numSectors = 16 --- @@ -154,14 +154,17 @@ local function main(args) core.console( ('hf mf nested 1 0 A %s d'):format(keyA) ) end + core.clearCommandBuffer() + -- Loading keyfile print('Loading dumpkeys.bin') - local infile = io.open(input, "rb") - if infile == nil then - return oops('Could not read file ', input) + local hex, err = utils.ReadDumpFile(input) + if not hex then + return oops(err) end - local akeys = readdumpkeys(infile):sub(0,12*16) - + + local akeys = hex:sub(0,12*16) + -- Read block 0 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA} err = core.SendCommand(cmd:getBytes()) @@ -206,13 +209,13 @@ local function main(args) -- Block 0-7 not encrypted blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata) 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 md5hash = md5.sumhexa(baseStr) local aestest = core.aes(md5hash, blockdata) - local hex = utils.ConvertAsciiStringToBytes(aestest) - hex = utils.ConvertBytes2HexString(hex) + local hex = utils.ConvertAsciiToBytes(aestest) + hex = utils.ConvertBytesToHex(hex) --local _,hex = bin.unpack(("H%d"):format(16),aestest) -- blocks with zero not encrypted. @@ -238,8 +241,8 @@ local function main(args) for _,s in pairs(blocks) do local slice = s:sub(8,#s) - local str = utils.ConvertBytesToAsciiString( - utils.ConvertHexStringToBytes(slice) + local str = utils.ConvertBytesToAscii( + utils.ConvertHexToBytes(slice) ) emldata = emldata..slice..'\n' for c in (str):gmatch('.') do @@ -266,47 +269,5 @@ local function main(args) print( (' CARDID : 0x%s'):format(cardid ) ) 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 main(args) \ No newline at end of file -- 2.39.2