日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lua 代码加密方案

發布時間:2025/7/14 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lua 代码加密方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

require 實現

  • require函數在實現上是依次調用package.searchers(lua51中是package.loaders)中的載入函數,成功后返回。在loadlib.c文件里有四個載入函數的實現,分別為searcher_preload, searcher_Lua, searcher_C, searcher_Croot。
  • searcher_preload是從讀取LUA_REGISTRYINDEX的_PRELOAD字段。已經require過的文件會寫入到該表中
  • searcher_Lua是依據文件名稱查找package.path中的全部路徑的lua文件。存在文件則返回
  • searcher_C是搜索package.cpath中的全部路徑下的庫文件
  • searcher_Croot是對require(“a.b.c”)的情況,讀取a庫。然后查找函數名為lua_a_b_c的lua_CFunction函數
static void findloader (lua_State *L, const char *name) {int i;luaL_Buffer msg; /* to build error message */luaL_buffinit(L, &msg);lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */if (!lua_istable(L, 3))luaL_error(L, LUA_QL("package.searchers") " must be a table");/* iterate over available searchers to find a loader */for (i = 1; ; i++) {lua_rawgeti(L, 3, i); /* get a searcher */if (lua_isnil(L, -1)) { /* no more searchers? */lua_pop(L, 1); /* remove nil */luaL_pushresult(&msg); /* create error message */luaL_error(L, "module " LUA_QS " not found:%s",name, lua_tostring(L, -1));}lua_pushstring(L, name);lua_call(L, 1, 2); /* call it */if (lua_isfunction(L, -2)) /* did it find a loader? */return; /* module loader found */else if (lua_isstring(L, -2)) { /* searcher returned error message? */lua_pop(L, 1); /* remove extra return */luaL_addvalue(&msg); /* concatenate error message */}elselua_pop(L, 2); /* remove both returns */} }
  • 如今要實如今require時能讀取加密文件,有兩種辦法,一種是直接改動源碼。即改動第二個載入函數,又一次實現當中讀取文件內容的函數,另外一種辦法是在lua中改動package.searchers表。在載入器的第一和另外一種之間加入一個載入器函數,該載入器模擬searcher_Lua函數。搜索path路徑,然后逐個匹配文件。然后讀取文件內容,解密。然后調用load載入并返回(c中為luaL_loadbufferx),這里在載入時最好傳入文件名稱作為來源參數。方便在調試信息中定位.
  • 加密方案可使用相似xxtea輕量級的加密算法
  • 在對lua文件進行加密打包時。能夠在文件頭寫入指定的簽名內容。以方便在解密前預先推斷是否為有效的加密文件

改動lua源碼方案

  • 在searcher_Lua中終于是調用lua_load(L, getF, &lf, lua_tostring(L, -1), mode)載入源文件,該函數的第二個參數getF是一個lua_Reader函數,所以這里能夠重寫該函數以實現解密,也能夠向外部暴露一個接口用來將自己定義的文件讀取函數作為參數傳給lua_load。以下是原版的getF實現
static const char *getF (lua_State *L, void *ud, size_t *size) {LoadF *lf = (LoadF *)ud;(void)L; /* not used */if (lf->n > 0) { /* are there pre-read characters to be read? */*size = lf->n; /* return them (chars already in buffer) */lf->n = 0; /* no more pre-read characters */}else { /* read a block from file *//* 'fread' can return > 0 *and* set the EOF flag. If next call to'getF' called 'fread', it might still wait for user input.The next check avoids this problem. */if (feof(lf->f)) return NULL;*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */}return lf->buff; }

外部改動載入器方案

  • 直接改動package.searchers表,向當中加入載入器,c版實現例如以下
void addLuaSearcher(lua_CFunction func) {if (!func) return;// stack content after the invoking of the function// get loader tablelua_getglobal(m_state, "package"); /* L: package */lua_getfield(m_state, -1, "loaders"); /* L: package, loaders */// insert loader into index 2lua_pushcfunction(m_state, func); /* L: package, loaders, func */for (int i = lua_objlen(m_state, -2) + 1; i > 2; --i){lua_rawgeti(m_state, -2, i - 1); /* L: package, loaders, func, function */// we call lua_rawgeti, so the loader table now is at -3lua_rawseti(m_state, -3, i); /* L: package, loaders, func */}lua_rawseti(m_state, -2, 2); /* L: package, loaders */// set loaders into packagelua_setfield(m_state, -2, "loaders"); /* L: package */lua_pop(m_state, 1); }
  • 載入器函數實現依據傳入的文件名稱。逐個匹配的package.path中的內容,存在文件后,然后讀取文件內容,解密,最后再將解出的內容調用load載入并返回(c中為luaL_loadbufferx),實現能夠參照lua源碼中的searcher_Lua實現

總結

以上是生活随笔為你收集整理的lua 代码加密方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。