为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

Lua代码加密探索

2023-06-22 5页 doc 117KB 4阅读

用户头像 个人认证

is_856463

暂无简介

举报
Lua代码加密探索     Lua代码加密探索          在skynet中嵌入加密算法https://github.com/luke-park/SecureCompatibleEncryptionExamples在skynet中添加代码:[root@localhostwtserver]#tree3rd3rdlua-cryption/├──cryptionlib.c├──SCEE.c└──SCEE.hlua-filesystem/├──lfs.c├──lfs.def└──lfs.h修改skynet的makefileCSERVICE=snl...
Lua代码加密探索
     Lua代码加密探索          在skynet中嵌入加密算法https://github.com/luke-park/SecureCompatibleEncryptionExamples在skynet中添加代码:[root@localhostwtserver]#tree3rd3rdlua-cryption/├──cryptionlib.c├──SCEE.c└──SCEE.hlua-filesystem/├──lfs.c├──lfs.def└──lfs.h修改skynet的makefileCSERVICE=snlualoggergateharborLUA_CLIB=skynet\client\bsonmd5sprotolpegcjsoncryptionlfs$(TLS_MODULE)$(LUA_CLIB_PATH)/cryption.so:3rd/lua-cryption/cryptionlib.c3rd/lua-cryption/SCEE.c|$(LUA_CLIB_PATH)$(CC)$(CFLAGS)$(SHARED)-I3rd/lua-cryption$^-o$@-lcrypto-lssl-ldl$(LUA_CLIB_PATH)/lfs.so:3rd/lua-filesystem/lfs.c|$(LUA_CLIB_PATH)$(CC)$(CFLAGS)$(SHARED)-I3rd/lua-filesystem$^-o$@在skynet中使用案例:在skynet中嵌入文件系统https://github.com/keplerproject/luafilesystem参考在项目中嵌入加密算法,将文件系统添加至skynet中。文件系统可以参考库的说明文档。lua源码修改一:参考博客:http://just4coding.com/2016/11/15/luajit-encrypt/思路:检查文件是否为加密文件,将加密文件读取出后解密,并存入到临时文件中。将临时文件的句柄替换lf.f的句柄方案已废弃:临时文件的创建会导致代码泄露。luaL_loadfilex函数中添加如下代码://lua代码lua_pushfstring(L,"@%s",filename);lf.f=fopen(filename,"r");if(lf.f==NULL)returnerrfile(L,"open",fnameindex);//新增加代码//检查文件是否为加密文件,加密文件需要嗲用解密接口//charfile_header[FILE_HEADER_LEN];char*file_header=malloc(FILE_HEADER_LEN);size_tsz=fread(file_header,sizeof(char),FILE_HEADER_LEN,lf.f);if(sz==FILE_HEADER_LEN){if(memcmp(file_header,"wt_cipher_lua_file\n",FILE_HEADER_LEN-1)==0){//lf.f=lf.flf.f=decrypt_file(lf.f);}}free(file_header);fseek(lf.f,0L,SEEK_SET);decrypt_file的实现:staticFILE*decrypt_file(FILE*ofp){intfd,len;size_tsz;FILE*fp;unsignedchar*buf,*obuf;charfile_temp[]="/tmp/luajit-XXXXXX";fp=NULL;buf=NULL;obuf=NULL;fd=-1;fseek(ofp,0L,SEEK_END);sz=ftell(ofp);obuf=malloc(sz);if(obuf==NULL){gotofailed;}fseek(ofp,0L,SEEK_SET);if(fread(obuf,1,sz,ofp)流程
复杂!代码修改过多,担心修改后影响后序流程。(能力不够)typedefstructLoadF{intn;/*numberofpre-readcharacters*/FILE*f;/*filebeingread*/charbuff[BUFSIZ];/*areaforreadingfile*/}LoadF;staticconstchar*getF(lua_State*L,void*ud,size_t*size){LoadF*lf=(LoadF*)ud;(void)L;/*notused*/if(lf->n>0){/*aretherepre-readcharacterstoberead?*/*size=lf->n;/*returnthem(charsalreadyinbuffer)*/lf->n=0;/*nomorepre-readcharacters*/}else{/*readablockfromfile*//*'fread'canreturn>0*and*settheEOFflag.Ifnextcallto'getF'called'fread',itmightstillwaitforuserinput.Thenextcheckavoidsthisproblem.*/if(feof(lf->f))returnNULL;*size=fread(lf->buff,1,sizeof(lf->buff),lf->f);/*readblock*/}returnlf->buff;}Lua中文件加密流程:--[[auth:zlfdate:2021.12.28note:对指定路径内所有的文件加密,按原文件子路径保存在特定路径中。]]locallfs=require"lfs"localcryption=require"cryption"localM={}locall_Api={}--请勿改动,改动后请随便改动lua的c代码相应文件locall_CiphertextHead="--jm_cipher_lua_file\n"--[[@note检查该文件是否在为指定的格式文件@paramtbFileType需要的文件类型@paramsFileName被检测的文件@returntrue:合适的文件]]functionl_Api.contain_file_type(tbFileType,sFileName)if(nottbFileType)or#tbFileType==0thenreturntrueend--只搜索文件尾部localsFileLen=string.len(sFileName)localnFindPlace=sFileLenfor_,sTypeinpairs(tbFileType)doifstring.find(sFileName,sType,(nFindPlace-string.len(sType)),true)thenreturntrueendendend--[[@note扫描指定目录的文件(可指定文件类型)@paramsRootPath需要扫描的目录@paramtbAllFilePath扫描到的文件存放@paramtbFileType需要的文件类型@returnnil]]functionl_Api.get_all_files(sRootPath,tbAllFilePath,tbFileType)forentryinlfs.dir(sRootPath)doifstring.sub(entry,1,1)~="."thenlocalsPath=sRootPath.."/"..entrylocalsAttr=lfs.attributes(sPath)assert(type(sAttr)=="table")--如果获取不到属性表则报错if(sAttr.mode=="directory")thenl_Api.get_all_files(sPath,tbAllFilePath,tbFileType)--自调用遍历子目录elseifsAttr.mode=="file"thenifl_Api.contain_file_type(tbFileType,sPath)thentable.insert(tbAllFilePath,sPath)endendendendend--[[@TODO:在sRootDir目录下,创建sDirPath中所有的目录]]functionl_Api.mkdir(sRootDir,sDirPath)--forsDirinstring.gmatch(sDirPath,"(/%g+/)")do--log_info("DDDDDDDDDDDD",sDir)--endlocals=0--locale=0localbSign=falselocalsCreatePathwhiletruedolocaln=string.find(sDirPath,"/",s+1)s=nifs==nilthenbreakend--创建目录ifbSignthensCreatePath=sRootDir..string.sub(sDirPath,1,s)--log_info("DDDDDDDDDDDD",sCreatePath)localtbRet={lfs.mkdir(sCreatePath)}--log_info("##############",table.printT(tbRet),sCreatePath)ifnottbRet[1]and(tbRet[3]or0)~=17then--17:文件夹已经存在assert(false,"Can'tcreatedirfaile"..sCreatePath)endendbSign=trueendend--[[@TODO:对指定路径的文件进行加密,加密后存放到指定路径中。可指定文件的需要转换的文件类型2021-12-2814:51:42@sSpecifyPathSource:加密源目录/data/jm/jmserver/jm@sSpecifyPathTarget:加密后输出根目录(需要手动创建好改目录)/data/jm/jmserver/jm/jm_encrypt@sPassWord:密码@tbFileType:需要的文件类型(不指定,所有文件都会被转换)@return@]]functionM.encrypt_path_all_files(sSpecifyPathSource,sSpecifyPathTarget,sPassWord,tbFileType)ifnotsSpecifyPathSourcethenlog_error("jm.tools.encrypt_fileencrypt_path_all_filesmissingparametersSpecifyPathSource")returnendifnotsSpecifyPathTargetthenlog_error("jm.tools.encrypt_fileencrypt_path_all_filesmissingparametersSpecifyPathTarget")returnendifnotsPassWordthenlog_error("jm.tools.encrypt_fileencrypt_path_all_filesmissingparametersPassWord")returnendifnottbFileTypethenlog_error("jm.tools.encrypt_fileencrypt_path_all_filesmissingparametertbFileType")returnend--搜索文件localtbAllFilePath={}l_Api.get_all_files(sSpecifyPathSource,tbAllFilePath,tbFileType);--路径长度localnLenPathSource=string.len(sSpecifyPathSource)--读取文件并加密localsFileContext=nillocalsCiphertext=nillocalsSavePath=nillocalnFilePrfor_,sFilePathinipairs(tbAllFilePath)donFilePr=assert(io.open(sFilePath),"Can'topenfaile:"..sFilePath)sFileContext=nFilePr:read("a")nFilePr:close()sCiphertext=cryption.encryption(sFileContext,sPassWord)--将文件写入到指定路径中--裁切sFileName,将sSpecifyPathSource剔除后,拼接到sSpecifyPathTarget后面sSavePath=sSpecifyPathTarget..string.sub(sFilePath,nLenPathSource+1)l_Api.mkdir(sSpecifyPathTarget,string.sub(sFilePath,nLenPathSource+1))--将文件写入到sSavePath中nFilePr=assert(io.open(sSavePath,"w+"),"Can'tcreatefaile:"..sSavePath)assert(nFilePr:write(l_CiphertextHead),"Can'twritefaile:"..sSavePath)assert(nFilePr:write(sCiphertext),"Can'twritefaile:"..sSavePath)nFilePr:close()--breakendend--[[@paramsciphertext密文@returnstring明文ornil]]functionM.decrypt(sCiphertext)--plaintext--log_error(":#############",sFileName)--测试解密--localtemp=cryption.decryption(sCiphertext,sPassWord)--log_error("=============\n",temp)endfunctionM.test()--localtbAllFilePath={}--localtbFileType={".lua"}--l_Api.get_all_files(lfs.currentdir(),tbAllFilePath,tbFileType);--log_info("#############",table.printT(tbAllFilePath))M.encrypt_path_all_files(lfs.currentdir().."/jm",lfs.currentdir().."/jm/jm_encrypt","dafasgsfdg",{".lua",".pb"})log_info("##############",string.len(l_CiphertextHead))--l_Api.mkdir("/data/jm/jmserver","/jm_encrypt/config/DockerTag.lua")endreturnM -完-
/
本文档为【Lua代码加密探索】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索