]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdscript.c
ADD: `analyse nuid` - generates NUID 4byte from a UID 7byte. Mifare Classic Ev1...
[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"
77cd612f 28#include "pm3_bitlib.h"
806dc075 29#include <lua.h>
30#include <lualib.h>
31#include <lauxlib.h>
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{
b804b9cd 66 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. ");
42daa759 67 return 0;
806dc075 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
bd94b978 74*
806dc075 75*/
76int CmdList(const char *Cmd)
77{
b804b9cd 78 DIR *dp;
806dc075 79 struct dirent *ep;
b804b9cd 80 char script_directory_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + 1];
81 strcpy(script_directory_path, get_my_executable_directory());
82 strcat(script_directory_path, LUA_SCRIPTS_DIRECTORY);
83 dp = opendir(script_directory_path);
a403a559 84
b804b9cd 85 if (dp != NULL)
86 {
bd94b978 87 while ((ep = readdir (dp)) != NULL)
88 {
89 if(str_ends_with(ep->d_name, ".lua"))
90 PrintAndLog("%-21s %s", ep->d_name, "A script file");
91 }
92 (void) closedir (dp);
b804b9cd 93 }
94 else
95 PrintAndLog ("Couldn't open the scripts-directory");
806dc075 96 return 0;
97}
bd94b978 98
99
806dc075 100/**
101 * Finds a matching script-file
102 * @brief CmdScript
103 * @param Cmd
104 * @return
105 */
4c36581b 106int CmdScript(const char *Cmd) {
107 clearCommandBuffer();
108 CmdsParse(CommandTable, Cmd);
109 return 0;
806dc075 110}
a403a559 111/**
112 * Utility to check the ending of a string (used to check file suffix)
113 */
114bool endsWith (char* base, char* str) {
115 int blen = strlen(base);
116 int slen = strlen(str);
117 return (blen >= slen) && (0 == strcmp(base + blen - slen, str));
118}
806dc075 119
120/**
121 * @brief CmdRun - executes a script file.
122 * @param argc
123 * @param argv
124 * @return
125 */
126int CmdRun(const char *Cmd)
127{
128 // create new Lua state
129 lua_State *lua_state;
130 lua_state = luaL_newstate();
131
132 // load Lua libraries
5a92cb52 133 luaL_openlibs(lua_state);
a0655c45 134
135 //Sets the pm3 core libraries, that go a bit 'under the hood'
136 set_pm3_libraries(lua_state);
137
f057bddb 138 //Add the 'bin' library
139 set_bin_library(lua_state);
140
77cd612f 141 //Add the 'bit' library
142 set_bit_library(lua_state);
a403a559 143
30a5d355 144 char script_name[128] = {0};
145 char arguments[256] = {0};
a403a559 146
147 int name_len = 0;
148 int arg_len = 0;
149 sscanf(Cmd, "%127s%n %255[^\n\r]%n", script_name,&name_len, arguments, &arg_len);
150
151 char *suffix = "";
152 if(!endsWith(script_name,".lua"))
153 {
154 suffix = ".lua";
155 }
806dc075 156
b804b9cd 157 char script_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + strlen(script_name) + strlen(suffix) + 1];
158 strcpy(script_path, get_my_executable_directory());
159 strcat(script_path, LUA_SCRIPTS_DIRECTORY);
160 strcat(script_path, script_name);
161 strcat(script_path, suffix);
162
163 printf("--- Executing: %s%s, args '%s'\n", script_name, suffix, arguments);
164
a403a559 165
806dc075 166
806dc075 167 // run the Lua script
a0655c45 168
b804b9cd 169 int error = luaL_loadfile(lua_state, script_path);
a0655c45 170 if(!error)
171 {
a403a559 172 lua_pushstring(lua_state, arguments);
173 lua_setglobal(lua_state, "args");
174
175 //Call it with 0 arguments
a0655c45 176 error = lua_pcall(lua_state, 0, LUA_MULTRET, 0); // once again, returns non-0 on error,
177 }
178 if(error) // if non-0, then an error
179 {
180 // the top of the stack should be the error string
181 if (!lua_isstring(lua_state, lua_gettop(lua_state)))
182 printf( "Error - but no error (?!)");
183
184 // get the top of the stack as the error and pop it off
185 const char * str = lua_tostring(lua_state, lua_gettop(lua_state));
186 lua_pop(lua_state, 1);
42daa759 187 puts(str);
a0655c45 188 }
189
190 //luaL_dofile(lua_state, buf);
806dc075 191 // close the Lua state
192 lua_close(lua_state);
96e7a3a5 193 printf("\n-----Finished\n");
42daa759 194 return 0;
806dc075 195}
196
Impressum, Datenschutz