LUA Code Encryption Scheme

Source: Internet
Author: User

Require implementation
    • The Require function is implemented by calling the load function in Package.searchers (package.loaders in Lua51) in turn, and returns after success. In the LOADLIB.C file, there are four load function implementations, respectively, Searcher_preload, Searcher_lua, Searcher_c, Searcher_croot.
    • Searcher_preload is the _preload field from which the Lua_registryindex is read, and the files that have been require are written to the table
    • Searcher_lua is a LUA file that finds all paths in the Package.path based on the file name, and the files that exist are returned
    • Searcher_c is the library file under all paths in the search Package.cpath
    • Searcher_croot is the case of require ("A.B.C"), read the C library, and then look for the Lua_cfunction function named Lua_a_b_c.
Static voidFindloader (Lua_state *l,Const Char*name) {intI Lual_buffer msg;/ * To build error message * /Lual_buffinit (L, &msg); Lua_getfield (L, Lua_upvalueindex (1),"Searchers");/* 'll is 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 * /}}
    • Now to implement the require can read encrypted files, there are two ways, one is to directly modify the source code, that is, modify the second load function, re-implementation of the contents of the file read the function, The second approach is to modify the Package.searchers table in Lua, adding a loader function between the first and second of the loader, which simulates the Searcher_lua function, searches the path, and then matches the file one by one, then reads the file contents, decrypts, Then call load to load and return (Lual_loadbufferx in C), where it is best to pass in the file name as the source parameter when loading, so that it is easy to locate in debug information.
    • Encryption schemes can use a xxtea-like, lightweight encryption algorithm
    • When you encrypt a LUA file, you can write the specified signature to the file header to make it easier to pre-determine if a valid encrypted file is available before decrypting
Modifying LUA source code scenarios
    • In Searcher_lua the final call Lua_load (L, GETF, &lf, Lua_tostring (L,-1), mode) loads the source file, the second parameter of the function getf is a lua_reader function, So you can override this function for decryption, or externally expose an interface to pass a custom file read function as a parameter to Lua_load. The following is the original GETF implementation
Static Const Char*GETF (Lua_state *l,void*ud, size_t *size) {LOADF *LF = (LOADF *) UD; (voidL/ * Not used * /  if(Lf->n >0) {/ * Is 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))returnNULL; *size = Fread (Lf->buff,1,sizeof(Lf->buff), lf->f);/ * Read Block * /}returnLf->buff;}
External modification Loader Scenarios
    • Directly modify the Package.searchers table, add the loader to it, the C version of the implementation of the following
voidAddluasearcher (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(inti = Lua_objlen (M_state,-2) +1; i >2; I.) {Lua_rawgeti (m_state,-2I1);/ * l:package, loaders, func, function * /        //We call Lua_rawgeti, so the loader table are now 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);}
    • The loader function implements the contents of the Package.path according to the incoming file name, one by one, after the file is present, then reads the contents of the file, decrypts, and finally the solution is called load loaded and returned (c is Lual_loadbufferx), Implementations can be implemented with reference to the Searcher_lua in the LUA source code

LUA Code Encryption Scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.