]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdscript.c
Fixed error from r769 where I added -ldl switch for linux, but still forgot to remove...
[proxmark3-svn] / client / cmdscript.c
CommitLineData
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 34static int CmdHelp(const char *Cmd);
35static int CmdList(const char *Cmd);
36static int CmdRun(const char *Cmd);
37
38command_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
46int 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 */
65int CmdHelp(const char * Cmd)
66{
67 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. ");
42daa759 68 return 0;
806dc075 69}
70
71/**
72* Generate list of available commands, what it does is
73* generate a file listing of the script-directory for files
74* ending with .lua
75*/
76int CmdList(const char *Cmd)
77{
78 DIR *dp;
79 struct dirent *ep;
80 dp = opendir ("./scripts/");
a403a559 81
806dc075 82 if (dp != NULL)
83 {
42daa759 84 while ((ep = readdir (dp)) != NULL)
806dc075 85 {
86 if(ep->d_name != NULL && str_ends_with(ep->d_name, ".lua"))
87 PrintAndLog("%-16s %s", ep->d_name, "A script file");
88 }
89 (void) closedir (dp);
90 }
91 else
a403a559 92 PrintAndLog ("Couldn't open the scripts-directory");
806dc075 93 return 0;
94}
95/**
96 * Finds a matching script-file
97 * @brief CmdScript
98 * @param Cmd
99 * @return
100 */
101int CmdScript(const char *Cmd)
102{
103 CmdsParse(CommandTable, Cmd);
104 return 0;
105}
106/*
107static int l_hfmf (lua_State *L) {
108 return CmdHFMF("wohoo");
109
110}
111*/
5a92cb52 112//static int l_CmdHelp(lua_State *L){ return CmdHelp(luaL_checkstring(L, 1));}
113static int l_CmdHF14AMfDbg(lua_State *L){ return CmdHF14AMfDbg(luaL_checkstring(L, 1));}
114static int l_CmdHF14AMfRdBl(lua_State *L){ return CmdHF14AMfRdBl(luaL_checkstring(L, 1));}
115static int l_CmdHF14AMfRdSc(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));}
116static int l_CmdHF14AMfDump(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));}
806dc075 117static int l_CmdHF14AMifare(lua_State *L){ return CmdHF14AMifare(luaL_checkstring(L, 1));}
5a92cb52 118static int l_CmdHF14AMfWrBl(lua_State *L){ return CmdHF14AMfWrBl(luaL_checkstring(L, 1));}
119static int l_CmdHF14AMfChk(lua_State *L){ return CmdHF14AMfChk(luaL_checkstring(L, 1));}
806dc075 120static int l_CmdHF14AMfNested(lua_State *L){ return CmdHF14AMfNested(luaL_checkstring(L, 1));}
5a92cb52 121static int l_CmdHF14AMfRestore(lua_State *L){ return CmdHF14AMfRestore(luaL_checkstring(L, 1));}
122static int l_CmdHF14AMfSniff(lua_State *L){ return CmdHF14AMfSniff(luaL_checkstring(L, 1));}
123static int l_CmdHF14AMf1kSim(lua_State *L){ return CmdHF14AMf1kSim(luaL_checkstring(L, 1));}
124static int l_CmdHF14AMfEClear(lua_State *L){ return CmdHF14AMfEClear(luaL_checkstring(L, 1));}
125static int l_CmdHF14AMfEGet(lua_State *L){ return CmdHF14AMfEGet(luaL_checkstring(L, 1));}
126static int l_CmdHF14AMfESet(lua_State *L){ return CmdHF14AMfESet(luaL_checkstring(L, 1));}
127static int l_CmdHF14AMfELoad(lua_State *L){ return CmdHF14AMfELoad(luaL_checkstring(L, 1));}
128static int l_CmdHF14AMfESave(lua_State *L){ return CmdHF14AMfESave(luaL_checkstring(L, 1));}
129static int l_CmdHF14AMfECFill(lua_State *L){ return CmdHF14AMfECFill(luaL_checkstring(L, 1));}
130static int l_CmdHF14AMfEKeyPrn(lua_State *L){ return CmdHF14AMfEKeyPrn(luaL_checkstring(L, 1));}
131static int l_CmdHF14AMfCSetUID(lua_State *L){ return CmdHF14AMfCSetUID(luaL_checkstring(L, 1));}
132static int l_CmdHF14AMfCSetBlk(lua_State *L){ return CmdHF14AMfCSetBlk(luaL_checkstring(L, 1));}
133static int l_CmdHF14AMfCGetBlk(lua_State *L){ return CmdHF14AMfCGetBlk(luaL_checkstring(L, 1));}
134static int l_CmdHF14AMfCGetSc(lua_State *L){ return CmdHF14AMfCGetSc(luaL_checkstring(L, 1));}
135static int l_CmdHF14AMfCLoad(lua_State *L){ return CmdHF14AMfCLoad(luaL_checkstring(L, 1));}
136static int l_CmdHF14AMfCSave(lua_State *L){ return CmdHF14AMfCSave(luaL_checkstring(L, 1));}
137
138
806dc075 139
806dc075 140
a0655c45 141static void set_cmdlibraries(lua_State *L)
806dc075 142{
143 static const luaL_Reg hfmf_lib[] = {
806dc075 144 {"dbg", l_CmdHF14AMfDbg},
145 {"rdbl", l_CmdHF14AMfRdBl},
146 {"rdsc", l_CmdHF14AMfRdSc},
147 {"dump", l_CmdHF14AMfDump},
148 {"restore", l_CmdHF14AMfRestore},
149 {"wrbl", l_CmdHF14AMfWrBl},
150 {"chk", l_CmdHF14AMfChk},
151 {"mifare", l_CmdHF14AMifare},
152 {"nested", l_CmdHF14AMfNested},
153 {"sniff", l_CmdHF14AMfSniff},
154 {"sim", l_CmdHF14AMf1kSim},
155 {"eclr", l_CmdHF14AMfEClear},
156 {"eget", l_CmdHF14AMfEGet},
157 {"eset", l_CmdHF14AMfESet},
158 {"eload", l_CmdHF14AMfELoad},
159 {"esave", l_CmdHF14AMfESave},
160 {"ecfill", l_CmdHF14AMfECFill},
161 {"ekeyprn", l_CmdHF14AMfEKeyPrn},
162 {"csetuid", l_CmdHF14AMfCSetUID},
163 {"csetblk", l_CmdHF14AMfCSetBlk},
164 {"cgetblk", l_CmdHF14AMfCGetBlk},
165 {"cgetsc", l_CmdHF14AMfCGetSc},
166 {"cload", l_CmdHF14AMfCLoad},
167 {"csave", l_CmdHF14AMfCSave},
168 {NULL, NULL}
169 };
170
171 lua_pushglobaltable(L);
172 // Core library is in this table. Contains 'hf'
173
174 //this is 'hf' table
175 lua_newtable(L);
176
177 //this is the mf table
178 lua_newtable(L);
179
180 //Put the function into the hash table.
181 for (int i = 0; hfmf_lib[i].name; i++) {
182 lua_pushcfunction(L, hfmf_lib[i].func);
183 lua_setfield(L, -2, hfmf_lib[i].name);//set the name, pop stack
184 }
185 //Name of 'mf'
186 lua_setfield(L, -2, "mf");
187
188 //Name of 'hf'
189 lua_setfield(L, -2, "hf");
190
191 //-- remove the global environment table from the stack
192 lua_pop(L, 1);
806dc075 193}
a403a559 194/**
195 * Utility to check the ending of a string (used to check file suffix)
196 */
197bool endsWith (char* base, char* str) {
198 int blen = strlen(base);
199 int slen = strlen(str);
200 return (blen >= slen) && (0 == strcmp(base + blen - slen, str));
201}
806dc075 202
203/**
204 * @brief CmdRun - executes a script file.
205 * @param argc
206 * @param argv
207 * @return
208 */
209int CmdRun(const char *Cmd)
210{
211 // create new Lua state
212 lua_State *lua_state;
213 lua_state = luaL_newstate();
214
215 // load Lua libraries
5a92cb52 216 luaL_openlibs(lua_state);
a0655c45 217
218 //Sets the pm3 core libraries, that go a bit 'under the hood'
219 set_pm3_libraries(lua_state);
220
221 //Sets the 'command line' libraries, basically just the commandline stuff
222 set_cmdlibraries(lua_state);
f057bddb 223
224 //Add the 'bin' library
225 set_bin_library(lua_state);
226
a403a559 227// char cmd_name[32];
228// memset(cmd_name, 0, 32);
229// sscanf(Cmd, "%31s%n", cmd_name, &len);
230
30a5d355 231 char script_name[128] = {0};
232 char arguments[256] = {0};
a403a559 233
234 int name_len = 0;
235 int arg_len = 0;
236 sscanf(Cmd, "%127s%n %255[^\n\r]%n", script_name,&name_len, arguments, &arg_len);
237
238 char *suffix = "";
239 if(!endsWith(script_name,".lua"))
240 {
241 suffix = ".lua";
242 }
806dc075 243
244 char buf[256];
a403a559 245 snprintf(buf, sizeof buf, "./scripts/%s%s", script_name, suffix);
246
30a5d355 247 printf("--- Executing: %s, args'%s'\n",buf,arguments);
a403a559 248
249
250
806dc075 251
806dc075 252 // run the Lua script
a0655c45 253
254 int error = luaL_loadfile(lua_state, buf);
255 if(!error)
256 {
a403a559 257
258 lua_pushstring(lua_state, arguments);
259 lua_setglobal(lua_state, "args");
260
261 //Call it with 0 arguments
a0655c45 262 error = lua_pcall(lua_state, 0, LUA_MULTRET, 0); // once again, returns non-0 on error,
263 }
264 if(error) // if non-0, then an error
265 {
266 // the top of the stack should be the error string
267 if (!lua_isstring(lua_state, lua_gettop(lua_state)))
268 printf( "Error - but no error (?!)");
269
270 // get the top of the stack as the error and pop it off
271 const char * str = lua_tostring(lua_state, lua_gettop(lua_state));
272 lua_pop(lua_state, 1);
42daa759 273 puts(str);
a0655c45 274 }
275
276 //luaL_dofile(lua_state, buf);
806dc075 277 // close the Lua state
278 lua_close(lua_state);
96e7a3a5 279 printf("\n-----Finished\n");
42daa759 280 return 0;
806dc075 281}
282
Impressum, Datenschutz