]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
CHG - some lua functions in utils.lua
authoriceman1001 <iceman@iuse.se>
Thu, 13 Nov 2014 19:14:14 +0000 (20:14 +0100)
committericeman1001 <iceman@iuse.se>
Thu, 13 Nov 2014 19:14:14 +0000 (20:14 +0100)
client/lualibs/commands.lua
client/lualibs/utils.lua
client/scripts/tnp3.lua

index bf2a8a1fb4e0995b01257513be9669cb732ac5b9..f88eeae2c40991384e548506f525c4380f547bea 100644 (file)
@@ -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
index 15b96ee50776eb5b7a40185b3edaa7f5a029cff3..9b36dfc862239736e0f16a9fc455883dc5dcb88c 100644 (file)
@@ -33,9 +33,86 @@ local Utils =
 \r
                return answer\r
        end,\r
+       \r
+       ------------ FILE READING\r
+       ReadDumpFile = function (filename)\r
+       \r
+               if filename == nil then \r
+                       return nil, 'Filename is empty'\r
+               end\r
+               if #filename == 0 then\r
+                       return nil, 'Filename length is zero'\r
+               end\r
+\r
+               infile = io.open(filename, "rb")\r
+               if infile == nil then \r
+                       return nil, string.format("Could not read file %s",filename)\r
+               end\r
+               local t = infile:read("*all")\r
+               len = string.len(t)\r
+               local  _,hex = bin.unpack(("H%d"):format(len),t)\r
+               io.close(infile)\r
+               return hex\r
+       end,\r
+       \r
+       ------------ string split function\r
+       Split = function( inSplitPattern, outResults )\r
+               if not outResults then\r
+                       outResults = {}\r
+               end\r
+               local start = 1\r
+               local splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
+               while splitStart do\r
+                       table.insert( outResults, string.sub( self, start, splitStart-1 ) )\r
+                       start = splitEnd + 1\r
+                       splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
+               end\r
+               table.insert( outResults, string.sub( self, start ) )\r
+               return outResults\r
+       end,\r
+       \r
+       ------------ CRC-16 ccitt checksums\r
+       \r
+       -- Takes a hex string and calculates a crc16\r
+       Crc16 = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       local asc = utils.ConvertHexToAscii(s)\r
+                       local hash = core.crc16(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,\r
+\r
+       -- input parameter is a string\r
+       -- Swaps the endianess and returns a number,  \r
+       -- IE:  'cd7a' -> '7acd'  -> 0x7acd\r
+       SwapEndianness = function(s, len)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return '' end\r
+               if  type(s) ~= 'string' then return nil end\r
+               \r
+               local retval = 0\r
+               if len == 16 then\r
+                       local t = s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               elseif len == 24 then\r
+                       local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               elseif len == 32 then\r
+                       local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+                       retval = tonumber(t,16)\r
+               end\r
+               return retval\r
+       end,\r
+       \r
+       ------------ CONVERSIONS\r
+       \r
        --\r
        -- Converts DECIMAL to HEX\r
-    ConvertDec2Hex = function(IN)\r
+    ConvertDecToHex = function(IN)\r
                local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
                while IN>0 do\r
                        I=I+1\r
@@ -46,7 +123,7 @@ local Utils =
        end,\r
        ---\r
        -- Convert Byte array to string of hex\r
-       ConvertBytes2HexString = function(bytes)\r
+       ConvertBytesToHex = function(bytes)\r
                if #bytes == 0 then\r
                        return ''\r
                end\r
@@ -57,7 +134,7 @@ local Utils =
                return table.concat(s)\r
        end,    \r
        -- Convert byte array to string with ascii\r
-    ConvertBytesToAsciiString = function(bytes)\r
+    ConvertBytesToAscii = function(bytes)\r
                if #bytes == 0 then\r
                        return ''\r
                end\r
@@ -67,7 +144,7 @@ local Utils =
                end\r
                return table.concat(s)          \r
        end,     \r
-       ConvertHexStringToBytes = function(s)\r
+       ConvertHexToBytes = function(s)\r
                local t={}\r
                if s == nil then return t end\r
                if #s == 0 then return t end\r
@@ -76,7 +153,7 @@ local Utils =
                end\r
                return t\r
        end,\r
-       ConvertAsciiStringToBytes = function(s)\r
+       ConvertAsciiToBytes = function(s)\r
                local t={}\r
                if s == nil then return t end\r
                if #s == 0 then return t end\r
index 006e5a5daf7d786a76c02c95edd9cc99af4f53f8..77de766f2ac66f26bc2735469740ab70de3de30f 100644 (file)
@@ -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
Impressum, Datenschutz