]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/scripts/tnp3.lua
FIX: I think the dumping of data is correct now in tnp3.lua. MD5 string vs bytearra...
[proxmark3-svn] / client / scripts / tnp3.lua
CommitLineData
c15d2bdc 1local cmds = require('commands')
2local getopt = require('getopt')
3local bin = require('bin')
4local lib14a = require('read14a')
5local utils = require('utils')
6local md5 = require('md5')
22f1c577 7local toyNames = require('default_toys')
c15d2bdc 8
9example =[[
10 1. script run tnp3
8aa79dee 11 2. script run tnp3 -n
12 3. script run tnp3 -k aabbccddeeff
13 4. script run tnp3 -k aabbccddeeff -n
c15d2bdc 14]]
15author = "Iceman"
8aa79dee 16usage = "script run tnp3 -k <key> -n"
c15d2bdc 17desc =[[
18This script will try to dump the contents of a Mifare TNP3xxx card.
19It will need a valid KeyA in order to find the other keys and decode the card.
20Arguments:
8aa79dee 21 -h : this help
22 -k <key> : Sector 0 Key A.
23 -n : Use the nested cmd to find all keys
c15d2bdc 24]]
25
8aa79dee 26-- AES konstant? LEN 0x24 36,
27-- I dekompilen är det för internal static array = 0x36 54
c15d2bdc 28local hashconstant = '20436F707972696768742028432920323031302041637469766973696F6E2E20416C6C205269676874732052657365727665642E20'
29
30local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
31local DEBUG = true -- the debug flag
32local numBlocks = 64
33local numSectors = 16
34---
35-- A debug printout-function
36function dbg(args)
37 if not DEBUG then
38 return
39 end
40
41 if type(args) == "table" then
42 local i = 1
43 while result[i] do
44 dbg(result[i])
45 i = i+1
46 end
47 else
48 print("###", args)
49 end
50end
51---
52-- This is only meant to be used when errors occur
53function oops(err)
54 print("ERROR: ",err)
55end
56---
57-- Usage help
58function help()
59 print(desc)
60 print("Example usage")
61 print(example)
62end
63--
64-- Exit message
65function ExitMsg(msg)
66 print( string.rep('--',20) )
67 print( string.rep('--',20) )
68 print(msg)
69 print()
70end
71
c70cef97 72local function readdumpkeys(infile)
73 t = infile:read("*all")
74 len = string.len(t)
75 local len,hex = bin.unpack(("H%d"):format(len),t)
c70cef97 76 return hex
77end
78
79local function waitCmd()
c15d2bdc 80 local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT)
81 if response then
82 local count,cmd,arg0 = bin.unpack('LL',response)
83 if(arg0==1) then
84 local count,arg1,arg2,data = bin.unpack('LLH511',response,count)
85 return data:sub(1,32)
86 else
87 return nil, "Couldn't read block.."
88 end
89 end
90 return nil, "No response from device"
91end
92
93local function main(args)
94
95 print( string.rep('--',20) )
cd5767d4 96 print( string.rep('--',20) )
22f1c577 97 --print()
c15d2bdc 98
99 local keyA
100 local cmd
101 local err
8aa79dee 102 local useNested = false
c15d2bdc 103 local cmdReadBlockString = 'hf mf rdbl %d A %s'
c70cef97 104 local input = "dumpkeys.bin"
c15d2bdc 105
106 -- Arguments for the script
8aa79dee 107 for o, a in getopt.getopt(args, 'hk:n') do
c15d2bdc 108 if o == "h" then return help() end
109 if o == "k" then keyA = a end
8aa79dee 110 if o == "n" then useNested = true end
c15d2bdc 111 end
112
113 -- validate input args.
114 keyA = keyA or '4b0b20107ccb'
115 if #(keyA) ~= 12 then
116 return oops( string.format('Wrong length of write key (was %d) expected 12', #keyA))
117 end
22f1c577 118
119 -- Turn off Debug
120 local cmdSetDbgOff = "hf mf dbg 0"
121 core.console( cmdSetDbgOff)
c15d2bdc 122
123 result, err = lib14a.read1443a(false)
124 if not result then
8aa79dee 125 return oops(err)
c15d2bdc 126 end
8aa79dee 127
c15d2bdc 128 core.clearCommandBuffer()
129
130 if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx
8aa79dee 131 return oops('This is not a TNP3xxx tag. aborting.')
c15d2bdc 132 end
133
22f1c577 134 print((' Found tag : %s'):format(result.name))
135
c15d2bdc 136 -- Show info
137 print(('Using keyA : %s'):format(keyA))
138 print( string.rep('--',20) )
139
22f1c577 140 --Trying to find the other keys
8aa79dee 141 if useNested then
142 core.console( ('hf mf nested 1 0 A %s d'):format(keyA) )
143 end
c70cef97 144
8aa79dee 145 -- Loading keyfile
1a5ff2c2 146 print('Loading dumpkeys.bin')
c70cef97 147 local infile = io.open(input, "rb")
148 if infile == nil then
149 return oops('Could not read file ', input)
150 end
151 local akeys = readdumpkeys(infile):sub(0,12*16)
9b989c45 152
c15d2bdc 153 -- Read block 0
154 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 0,arg2 = 0,arg3 = 0, data = keyA}
155 err = core.SendCommand(cmd:getBytes())
156 if err then return oops(err) end
157 local block0, err = waitCmd()
158 if err then return oops(err) end
159
160 -- Read block 1
161 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = 1,arg2 = 0,arg3 = 0, data = keyA}
8aa79dee 162 err = core.SendCommand(cmd:getBytes())
c15d2bdc 163 if err then return oops(err) end
164 local block1, err = waitCmd()
165 if err then return oops(err) end
166
8aa79dee 167 local key
168 local pos = 0
169 local blockNo
170 local blocks = {}
c15d2bdc 171
22f1c577 172 print('Reading card data')
173
c15d2bdc 174 -- main loop
1a5ff2c2 175 for blockNo = 0, numBlocks-1, 1 do
176
177 if core.ukbhit() then
178 print("aborted by user")
179 break
180 end
181
182 pos = (math.floor( blockNo / 4 ) * 12)+1
9b989c45 183 key = akeys:sub(pos, pos + 11 )
1a5ff2c2 184 cmd = Command:new{cmd = cmds.CMD_MIFARE_READBL, arg1 = blockNo ,arg2 = 0,arg3 = 0, data = key}
185 local err = core.SendCommand(cmd:getBytes())
186 if err then return oops(err) end
187 local blockdata, err = waitCmd()
188 if err then return oops(err) end
189
cd5767d4 190 if blockNo%4 ~= 3 then
1a5ff2c2 191 if blockNo < 8 then
192 -- Block 0-7 not encrypted
cd5767d4 193 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
8aa79dee 194 else
cd5767d4 195 local base = ('%s%s%02d%s'):format(block0, block1, blockNo, hashconstant)
196 local baseArr = utils.ConvertHexStringToBytes(base)
197 local baseStr = utils.ConvertBytesToAsciiString(baseArr)
198 local md5hash = md5.sumhexa(baseStr)
1a5ff2c2 199 local aestest = core.aes(md5hash, blockdata)
cd5767d4 200 --print(aestest, type(aestest))
201 local hex = utils.ConvertAsciiStringToBytes(aestest)
202 hex = utils.ConvertBytes2HexString(hex)
203 --local _,hex = bin.unpack(("H%d"):format(16),aestest)
1a5ff2c2 204
205 if string.find(blockdata, '^0+$') then
cd5767d4 206 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
1a5ff2c2 207 else
cd5767d4 208 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
1a5ff2c2 209 end
c70cef97 210 end
1a5ff2c2 211 else
212 -- Sectorblocks, not encrypted
cd5767d4 213 blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
c70cef97 214 end
c15d2bdc 215 end
8aa79dee 216
217 -- Print results
22f1c577 218 local uid = block0:sub(1,8)
219 local itemtype = block1:sub(1,4)
220 local cardid = block1:sub(9,24)
221 print( (' UID : %s'):format(uid) )
222 print( (' ITEM TYPE : %s - %s'):format(itemtype, toyNames[itemtype]) )
223 print( (' CARDID : %s'):format(cardid ) )
cd5767d4 224 print('BLK :: Decrypted Ascii' )
8aa79dee 225 print( string.rep('--',36) )
226 for _,s in pairs(blocks) do
cd5767d4 227 local arr = utils.ConvertHexStringToBytes(s:sub(7,#s))
228 print( s, utils.ConvertBytesToAsciiString(arr) )
8aa79dee 229 end
c15d2bdc 230end
231
232main(args)
Impressum, Datenschutz