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