text
[RRG-proxmark3.git] / client / luascripts / hf_mf_em_util.lua
blobe82bc9d1e9d13430f6e994394defa1a74aee2ff1
1 local getopt = require('getopt')
2 local ansicolors = require('ansicolors')
4 --Copyright
5 copyright = ''
6 author = 'nisgola'
7 version = 'v1'
9 -- Script description
10 desc = [[
11 This is a script that write Sector Trailers to the emulator memory.
13 By default, both keys A and B are set to 0xFFFFFFFFFFFF.
14 The Access Bytes are set to 0xFF0780 and User Bytes to 0x00.
16 example = [[
17 -- Use default formatting
18 1. script run hf_mf_em_util
20 -- Change keys A and B
21 2. script run hf_mf_em_util -a 112233445566 -b AABBCCDDEEFF
23 -- Define access bits and User byte
24 3. script run hf_mf_em_util -x 00f0ff -u 12
26 -- Usage info
27 usage = [[
28 script run hf_mf_em_util [-h] [-4] [-a <hex>] [-b <hex>] [-x <hex>] [-u <hex>]
30 -- Arguments
31 arguments = [[
32 -h this help
33 -4 format as 4K card
34 -a <hex> define key A
35 -b <hex> define key B
36 -x <hex> define Access Bytes
37 -u <hex> define User Byte
39 -- Help function
40 local function help()
41 print(copyright)
42 print(author)
43 print(version)
44 print(desc)
45 print(ansicolors.cyan..'Usage'..ansicolors.reset)
46 print(usage)
47 print(ansicolors.cyan..'Arguments'..ansicolors.reset)
48 print(arguments)
49 print(ansicolors.cyan..'Example usage'..ansicolors.reset)
50 print(example)
51 end
52 -- Print error
53 local function oops(err)
54 print('ERROR:', err)
55 return nil,err
56 end
58 -- Memory formatting
59 local function card_format(key_a,key_b,ab,user,s70)
60 local blocks = {3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,63,67,71,75,79,83,87,91,95,99,103,107,111,115,119,123,127,143,159,175,191,207,223,239,255}
61 for k,v in ipairs(blocks) do
62 local cmd = string.format("hf mf esetblk --blk %s -d %s%s%s%s",v,key_a,ab,user,key_b)
63 core.console(cmd)
64 print(cmd)
65 core.clearCommandBuffer()
66 if s70 == false and k > 15 then
67 return
68 end
69 end
70 end
72 local function main(args)
73 -- Receive parameters
74 for o, a in getopt.getopt(args, 'ha:b:x:u:4') do
75 if o == 'h' then return help() end
76 if o == 'a' then KeyA = a end
77 if o == 'b' then KeyB = a end
78 if o == 'x' then Accessbit = a end
79 if o == 'u' then User = a end
80 if o == '4' then kkkk = true end
81 end
83 local KeyA = KeyA or 'FFFFFFFFFFFF'
84 if #(KeyA) ~= 12 then
85 return oops( string.format('Wrong length of the Key A, receveid %d, expected 12', #KeyA))
86 end
88 local KeyB = KeyB or 'FFFFFFFFFFFF'
89 if #(KeyB) ~= 12 then
90 return oops( string.format('Wrong length of the Key B, received %d, expected 12', #KeyB))
91 end
93 local Accessbit = Accessbit or 'FF0780'
94 if #(Accessbit) ~= 6 then
95 return oops( string.format('Wrong length of the Access bit, received %d, expected 6', #Accessbit))
96 end
98 local User = User or '00'
99 if #(User) ~= 2 then
100 return oops( string.format('Wrong lenght for the user defined byte, received %d, expected 2', #User))
103 local kkkk = kkkk or false
105 -- Call card_format function
106 card_format(KeyA,KeyB,Accessbit,User,kkkk)
108 main (args)