Merge pull request #52 from CumpsD/master

Update Legic.lua script
This commit is contained in:
RFID Research Group 2018-11-11 15:38:54 +01:00 committed by GitHub
commit c3581fd4b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -386,7 +386,7 @@ end
-- put bytes into tag-table -- put bytes into tag-table
function bytesToTag(bytes, tag) function bytesToTag(bytes, tag)
if istable(tag) == false then return oops("tag is no table in: bytesToTag ("..type(tag)..")") end if istable(tag) == false then 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];
tag.MSN1=bytes[3]; tag.MSN1=bytes[3];
@ -453,8 +453,8 @@ end
--- ---
-- read Tag-Table in bytes-table -- read Tag-Table in bytes-table
function tagToBytes(tag) function tagToBytes(tag)
if istable(tag) == false then return oops("tag is no table in tagToBytes ("..type(tag)..")") end if istable(tag) == false then return oops("tag is no table in tagToBytes ("..type(tag)..")") end
local bytes = {} local bytes = {}
local i, i2 local i, i2
-- main token-data -- main token-data
@ -506,12 +506,21 @@ function tagToBytes(tag)
function readFromPM3() function readFromPM3()
local tag, bytes, infile local tag, bytes, infile
infile="legic.temp" infile="legic.temp"
core.console("hf legic reader") -- core.console("hf legic reader")
core.console("hf legic esave "..infile) -- core.console("hf legic esave "..infile)
core.console("hf legic dump o "..infile)
tag=readFile(infile..".bin") tag=readFile(infile..".bin")
return tag return tag
end end
function padString(str)
if (str:len() == 1) then
return '0'..str
end
return str
end
--- ---
-- write virtual Tag to real Tag -- write virtual Tag to real Tag
function writeToTag(tag) function writeToTag(tag)
@ -558,32 +567,33 @@ function writeToTag(tag)
if (bytes) then if (bytes) then
print("write temp-file '"..filename.."'") print("write temp-file '"..filename.."'")
print(accyan) print(accyan)
writeFile(bytes, filename) writeFile(bytes, filename..".bin")
--writeToTag(bytes, taglen, 'MylegicClone.hex')
print(acoff) print(acoff)
end end
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 load '..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<5 or i>6) then if (i > 6) then
cmd = ('hf legic write o 0x%02x d 0x01'):format(i) cmd = 'hf legic write o '..string.format("%x", i)..' d '..padString(bytes[i])
core.console(cmd)
--print(cmd)
elseif (i == 6) then
-- write DCF in reverse order (requires 'mosci-patch')
cmd = 'hf legic write o 0x05 d 0x02'
print(acgreen..cmd..acoff) print(acgreen..cmd..acoff)
core.console(cmd) core.console(cmd)
--print(cmd) elseif (i == 6) then
else -- write DCF in reverse order (requires 'mosci-patch')
cmd = 'hf legic write o 05 d '..padString(bytes[i-1])..padString(bytes[i])
print(acgreen..cmd..acoff)
core.console(cmd)
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
print(acgreen.."skip byte 0x00-0x04 - unwritable area"..acoff)
end end
utils.Sleep(0.2) utils.Sleep(0.2)
end end
@ -597,10 +607,10 @@ 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
@ -611,7 +621,7 @@ function readFile(filename)
-- 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
@ -620,14 +630,14 @@ end
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.." already 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 ... failed 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
@ -704,9 +714,9 @@ end
--- ---
-- toggle higligh -- toggle higligh
function toggleHighlight(tbl) 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
@ -1302,9 +1312,9 @@ function dumpTable(tab, header, tstart, tend)
for i=tstart, tend do for i=tstart, tend do
res=res..tab[i].." " res=res..tab[i].." "
end end
if (#header == 0) then if (#header == 0) then
return res return res
else else
return (header.." #"..(tend-tstart+1).."\n"..res) return (header.." #"..(tend-tstart+1).."\n"..res)
end end
end end
@ -1351,9 +1361,9 @@ end
--- ---
-- dump Legic-Cash data -- dump Legic-Cash data
function dumpLegicCash(tag, x) function dumpLegicCash(tag, x)
if istable(tag.SEG[x]) == false 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--------------------------------")
local limit, curr, balance, rid, tcv local limit, curr, balance, rid, tcv
@ -1379,7 +1389,7 @@ function dumpLegicCash(tag, x)
function print3rdPartyCash1(tag, x) function print3rdPartyCash1(tag, x)
if istable(tag.SEG[x]) == false 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))
print("\t\tBlock 0: "..dumpTable(tag.SEG[x].data, "", 3 , 18)) print("\t\tBlock 0: "..dumpTable(tag.SEG[x].data, "", 3 , 18))
@ -1455,11 +1465,11 @@ function makeToken()
-- 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
@ -1704,7 +1714,7 @@ function editSegmentData(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
@ -2276,7 +2286,7 @@ function modifyMode()
-- load file into mainTAG -- load file into mainTAG
["lf"] = function(x) ["lf"] = function(x)
if (type(x)=='string' and file_check(x)) then if (type(x)=='string' and file_check(x)) then
filename = x filename = x
else else
filename = input("enter filename: ", "legic.temp") filename = input("enter filename: ", "legic.temp")
@ -2382,9 +2392,9 @@ function modifyMode()
--- ---
-- dump single segment -- dump single segment
["ds"] = function(x) ["ds"] = function(x)
if (type(x)=="string" and string.len(x)>0) then if (type(x)=="string" and string.len(x)>0) then
sel = tonumber(x,10) sel = tonumber(x,10)
else else
sel = selectSegment(inTAG) sel = selectSegment(inTAG)
end end
if (sel) then print("\n"..(dumpSegment(inTAG, sel) or acred.."no Segments available") ..acoff.."\n") end if (sel) then print("\n"..(dumpSegment(inTAG, sel) or acred.."no Segments available") ..acoff.."\n") end
@ -2398,9 +2408,9 @@ function modifyMode()
if(istable(inTAG.SEG[0])) then if(istable(inTAG.SEG[0])) then
inTAG=editSegment(inTAG, sel) inTAG=editSegment(inTAG, sel)
inTAG.SEG[sel]=regenSegmentHeader(inTAG.SEG[sel]) inTAG.SEG[sel]=regenSegmentHeader(inTAG.SEG[sel])
else else
print(acyellow.."no Segments in Tag"..acoff) print(acyellow.."no Segments in Tag"..acoff)
end end
end end
end, end,
--- ---
@ -2455,14 +2465,14 @@ function modifyMode()
-- toggle kgh-crc-flag on a single segment -- toggle kgh-crc-flag on a single segment
["tk"] = function(x) ["tk"] = function(x)
if (istable(inTAG) and istable(inTAG.SEG[0])) then if (istable(inTAG) and istable(inTAG.SEG[0])) then
if (type(x)=="string" and string.len(x)>0) then if (type(x)=="string" and string.len(x)>0) then
sel = tonumber(x,10) sel = tonumber(x,10)
else else
sel = selectSegment(inTAG) sel = selectSegment(inTAG)
end end
if(inTAG.SEG[sel].kgh) then if(inTAG.SEG[sel].kgh) then
inTAG.SEG[sel].kgh = false inTAG.SEG[sel].kgh = false
else else
inTAG.SEG[sel].kgh = true inTAG.SEG[sel].kgh = true
end end
end end
@ -2486,18 +2496,18 @@ function modifyMode()
sel = tonumber(x,10) sel = tonumber(x,10)
else else
sel = selectSegment(inTAG) sel = selectSegment(inTAG)
end end
print("k "..kghCrcCredentials(inTAG, sel)) print("k "..kghCrcCredentials(inTAG, sel))
end end
end, end,
--- ---
-- fix legic-cash checksums -- fix legic-cash checksums
["flc"] = function(x) ["flc"] = function(x)
if (type(x)=="string" and string.len(x)>0) then if (type(x)=="string" and string.len(x)>0) then
x = tonumber(x,10) x = tonumber(x,10)
else else
x = selectSegment(inTAG) x = selectSegment(inTAG)
end end
inTAG.SEG[x].data=fixLegicCash(inTAG.SEG[x].data) inTAG.SEG[x].data=fixLegicCash(inTAG.SEG[x].data)
end, end,
--- ---
@ -2531,7 +2541,7 @@ function modifyMode()
else else
-- or try to find match -- or try to find match
x = autoSelectSegment(inTAG, "3rdparty") x = autoSelectSegment(inTAG, "3rdparty")
end end
if (istable(inTAG) and istable(inTAG.SEG[x]) and inTAG.SEG[x].len == 100) then if (istable(inTAG) and istable(inTAG.SEG[x]) and inTAG.SEG[x].len == 100) then
uid=inTAG.MCD..inTAG.MSN0..inTAG.MSN1..inTAG.MSN2 uid=inTAG.MCD..inTAG.MSN0..inTAG.MSN1..inTAG.MSN2
if (check43rdPartyCash1(uid, inTAG.SEG[x].data)) then if (check43rdPartyCash1(uid, inTAG.SEG[x].data)) then
@ -2561,11 +2571,11 @@ function modifyMode()
if (type(x)=="string" and string.len(x)>0) then if (type(x)=="string" and string.len(x)>0) then
x=tonumber(x,10) x=tonumber(x,10)
print(string.format("User-Selected Index %02d", x)) print(string.format("User-Selected Index %02d", x))
else else
-- or try to find match -- or try to find match
x = autoSelectSegment(inTAG, "3rdparty") x = autoSelectSegment(inTAG, "3rdparty")
end end
if (istable(inTAG) and istable(inTAG.SEG[x]) and inTAG.SEG[x].len == 100) then if (istable(inTAG) and istable(inTAG.SEG[x]) and inTAG.SEG[x].len == 100) then
inTAG=edit3rdPartyCash1(inTAG, x) inTAG=edit3rdPartyCash1(inTAG, x)
dump3rdPartyCash1(inTAG, x) dump3rdPartyCash1(inTAG, x)
@ -2587,7 +2597,7 @@ function modifyMode()
--- ---
-- get stamp from single segment -- get stamp from single segment
["gs"] = function(x) ["gs"] = function(x)
if(type(x)=="string" and string.len(x)>=2) then if(type(x)=="string" and string.len(x)>=2) then
x = tonumber(x, 10) x = tonumber(x, 10)
else else
x = selectSegment(inTAG) x = selectSegment(inTAG)
@ -2637,7 +2647,7 @@ function modifyMode()
actions[string.lower(string.sub(ic,0,2))](string.sub(ic,4)) actions[string.lower(string.sub(ic,0,2))](string.sub(ic,4))
elseif (type(actions[string.lower(string.sub(ic,0,1))])=='function') then elseif (type(actions[string.lower(string.sub(ic,0,1))])=='function') then
actions[string.lower(string.sub(ic,0,1))](string.sub(ic,3)) actions[string.lower(string.sub(ic,0,1))](string.sub(ic,3))
else else
actions.h('') actions.h('')
end end
until (string.sub(ic,0,1)=="q") until (string.sub(ic,0,1)=="q")
@ -2664,25 +2674,25 @@ function main(args)
-- dump virtual-Tag -- dump virtual-Tag
if o == "d" then dfs=true end if o == "d" then dfs=true end
-- interacive modifying -- interacive modifying
if o == "m" then if o == "m" then
interactive = true interactive = true
modifyMode() modifyMode()
end end
-- xor (e.g. for clone or plain file) -- xor (e.g. for clone or plain file)
if o == "c" then if o == "c" then
cfs = true cfs = true
crc = a crc = a
end end
-- output file -- output file
if o == "o" then if o == "o" then
outfile = a outfile = a
ofs = true ofs = true
end end
end end
-- file conversion (output to file) -- file conversion (output to file)
if ofs == false then return end if ofs == false then return end
-- dump infile / tag-read -- dump infile / tag-read
if (dfs) then if (dfs) then
print("-----------------------------------------") print("-----------------------------------------")
@ -2714,4 +2724,4 @@ end
--- ---
-- script start -- script start
main(args) main(args)