]> git.zerfleddert.de Git - proxmark3-svn/blob - client/scripting.c
f9f4999df37b5d2280bad1800b9383d0abe60b3a
[proxmark3-svn] / client / scripting.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2013 m h swende <martin at swende.se>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Some lua scripting glue to proxmark core.
9 //-----------------------------------------------------------------------------
10
11 #include <lua.h>
12 #include <lualib.h>
13 #include <lauxlib.h>
14 #include "proxmark3.h"
15 #include "usb_cmd.h"
16 #include "cmdmain.h"
17 #include "scripting.h"
18 /**
19 * The following params expected:
20 * UsbCommand c
21 *@brief l_SendCommand
22 * @param L
23 * @return
24 */
25 static int l_SendCommand(lua_State *L){
26
27 /*
28 *
29 The SendCommand (native) expects the following structure:
30
31 typedef struct {
32 uint64_t cmd; //8 bytes
33 uint64_t arg[3]; // 8*3 bytes = 24 bytes
34 union {
35 uint8_t asBytes[USB_CMD_DATA_SIZE]; // 1 byte * 512 = 512 bytes (OR)
36 uint32_t asDwords[USB_CMD_DATA_SIZE/4]; // 4 byte * 128 = 512 bytes
37 } d;
38 } PACKED UsbCommand;
39
40 ==> A 544 byte buffer will do.
41 **/
42 //Pop cmd
43 UsbCommand *c = (UsbCommand *)lua_touserdata(L, 1);
44 luaL_argcheck(L, c != NULL, 1, "'UsbCommand' expected");
45 SendCommand(c);
46 return 0;
47 }
48 /**
49 * @brief The following params expected:
50 * uint32_t cmd
51 * size_t ms_timeout
52 * @param L
53 * @return
54 */
55 static int l_WaitForResponseTimeout(lua_State *L){
56
57 //pop cmd
58 uint32_t cmd = luaL_checkunsigned(L,1);
59 printf("in l_WaitForResponseTimeout, got cmd 0x%0x\n",(int) cmd);
60 //UsbCommand response;
61
62 //We allocate the usbcommand as userdata on the Lua-stack
63 size_t nbytes = sizeof(UsbCommand);
64
65 UsbCommand *response = (UsbCommand *)lua_newuserdata(L, nbytes);
66
67 size_t ms_timeout = 2000;
68 //Did the user send a timeout ?
69 //Check if the current top of stack is an integer
70
71 if(lua_isnumber( L, 2))
72 {
73 printf("You sent a timout-value\n");
74 ms_timeout = luaL_checkunsigned(L,2);
75 }
76 printf("Timeout set to %dms\n" , (int) ms_timeout);
77
78 if(WaitForResponseTimeout(cmd, response, ms_timeout))
79 {
80 //Return the UsbCommand as userdata
81 //the usbcommand is already on the stack
82 // return 1 to signal one return value
83 return 1;
84 }else
85 {
86 //Don't return the UsbCommand. Pop it.
87 lua_pop(L,-1);
88 //Push a Nil instead
89 lua_pushnil(L);
90 return 1;
91 }
92 }
93 static int l_nonce2key(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));}
94 static int l_PrintAndLog(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));}
95
96 void set_pm3_libraries(lua_State *L)
97 {
98 static const luaL_Reg libs[] = {
99 {"SendCommand", l_SendCommand},
100 {"WaitForResponseTimeout", l_WaitForResponseTimeout},
101 {"nonce2key", l_nonce2key},
102 {"PrintAndLog", l_PrintAndLog},
103 {NULL, NULL}
104 };
105
106 lua_pushglobaltable(L);
107 // Core library is in this table. Contains '
108 //this is 'pm3' table
109 lua_newtable(L);
110
111 //Put the function into the hash table.
112 for (int i = 0; libs[i].name; i++) {
113 lua_pushcfunction(L, libs[i].func);
114 lua_setfield(L, -2, libs[i].name);//set the name, pop stack
115 }
116 //Name of 'core'
117 lua_setfield(L, -2, "core");
118
119 //-- remove the global environment table from the stack
120 lua_pop(L, 1);
121 return 1;
122 }
Impressum, Datenschutz