chg: 'script run legic' - minor adjustments.

This commit is contained in:
iceman1001 2019-01-26 19:10:29 +01:00
parent 8059f533af
commit fa3dfa59b9

View file

@ -513,8 +513,8 @@ function readFromPM3()
return tag return tag
end end
function padString(str) local function padString(str)
if (str:len() == 1) then if (#str == 1) then
return '0'..str return '0'..str
end end
@ -524,73 +524,84 @@ end
--- ---
-- write virtual Tag to real Tag -- write virtual Tag to real Tag
function writeToTag(tag) function writeToTag(tag)
local bytes local bytes
local filename='MylegicClone.hex' local filename = 'MylegicClone.hex'
local taglen=22 local taglen = 22
if(utils.confirm(acred.."\nplace the (empty) Tag onto the PM3\nand confirm writing to this Tag: "..acoff) == false) then if(utils.confirm(acred.."\nplace the (empty) Tag onto the PM3\nand confirm writing to this Tag: "..acoff) == false) then
return return
end end
-- get used bytes / tag-len
if(istable(tag.SEG)) then -- get used bytes / tag-len
if (istable(tag.Bck)) then if (istable(tag.SEG)) then
for i=0, #tag.SEG do if (istable(tag.Bck)) then
taglen=taglen+tag.SEG[i].len+5 for i=0, #tag.SEG do
end taglen = taglen + tag.SEG[i] . len + 5
end end
local uid_old=tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2 end
-- read new tag into memory so we can xor the new data with the new MCC local uid_old = tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2
outTAG=readFromPM3()
outbytes=tagToBytes(outTAG) -- read new tag into memory so we can xor the new data with the new MCC
-- copy 'inputbuffer' to 'outputbuffer' outTAG = readFromPM3()
tag.MCD = outbytes[1] outbytes = tagToBytes(outTAG)
tag.MSN0 = outbytes[2] -- copy 'inputbuffer' to 'outputbuffer'
tag.MSN1 = outbytes[3] tag.MCD = outbytes[1]
tag.MSN2 = outbytes[4] tag.MSN0 = outbytes[2]
tag.MCC = outbytes[5] tag.MSN1 = outbytes[3]
-- recheck all segments-crc/kghcrc (only on a credential) tag.MSN2 = outbytes[4]
if(istable(tag.Bck)) then tag.MCC = outbytes[5]
checkAllSegCrc(tag) -- recheck all segments-crc/kghcrc (only on a credential)
checkAllKghCrc(tag) if (istable(tag.Bck)) then
local uid_new=tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2 checkAllSegCrc(tag)
for i=0, #tag.SEG do checkAllKghCrc(tag)
if (check43rdPartyCash1(uid_old, tag.SEG[i].data)) then local uid_new = tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2
io.write(accyan.."\nfixing known checksums"..acoff.." ... ") for i=0, #tag.SEG do
if (fix3rdPartyCash1(uid_new, tag.SEG[i].data)) then if (check43rdPartyCash1(uid_old, tag.SEG[i].data)) then
io.write(acgreen.." done\n"..acoff) io.write(accyan.."\nfixing known checksums"..acoff.." ... ")
else oops("\nsomething went wrong at the repair of the 3rd-party-cash-segment") end if (fix3rdPartyCash1(uid_new, tag.SEG[i].data)) then
end io.write(acgreen.." done\n"..acoff)
end else
end oops("\nsomething went wrong at the repair of the 3rd-party-cash-segment")
bytes=tagToBytes(tag) end
-- master-token-crc end
if (tag.Type ~= "SAM") then bytes[22] = calcMtCrc(bytes) end end
if (bytes) then end
print("write temp-file '"..filename.."'") bytes = tagToBytes(tag)
print(accyan) -- master-token-crc
writeFile(bytes, filename..".bin") if (tag.Type ~= "SAM") then
print(acoff) bytes[22] = calcMtCrc(bytes)
end end
end if (bytes) then
print("write temp-file '"..filename.."'")
print(accyan)
writeFile(bytes, filename..".bin")
print(acoff)
end
end
-- write data to file -- write data to file
if (taglen > 0) then if (taglen > 0) then
WriteBytes = utils.input(acyellow.."enter number of bytes to write?"..acoff, taglen) WriteBytes = utils.input(acyellow.."enter number of bytes to write?"..acoff, taglen)
-- load file into pm3-buffer -- load file into pm3-buffer
if (type(filename) ~= "string") then filename=input(acyellow.."filename to load to pm3-buffer?"..acoff,"legic.temp") end if (type(filename) ~= "string") then
filename = input(acyellow.."filename to load to pm3-buffer?"..acoff, "legic.temp")
end
cmd = 'hf legic eload 2 '..filename cmd = 'hf legic eload 2 '..filename
core.console(cmd) core.console(cmd)
-- write pm3-buffer to Tag -- write pm3-buffer to Tag
for i=0, WriteBytes do for i=0, WriteBytes do
if (i > 6) then if (i > 6) then
cmd = 'hf legic write o '..string.format("%x", i)..' d '..padString(bytes[i]) cmd = ("hf legic write o %x d %s "):format(i, padString(bytes[i]))
print(acgreen..cmd..acoff) print(acgreen..cmd..acoff)
core.console(cmd) core.console(cmd)
core.clearCommandBuffer()
elseif (i == 6) then elseif (i == 6) then
-- write DCF in reverse order (requires 'mosci-patch') -- write DCF in reverse order (requires 'mosci-patch')
cmd = 'hf legic write o 05 d '..padString(bytes[i-1])..padString(bytes[i]) cmd = ('hf legic write o 05 d %s%s'):format(padString(bytes[i-1]), padString(bytes[i]))
print(acgreen..cmd..acoff) print(acgreen..cmd..acoff)
core.console(cmd) core.console(cmd)
elseif (i == 5) then core.clearCommandBuffer()
elseif (i == 5) then
print(acgreen.."skip byte 0x05 - will be written next step"..acoff) print(acgreen.."skip byte 0x05 - will be written next step"..acoff)
else else
print(acgreen.."skip byte 0x00-0x04 - unwritable area"..acoff) print(acgreen.."skip byte 0x00-0x04 - unwritable area"..acoff)
@ -603,26 +614,28 @@ end
--- File I/O --- --- File I/O ---
--- ---
-- read file into virtual-tag -- read file into virtual-tag
function readFile(filename) local function readFile(filename)
print(accyan) print(accyan)
local bytes = {} local bytes = {}
local tag = {} local tag = {}
if file_check(filename) == false then return oops("input file: "..filename.." not found") end if file_check(filename) == false then
return oops("input file: "..filename.." not found")
end
bytes = getInputBytes(filename) bytes = getInputBytes(filename)
if bytes == false then return oops('couldnt get input bytes') end if bytes == false then return oops('couldnt get input bytes') end
-- make plain bytes -- make plain bytes
bytes = xorBytes(bytes,bytes[5]) bytes = xorBytes(bytes,bytes[5])
print("create virtual tag from ".. #bytes .. " bytes") print("create virtual tag from ".. #bytes .. " bytes")
-- create Tag for plain bytes -- create Tag for plain bytes
tag=createTagTable() tag = createTagTable()
-- load plain bytes to tag-table -- load plain bytes to tag-table
print(acoff) print(acoff)
tag=bytesToTag(bytes, tag) tag = bytesToTag(bytes, tag)
return tag return tag
end end
--- ---
@ -631,14 +644,16 @@ function writeFile(bytes, filename)
if (filename ~= 'MylegicClone.hex') then if (filename ~= 'MylegicClone.hex') then
if (file_check(filename)) then if (file_check(filename)) then
local answer = confirm("\nthe output-file "..filename.." already exists!\nthis will delete the previous content!\ncontinue?") local answer = confirm("\nthe output-file "..filename.." already exists!\nthis will delete the previous content!\ncontinue?")
if (answer==false) then return print("user abort") end if not answer then return print("user abort") end
end end
end end
local line local line
local bcnt=0 local bcnt = 0
local fho,err = io.open(filename, "w") local fho, err = io.open(filename, "w")
if err then oops("OOps ... failed to open output-file ".. filename) end if err then
bytes=xorBytes(bytes, bytes[5]) return oops("OOps ... failed to open output-file ".. filename)
end
bytes = xorBytes(bytes, bytes[5])
for i = 1, #bytes do for i = 1, #bytes do
if (bcnt == 0) then if (bcnt == 0) then
line = bytes[i] line = bytes[i]
@ -662,96 +677,96 @@ end
--- Map related --- --- Map related ---
--- ---
-- make tagMap -- make tagMap
function makeTagMap() local function makeTagMap()
local tagMap={} local tagMap = {}
if (#tagMap == 0) then if (#tagMap == 0) then
tagMap['name'] = input(accyan.."enter Name for this Map: "..acoff , "newTagMap") tagMap['name'] = input(accyan.."enter Name for this Map: "..acoff , "newTagMap")
tagMap['mappings']={} tagMap['mappings'] = {}
tagMap['crc8']={} tagMap['crc8'] = {}
-- insert fixed Tag-CRC -- insert fixed Tag-CRC
table.insert(tagMap.crc8, {name='TAG-CRC', pos=5, seq={1, 4}}) table.insert(tagMap.crc8, {name = 'TAG-CRC', pos = 5, seq = {1, 4}})
tagMap['crc16']={} tagMap['crc16'] = {}
end end
print(accyan.."new tagMap created"..acoff) print(accyan.."new tagMap created"..acoff)
return tagMap return tagMap
end end
--- ---
-- save mapping to file -- save mapping to file
function saveTagMap(map, filename) local function saveTagMap(map, filename)
if (string.len(filename)>0) then if (string.len(filename)>0) then
if (file_check(filename)) then if (file_check(filename)) then
local answer = confirm("\nthe output-file "..filename.." alredy exists!\nthis will delete the previous content!\ncontinue?") local answer = confirm("\nthe output-file "..filename.." alredy exists!\nthis will delete the previous content!\ncontinue?")
if (answer==false) then return print("user abort") end if not answer then return print("user abort") end
end end
end end
local line local line
local fho,err = io.open(filename, "w") local fho,err = io.open(filename, "w")
if err then oops("OOps ... faild to open output-file ".. filename) end if err then oops("OOps ... faild to open output-file ".. filename) end
-- write line to new file -- write line to new file
for k, v in pairs(map) do for k, v in pairs(map) do
if (istable(v)) then if (istable(v)) then
for k2, v2 in pairs(v) do for k2, v2 in pairs(v) do
if (k=='mappings') then if (k == 'mappings') then
fho:write(k..","..k2..","..v2['name']..","..v2['start']..","..v2['end']..","..((v2['highlight']) and "1" or "0").."\n") fho:write(k..","..k2..","..v2['name']..","..v2['start']..","..v2['end']..","..((v2['highlight']) and "1" or "0").."\n")
elseif (k=="crc8") then elseif (k == "crc8") then
local tmp="" local tmp = ""
tmp=k..","..k2..","..v2['name']..","..v2['pos'].."," tmp = k..","..k2..","..v2['name']..","..v2['pos']..","
tmp=tmp..tbl2seqstr(v2['seq']) tmp=tmp..tbl2seqstr(v2['seq'])
fho:write(tmp.."\n") fho:write(tmp.."\n")
end end
end end
else else
fho:write(k..","..v.."\n") fho:write(k..","..v.."\n")
end end
end end
fho:close() fho:close()
return true return true
end end
--- ---
-- toggle higligh -- toggle higligh
function toggleHighlight(tbl) local function toggleHighlight(tbl)
if (tbl['highlight']) then if (tbl['highlight']) then
tbl['highlight'] = false tbl['highlight'] = false
else else
tbl['highlight'] = true tbl['highlight'] = true
end end
return tbl return tbl
end end
--- ---
-- return table od seqence-string -- return table od seqence-string
function seqstr2tbl(seqstr) local function seqstr2tbl(seqstr)
local s=split(seqstr) local s = split(seqstr)
local res={} local res = {}
if (#s>=1) then if (#s >= 1) then
for sk, sv in pairs(s) do for sk, sv in pairs(s) do
s2=split(sv, '-') s2 = split(sv, '-')
if(#s2==2) then if(#s2 == 2) then
table.insert(res, s2[1]) table.insert(res, s2[1])
table.insert(res, s2[2]) table.insert(res, s2[2])
end end
end end
end end
return res return res
end end
--- ---
-- return sequence-string from table -- return sequence-string from table
function tbl2seqstr(seqtbl) local function tbl2seqstr(seqtbl)
local res="" local res = ""
if (istable(seqtbl)) then if (istable(seqtbl)) then
for sk, sv in pairs(seqtbl) do for sk, sv in pairs(seqtbl) do
res=res..sv..((sk%2==0) and "," or "-") res = res..sv..((sk%2==0) and "," or "-")
end end
if (string.sub(res, string.len(res))==",") then if (string.sub(res, string.len(res))== ",") then
res=string.sub(res, 1, string.len(res)-1) res = string.sub(res, 1, string.len(res)-1)
end end
end end
return res return res
end end
--- ---