- Lua有一种预编译机制,能够把文本代码预编译成Bytecode/Opcode 提高解析、执行速度,降低内存占用
- 原版Lua(Vanilla Lua)默认的Bytecode的字节结构和OpenWrt的并不相同,因为OpenWrt为了一系列需要,在截止我写此文时候,在Lua5.1.5的版本主线上,对原版的LUA引擎打了补丁 ,导致其产生的字节码和原版的Lua产生的并不一样,( http://lua-users.org/lists/lua-l/2012-06/msg00065.html ),因此也不能使用原版的Lua引擎解释,会报类似 bad header in precompiled chunk 的错误
- 本机尝试逆向原版的Lua产生的LuaC, 使用这个luadec 项目无任何问题 ,但是逆向Openwrt上的luaC失败,所以需要在Openwrt的Patch过的Lua lib的基础上,编译luadec
- 过程记录如下,全程在Linux Deepin 15.5上完成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#安装依赖 sudo apt install libncurses-dev libreadline-dev #获得luadec源码 git clone https://github.com/viruscamp/luadec cd luadec git submodule update --init lua-5.1 #下面开始不同 ref=master patch_dir=patches.$ref mkdir $patch_dir && cd $patch_dir #如下命令需要grep支持pcre正则,如果不支持,请自己手动处理把。 patchs=$(curl -sSL -H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/repos/openwrt/openwrt/contents/package/utils/lua/patches?ref='"$ref" |grep -oP 'name\"\s*:\s*\".*\.patch' |grep -oP '\d+.*\.patch') #下载补丁文件 for p in $patchs;do wget 'https://github.com/openwrt-mirror/openwrt/raw/'"$ref"'/package/utils/lua/patches/'${p} -O $p; done cd ../lua-5.1 #打上补丁 for i in ../${patch_dir}/*.patch; do patch -p1 <$i ; done make linux |
到这一步 其实会出现错误的,有提示
lauxlib.o: relocation R_X86_64_PC32 against symbol `[email protected]@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
需要修改Makefile 给CFLAGS加上 -fPIC 选项 另外,为了使得Patch过后生成的so文件带版本号,还需要加上版本,最终生成的是如下的lua-5.1/src/Makefile的补丁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--- Makefile.bak 2018-05-29 18:04:44.979014076 +0800 +++ Makefile 2018-05-29 18:21:35.110112120 +0800 @@ -8,7 +8,7 @@ PLAT= none CC= gcc -CFLAGS= -O2 -Wall $(MYCFLAGS) +CFLAGS= -fPIC -O2 -Wall $(MYCFLAGS) AR= ar rcu RANLIB= ranlib RM= rm -f @@ -18,7 +18,7 @@ MYLDFLAGS= MYLIBS= # USE_READLINE=1 - +PKG_VERSION = 5.1.5 # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris |
继续执行make linux应该不会报错了,然后按照luadec的教程继续做完就可以了
1 2 3 4 |
make linux export LD_LIBRARY_PATH=`pwd`/src/ cd ../luadec make LUAVER=5.1 |
生成的3个可执行文件luadec luaopswap luareplace就是全部,她就可以直接解码openwrt的luaC的Bytecode了 这里提供编译好的文件下载
gcc -o lua -L. -llua lua.o -lm -Wl,-E -ldl
/usr/bin/ld: lua.o: in function
lstop':
lua_sethook'lua.c:(.text+0x3b): undefined reference to
/usr/bin/ld: lua.o: in function
pushline':
lua_getfield'lua.c:(.text+0xfa): undefined reference to
/usr/bin/ld: lua.c:(.text+0x109): undefined reference to
lua_tolstring'
lua_settop'/usr/bin/ld: lua.c:(.text+0x127): undefined reference to
/usr/bin/ld: lua.c:(.text+0x1c7): undefined reference to
lua_pushstring'
lua_getfield'/usr/bin/ld: lua.c:(.text+0x205): undefined reference to
/usr/bin/ld: lua.c:(.text+0x214): undefined reference to
lua_tolstring'
lua_pushfstring'/usr/bin/ld: lua.c:(.text+0x251): undefined reference to
/usr/bin/ld: lua.o: in function `docall':
make linux 报错
大人,请问一下,为什么我拿到的lua文件中的000B这个头地址,数据是04?不是01也不是02。该怎么反编译?
你是怎么编译通过的,我这总报错啊
老哥 能发一下 编译好的吗 我按照你的方式不行了,因为好多资源都提示 404 了。
谢谢
openwrt-mirror仓库改地址为openwrt了