diff --git a/client/lualibs/read14a.lua b/client/lualibs/read14a.lua index 60fc0e68..cdddfac3 100644 --- a/client/lualibs/read14a.lua +++ b/client/lualibs/read14a.lua @@ -80,6 +80,7 @@ end local function sendToDevice(command, ignoreresponse) core.clearCommandBuffer() + print("Sent a UsbCommand via read14a.lua.") local err = core.SendCommand(command:getBytes()) if err then print(err) @@ -88,13 +89,14 @@ local function sendToDevice(command, ignoreresponse) if ignoreresponse then return nil,nil end local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT) + print("Received a UsbCommand packet in read14a.lua.") return response,nil end --- This function does a connect and retrieves som einfo +-- This function does a connect and retrieves some info -- @param dont_disconnect - if true, does not disable the field -- @param no_rats - if true, skips ISO14443-4 select (RATS) --- @return if successfull: an table containing card info +-- @return if successfull: a table containing card info -- @return if unsuccessfull : nil, error local function read14443a(dont_disconnect, no_rats) local command, result, info, err, data @@ -109,6 +111,7 @@ local function read14443a(dont_disconnect, no_rats) end local result,err = sendToDevice(command) if result then + print("Currently trying to decode the UsbCommand packet just received (an ACK of the connection).") local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) if arg0 == 0 then return nil, "iso14443a card select failed" @@ -143,6 +146,8 @@ end local library = { read14443a = read14443a, read = read14443a, + read14443a = read14443a, + read = read14443a, waitFor14443a = waitFor14443a, parse14443a = parse14443a, sendToDevice = sendToDevice, diff --git a/client/scripting.c b/client/scripting.c index 0c761cb2..c118307b 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -102,6 +102,38 @@ static int l_WaitForResponseTimeout(lua_State *L){ if(WaitForResponseTimeout(cmd, &response, ms_timeout)) { + //BEGIN ADDED CODE + uint8_t *recv; + char *hexout; + recv = response.d.asBytes; + uint8_t iLen = response.arg[0]; + if (0){ + iLen = response.arg[1]; + if (iLen){ + PrintAndLog("Card selected. UID[%i]:", iLen); + } else { + PrintAndLog("Can't select card."); + } + } else { + PrintAndLog("received %i bytes:", iLen); + } + if(!iLen) + return 1; + hexout = (char *)malloc(iLen * 3 + 1); + if (hexout != NULL) { + for (int i = 0; i < iLen; i++) { // data in hex + sprintf(&hexout[i * 3], "%02X ", recv[i]); + } + PrintAndLog("%s", hexout); + free(hexout); + } else { + PrintAndLog("malloc failed your client has low memory?"); + return 2; + } + printf("Command response just sent back to Lua script with the bytes which were just printed. Should get sent to read14a.lua.\n"); + //END ADDED CODE + + //Push it as a string lua_pushlstring(L,(const char *)&response,sizeof(UsbCommand)); @@ -109,6 +141,7 @@ static int l_WaitForResponseTimeout(lua_State *L){ }else{ //Push a Nil instead lua_pushnil(L); + printf("Nil pushed back to Lua script - no response received.\n"); return 1;// one return value } } diff --git a/client/scripts/myscript.lua b/client/scripts/myscript.lua new file mode 100644 index 00000000..b5314e9e --- /dev/null +++ b/client/scripts/myscript.lua @@ -0,0 +1,84 @@ +local cmds = require('commands') +local lib14a = require('read14a') + + +--- +-- This is only meant to be used when errors occur +function oops(err) + print("ERROR: ",err) +end + +function sendRaw(rawdata, crc) + print(">> ", rawdata) + + -- if crc + -- then local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + -- else local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_APPEND_CRC + -- end + + local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC + + -- local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW + + local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, + arg1 = flags, -- Send raw + arg2 = string.len(rawdata) / 2, -- arg2 contains the length, which is half the length of the ASCII-string rawdata + data = rawdata} + local ignore_response = false + return lib14a.sendToDevice(command, ignore_response) +end + +--- +-- The main entry point +function main(args) + + + + -- Manually send the card the init commands + -- firstcommand = Command:new{cmd = cmds.CMD_READER_ISO_14443a, + -- arg1 = lib14a.ISO14A_COMMAND.ISO14A_CONNECT + lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT, + -- arg2 = 0, + -- data = ""} + -- local restwo,errtwo = lib14a.sendToDevice(firstcommand) + -- print(firstcommand.arg1) + + -- Call the program via the command line + -- result = core.console("hf 14a raw -p -b 7 -a 26") --I can do this from the command line, but I can't capture the output easily + -- print(result) + + -- Send the card the init commands using the read14a library, reusing the connect functionality from a common library + info,err = lib14a.read14443a(true, no_rats) + if err + then oops(err) + else print(("Connected to card with a UID of %s"):format(info.uid)) + end + + --Attempt to send raw data + getvers = "0360" -- 0x0360 should begin the "get version" commands + --print(string.byte(getvers, 1, 99999)) + --crc = core.crc16(getvers) -- under the hood, calls ComputeCrc14443, which is the same function which is called by "hf 14a raw" + --print(string.byte(crc, 1, 99999)) + + local result,err = sendRaw(getvers, true) + if result then + print("Currently trying to decode raw UsbCommand packet received.") + print(result) + + local count,cmd,arg1,arg2,arg3,data = bin.unpack('LLLLH512',result) + print(data) + --data = string.sub(result,count) + --local cmd_response = Command.parse(res) + else + err ="No response from card" + end + -- local cmd_response = Command.parse(res) + -- local len = tonumber(cmd_response.arg1) *2 + -- print("data length:",len) + -- local data = string.sub(tostring(cmd_response.data), 0, len); + -- print("<< ",data) + +end + + + +main(args) -- Call the main function