textual
[RRG-proxmark3.git] / client / src / scripting.c
blob0184c78a570a2ef3cfcf496be75791eaebde9baa
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2013 m h swende <martin at swende.se>
3 // Modified 2015,2016, iceman
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Some lua scripting glue to proxmark core.
10 //-----------------------------------------------------------------------------
11 #include "scripting.h"
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
17 #include "lauxlib.h"
18 #include "cmdmain.h"
19 #include "proxmark3.h"
20 #include "comms.h"
21 #include "mifare/mifarehost.h"
22 #include "crc.h"
23 #include "crc64.h"
24 #include "sha1.h"
25 #include "aes.h"
26 #include "cmdcrc.h"
27 #include "cmdhfmfhard.h"
28 #include "cmdhfmfu.h"
29 #include "cmdlft55xx.h" // read t55xx etc
30 #include "nfc/ndef.h" // ndef parsing
31 #include "commonutil.h"
32 #include "ui.h"
34 #include "crc16.h"
35 #include "protocols.h"
36 #include "fileutils.h" // searchfile
37 #include "cmdlf.h" // lf_config
38 #include "generator.h"
39 #include "cmdlfem4x05.h" // read 4305
40 #include "cmdlfem4x50.h" // read 4350
41 #include "em4x50.h" // 4x50 structs
43 static int returnToLuaWithError(lua_State *L, const char *fmt, ...) {
44 char buffer[200];
45 va_list args;
46 va_start(args, fmt);
47 vsnprintf(buffer, sizeof(buffer), fmt, args);
48 va_end(args);
50 lua_pushnil(L);
51 lua_pushstring(L, buffer);
52 return 2;
55 static int l_clearCommandBuffer(lua_State *L) {
56 clearCommandBuffer();
57 return 0;
60 /**
61 * Enable / Disable fast push mode for lua scripts like hf_mf_keycheck
62 * The following params expected:
64 *@brief l_fast_push_mode
65 * @param L
66 * @return
68 static int l_fast_push_mode(lua_State *L) {
70 luaL_checktype(L, 1, LUA_TBOOLEAN);
72 bool enable = lua_toboolean(L, 1);
74 conn.block_after_ACK = enable;
76 // Disable fast mode and send a dummy command to make it effective
77 if (enable == false) {
78 SendCommandNG(CMD_PING, NULL, 0);
79 if (!WaitForResponseTimeout(CMD_PING, NULL, 1000)) {
80 PrintAndLogEx(WARNING, "command execution time out");
81 return returnToLuaWithError(L, "command execution time out");
85 //Push the retval on the stack
86 lua_pushboolean(L, enable);
87 return 1;
90 /**
91 * The following params expected:
92 * @brief l_SendCommandOLD
93 * @param L - a lua string with the following five params.
94 * @param cmd must be hexstring, max u64
95 * @param arg0 must be hexstring, max u64
96 * @param arg1 must be hexstring, max u64
97 * @param arg2 must be hexstring, max u64
98 * @param data must be hexstring less than 1024 chars(512bytes)
99 * @return
101 static int l_SendCommandOLD(lua_State *L) {
102 // SendCommandMIX(CMD_HF_SNIFF, skippairs, skiptriggers, 0, NULL, 0);
103 // (uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len)
105 uint64_t cmd, arg0, arg1, arg2;
106 uint8_t data[PM3_CMD_DATA_SIZE] = {0};
107 size_t len = 0, size;
109 //Check number of arguments
110 int n = lua_gettop(L);
111 if (n != 5) {
112 return returnToLuaWithError(L, "You need to supply five parameters");
115 // parse input
116 cmd = luaL_checknumber(L, 1);
117 arg0 = luaL_checknumber(L, 2);
118 arg1 = luaL_checknumber(L, 3);
119 arg2 = luaL_checknumber(L, 4);
121 // data
122 const char *p_data = luaL_checklstring(L, 5, &size);
123 if (size) {
124 if (size > 1024)
125 size = 1024;
127 uint32_t tmp;
128 for (int i = 0; i < size; i += 2) {
129 sscanf(&p_data[i], "%02x", &tmp);
130 data[i >> 1] = tmp & 0xFF;
131 len++;
135 clearCommandBuffer();
136 SendCommandOLD(cmd, arg0, arg1, arg2, data, len);
137 lua_pushboolean(L, true);
138 return 1;
142 * The following params expected:
143 * @brief l_SendCommandMIX
144 * @param L - a lua string with the following five params.
145 * @param cmd must be hexstring, max u64
146 * @param arg0 must be hexstring, max u64
147 * @param arg1 must be hexstring, max u64
148 * @param arg2 must be hexstring, max u64
149 * @param data must be hexstring less than 1024 chars(512bytes)
150 * @return
152 static int l_SendCommandMIX(lua_State *L) {
154 uint64_t cmd, arg0, arg1, arg2;
155 uint8_t data[PM3_CMD_DATA_SIZE] = {0};
156 size_t len = 0, size;
158 // check number of arguments
159 int n = lua_gettop(L);
160 if (n != 5)
161 return returnToLuaWithError(L, "You need to supply five parameters");
163 // parse input
164 cmd = luaL_checknumber(L, 1);
165 arg0 = luaL_checknumber(L, 2);
166 arg1 = luaL_checknumber(L, 3);
167 arg2 = luaL_checknumber(L, 4);
169 // data
170 const char *p_data = luaL_checklstring(L, 5, &size);
171 if (size) {
172 if (size > 1024)
173 size = 1024;
175 uint32_t tmp;
176 for (int i = 0; i < size; i += 2) {
177 sscanf(&p_data[i], "%02x", &tmp);
178 data[i >> 1] = tmp & 0xFF;
179 len++;
183 clearCommandBuffer();
184 SendCommandMIX(cmd, arg0, arg1, arg2, data, len);
185 lua_pushboolean(L, true);
186 return 1;
189 * The following params expected:
190 * @brief l_SendCommandMIX
191 * @param L - a lua string with the following two params.
192 * @param cmd must be hexstring, max u64
193 * @param data must be hexstring less than 1024 chars(512bytes)
194 * @return
196 static int l_SendCommandNG(lua_State *L) {
198 uint8_t data[PM3_CMD_DATA_SIZE] = {0};
199 size_t len = 0, size;
201 // check number of arguments
202 int n = lua_gettop(L);
203 if (n != 2)
204 return returnToLuaWithError(L, "You need to supply two parameters");
206 // parse input
207 uint16_t cmd = luaL_checknumber(L, 1);
209 // data
210 const char *p_data = luaL_checklstring(L, 2, &size);
211 if (size) {
212 if (size > 1024)
213 size = 1024;
215 uint32_t tmp;
216 for (int i = 0; i < size; i += 2) {
217 sscanf(&p_data[i], "%02x", &tmp);
218 data[i >> 1] = tmp & 0xFF;
219 len++;
223 clearCommandBuffer();
224 SendCommandNG(cmd, data, len);
225 lua_pushboolean(L, true);
226 return 1;
231 * @brief The following params expected:
232 * uint8_t *dest
233 * int bytes
234 * int start_index
235 * @param L
236 * @return
238 static int l_GetFromBigBuf(lua_State *L) {
240 int len = 0, startindex = 0;
242 //Check number of arguments
243 int n = lua_gettop(L);
244 if (n == 0) {
245 return returnToLuaWithError(L, "You need to supply number of bytes and startindex");
248 if (n >= 2) {
249 startindex = luaL_checknumber(L, 1);
250 len = luaL_checknumber(L, 2);
253 if (len == 0) {
254 return returnToLuaWithError(L, "You need to supply number of bytes larger than zero");
257 uint8_t *data = calloc(len, sizeof(uint8_t));
258 if (!data) {
259 return returnToLuaWithError(L, "Allocating memory failed");
262 if (!GetFromDevice(BIG_BUF, data, len, startindex, NULL, 0, NULL, 2500, false)) {
263 free(data);
264 return returnToLuaWithError(L, "command execution time out");
267 //Push it as a string
268 lua_pushlstring(L, (const char *)data, len);
269 free(data);
270 return 1; // return 1 to signal one return value
274 * @brief The following params expected:
275 * uint8_t *dest
276 * int bytes
277 * int start_index
278 * @param L
279 * @return
281 static int l_GetFromFlashMem(lua_State *L) {
283 if (IfPm3Flash()) {
284 int len = 0, startindex = 0;
286 int n = lua_gettop(L);
287 if (n == 0)
288 return returnToLuaWithError(L, "You need to supply number of bytes and startindex");
290 if (n >= 2) {
291 startindex = luaL_checknumber(L, 1);
292 len = luaL_checknumber(L, 2);
295 if (len == 0)
296 return returnToLuaWithError(L, "You need to supply number of bytes larger than zero");
298 uint8_t *data = calloc(len, sizeof(uint8_t));
299 if (!data)
300 return returnToLuaWithError(L, "Allocating memory failed");
302 if (!GetFromDevice(FLASH_MEM, data, len, startindex, NULL, 0, NULL, -1, false)) {
303 free(data);
304 return returnToLuaWithError(L, "command execution time out");
307 lua_pushlstring(L, (const char *)data, len);
308 free(data);
309 return 1;
310 } else {
311 return returnToLuaWithError(L, "No FLASH MEM support");
316 * @brief The following params expected:
317 * uint8_t *destfilename
318 * @param L
319 * @return
321 static int l_GetFromFlashMemSpiffs(lua_State *L) {
323 if (IfPm3Flash() == false) {
324 return returnToLuaWithError(L, "No FLASH MEM support");
327 uint32_t start_index = 0, len = 0x40000; //FLASH_MEM_MAX_SIZE
328 char destfilename[32] = {0};
329 size_t size;
331 int n = lua_gettop(L);
332 if (n == 0)
333 return returnToLuaWithError(L, "You need to supply the destination filename");
335 if (n >= 1) {
336 const char *p_filename = luaL_checklstring(L, 1, &size);
337 if (size != 0)
338 memcpy(destfilename, p_filename, 31);
341 if (destfilename[0] == '\0')
342 return returnToLuaWithError(L, "Filename missing or invalid");
344 // get size from spiffs itself !
345 SendCommandNG(CMD_SPIFFS_STAT, (uint8_t *)destfilename, 32);
346 PacketResponseNG resp;
347 if (!WaitForResponseTimeout(CMD_SPIFFS_STAT, &resp, 2000))
348 return returnToLuaWithError(L, "No response from the device");
350 len = resp.data.asDwords[0];
352 if (len == 0)
353 return returnToLuaWithError(L, "Filename invalid or empty");
355 uint8_t *data = calloc(len, sizeof(uint8_t));
356 if (!data)
357 return returnToLuaWithError(L, "Allocating memory failed");
359 if (!GetFromDevice(SPIFFS, data, len, start_index, (uint8_t *)destfilename, 32, NULL, -1, true)) {
360 free(data);
361 return returnToLuaWithError(L, "ERROR; downloading from spiffs(flashmemory)");
364 lua_pushlstring(L, (const char *)data, len);
365 lua_pushunsigned(L, len);
366 free(data);
367 return 2;
371 * @brief The following params expected:
372 * uint32_t cmd
373 * size_t ms_timeout
374 * @param L
375 * @return struct of PacketResponseNG
377 static int l_WaitForResponseTimeout(lua_State *L) {
379 uint32_t cmd = 0;
380 size_t ms_timeout = -1;
382 //Check number of arguments
383 int n = lua_gettop(L);
384 if (n == 0)
385 return returnToLuaWithError(L, "You need to supply at least command to wait for");
387 // extract first param. cmd byte to look for
388 if (n >= 1)
389 cmd = luaL_checkunsigned(L, 1);
391 // extract second param. timeout value
392 if (n >= 2)
393 ms_timeout = luaL_checkunsigned(L, 2);
395 PacketResponseNG resp;
396 if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == false) {
397 return returnToLuaWithError(L, "No response from the device");
400 char foo[sizeof(PacketResponseNG)];
401 n = 0;
403 memcpy(foo + n, &resp.cmd, sizeof(resp.cmd));
404 n += sizeof(resp.cmd);
406 memcpy(foo + n, &resp.length, sizeof(resp.length));
407 n += sizeof(resp.length);
409 memcpy(foo + n, &resp.magic, sizeof(resp.magic));
410 n += sizeof(resp.magic);
412 memcpy(foo + n, &resp.status, sizeof(resp.status));
413 n += sizeof(resp.status);
415 memcpy(foo + n, &resp.crc, sizeof(resp.crc));
416 n += sizeof(resp.crc);
418 memcpy(foo + n, &resp.oldarg[0], sizeof(resp.oldarg[0]));
419 n += sizeof(resp.oldarg[0]);
421 memcpy(foo + n, &resp.oldarg[1], sizeof(resp.oldarg[1]));
422 n += sizeof(resp.oldarg[1]);
424 memcpy(foo + n, &resp.oldarg[2], sizeof(resp.oldarg[2]));
425 n += sizeof(resp.oldarg[2]);
427 memcpy(foo + n, resp.data.asBytes, sizeof(resp.data));
428 n += sizeof(resp.data);
430 memcpy(foo + n, &resp.ng, sizeof(resp.ng));
431 n += sizeof(resp.ng);
432 (void) n;
434 //Push it as a string
435 lua_pushlstring(L, (const char *)&foo, sizeof(foo));
436 return 1;
439 static int l_mfDarkside(lua_State *L) {
441 uint32_t blockno = 0;
442 uint32_t keytype = MIFARE_AUTH_KEYA;
443 uint64_t key = 0;
444 size_t size;
446 //Check number of arguments
447 int n = lua_gettop(L);
448 switch (n) {
449 case 2: {
450 const char *p_keytype = luaL_checklstring(L, 2, &size);
451 if (size != 2) return returnToLuaWithError(L, "Wrong size of keytype, got %d bytes, expected 1", (int) size);
452 sscanf(p_keytype, "%x", &keytype);
454 case 1: {
455 const char *p_blockno = luaL_checklstring(L, 1, &size);
456 if (size != 2) return returnToLuaWithError(L, "Wrong size of blockno, got %d bytes, expected 2", (int) size);
457 sscanf(p_blockno, "%02x", &blockno);
458 break;
460 default :
461 break;
464 int retval = mfDarkside(blockno & 0xFF, keytype & 0xFF, &key);
466 uint8_t dest_key[8];
467 num_to_bytes(key, sizeof(dest_key), dest_key);
469 //Push the retval on the stack
470 lua_pushinteger(L, retval);
471 lua_pushlstring(L, (const char *) dest_key, sizeof(dest_key));
472 return 2;
476 * @brief l_foobar is a dummy function to test lua-integration with
477 * @param L
478 * @return
480 static int l_foobar(lua_State *L) {
481 //Check number of arguments
482 int n = lua_gettop(L);
483 PrintAndLogEx(INFO, "foobar called with %d arguments", n);
484 lua_settop(L, 0);
485 PrintAndLogEx(INFO, "Arguments discarded, stack now contains %d elements", lua_gettop(L));
487 // todo: this is not used, where was it intended for?
488 // PacketCommandOLD response = {CMD_HF_MIFARE_READBL, {1337, 1338, 1339}, {{0}}};
490 PrintAndLogEx(INFO, "Now returning a uint64_t as a string");
491 uint64_t x = 0xDEADC0DE;
492 uint8_t destination[8];
493 num_to_bytes(x, sizeof(x), destination);
494 lua_pushlstring(L, (const char *)&x, sizeof(x));
495 lua_pushlstring(L, (const char *)destination, sizeof(destination));
496 return 2;
500 * @brief Utility to check if a key has been pressed by the user. This method does not block.
501 * @param L
502 * @return boolean, true if kbhit, false otherwise.
504 static int l_kbd_enter_pressed(lua_State *L) {
505 lua_pushboolean(L, kbd_enter_pressed() ? true : false);
506 return 1;
510 * @brief Calls the command line parser to deal with the command. This enables
511 * lua-scripts to do stuff like "core.console('hf mf mifare')"
512 * @param L
513 * @return
515 static int l_CmdConsole(lua_State *L) {
516 CommandReceived((char *)luaL_checkstring(L, 1));
517 return 0;
520 static int l_iso15693_crc(lua_State *L) {
521 uint32_t tmp;
522 unsigned char buf[PM3_CMD_DATA_SIZE] = {0x00};
523 size_t size = 0;
524 const char *data = luaL_checklstring(L, 1, &size);
526 for (int i = 0; i < size; i += 2) {
527 sscanf(&data[i], "%02x", &tmp);
528 buf[i / 2] = tmp & 0xFF;
531 size /= 2;
532 compute_crc(CRC_15693, buf, size, &buf[size], &buf[size + 1]);
533 lua_pushlstring(L, (const char *)&buf, size + 2);
534 return 1;
537 static int l_iso14443b_crc(lua_State *L) {
538 uint32_t tmp;
539 unsigned char buf[PM3_CMD_DATA_SIZE] = {0x00};
540 size_t size = 0;
541 const char *data = luaL_checklstring(L, 1, &size);
543 for (int i = 0; i < size; i += 2) {
544 sscanf(&data[i], "%02x", &tmp);
545 buf[i / 2] = tmp & 0xFF;
548 size /= 2;
549 compute_crc(CRC_14443_B, buf, size, &buf[size], &buf[size + 1]);
550 lua_pushlstring(L, (const char *)&buf, size + 2);
551 return 1;
555 Simple AES 128 cbc hook up to OpenSSL.
556 params: key, input
558 static int l_aes128decrypt_cbc(lua_State *L) {
559 //Check number of arguments
560 int i;
561 uint32_t tmp;
562 size_t size;
563 const char *p_key = luaL_checklstring(L, 1, &size);
564 if (size != 32)
565 return returnToLuaWithError(L, "Wrong size of key, got %d bytes, expected 32", (int) size);
567 const char *p_encTxt = luaL_checklstring(L, 2, &size);
569 unsigned char indata[16] = {0x00};
570 unsigned char outdata[16] = {0x00};
571 unsigned char aes_key[16] = {0x00};
572 unsigned char iv[16] = {0x00};
574 // convert key to bytearray and convert input to bytearray
575 for (i = 0; i < 32; i += 2) {
576 sscanf(&p_encTxt[i], "%02x", &tmp);
577 indata[i / 2] = tmp & 0xFF;
578 sscanf(&p_key[i], "%02x", &tmp);
579 aes_key[i / 2] = tmp & 0xFF;
582 mbedtls_aes_context ctx;
583 mbedtls_aes_init(&ctx);
584 mbedtls_aes_setkey_dec(&ctx, aes_key, 128);
585 mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, sizeof(indata), iv, indata, outdata);
586 //Push decrypted array as a string
587 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
588 return 1;// return 1 to signal one return value
590 static int l_aes128decrypt_ecb(lua_State *L) {
591 //Check number of arguments
592 int i;
593 uint32_t tmp;
594 size_t size;
595 const char *p_key = luaL_checklstring(L, 1, &size);
596 if (size != 32)
597 return returnToLuaWithError(L, "Wrong size of key, got %d bytes, expected 32", (int) size);
599 const char *p_encTxt = luaL_checklstring(L, 2, &size);
601 unsigned char indata[16] = {0x00};
602 unsigned char outdata[16] = {0x00};
603 unsigned char aes_key[16] = {0x00};
605 // convert key to bytearray and convert input to bytearray
606 for (i = 0; i < 32; i += 2) {
607 sscanf(&p_encTxt[i], "%02x", &tmp);
608 indata[i / 2] = tmp & 0xFF;
609 sscanf(&p_key[i], "%02x", &tmp);
610 aes_key[i / 2] = tmp & 0xFF;
612 mbedtls_aes_context ctx;
613 mbedtls_aes_init(&ctx);
614 mbedtls_aes_setkey_dec(&ctx, aes_key, 128);
615 mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_DECRYPT, indata, outdata);
617 //Push decrypted array as a string
618 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
619 return 1;// return 1 to signal one return value
622 static int l_aes128encrypt_cbc(lua_State *L) {
623 //Check number of arguments
624 int i;
625 uint32_t tmp;
626 size_t size;
627 const char *p_key = luaL_checklstring(L, 1, &size);
628 if (size != 32)
629 return returnToLuaWithError(L, "Wrong size of key, got %d bytes, expected 32", (int) size);
631 const char *p_txt = luaL_checklstring(L, 2, &size);
633 unsigned char indata[16] = {0x00};
634 unsigned char outdata[16] = {0x00};
635 unsigned char aes_key[16] = {0x00};
636 unsigned char iv[16] = {0x00};
638 for (i = 0; i < 32; i += 2) {
639 sscanf(&p_txt[i], "%02x", &tmp);
640 indata[i / 2] = tmp & 0xFF;
641 sscanf(&p_key[i], "%02x", &tmp);
642 aes_key[i / 2] = tmp & 0xFF;
645 mbedtls_aes_context ctx;
646 mbedtls_aes_init(&ctx);
647 mbedtls_aes_setkey_enc(&ctx, aes_key, 128);
648 mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, sizeof(indata), iv, indata, outdata);
649 //Push encrypted array as a string
650 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
651 return 1;// return 1 to signal one return value
654 static int l_aes128encrypt_ecb(lua_State *L) {
655 //Check number of arguments
656 int i;
657 uint32_t tmp;
658 size_t size;
659 const char *p_key = luaL_checklstring(L, 1, &size);
660 if (size != 32)
661 return returnToLuaWithError(L, "Wrong size of key, got %d bytes, expected 32", (int) size);
663 const char *p_txt = luaL_checklstring(L, 2, &size);
665 unsigned char indata[16] = {0x00};
666 unsigned char outdata[16] = {0x00};
667 unsigned char aes_key[16] = {0x00};
669 for (i = 0; i < 32; i += 2) {
670 sscanf(&p_txt[i], "%02x", &tmp);
671 indata[i / 2] = tmp & 0xFF;
672 sscanf(&p_key[i], "%02x", &tmp);
673 aes_key[i / 2] = tmp & 0xFF;
675 mbedtls_aes_context ctx;
676 mbedtls_aes_init(&ctx);
677 mbedtls_aes_setkey_enc(&ctx, aes_key, 128);
678 mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, indata, outdata);
679 //Push encrypted array as a string
680 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
681 return 1;// return 1 to signal one return value
684 static int l_crc8legic(lua_State *L) {
685 size_t size;
686 const char *p_str = luaL_checklstring(L, 1, &size);
688 uint16_t retval = CRC8Legic((uint8_t *) p_str, size);
689 lua_pushunsigned(L, retval);
690 return 1;
693 static int l_crc16(lua_State *L) {
694 size_t size;
695 const char *p_str = luaL_checklstring(L, 1, &size);
697 uint16_t checksum = Crc16ex(CRC_CCITT, (uint8_t *) p_str, size);
698 lua_pushunsigned(L, checksum);
699 return 1;
702 static int l_crc64(lua_State *L) {
703 size_t size;
704 uint64_t crc = 0;
705 unsigned char outdata[8] = {0x00};
707 const char *p_str = luaL_checklstring(L, 1, &size);
709 crc64((uint8_t *) p_str, size, &crc);
711 outdata[0] = (uint8_t)(crc >> 56) & 0xff;
712 outdata[1] = (uint8_t)(crc >> 48) & 0xff;
713 outdata[2] = (uint8_t)(crc >> 40) & 0xff;
714 outdata[3] = (uint8_t)(crc >> 32) & 0xff;
715 outdata[4] = (uint8_t)(crc >> 24) & 0xff;
716 outdata[5] = (uint8_t)(crc >> 16) & 0xff;
717 outdata[6] = (uint8_t)(crc >> 8) & 0xff;
718 outdata[7] = crc & 0xff;
719 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
720 return 1;
723 // TO BE IMPLEMENTED
724 static int l_crc64_ecma182(lua_State *L) {
725 //size_t size;
726 uint64_t crc = 0;
727 unsigned char outdata[8] = {0x00};
728 //const char *p_str = luaL_checklstring(L, 1, &size);
730 //init
731 //crc64_ecma182(NULL, 0, &crc);
732 crc = 0x338103260CC4;
734 // calc hash
735 //crc64_ecma182((uint8_t*) p_str, size, &crc);
737 outdata[0] = (uint8_t)(crc >> 56) & 0xff;
738 outdata[1] = (uint8_t)(crc >> 48) & 0xff;
739 outdata[2] = (uint8_t)(crc >> 40) & 0xff;
740 outdata[3] = (uint8_t)(crc >> 32) & 0xff;
741 outdata[4] = (uint8_t)(crc >> 24) & 0xff;
742 outdata[5] = (uint8_t)(crc >> 16) & 0xff;
743 outdata[6] = (uint8_t)(crc >> 8) & 0xff;
744 outdata[7] = crc & 0xff;
745 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
746 return 1;
749 static int l_sha1(lua_State *L) {
750 size_t size;
751 const char *p_str = luaL_checklstring(L, 1, &size);
752 unsigned char outdata[20] = {0x00};
753 mbedtls_sha1((uint8_t *) p_str, size, outdata);
754 lua_pushlstring(L, (const char *)&outdata, sizeof(outdata));
755 return 1;
758 static int l_reveng_models(lua_State *L) {
760 // This array needs to be adjusted if RevEng adds more crc-models.
761 #define NMODELS 106
763 int count = 0;
764 uint8_t in_width = luaL_checkunsigned(L, 1);
765 if (in_width > 89)
766 return returnToLuaWithError(L, "Width cannot exceed 89, got %d", in_width);
768 uint8_t width[NMODELS];
769 memset(width, 0, sizeof(width));
770 char *models[NMODELS];
772 width[0] = in_width;
774 if (!GetModels(models, &count, width))
775 return returnToLuaWithError(L, "didn't find any models");
777 lua_newtable(L);
778 for (int i = 0; i < count; i++) {
779 lua_pushstring(L, (const char *)models[i]);
780 lua_rawseti(L, -2, i + 1);
781 free(models[i]);
783 return 1;
786 //Called with 4 parameters.
787 // inModel ,string containing the crc model name: 'CRC-8'
788 // inHexStr ,string containing the hex representation of the data that will be used for CRC calculations.
789 // reverse ,int 0/1 (bool) if 1, calculate the reverse CRC
790 // endian ,char, 'B','b','L','l','t','r' describing if Big-Endian or Little-Endian should be used in different combinations.
792 // outputs: string with hex representation of the CRC result
793 static int l_reveng_runmodel(lua_State *L) {
794 //-c || -v
795 //inModel = valid model name string - CRC-8
796 //inHexStr = input hex string to calculate crc on
797 //reverse = reverse calc option if true
798 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
799 // l = little endian input and output, L = little endian output only, t = left justified}
800 //result = calculated crc hex string
801 char result[50];
802 memset(result, 0x00, sizeof(result));
804 const char *inModel = luaL_checkstring(L, 1);
805 const char *inHexStr = luaL_checkstring(L, 2);
806 bool reverse = lua_toboolean(L, 3);
807 const char endian = luaL_checkstring(L, 4)[0];
809 int ans = RunModel((char *)inModel, (char *)inHexStr, reverse, endian, result);
810 if (!ans)
811 return returnToLuaWithError(L, "Reveng failed");
813 lua_pushstring(L, result);
814 return 1;
817 static int l_hardnested(lua_State *L) {
819 bool haveTarget = true;
820 size_t size;
821 uint32_t tmp;
822 const char *p_blockno = luaL_checklstring(L, 1, &size);
823 if (size != 2)
824 return returnToLuaWithError(L, "Wrong size of blockNo, got %d bytes, expected 2", (int) size);
826 const char *p_keytype = luaL_checklstring(L, 2, &size);
827 if (size != 1)
828 return returnToLuaWithError(L, "Wrong size of keyType, got %d bytes, expected 1", (int) size);
830 const char *p_key = luaL_checklstring(L, 3, &size);
831 if (size != 12)
832 return returnToLuaWithError(L, "Wrong size of key, got %d bytes, expected 12", (int) size);
834 const char *p_trg_blockno = luaL_checklstring(L, 4, &size);
835 if (size != 2)
836 return returnToLuaWithError(L, "Wrong size of trgBlockNo, got %d bytes, expected 2", (int) size);
838 const char *p_trg_keytype = luaL_checklstring(L, 5, &size);
839 if (size != 1)
840 return returnToLuaWithError(L, "Wrong size of trgKeyType, got %d bytes, expected 1", (int) size);
842 const char *p_trgkey = luaL_checklstring(L, 6, &size);
843 if (size != 12)
844 haveTarget = false;
846 const char *p_nonce_file_read = luaL_checklstring(L, 7, &size);
847 if (size != 1)
848 return returnToLuaWithError(L, "Wrong size of nonce_file_read, got %d bytes, expected 1", (int) size);
850 const char *p_nonce_file_write = luaL_checklstring(L, 8, &size);
851 if (size != 1)
852 return returnToLuaWithError(L, "Wrong size of nonce_file_write, got %d bytes, expected 1", (int) size);
854 const char *p_slow = luaL_checklstring(L, 9, &size);
855 if (size != 1)
856 return returnToLuaWithError(L, "Wrong size of slow, got %d bytes, expected 1", (int) size);
858 const char *p_tests = luaL_checklstring(L, 10, &size);
859 if (size != 1)
860 return returnToLuaWithError(L, "Wrong size of tests, got %d bytes, expected 1", (int) size);
862 char filename[FILE_PATH_SIZE] = "nonces.bin";
863 const char *p_filename = luaL_checklstring(L, 11, &size);
864 if (size != 0)
865 memcpy(filename, p_filename, FILE_PATH_SIZE - 1);
867 uint32_t blockNo = 0, keyType = 0;
868 uint32_t trgBlockNo = 0, trgKeyType = 0;
869 uint32_t slow = 0, tests = 0;
870 uint32_t nonce_file_read = 0, nonce_file_write = 0;
871 sscanf(p_blockno, "%02x", &blockNo);
872 sscanf(p_keytype, "%x", &keyType);
873 sscanf(p_trg_blockno, "%02x", &trgBlockNo);
874 sscanf(p_trg_keytype, "%x", &trgKeyType);
875 sscanf(p_nonce_file_read, "%x", &nonce_file_read);
876 sscanf(p_nonce_file_write, "%x", &nonce_file_write);
878 sscanf(p_slow, "%x", &slow);
879 sscanf(p_tests, "%x", &tests);
881 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
882 uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};
883 for (int i = 0; i < 12; i += 2) {
884 sscanf(&p_key[i], "%02x", &tmp);
885 key[i / 2] = tmp & 0xFF;
886 if (haveTarget) {
887 sscanf(&p_trgkey[i], "%02x", &tmp);
888 trgkey[i / 2] = tmp & 0xFF;
892 uint64_t foundkey = 0;
893 int retval = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, haveTarget ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey, filename);
894 DropField();
896 //Push the key onto the stack
897 uint8_t dest_key[6];
898 num_to_bytes(foundkey, sizeof(dest_key), dest_key);
900 //Push the retval on the stack
901 lua_pushinteger(L, retval);
902 lua_pushlstring(L, (const char *) dest_key, sizeof(dest_key));
903 return 2; //Two return values
907 * @brief l_validate_prng is a function to test is a nonce is using the weak PRNG
908 * detection = 1 == weak, 0 == hard , -1 = failed
909 * @param L
910 * @return
912 static int l_detect_prng(lua_State *L) {
913 int res = detect_classic_prng();
914 lua_pushinteger(L, res);
915 return 1;
918 * @brief l_keygen_algoD is a function to calculate pwd/pack using UID, by algo D
919 * @param L
920 * @return
922 static int l_keygen_algoD(lua_State *L) {
923 //Check number of arguments
924 int n = lua_gettop(L);
925 if (n != 1) {
926 return returnToLuaWithError(L, "Only UID");
929 size_t size;
930 uint32_t tmp;
931 const char *p_uid = luaL_checklstring(L, 1, &size);
932 if (size != 14)
933 return returnToLuaWithError(L, "Wrong size of UID, got %d bytes, expected 14", (int) size);
935 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};
937 for (int i = 0; i < 14; i += 2) {
938 sscanf(&p_uid[i], "%02x", &tmp);
939 uid[i / 2] = tmp & 0xFF;
942 uint32_t pwd = ul_ev1_pwdgenD(uid);
943 uint16_t pack = ul_ev1_packgenD(uid);
945 lua_pushunsigned(L, pwd);
946 lua_pushunsigned(L, pack);
947 return 2;
951 Read T55Xx block.
952 param1 uint8_t block
953 param2 bool page1
954 param3 bool override
955 param4 uint32_t password
957 static int l_T55xx_readblock(lua_State *L) {
959 //Check number of arguments
960 int n = lua_gettop(L);
961 if (n != 4)
962 return returnToLuaWithError(L, "Wrong number of arguments, got %d bytes, expected 4", n);
964 uint32_t block, usepage1, override, password = 0;
965 bool usepwd;
966 size_t size;
968 const char *p_blockno = luaL_checklstring(L, 1, &size);
969 if (size < 1 || size > 2)
970 return returnToLuaWithError(L, "Wrong size of blockNo, got %d, expected 1 or 2", (int) size);
972 sscanf(p_blockno, "%x", &block);
974 const char *p_usepage1 = luaL_checklstring(L, 2, &size);
975 if (size != 1)
976 return returnToLuaWithError(L, "Wrong size of usePage1, got %d, expected 1", (int) size);
978 sscanf(p_usepage1, "%x", &usepage1);
980 const char *p_override = luaL_checklstring(L, 3, &size);
981 if (size != 1)
982 return returnToLuaWithError(L, "Wrong size of override, got %d, expected 1", (int) size);
984 sscanf(p_override, "%x", &override);
986 const char *p_pwd = luaL_checklstring(L, 4, &size);
987 if (size == 0) {
988 usepwd = false;
989 } else {
991 if (size != 8)
992 return returnToLuaWithError(L, "Wrong size of pwd, got %d , expected 8", (int) size);
994 sscanf(p_pwd, "%08x", &password);
995 usepwd = true;
998 //Password mode
999 if (usepwd) {
1000 // try reading the config block and verify that PWD bit is set before doing this!
1001 if (!override) {
1003 if (!AcquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0, 0)) {
1004 return returnToLuaWithError(L, "Failed to read config block");
1007 if (!t55xxTryDetectModulation(0, true)) { // Default to prev. behaviour (default dl mode and print config)
1008 PrintAndLogEx(NORMAL, "Safety Check: Could not detect if PWD bit is set in config block. Exits.");
1009 return 0;
1010 } else {
1011 PrintAndLogEx(NORMAL, "Safety Check: PWD bit is NOT set in config block. Reading without password...");
1012 usepwd = false;
1013 usepage1 = false;
1015 } else {
1016 PrintAndLogEx(NORMAL, "Safety Check Overridden - proceeding despite risk");
1020 if (!AcquireData(usepage1, block, usepwd, password, 0)) {
1021 return returnToLuaWithError(L, "Failed to acquire data from card");
1024 if (!DecodeT55xxBlock()) {
1025 return returnToLuaWithError(L, "Failed to decode signal");
1028 uint32_t blockData = 0;
1029 if (GetT55xxBlockData(&blockData) == false) {
1030 return returnToLuaWithError(L, "Failed to get actual data");
1033 lua_pushunsigned(L, blockData);
1034 return 1;
1037 // arg 1 = pwd
1038 // arg 2 = use GB
1039 static int l_T55xx_detect(lua_State *L) {
1040 bool useGB = false, usepwd = false, isok;
1041 uint32_t gb, password = 0;
1042 size_t size;
1044 //Check number of arguments
1045 int n = lua_gettop(L);
1047 switch (n) {
1048 case 2: {
1049 const char *p_gb = luaL_checklstring(L, 2, &size);
1050 if (size != 1)
1051 return returnToLuaWithError(L, "Wrong size of useGB, got %d , expected 1", (int) size);
1053 sscanf(p_gb, "%u", &gb);
1054 useGB = (gb) ? true : false;
1055 PrintAndLogEx(INFO, "p_gb size %zu | %c", size, useGB ? 'Y' : 'N');
1057 case 1: {
1058 const char *p_pwd = luaL_checklstring(L, 1, &size);
1059 if (size == 0) {
1060 usepwd = false;
1061 } else {
1063 if (size != 8)
1064 return returnToLuaWithError(L, "Wrong size of pwd, got %d , expected 8", (int) size);
1066 sscanf(p_pwd, "%08x", &password);
1067 usepwd = true;
1069 break;
1071 default :
1072 break;
1075 if (!useGB) {
1077 isok = AcquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password, 0);
1078 if (isok == false) {
1079 return returnToLuaWithError(L, "Failed to acquire LF signal data");
1083 isok = t55xxTryDetectModulation(0, true); // Default to prev. behaviour (default dl mode and print config)
1084 if (isok == false) {
1085 return returnToLuaWithError(L, "Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");
1088 lua_pushinteger(L, isok);
1089 lua_pushstring(L, "Success");
1090 return 2;
1093 // 4305
1094 static int l_em4x05_read(lua_State *L) {
1096 bool use_pwd = false;
1097 uint32_t addr, password = 0;
1099 //Check number of arguments
1100 //int n = lua_gettop(L);
1102 // get addr
1103 size_t size = 0;
1104 const char *p_addr = luaL_checklstring(L, 1, &size);
1105 sscanf(p_addr, "%u", &addr);
1107 // get password
1108 const char *p_pwd = luaL_checkstring(L, 2);
1109 if (p_pwd == NULL || strlen(p_pwd) == 0) {
1110 use_pwd = false;
1111 } else {
1112 if (strlen(p_pwd) != 8)
1113 return returnToLuaWithError(L, "Wrong size of password, got %zu , expected 8", strlen(p_pwd));
1115 sscanf(p_pwd, "%08x", &password);
1116 use_pwd = true;
1119 PrintAndLogEx(DEBUG, "Addr %u", addr);
1120 if (use_pwd)
1121 PrintAndLogEx(DEBUG, " Pwd %08X", password);
1123 uint32_t word = 0;
1124 int res = em4x05_read_word_ext(addr, password, use_pwd, &word);
1125 if (res != PM3_SUCCESS) {
1126 return returnToLuaWithError(L, "Failed to read EM4x05 data");
1129 lua_pushinteger(L, word);
1130 return 1;
1133 // 4350
1134 static int l_em4x50_read(lua_State *L) {
1136 // get addr
1137 size_t size = 0;
1138 const char *p_addr = luaL_checklstring(L, 1, &size);
1139 uint32_t addr = 0;
1140 sscanf(p_addr, "%u", &addr);
1142 if (addr > 31)
1143 return returnToLuaWithError(L, "Address out-of-range (0..31) got %u", addr);
1145 // setting up structures
1146 em4x50_data_t etd;
1147 memset(&etd, 0x00, sizeof(em4x50_data_t));
1148 etd.addr_given = true;
1149 etd.addresses = addr & 0xFF;
1151 // get password
1152 const char *p_pwd = luaL_checkstring(L, 2);
1153 if (p_pwd == NULL || strlen(p_pwd) == 0) {
1154 etd.pwd_given = false;
1155 } else {
1156 if (strlen(p_pwd) != 8)
1157 return returnToLuaWithError(L, "Wrong size of password, got %zu , expected 8", strlen(p_pwd));
1159 uint32_t pwd = 0;
1160 sscanf(p_pwd, "%08x", &pwd);
1162 PrintAndLogEx(DEBUG, " Pwd %08X", pwd);
1164 etd.password1 = pwd;
1165 etd.pwd_given = true;
1168 PrintAndLogEx(DEBUG, "Addr %u", etd.addresses & 0xFF);
1169 if (etd.pwd_given)
1170 PrintAndLogEx(DEBUG, " Pwd %08x", etd.password1);
1172 em4x50_word_t words[EM4X50_NO_WORDS];
1174 int res = em4x50_read(&etd, words);
1175 if (res != PM3_SUCCESS) {
1176 return returnToLuaWithError(L, "Failed to read EM4x50 data");
1179 uint32_t word = (
1180 words[etd.addresses & 0xFF].byte[0] << 24 |
1181 words[etd.addresses & 0xFF].byte[1] << 16 |
1182 words[etd.addresses & 0xFF].byte[2] << 8 |
1183 words[etd.addresses & 0xFF].byte[3]
1185 lua_pushinteger(L, word);
1187 return 1;
1191 static int l_ndefparse(lua_State *L) {
1193 size_t size;
1195 //Check number of arguments
1196 int n = lua_gettop(L);
1197 if (n != 3) {
1198 return returnToLuaWithError(L, "You need to supply three parameters");
1201 size_t datalen = luaL_checknumber(L, 1);
1202 bool verbose = luaL_checknumber(L, 2);
1204 uint8_t *data = calloc(datalen, sizeof(uint8_t));
1205 if (data == 0) {
1206 return returnToLuaWithError(L, "Allocating memory failed");
1209 // data
1210 const char *p_data = luaL_checklstring(L, 3, &size);
1211 if (size) {
1212 if (size > (datalen << 1))
1213 size = (datalen << 1);
1215 uint32_t tmp;
1216 for (int i = 0; i < size; i += 2) {
1217 sscanf(&p_data[i], "%02x", &tmp);
1218 data[i >> 1] = tmp & 0xFF;
1222 int res = NDEFDecodeAndPrint(data, datalen, verbose);
1223 lua_pushinteger(L, res);
1224 return 1;
1227 static int l_remark(lua_State *L) {
1228 //Check number of arguments
1229 int n = lua_gettop(L);
1230 if (n != 1) {
1231 return returnToLuaWithError(L, "Only one string allowed");
1234 size_t size;
1235 const char *s = luaL_checklstring(L, 1, &size);
1236 int res = CmdRem(s);
1237 lua_pushinteger(L, res);
1238 return 1;
1241 // 1. filename
1242 // 2. extension
1243 // output: full search path to file
1244 static int l_searchfile(lua_State *L) {
1245 //Check number of arguments
1246 int n = lua_gettop(L);
1247 if (n != 2) {
1248 return returnToLuaWithError(L, "Only filename and extension");
1251 size_t size;
1252 // data
1253 const char *filename = luaL_checklstring(L, 1, &size);
1254 if (size == 0)
1255 return returnToLuaWithError(L, "Must specify filename");
1257 const char *suffix = luaL_checklstring(L, 2, &size);
1258 char *path;
1259 int res = searchFile(&path, "", filename, suffix, false);
1260 if (res != PM3_SUCCESS) {
1261 return returnToLuaWithError(L, "Failed to find file");
1264 lua_pushstring(L, path);
1265 free(path);
1266 return 1;
1269 static int l_ud(lua_State *L) {
1270 const char *ud = get_my_user_directory();
1271 lua_pushstring(L, ud);
1272 return 1;
1274 static int l_ewd(lua_State *L) {
1275 const char *ewd = get_my_executable_directory();
1276 lua_pushstring(L, ewd);
1277 return 1;
1279 static int l_cwd(lua_State *L) {
1281 uint16_t path_len = FILENAME_MAX; // should be a good starting point
1282 char *cwd = (char *)calloc(path_len, sizeof(uint8_t));
1283 if (cwd == NULL) {
1284 return returnToLuaWithError(L, "Failed to allocate memory");
1287 while (GetCurrentDir(cwd, path_len) == NULL) {
1288 if (errno == ERANGE) { // Need bigger buffer
1289 path_len += 10; // if buffer was too small add 10 characters and try again
1290 cwd = realloc(cwd, path_len);
1291 if (cwd == NULL) {
1292 free(cwd);
1293 return returnToLuaWithError(L, "Failed to allocate memory");
1295 } else {
1296 free(cwd);
1297 return returnToLuaWithError(L, "Failed to get current working directory");
1300 lua_pushstring(L, cwd);
1301 free(cwd);
1302 return 1;
1305 // ref: https://github.com/RfidResearchGroup/proxmark3/issues/891
1306 // redirect LUA's print to Proxmark3 PrintAndLogEx
1307 static int l_printandlogex(lua_State *L) {
1308 int n = lua_gettop(L);
1309 for (int i = 1; i <= n; i++) {
1310 if (lua_isstring(L, i)) {
1311 PrintAndLogEx(NORMAL, "%s\t" NOLF, lua_tostring(L, i));
1314 PrintAndLogEx(NORMAL, "");
1315 return 0;
1319 * @brief Sets the lua path to include "./lualibs/?.lua", in order for a script to be
1320 * able to do "require('foobar')" if foobar.lua is within lualibs folder.
1321 * Taken from http://stackoverflow.com/questions/4125971/setting-the-global-lua-path-variable-from-c-c
1322 * @param L
1323 * @param path
1324 * @return
1326 static int setLuaPath(lua_State *L, const char *path) {
1327 lua_getglobal(L, "package");
1328 lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1)
1329 const char *cur_path = lua_tostring(L, -1); // grab path string from top of stack
1330 int requiredLength = strlen(cur_path) + strlen(path) + 10; //A few bytes too many, whatever we can afford it
1331 char *buf = calloc(requiredLength, sizeof(char));
1332 snprintf(buf, requiredLength, "%s;%s", cur_path, path);
1333 lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5
1334 lua_pushstring(L, buf); // push the new one
1335 lua_setfield(L, -2, "path"); // set the field "path" in table at -2 with value at top of stack
1336 lua_pop(L, 1); // get rid of package table from top of stack
1337 free(buf);
1338 return 0; // all done!
1341 int set_pm3_libraries(lua_State *L) {
1342 static const luaL_Reg libs[] = {
1343 {"SendCommandOLD", l_SendCommandOLD},
1344 {"SendCommandMIX", l_SendCommandMIX},
1345 {"SendCommandNG", l_SendCommandNG},
1346 {"GetFromBigBuf", l_GetFromBigBuf},
1347 {"GetFromFlashMem", l_GetFromFlashMem},
1348 {"GetFromFlashMemSpiffs", l_GetFromFlashMemSpiffs},
1349 {"WaitForResponseTimeout", l_WaitForResponseTimeout},
1350 {"mfDarkside", l_mfDarkside},
1351 {"foobar", l_foobar},
1352 {"kbd_enter_pressed", l_kbd_enter_pressed},
1353 {"clearCommandBuffer", l_clearCommandBuffer},
1354 {"console", l_CmdConsole},
1355 {"iso15693_crc", l_iso15693_crc},
1356 {"iso14443b_crc", l_iso14443b_crc},
1357 {"aes128_decrypt", l_aes128decrypt_cbc},
1358 {"aes128_decrypt_ecb", l_aes128decrypt_ecb},
1359 {"aes128_encrypt", l_aes128encrypt_cbc},
1360 {"aes128_encrypt_ecb", l_aes128encrypt_ecb},
1361 {"crc8legic", l_crc8legic},
1362 {"crc16", l_crc16},
1363 {"crc64", l_crc64},
1364 {"crc64_ecma182", l_crc64_ecma182},
1365 {"sha1", l_sha1},
1366 {"reveng_models", l_reveng_models},
1367 {"reveng_runmodel", l_reveng_runmodel},
1368 {"hardnested", l_hardnested},
1369 {"detect_prng", l_detect_prng},
1370 // {"keygen.algoA", l_keygen_algoA},
1371 // {"keygen.algoB", l_keygen_algoB},
1372 // {"keygen.algoC", l_keygen_algoC},
1373 {"keygen_algo_d", l_keygen_algoD},
1374 {"t55xx_readblock", l_T55xx_readblock},
1375 {"t55xx_detect", l_T55xx_detect},
1376 {"ndefparse", l_ndefparse},
1377 {"fast_push_mode", l_fast_push_mode},
1378 {"search_file", l_searchfile},
1379 {"cwd", l_cwd},
1380 {"ewd", l_ewd},
1381 {"ud", l_ud},
1382 {"rem", l_remark},
1383 {"em4x05_read", l_em4x05_read},
1384 {"em4x50_read", l_em4x50_read},
1385 {NULL, NULL}
1388 lua_pushglobaltable(L);
1389 // Core library is in this table. Contains '
1390 // this is 'pm3' table
1391 lua_newtable(L);
1393 // put the function into the hash table.
1394 for (int i = 0; libs[i].name; i++) {
1395 lua_pushcfunction(L, libs[i].func);
1396 lua_setfield(L, -2, libs[i].name);//set the name, pop stack
1398 // Name of 'core'
1399 lua_setfield(L, -2, "core");
1401 // remove the global environment table from the stack
1402 lua_pop(L, 1);
1404 // print redirect here
1405 lua_register(L, "print", l_printandlogex);
1407 // add to the LUA_PATH (package.path in lua)
1408 // so we can load scripts from various places:
1409 const char *exec_path = get_my_executable_directory();
1410 if (exec_path != NULL) {
1411 // from the ./luascripts/ directory
1412 char scripts_path[strlen(exec_path) + strlen(LUA_SCRIPTS_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1413 strcpy(scripts_path, exec_path);
1414 strcat(scripts_path, LUA_SCRIPTS_SUBDIR);
1415 strcat(scripts_path, LUA_LIBRARIES_WILDCARD);
1416 setLuaPath(L, scripts_path);
1417 // from the ./lualib/ directory
1418 char libraries_path[strlen(exec_path) + strlen(LUA_LIBRARIES_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1419 strcpy(libraries_path, exec_path);
1420 strcat(libraries_path, LUA_LIBRARIES_SUBDIR);
1421 strcat(libraries_path, LUA_LIBRARIES_WILDCARD);
1422 setLuaPath(L, libraries_path);
1424 const char *user_path = get_my_user_directory();
1425 if (user_path != NULL) {
1426 // from the $HOME/.proxmark3/luascripts/ directory
1427 char scripts_path[strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(LUA_SCRIPTS_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1428 strcpy(scripts_path, user_path);
1429 strcat(scripts_path, PM3_USER_DIRECTORY);
1430 strcat(scripts_path, LUA_SCRIPTS_SUBDIR);
1431 strcat(scripts_path, LUA_LIBRARIES_WILDCARD);
1432 setLuaPath(L, scripts_path);
1434 // from the $HOME/.proxmark3/lualib/ directory
1435 char libraries_path[strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(LUA_LIBRARIES_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1436 strcpy(libraries_path, user_path);
1437 strcat(libraries_path, PM3_USER_DIRECTORY);
1438 strcat(libraries_path, LUA_LIBRARIES_SUBDIR);
1439 strcat(libraries_path, LUA_LIBRARIES_WILDCARD);
1440 setLuaPath(L, libraries_path);
1443 if (exec_path != NULL) {
1444 // from the $PREFIX/share/proxmark3/luascripts/ directory
1445 char scripts_path[strlen(exec_path) + strlen(PM3_SHARE_RELPATH) + strlen(LUA_SCRIPTS_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1446 strcpy(scripts_path, exec_path);
1447 strcat(scripts_path, PM3_SHARE_RELPATH);
1448 strcat(scripts_path, LUA_SCRIPTS_SUBDIR);
1449 strcat(scripts_path, LUA_LIBRARIES_WILDCARD);
1450 setLuaPath(L, scripts_path);
1451 // from the $PREFIX/share/proxmark3/lualib/ directory
1452 char libraries_path[strlen(exec_path) + strlen(PM3_SHARE_RELPATH) + strlen(LUA_LIBRARIES_SUBDIR) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
1453 strcpy(libraries_path, exec_path);
1454 strcat(libraries_path, PM3_SHARE_RELPATH);
1455 strcat(libraries_path, LUA_LIBRARIES_SUBDIR);
1456 strcat(libraries_path, LUA_LIBRARIES_WILDCARD);
1457 setLuaPath(L, libraries_path);
1459 return 1;