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