]>
Commit | Line | Data |
---|---|---|
806dc075 | 1 | //----------------------------------------------------------------------------- |
a0655c45 | 2 | // Copyright (C) 2013 m h swende <martin at swende.se> |
806dc075 | 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 | //----------------------------------------------------------------------------- | |
a0655c45 | 8 | // Some lua scripting glue to proxmark core. |
806dc075 | 9 | //----------------------------------------------------------------------------- |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
15 | #include <sys/types.h> | |
16 | #include <dirent.h> | |
17 | ||
18 | #include "proxmark3.h" | |
a0655c45 | 19 | #include "scripting.h" |
806dc075 | 20 | #include "data.h" |
21 | #include "ui.h" | |
22 | #include "graph.h" | |
23 | #include "cmdparser.h" | |
24 | #include "cmdmain.h" | |
25 | #include "cmdscript.h" | |
26 | #include "cmdhfmf.h" | |
f057bddb | 27 | #include "pm3_binlib.h" |
806dc075 | 28 | |
29 | #include <lua.h> | |
30 | #include <lualib.h> | |
31 | #include <lauxlib.h> | |
32 | ||
33 | ||
806dc075 | 34 | static int CmdHelp(const char *Cmd); |
35 | static int CmdList(const char *Cmd); | |
36 | static int CmdRun(const char *Cmd); | |
37 | ||
38 | command_t CommandTable[] = | |
39 | { | |
40 | {"help", CmdHelp, 1, "This help"}, | |
a0655c45 | 41 | {"list", CmdList, 1, "List available scripts"}, |
42 | {"run", CmdRun, 1, "<name> -- Execute a script"}, | |
806dc075 | 43 | {NULL, NULL, 0, NULL} |
44 | }; | |
45 | ||
46 | int str_ends_with(const char * str, const char * suffix) { | |
47 | ||
48 | if( str == NULL || suffix == NULL ) | |
49 | return 0; | |
50 | ||
51 | size_t str_len = strlen(str); | |
52 | size_t suffix_len = strlen(suffix); | |
53 | ||
54 | if(suffix_len > str_len) | |
55 | return 0; | |
56 | ||
57 | return 0 == strncmp( str + str_len - suffix_len, suffix, suffix_len ); | |
58 | } | |
59 | /** | |
60 | * Shows some basic help | |
61 | * @brief CmdHelp | |
62 | * @param Cmd | |
63 | * @return | |
64 | */ | |
65 | int CmdHelp(const char * Cmd) | |
66 | { | |
67 | PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. "); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Generate list of available commands, what it does is | |
72 | * generate a file listing of the script-directory for files | |
73 | * ending with .lua | |
74 | */ | |
75 | int CmdList(const char *Cmd) | |
76 | { | |
77 | DIR *dp; | |
78 | struct dirent *ep; | |
79 | dp = opendir ("./scripts/"); | |
80 | if (dp != NULL) | |
81 | { | |
82 | while (ep = readdir (dp)) | |
83 | { | |
84 | if(ep->d_name != NULL && str_ends_with(ep->d_name, ".lua")) | |
85 | PrintAndLog("%-16s %s", ep->d_name, "A script file"); | |
86 | } | |
87 | (void) closedir (dp); | |
88 | } | |
89 | else | |
90 | PrintAndLog ("Couldn't open the directory"); | |
91 | return 0; | |
92 | } | |
93 | /** | |
94 | * Finds a matching script-file | |
95 | * @brief CmdScript | |
96 | * @param Cmd | |
97 | * @return | |
98 | */ | |
99 | int CmdScript(const char *Cmd) | |
100 | { | |
101 | CmdsParse(CommandTable, Cmd); | |
102 | return 0; | |
103 | } | |
104 | /* | |
105 | static int l_hfmf (lua_State *L) { | |
106 | return CmdHFMF("wohoo"); | |
107 | ||
108 | } | |
109 | */ | |
5a92cb52 | 110 | //static int l_CmdHelp(lua_State *L){ return CmdHelp(luaL_checkstring(L, 1));} |
111 | static int l_CmdHF14AMfDbg(lua_State *L){ return CmdHF14AMfDbg(luaL_checkstring(L, 1));} | |
112 | static int l_CmdHF14AMfRdBl(lua_State *L){ return CmdHF14AMfRdBl(luaL_checkstring(L, 1));} | |
113 | static int l_CmdHF14AMfRdSc(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));} | |
114 | static int l_CmdHF14AMfDump(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));} | |
806dc075 | 115 | static int l_CmdHF14AMifare(lua_State *L){ return CmdHF14AMifare(luaL_checkstring(L, 1));} |
5a92cb52 | 116 | static int l_CmdHF14AMfWrBl(lua_State *L){ return CmdHF14AMfWrBl(luaL_checkstring(L, 1));} |
117 | static int l_CmdHF14AMfChk(lua_State *L){ return CmdHF14AMfChk(luaL_checkstring(L, 1));} | |
806dc075 | 118 | static int l_CmdHF14AMfNested(lua_State *L){ return CmdHF14AMfNested(luaL_checkstring(L, 1));} |
5a92cb52 | 119 | static int l_CmdHF14AMfRestore(lua_State *L){ return CmdHF14AMfRestore(luaL_checkstring(L, 1));} |
120 | static int l_CmdHF14AMfSniff(lua_State *L){ return CmdHF14AMfSniff(luaL_checkstring(L, 1));} | |
121 | static int l_CmdHF14AMf1kSim(lua_State *L){ return CmdHF14AMf1kSim(luaL_checkstring(L, 1));} | |
122 | static int l_CmdHF14AMfEClear(lua_State *L){ return CmdHF14AMfEClear(luaL_checkstring(L, 1));} | |
123 | static int l_CmdHF14AMfEGet(lua_State *L){ return CmdHF14AMfEGet(luaL_checkstring(L, 1));} | |
124 | static int l_CmdHF14AMfESet(lua_State *L){ return CmdHF14AMfESet(luaL_checkstring(L, 1));} | |
125 | static int l_CmdHF14AMfELoad(lua_State *L){ return CmdHF14AMfELoad(luaL_checkstring(L, 1));} | |
126 | static int l_CmdHF14AMfESave(lua_State *L){ return CmdHF14AMfESave(luaL_checkstring(L, 1));} | |
127 | static int l_CmdHF14AMfECFill(lua_State *L){ return CmdHF14AMfECFill(luaL_checkstring(L, 1));} | |
128 | static int l_CmdHF14AMfEKeyPrn(lua_State *L){ return CmdHF14AMfEKeyPrn(luaL_checkstring(L, 1));} | |
129 | static int l_CmdHF14AMfCSetUID(lua_State *L){ return CmdHF14AMfCSetUID(luaL_checkstring(L, 1));} | |
130 | static int l_CmdHF14AMfCSetBlk(lua_State *L){ return CmdHF14AMfCSetBlk(luaL_checkstring(L, 1));} | |
131 | static int l_CmdHF14AMfCGetBlk(lua_State *L){ return CmdHF14AMfCGetBlk(luaL_checkstring(L, 1));} | |
132 | static int l_CmdHF14AMfCGetSc(lua_State *L){ return CmdHF14AMfCGetSc(luaL_checkstring(L, 1));} | |
133 | static int l_CmdHF14AMfCLoad(lua_State *L){ return CmdHF14AMfCLoad(luaL_checkstring(L, 1));} | |
134 | static int l_CmdHF14AMfCSave(lua_State *L){ return CmdHF14AMfCSave(luaL_checkstring(L, 1));} | |
135 | ||
136 | ||
806dc075 | 137 | |
806dc075 | 138 | |
a0655c45 | 139 | static void set_cmdlibraries(lua_State *L) |
806dc075 | 140 | { |
141 | static const luaL_Reg hfmf_lib[] = { | |
806dc075 | 142 | {"dbg", l_CmdHF14AMfDbg}, |
143 | {"rdbl", l_CmdHF14AMfRdBl}, | |
144 | {"rdsc", l_CmdHF14AMfRdSc}, | |
145 | {"dump", l_CmdHF14AMfDump}, | |
146 | {"restore", l_CmdHF14AMfRestore}, | |
147 | {"wrbl", l_CmdHF14AMfWrBl}, | |
148 | {"chk", l_CmdHF14AMfChk}, | |
149 | {"mifare", l_CmdHF14AMifare}, | |
150 | {"nested", l_CmdHF14AMfNested}, | |
151 | {"sniff", l_CmdHF14AMfSniff}, | |
152 | {"sim", l_CmdHF14AMf1kSim}, | |
153 | {"eclr", l_CmdHF14AMfEClear}, | |
154 | {"eget", l_CmdHF14AMfEGet}, | |
155 | {"eset", l_CmdHF14AMfESet}, | |
156 | {"eload", l_CmdHF14AMfELoad}, | |
157 | {"esave", l_CmdHF14AMfESave}, | |
158 | {"ecfill", l_CmdHF14AMfECFill}, | |
159 | {"ekeyprn", l_CmdHF14AMfEKeyPrn}, | |
160 | {"csetuid", l_CmdHF14AMfCSetUID}, | |
161 | {"csetblk", l_CmdHF14AMfCSetBlk}, | |
162 | {"cgetblk", l_CmdHF14AMfCGetBlk}, | |
163 | {"cgetsc", l_CmdHF14AMfCGetSc}, | |
164 | {"cload", l_CmdHF14AMfCLoad}, | |
165 | {"csave", l_CmdHF14AMfCSave}, | |
166 | {NULL, NULL} | |
167 | }; | |
168 | ||
169 | lua_pushglobaltable(L); | |
170 | // Core library is in this table. Contains 'hf' | |
171 | ||
172 | //this is 'hf' table | |
173 | lua_newtable(L); | |
174 | ||
175 | //this is the mf table | |
176 | lua_newtable(L); | |
177 | ||
178 | //Put the function into the hash table. | |
179 | for (int i = 0; hfmf_lib[i].name; i++) { | |
180 | lua_pushcfunction(L, hfmf_lib[i].func); | |
181 | lua_setfield(L, -2, hfmf_lib[i].name);//set the name, pop stack | |
182 | } | |
183 | //Name of 'mf' | |
184 | lua_setfield(L, -2, "mf"); | |
185 | ||
186 | //Name of 'hf' | |
187 | lua_setfield(L, -2, "hf"); | |
188 | ||
189 | //-- remove the global environment table from the stack | |
190 | lua_pop(L, 1); | |
191 | return 1; | |
192 | } | |
193 | ||
194 | /** | |
195 | * @brief CmdRun - executes a script file. | |
196 | * @param argc | |
197 | * @param argv | |
198 | * @return | |
199 | */ | |
200 | int CmdRun(const char *Cmd) | |
201 | { | |
202 | // create new Lua state | |
203 | lua_State *lua_state; | |
204 | lua_state = luaL_newstate(); | |
205 | ||
206 | // load Lua libraries | |
5a92cb52 | 207 | luaL_openlibs(lua_state); |
a0655c45 | 208 | |
209 | //Sets the pm3 core libraries, that go a bit 'under the hood' | |
210 | set_pm3_libraries(lua_state); | |
211 | ||
212 | //Sets the 'command line' libraries, basically just the commandline stuff | |
213 | set_cmdlibraries(lua_state); | |
f057bddb | 214 | |
215 | //Add the 'bin' library | |
216 | set_bin_library(lua_state); | |
217 | ||
806dc075 | 218 | char cmd_name[32]; |
219 | int len = 0; | |
220 | memset(cmd_name, 0, 32); | |
221 | sscanf(Cmd, "%31s%n", cmd_name, &len); | |
222 | ||
223 | char buf[256]; | |
224 | snprintf(buf, sizeof buf, "./scripts/%s", cmd_name); | |
225 | ||
96e7a3a5 | 226 | printf("-----Executing file '%s'\n" , cmd_name); |
806dc075 | 227 | // run the Lua script |
a0655c45 | 228 | |
229 | int error = luaL_loadfile(lua_state, buf); | |
230 | if(!error) | |
231 | { | |
232 | error = lua_pcall(lua_state, 0, LUA_MULTRET, 0); // once again, returns non-0 on error, | |
233 | } | |
234 | if(error) // if non-0, then an error | |
235 | { | |
236 | // the top of the stack should be the error string | |
237 | if (!lua_isstring(lua_state, lua_gettop(lua_state))) | |
238 | printf( "Error - but no error (?!)"); | |
239 | ||
240 | // get the top of the stack as the error and pop it off | |
241 | const char * str = lua_tostring(lua_state, lua_gettop(lua_state)); | |
242 | lua_pop(lua_state, 1); | |
243 | printf(str); | |
244 | } | |
245 | ||
246 | //luaL_dofile(lua_state, buf); | |
806dc075 | 247 | // close the Lua state |
248 | lua_close(lua_state); | |
96e7a3a5 | 249 | printf("\n-----Finished\n"); |
806dc075 | 250 | } |
251 |