]> git.zerfleddert.de Git - proxmark3-svn/blob - client/scripts/dumptoemul.lua
FIX: "hf mf hardnested" when "key found" exting the do-while loop doesn't need to...
[proxmark3-svn] / client / scripts / dumptoemul.lua
1 -- The getopt-functionality is loaded from pm3/getopt.lua
2 -- Have a look there for further details
3 getopt = require('getopt')
4 bin = require('bin')
5
6 example = "script run dumptoemul -i dumpdata-foobar.bin"
7 author = "Martin Holst Swende"
8 usage = "script run dumptoemul [-i <file>] [-o <file>]"
9 desc =[[
10 This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used
11 by the emulator
12
13 Arguments:
14 -h This help
15 -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used
16 -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
17
18 ]]
19
20 -------------------------------
21 -- Some utilities
22 -------------------------------
23
24 ---
25 -- A debug printout-function
26 function dbg(args)
27 if DEBUG then
28 print("###", args)
29 end
30 end
31 ---
32 -- This is only meant to be used when errors occur
33 function oops(err)
34 print("ERROR: ",err)
35 end
36 ---
37 -- Usage help
38 function help()
39 print(desc)
40 print(author)
41 print("Example usage")
42 print(example)
43 end
44
45 local function convert_to_ascii(hexdata)
46 if string.len(hexdata) % 32 ~= 0 then
47 return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata)))
48 end
49
50 local js,i = "[";
51 for i = 1, string.len(hexdata),32 do
52 js = js .."'" ..string.sub(hexdata,i,i+31).."',\n"
53 end
54 js = js .. "]"
55 return js
56 end
57
58 local function readdump(infile)
59 t = infile:read("*all")
60 len = string.len(t)
61 local len,hex = bin.unpack(("H%d"):format(len),t)
62 return hex
63 end
64
65 local function convert_to_emulform(hexdata)
66 if string.len(hexdata) % 32 ~= 0 then
67 return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata)))
68 end
69 local ascii,i = "";
70 for i = 1, string.len(hexdata),32 do
71 ascii = ascii ..string.sub(hexdata,i,i+31).."\n"
72 end
73
74 return string.sub(ascii,1,-1)
75 end
76
77 local function main(args)
78
79 local input = "dumpdata.bin"
80 local output
81
82 for o, a in getopt.getopt(args, 'i:o:h') do
83 if o == "h" then return help() end
84 if o == "i" then input = a end
85 if o == "o" then output = a end
86 end
87 -- Validate the parameters
88
89 local infile = io.open(input, "rb")
90 if infile == nil then
91 return oops("Could not read file ", input)
92 end
93 local dumpdata = readdump(infile)
94 -- The hex-data is now in ascii-format,
95
96 -- But first, check the uid
97 local uid = string.sub(dumpdata,1,8)
98 output = output or (uid .. ".eml")
99
100 -- Format some linebreaks
101 dumpdata = convert_to_emulform(dumpdata)
102
103 local outfile = io.open(output, "w")
104 if outfile == nil then
105 return oops("Could not write to file ", output)
106 end
107
108 outfile:write(dumpdata:lower())
109 io.close(outfile)
110 print(("Wrote an emulator-dump to the file %s"):format(output))
111 end
112
113
114 --[[
115 In the future, we may implement so that scripts are invoked directly
116 into a 'main' function, instead of being executed blindly. For future
117 compatibility, I have done so, but I invoke my main from here.
118 --]]
119 main(args)
Impressum, Datenschutz