this should fix the ~ (tilde) bug I introduced...

In lua,  you need to evaluate false ie;   statement == false,    not like in C-style  ~statement
This commit is contained in:
iceman1001 2017-08-13 15:06:30 +02:00
commit 69c8970201

View file

@ -317,7 +317,7 @@ end
--- ---
-- put a string into a bytes-table -- put a string into a bytes-table
function str2bytes(s) function str2bytes(s)
if (string.len(s)%2~=0) then if (string.len(s)%2 ~= 0) then
return print("stamp should be a even hexstring e.g.: deadbeef or 0badc0de") return print("stamp should be a even hexstring e.g.: deadbeef or 0badc0de")
end end
local res={} local res={}
@ -330,11 +330,11 @@ end
--- ---
-- put certain bytes into a new table -- put certain bytes into a new table
function bytesToTable(bytes, bstart, bend) function bytesToTable(bytes, bstart, bend)
local t={} local t={}
for i=0, (bend-bstart) do for i=0, (bend-bstart) do
t[i]=bytes[bstart+i] t[i]=bytes[bstart+i]
end end
return t return t
end end
--- ---
@ -345,14 +345,13 @@ function getInputBytes(infile)
local fhi,err = io.open(infile,"rb") local fhi,err = io.open(infile,"rb")
if err then oops("faild to read from file ".. infile); return false; end if err then oops("faild to read from file ".. infile); return false; end
file_data = fhi:read("*a"); file_data = fhi:read("*a");
for i = 1, #file_data for i = 1, #file_data do
do bytes[i] = string.format("%x",file_data:byte(i))
bytes[i] = string.format("%x",file_data:byte(i)) end
end
fhi:close() fhi:close()
if (bytes[7]=='00') then return false end if (bytes[7]=='00') then return false end
print(#bytes .. " bytes from "..infile.." loaded") print(#bytes .. " bytes from "..infile.." loaded")
return bytes return bytes
end end
@ -386,9 +385,7 @@ end
--- ---
-- put bytes into tag-table -- put bytes into tag-table
function bytesToTag(bytes, tag) function bytesToTag(bytes, tag)
if ~istable(tag) then if istable(tag) == false then return oops("tag is no table in: bytesToTag ("..type(tag)..")") end
return oops("tag is no table in: bytesToTag ("..type(tag)..")")
end
tag.MCD =bytes[1]; tag.MCD =bytes[1];
tag.MSN0=bytes[2]; tag.MSN0=bytes[2];
@ -456,9 +453,7 @@ end
--- ---
-- read Tag-Table in bytes-table -- read Tag-Table in bytes-table
function tagToBytes(tag) function tagToBytes(tag)
if ~istable(tag) then if istable(tag) == false then return oops("tag is no table in tagToBytes ("..type(tag)..")") end
return oops("tag is no table in tagToBytes ("..type(tag)..")")
end
local bytes = {} local bytes = {}
local i, i2 local i, i2
@ -559,7 +554,7 @@ function writeToTag(tag)
end end
bytes=tagToBytes(tag) bytes=tagToBytes(tag)
-- master-token-crc -- master-token-crc
if (tag.Type~="SAM") then bytes[22]=calcMtCrc(bytes) end if (tag.Type ~= "SAM") then bytes[22] = calcMtCrc(bytes) end
if (bytes) then if (bytes) then
print("write temp-file '"..filename.."'") print("write temp-file '"..filename.."'")
print(accyan) print(accyan)
@ -572,7 +567,7 @@ function writeToTag(tag)
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 load '..filename cmd = 'hf legic load '..filename
core.console(cmd) core.console(cmd)
-- write pm3-buffer to Tag -- write pm3-buffer to Tag
@ -602,20 +597,20 @@ function readFile(filename)
print(accyan) print(accyan)
local bytes = {} local bytes = {}
local tag = {} local tag = {}
if ~file_check(filename) 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 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
@ -623,31 +618,31 @@ end
--- ---
-- write bytes to file -- write bytes to file
function writeFile(bytes, filename) 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.." 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 (answer==false) 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 ... faild to open output-file ".. filename) end if err then oops("OOps ... faild to open output-file ".. filename) end
bytes=xorBytes(bytes, bytes[5]) 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]
elseif (bcnt <= 7) then elseif (bcnt <= 7) then
line=line.." "..bytes[i] line = line.." "..bytes[i]
end end
if (bcnt == 7) then if (bcnt == 7) then
-- write line to new file -- write line to new file
fho:write(line.."\n") fho:write(line.."\n")
-- reset counter & line -- reset counter & line
bcnt=-1 bcnt = -1
line="" line = ""
end end
bcnt=bcnt+1 bcnt = bcnt + 1
end end
fho:close() fho:close()
print("\nwrote ".. #bytes .." bytes to " .. filename) print("\nwrote ".. #bytes .." bytes to " .. filename)
@ -659,8 +654,8 @@ end
-- make tagMap -- make tagMap
function makeTagMap() 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
@ -990,18 +985,18 @@ end
--- ---
-- show bytes used for crc-calculation -- show bytes used for crc-calculation
function getSequences(bytes, seqstr) function getSequences(bytes, seqstr)
if (type(seqstr)~="string") then seqstr=input("enter comma-seperated sequences (e.g.: '1-4,23-26')", '1-4,23-26') end if (type(seqstr) ~= "string") then seqstr = input("enter comma-seperated sequences (e.g.: '1-4,23-26')", '1-4,23-26') end
local seqs=split(seqstr, ',') local seqs = split(seqstr, ',')
local res = "" local res = ""
if(#seqs>0) then if(#seqs>0) then
for k, v in pairs(seqs) do for k, v in pairs(seqs) do
local seq = split(v,'-') local seq = split(v,'-')
if (#seq>=2) then if (#seq >= 2) then
for i=seq[1], seq[2] do for i = seq[1], seq[2] do
res=res..bytes[i].." " res = res..bytes[i].." "
end end
end end
if(string.len(res)>0) then res=res.." " end if(string.len(res)>0) then res = res.." " end
end end
else else
oops("no sequence found in '"..seqstr.."'") oops("no sequence found in '"..seqstr.."'")
@ -1060,13 +1055,13 @@ end
--- ---
-- add interactive mapping -- add interactive mapping
function addMapping(tag, tagMap, x) function addMapping(tag, tagMap, x)
if (type(x)~="number") then x=#tagMap.mappings+1 end if (type(x) ~= "number") then x = #tagMap.mappings + 1 end
local bytes=tagToBytes(tag) local bytes = tagToBytes(tag)
local myMapping={} local myMapping={}
myMapping['name'] =input(accyan.."enter Maping-Name:"..acoff, string.format("mapping %d", #tagMap.mappings+1)) myMapping['name'] = input(accyan.."enter Maping-Name:"..acoff, string.format("mapping %d", #tagMap.mappings+1))
myMapping['start']=tonumber(input(accyan.."enter start-addr:"..acoff, '1'), 10) myMapping['start'] = tonumber(input(accyan.."enter start-addr:"..acoff, '1'), 10)
myMapping['end'] =tonumber(input(accyan.."enter end-addr:"..acoff, #bytes), 10) myMapping['end'] = tonumber(input(accyan.."enter end-addr:"..acoff, #bytes), 10)
myMapping['highlight']=confirm("set highlighted") myMapping['highlight'] = confirm("set highlighted")
table.insert(tagMap.mappings, x, myMapping) table.insert(tagMap.mappings, x, myMapping)
return tagMap return tagMap
end end
@ -1087,7 +1082,7 @@ end
--- ---
-- select a mapping from a tagmap -- select a mapping from a tagmap
function selectTableEntry(table, action) function selectTableEntry(table, action)
if (type(action)~="string") then action="select number of item:" end if (type(action) ~= "string") then action = "select number of item:" end
for k, v in pairs(table) do for k, v in pairs(table) do
print(accyan..k..acoff.."\t-> "..accyan..v['name']..acoff) print(accyan..k..acoff.."\t-> "..accyan..v['name']..acoff)
end end
@ -1357,7 +1352,7 @@ end
-- dump Legic-Cash data -- dump Legic-Cash data
function dumpLegicCash(tag, x) function dumpLegicCash(tag, x)
if ~istable(tag.SEG[x]) then return end if istable(tag.SEG[x]) == false then return end
io.write("in Segment "..tag.SEG[x].index.." :\n") io.write("in Segment "..tag.SEG[x].index.." :\n")
print("--------------------------------\n\tLegic-Cash Values\n--------------------------------") print("--------------------------------\n\tLegic-Cash Values\n--------------------------------")
@ -1383,7 +1378,7 @@ function dumpLegicCash(tag, x)
-- raw 3rd-party -- raw 3rd-party
function print3rdPartyCash1(tag, x) function print3rdPartyCash1(tag, x)
if ~istable(tag.SEG[x]) then return end if istable(tag.SEG[x]) == false then return end
local uid=tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2 local uid=tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2
print("\n\t\tStamp : "..dumpTable(tag.SEG[x].data, "", 0 , 2)) print("\n\t\tStamp : "..dumpTable(tag.SEG[x].data, "", 0 , 2))
@ -1437,31 +1432,34 @@ function makeToken()
} }
ttype="" ttype=""
for k, v in pairs(mt.Type) do for k, v in pairs(mt.Type) do
ttype=ttype..k..") "..v.." " ttype = ttype..k..") "..v.." "
end end
mtq=tonumber(input("select number for Token-Type\n"..ttype, '1'), 10) mtq = tonumber(input("select number for Token-Type\n"..ttype, '1'), 10)
if (type(mtq)~="number") then return print("selection invalid!") if (type(mtq) ~= "number") then return print("selection invalid!")
elseif (mtq>#mt.Type) then return print("selection invalid!") elseif (mtq > #mt.Type) then return print("selection invalid!")
else print("Token-Type '"..mt.Type[mtq].."' selected") end else print("Token-Type '"..mt.Type[mtq].."' selected") end
local raw=calcHeaderRaw(mt.WRP[mtq], mt.WRC[mtq], mt.RD[mtq]) local raw = calcHeaderRaw(mt.WRP[mtq], mt.WRC[mtq], mt.RD[mtq])
local mtCRC="00" local mtCRC = "00"
bytes={"01", "02", "03", "04", "cb", string.sub(mt.DCF[mtq], 0, 2), string.sub(mt.DCF[mtq], 3), raw, bytes = {"01", "02", "03", "04", "cb", string.sub(mt.DCF[mtq], 0, 2), string.sub(mt.DCF[mtq], 3), raw,
"00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00",
"00", "00", "00", "00", "00", "00"} "00", "00", "00", "00", "00", "00"}
if (mtq==1) then if (mtq == 1) then
for i=0, #mt.Segment do for i = 0, #mt.Segment do
table.insert(bytes, mt.Segment[i]) table.insert(bytes, mt.Segment[i])
end end
bytes[9]="ff" bytes[9] = "ff"
end end
-- fill bytes -- fill bytes
for i=#bytes, 1023 do table.insert(bytes, "00") end for i = #bytes, 1023 do table.insert(bytes, "00") end
-- if Master-Token -> calc Master-Token-CRC -- if Master-Token -> calc Master-Token-CRC
if (mtq>1) then bytes[22]=calcMtCrc(bytes) end if (mtq>1) then bytes[22] = calcMtCrc(bytes) end
local tempTag=createTagTable()
local tempTag = createTagTable()
-- remove segment if MasterToken -- remove segment if MasterToken
if (mtq>1) then tempTag.SEG[0]=nil end if (mtq>1) then tempTag.SEG[0] = nil end
return bytesToTag(bytes, tempTag) return bytesToTag(bytes, tempTag)
end end
@ -1469,72 +1467,71 @@ end
-- edit token-data -- edit token-data
function editTag(tag) function editTag(tag)
-- for simulation it makes sense to edit everything -- for simulation it makes sense to edit everything
local edit_sim="MCD MSN0 MSN1 MSN2 MCC DCFl DCFh WRP WRC RD" local edit_sim = "MCD MSN0 MSN1 MSN2 MCC DCFl DCFh WRP WRC RD"
-- on real tags it makes only sense to edit DCF, WRP, WRC, RD -- on real tags it makes only sense to edit DCF, WRP, WRC, RD
local edit_real="DCFl DCFh WRP WRC RD" local edit_real = "DCFl DCFh WRP WRC RD"
if (confirm(acyellow.."do you want to edit non-writeable values (e.g. for simulation)?"..acoff)) then if (confirm(acyellow.."do you want to edit non-writeable values (e.g. for simulation)?"..acoff)) then
edit_tag=edit_sim edit_tag = edit_sim
else edit_tag=edit_real end else edit_tag = edit_real end
if(istable(tag)) then if(istable(tag)) then
for k,v in pairs(tag) do for k,v in pairs(tag) do
if(type(v)~="table" and type(v)~="boolean" and string.find(edit_tag, k)) then if(type(v) ~= "table" and type(v) ~= "boolean" and string.find(edit_tag, k)) then
tag[k]=input("value for: "..accyan..k..acoff, v) tag[k] = input("value for: "..accyan..k..acoff, v)
end end
end end
if (tag.Type=="SAM") then ttype="Header"; else ttype="Stamp"; end if (tag.Type == "SAM") then ttype = "Header"; else ttype = "Stamp"; end
if (confirm(acyellow.."do you want to edit "..ttype.." Data?"..acoff)) then if (confirm(acyellow.."do you want to edit "..ttype.." Data?"..acoff)) then
-- master-token specific -- master-token specific
if(istable(tag.Bck)==false) then if(istable(tag.Bck) == false) then
-- stamp-data length=(0xfc-DCFh) -- stamp-data length=(0xfc-DCFh)
-- on MT: SSC holds the Starting Stamp Character (Stamp0) -- on MT: SSC holds the Starting Stamp Character (Stamp0)
tag.SSC=input(ttype.."0: ", tag.SSC) tag.SSC=input(ttype.."0: ", tag.SSC)
-- rest of stamp-bytes are in tag.data 0..n -- rest of stamp-bytes are in tag.data 0..n
for i=0, (tonumber(0xfc ,10)-("%d"):format('0x'..tag.DCFh))-2 do for i=0, (tonumber(0xfc ,10)-("%d"):format('0x'..tag.DCFh))-2 do
tag.data[i]=input(ttype.. i+1 ..": ", tag.data[i]) tag.data[i] = input(ttype.. i+1 ..": ", tag.data[i])
end end
else else
--- on credentials byte7 should always be 9f and byte8 ff --- on credentials byte7 should always be 9f and byte8 ff
-- on Master-Token not (even on SAM63/64 not) -- on Master-Token not (even on SAM63/64 not)
-- tag.SSC=input(ttype.."0: ", tag.SSC) -- tag.SSC=input(ttype.."0: ", tag.SSC)
for i=0, #tag.data do for i=0, #tag.data do
tag.data[i]=input(ttype.. i ..": ", tag.data[i]) tag.data[i] = input(ttype.. i ..": ", tag.data[i])
end end
end end
end end
bytes=tagToBytes(tag) bytes = tagToBytes(tag)
--- check data-consistency (calculate tag.raw) --- check data-consistency (calculate tag.raw)
bytes[8]=calcHeaderRaw(tag.WRP, tag.WRC, tag.RD) bytes[8] = calcHeaderRaw(tag.WRP, tag.WRC, tag.RD)
--- Master-Token specific --- Master-Token specific
-- should be triggered if a SAM was converted to a non-SAM (user-Token to Master-Token) -- should be triggered if a SAM was converted to a non-SAM (user-Token to Master-Token)
-- or a Master-Token has being edited (also SAM64 & SAM63 - which are in fact Master-Token) -- or a Master-Token has being edited (also SAM64 & SAM63 - which are in fact Master-Token)
if(tag.Type~="SAM" or bytes[6]..bytes[7]~="60ea") then if(tag.Type ~= "SAM" or bytes[6]..bytes[7] ~= "60ea") then
-- calc new Master-Token crc -- calc new Master-Token crc
bytes[22]=calcMtCrc(bytes) bytes[22] = calcMtCrc(bytes)
else else
-- ensure tag.SSC set to 'ff' on credential-token (SAM) -- ensure tag.SSC set to 'ff' on credential-token (SAM)
bytes[9]='ff' bytes[9] = 'ff'
-- if a Master-Token was converted to a Credential-Token -- if a Master-Token was converted to a Credential-Token
-- lets unset the Time-Area to 00 00 (will contain Stamp-Data on MT) -- lets unset the Time-Area to 00 00 (will contain Stamp-Data on MT)
bytes[21]='00' bytes[21] = '00'
bytes[22]='00' bytes[22] = '00'
end end
tag=bytesToTag(bytes, tag) tag = bytesToTag(bytes, tag)
end end
end end
--- ---
-- calculates header-byte (addr 0x07) -- calculates header-byte (addr 0x07)
function calcHeaderRaw(wrp, wrc, rd) function calcHeaderRaw(wrp, wrc, rd)
local res wrp = ("%02x"):format(tonumber(wrp, 10))
wrp=("%02x"):format(tonumber(wrp, 10)) rd = tonumber(rd, 16)
rd=tonumber(rd, 16) local res = ("%02x"):format(tonumber(wrp, 16)+tonumber(wrc.."0", 16)+((rd>0) and tonumber("8"..(rd-1), 16) or 0))
res=("%02x"):format(tonumber(wrp, 16)+tonumber(wrc.."0", 16)+((rd>0) and tonumber("8"..(rd-1), 16) or 0))
return res return res
end end
@ -1699,19 +1696,17 @@ end
--- ---
-- edit Segment Data -- edit Segment Data
function editSegmentData(data) function editSegmentData(data)
io.write("\n") io.write("\n")
if ~istable(data) then if istable(data) == false then print("no Segment-Data found") end
print("no Segment-Data found")
end
local lc = check4LegicCash(data) local lc = check4LegicCash(data)
for i=0, #data-1 do for i=0, #data-1 do
data[i]=input(accyan.."Data"..i..acoff..": ", data[i]) data[i]=input(accyan.."Data"..i..acoff..": ", data[i])
end end
if (lc) then if (lc) then
data = fixLegicCash(data) data = fixLegicCash(data)
end end
return data return data
end end
@ -2686,7 +2681,7 @@ function main(args)
end end
-- file conversion (output to file) -- file conversion (output to file)
if ~ofs then return end if ofs == false then return end
-- dump infile / tag-read -- dump infile / tag-read
if (dfs) then if (dfs) then