diff --git a/client/scripts/mifareplus.lua b/client/scripts/mifareplus.lua index fbc374d72..b4e274c99 100644 --- a/client/scripts/mifareplus.lua +++ b/client/scripts/mifareplus.lua @@ -67,19 +67,19 @@ function writePerso() -- 0x0C - unexpected command length - -- First, set all the data in the card (4kB of data) to zeros. The keys, stored in the sector trailer block, are also set to zeros. - -- The only block which cannot be explicitly set is block 0x0000, the manufacturer block. - print("Setting values of normal blocks") - for i=1,255,1 do --skip block 0 - --convert the number to hex with leading zeros, then use it as the block number in writeBlock() - blocknum = string.format("%04x", i) - writeBlock(blocknum, SIXTEEN_BYTES_ZEROS) - end - print("Finished setting values of normal blocks") + cardsize = 4 --need to set to 4 for 4k or 2 for 2k + if(cardsize == 4) then + numsectors = 39 + elseif(cardsize == 2) then + numsectors = 31 + else + oops("Invalid card size") + end + + -- Write to the AES sector keys print("Setting AES Sector keys") - -- Next, write to the AES sector keys - for i=0,39 do --for each sector number + for i=0,numsectors do --for each sector number local keyA_block = "40" .. string.format("%02x", i * 2) local keyB_block = "40" .. string.format("%02x", (i * 2) + 1) --Can also calculate the keys fancily to make them unique, if desired @@ -153,9 +153,9 @@ function getVersion() sendRaw(GETVERS_CONT, true, true) end -function commitPerso() - -- commandString = COMMITPERSO .. "01" --switch to SL1 - commandString = COMMITPERSO .. "03" --switch to SL3 +function commitPerso(SL) + --pass SL as "01" to move to SL1 or "03" to move to SL3. + commandString = COMMITPERSO .. SL response = sendRaw(commandString, true, true) --0x90 is returned upon success if string.sub(response, 3, 4) ~= "90" then oops("error occurred while trying to switch security level") @@ -205,9 +205,20 @@ function proximityCheck() --PC-- RndC = "0001020304050607" --Random Challenge - commandString = PROXIMITYCHECK .. "08" .. RndC - response = sendRaw(commandString, true, true) - RndR = string.sub(response, 3, 18) + num_rounds = 8 --Needs to be 1, 2, 4, or 8 + part_len = 8 / num_rounds + j = 1 + RndR = "" + for i = 1,num_rounds do + pRndC = "" + for q = 1,(part_len*2) do + pRndC = pRndC .. string.sub(RndC,j,j) + j = j + 1 + end + commandString = PROXIMITYCHECK .. "0" .. tostring(part_len) .. pRndC + pRndR = string.sub(sendRaw(commandString, true, true), 3, 3+part_len) + RndR = RndR .. pRndR + end print("RndC = " .. RndC .. " RndR = " .. RndR) --VerifyPC-- @@ -245,8 +256,14 @@ function main(args) -- Initialize the card using the already-present read14a library info,err = lib14a.read14443a(true, false) --Perform PPS (Protocol and Parameter Selection) check to finish the ISO 14443-4 protocol. - sendRaw("e050", true, true) - sendRaw("D01100", true, true) + response = sendRaw("e050", true, true) + if(response == nil) then + err = "No response from RATS" + end + response = sendRaw("D01100", true, true) + if(response == nil) then + err = "No response from PPS check" + end if err then oops(err) else @@ -257,7 +274,7 @@ function main(args) -- Now, the card is initialized and we can do more interesting things. --writePerso() - --commitPerso() + --commitPerso("03") --move to SL3 --getVersion() proximityCheck() @@ -278,4 +295,4 @@ function main(args) end -main(args) -- Call the main function \ No newline at end of file +main(args) -- Call the main function