Skip to content

Commit

Permalink
the string params for scripting was limited to 256 chars, this has be…
Browse files Browse the repository at this point in the history
…en increased to 1024 chars, allowing for 512 hex bytes to be read. remember spaces will count as a char when using the quotes. Also increased file name array to match the rest of the pm3 client length.
  • Loading branch information
iceman1001 committed Sep 11, 2024
1 parent 781bde8 commit a18ec2b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...

## [unreleased][unreleased]
- Changed scripting string params to accept 1024 chars, Thanks @evildaemond! (@iceman1001)
- Added detection for FM11NT021 (@iceman1001)
- Added detection of a magic NTAG 215 (@iceman1001)

Expand Down
47 changes: 36 additions & 11 deletions client/src/cmdscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,73 @@ extern PyObject *PyInit__pm3(void);
static int Pm3PyRun_SimpleFileNoExit(FILE *fp, const char *filename) {
PyObject *m, *d, *v;
int set_file_name = 0, ret = -1;

m = PyImport_AddModule("__main__");
if (m == NULL)
if (m == NULL) {
return -1;
}

Py_INCREF(m);
d = PyModule_GetDict(m);

if (PyDict_GetItemString(d, "__file__") == NULL) {

PyObject *f;
f = PyUnicode_DecodeFSDefault(filename);
if (f == NULL)
if (f == NULL) {
goto done;
}

if (PyDict_SetItemString(d, "__file__", f) < 0) {
Py_DECREF(f);
goto done;
}

if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
Py_DECREF(f);
goto done;
}

set_file_name = 1;
Py_DECREF(f);
}

v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 1, NULL);
if (v == NULL) {

Py_CLEAR(m);

if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
// PyErr_Print() exists if SystemExit so we've to handle it ourselves
PyObject *ty = 0, *er = 0, *tr = 0;
PyErr_Fetch(&ty, &er, &tr);

long err = PyLong_AsLong(er);
if (err) {
PrintAndLogEx(WARNING, "\nScript terminated by " _YELLOW_("SystemExit %li"), err);
} else {
ret = 0;
}

Py_DECREF(ty);
Py_DECREF(er);
Py_DECREF(er);
PyErr_Clear();
goto done;

} else {
PyErr_Print();
}
goto done;
}

Py_DECREF(v);
ret = 0;

done:
if (set_file_name && PyDict_DelItemString(d, "__file__"))
if (set_file_name && PyDict_DelItemString(d, "__file__")) {
PyErr_Clear();
}
Py_XDECREF(m);
return ret;
}
Expand All @@ -129,16 +147,20 @@ static int split(char *str, char **arr) {
int word_cnt = 0;

while (1) {

while (isspace(str[begin_index])) {
++begin_index;
}

if (str[begin_index] == '\0') {
break;
}

int end_index = begin_index;
while (str[end_index] && !isspace(str[end_index])) {
++end_index;
}

int len = end_index - begin_index;
char *tmp = calloc(len + 1, sizeof(char));
memcpy(tmp, &str[begin_index], len);
Expand Down Expand Up @@ -227,13 +249,16 @@ static int CmdScriptList(const char *Cmd) {
CLIParserFree(ctx);
PrintAndLogEx(NORMAL, "\n" _YELLOW_("[ Lua scripts ]"));
int ret = searchAndList(LUA_SCRIPTS_SUBDIR, ".lua");
if (ret != PM3_SUCCESS)
if (ret != PM3_SUCCESS) {
return ret;
}

PrintAndLogEx(NORMAL, "\n" _YELLOW_("[ Cmd scripts ]"));
ret = searchAndList(CMD_SCRIPTS_SUBDIR, ".cmd");
if (ret != PM3_SUCCESS)
if (ret != PM3_SUCCESS) {
return ret;
}

#ifdef HAVE_PYTHON
PrintAndLogEx(NORMAL, "\n" _YELLOW_("[ Python scripts ]"));
return searchAndList(PYTHON_SCRIPTS_SUBDIR, ".py");
Expand Down Expand Up @@ -265,19 +290,19 @@ static int CmdScriptRun(const char *Cmd) {
};

int fnlen = 0;
char filename[128] = {0};
char filename[FILE_PATH_SIZE] = {0};
int arg_len = 0;
char arguments[256] = {0};
char arguments[1025] = {0};

sscanf(Cmd, "%127s%n %255[^\n\r]%n", filename, &fnlen, arguments, &arg_len);
sscanf(Cmd, "%999s%n %1024[^\n\r]%n", filename, &fnlen, arguments, &arg_len);

// hack
// since we don't want to use "-f" for script filename,
// and be able to send in parameters into script meanwhile
// being able to "-h" here too.
if ((strlen(filename) == 0) ||
(strcmp(filename, "-h") == 0) ||
(strcmp(filename, "--help") == 0)) {
(strcmp(filename, "-h") == 0) ||
(strcmp(filename, "--help") == 0)) {
ctx->argtable = argtable;
ctx->argtableLen = arg_getsize(argtable);
CLIParserPrintHelp(ctx);
Expand Down Expand Up @@ -428,7 +453,7 @@ static int CmdScriptRun(const char *Cmd) {
#endif

//int argc, char ** argv
char *argv[128];
char *argv[FILE_PATH_SIZE];
argv[0] = script_path;
int argc = split(arguments, &argv[1]);
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 10
Expand Down
9 changes: 5 additions & 4 deletions client/src/scripting.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include "iso7816/iso7816core.h" // ISODEPSTATE

static int returnToLuaWithError(lua_State *L, const char *fmt, ...) {
char buffer[200];
char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
Expand Down Expand Up @@ -83,7 +83,7 @@ static int l_fast_push_mode(lua_State *L) {
// Disable fast mode and send a dummy command to make it effective
if (enable == false) {
SendCommandNG(CMD_PING, NULL, 0);
if (!WaitForResponseTimeout(CMD_PING, NULL, 1000)) {
if (WaitForResponseTimeout(CMD_PING, NULL, 1000) == false) {
PrintAndLogEx(WARNING, "command execution time out");
return returnToLuaWithError(L, "command execution time out");
}
Expand Down Expand Up @@ -113,8 +113,9 @@ static int l_SendCommandMIX(lua_State *L) {

// check number of arguments
int n = lua_gettop(L);
if (n != 5)
if (n != 5) {
return returnToLuaWithError(L, "You need to supply five parameters");
}

// parse input
cmd = luaL_checknumber(L, 1);
Expand Down Expand Up @@ -1441,7 +1442,7 @@ int set_pm3_libraries(lua_State *L) {
// put the function into the hash table.
for (int i = 0; libs[i].name; i++) {
lua_pushcfunction(L, libs[i].func);
lua_setfield(L, -2, libs[i].name);//set the name, pop stack
lua_setfield(L, -2, libs[i].name); // set the name, pop stack
}
// Name of 'core'
lua_setfield(L, -2, "core");
Expand Down

0 comments on commit a18ec2b

Please sign in to comment.