lua 代碼加密方案

來源:互聯網
上載者:User

標籤: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”)的情況,讀取c庫,然後尋找函數名為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 */    }    else      lua_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 table    lua_getglobal(m_state, "package");          /* L: package */    lua_getfield(m_state, -1, "loaders");       /* L: package, loaders */    // insert loader into index 2    lua_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 -3        lua_rawseti(m_state, -3, i);            /* L: package, loaders, func */    }    lua_rawseti(m_state, -2, 2);                /* L: package, loaders */    // set loaders into package    lua_setfield(m_state, -2, "loaders");       /* L: package */    lua_pop(m_state, 1);}
  • 載入器函數實現根據傳入的檔案名稱,逐個匹配的package.path中的內容,存在檔案後,然後讀取檔案內容,解密,最後再將解出的內容調用load載入並返回(c中為luaL_loadbufferx),實現可以參照lua源碼中的searcher_Lua實現

lua 代碼加密方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.