maur keys
[RRG-proxmark3.git] / client / luascripts / data_mf_bin2eml.lua
blob5f9354e552e64ccdb6b4e1e05a3695b625c9d85c
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 local ansicolors = require('ansicolors')
7 copyright = ''
8 author = 'Martin Holst Swende'
9 version = 'v1.0.3'
10 desc = [[
11 This script takes a dumpfile from 'hf mf dump' and converts it to a format that can be used
12 by the emulator
14 example = [[
15 script run data_mf_bin2eml -i dumpdata-foobar.bin
17 usage = [[
18 script run data_mf_bin2eml [-i <file>] [-o <file>]
20 arguments = [[
21 -h This help
22 -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used
23 -o <filename> Specifies the output file. If omitted, <uid>.eml is used.
27 local DEBUG = false
28 -------------------------------
29 -- Some utilities
30 -------------------------------
32 ---
33 -- A debug printout-function
34 local function dbg(args)
35 if not DEBUG then return end
36 if type(args) == 'table' then
37 local i = 1
38 while result[i] do
39 dbg(result[i])
40 i = i+1
41 end
42 else
43 print('###', args)
44 end
45 end
46 ---
47 -- This is only meant to be used when errors occur
48 local function oops(err)
49 print('[!!] ERROR:', err)
50 core.clearCommandBuffer()
51 return nil, err
52 end
53 ---
54 -- Usage help
55 function help()
56 print(copyright)
57 print(author)
58 print(version)
59 print(desc)
60 print(ansicolors.cyan..'Usage'..ansicolors.reset)
61 print(usage)
62 print(ansicolors.cyan..'Arguments'..ansicolors.reset)
63 print(arguments)
64 print(ansicolors.cyan..'Example usage'..ansicolors.reset)
65 print(example)
66 end
68 local function convert_to_ascii(hexdata)
69 if string.len(hexdata) % 32 ~= 0 then
70 return oops(('Bad data, length should be a multiple of 32 (was %d)'):format(string.len(hexdata)))
71 end
73 local js,i = '[';
74 for i = 1, string.len(hexdata),32 do
75 js = js .."'" ..string.sub(hexdata,i,i+31).."',\n"
76 end
77 js = js .. ']'
78 return js
79 end
81 local function readdump(infile)
82 t = infile:read('*all')
83 len = string.len(t)
84 local len,hex = bin.unpack(('H%d'):format(len),t)
85 return hex
86 end
88 local function convert_to_emulform(hexdata)
89 if string.len(hexdata) % 32 ~= 0 then
90 return oops(('Bad data, length should be a multiple of 32 (was %d)'):format(string.len(hexdata)))
91 end
92 local ascii,i = '';
93 for i = 1, string.len(hexdata),32 do
94 ascii = ascii..string.sub(hexdata,i,i+31)..'\n'
95 end
96 return string.sub(ascii, 1, -2)
97 end
99 local function main(args)
101 local input = 'dumpdata.bin'
102 local output
104 for o, a in getopt.getopt(args, 'i:o:h') do
105 if o == 'h' then return help() end
106 if o == 'i' then input = a end
107 if o == 'o' then output = a end
109 -- Validate the parameters
111 local infile = io.open(input, 'rb')
112 if infile == nil then
113 return oops('Could not read file ', input)
116 local dumpdata = readdump(infile)
117 -- The hex-data is now in ascii-format,
118 if dumpdata == nil then return oops('Dumpfle not loaded') end
120 -- But first, check the uid
121 local uid = string.sub(dumpdata, 1, 8)
122 output = output or (uid .. '.eml')
124 -- Format some linebreaks
125 dumpdata = convert_to_emulform(dumpdata)
126 if dumpdata == nil then return oops('Dumpfle not loaded') end
128 local outfile = io.open(output, 'w')
129 if outfile == nil then
130 return oops('Could not write to file ', output)
133 outfile:write(dumpdata:lower())
134 io.close(outfile)
135 print(('[+] Wrote an emulator-dump to the file %s'):format(output))
139 --[[
140 In the future, we may implement so that scripts are invoked directly
141 into a 'main' function, instead of being executed blindly. For future
142 compatibility, I have done so, but I invoke my main from here.
143 --]]
144 main(args)