8

反向编译OpenWrt的Lua字节码 (Decompile Lua bytecode of OpenWRT)

 3 years ago
source link: https://blog.ihipop.info/2018/05/5110.html/comment-page-1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

反向编译OpenWrt的Lua字节码 (Decompile Lua bytecode of OpenWRT)

  1. Lua有一种预编译机制,能够把文本代码预编译成Bytecode/Opcode 提高解析、执行速度,降低内存占用
  2. 原版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 的错误
  3. 本机尝试逆向原版的Lua产生的LuaC, 使用这个luadec 项目无任何问题   ,但是逆向Openwrt上的luaC失败,所以需要在Openwrt的Patch过的Lua lib的基础上,编译luadec
  4. 过程记录如下,全程在Linux Deepin 15.5上完成:
#安装依赖
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;
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 `stderr@@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的补丁

--- 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的教程继续做完就可以了

make linux
export LD_LIBRARY_PATH=`pwd`/src/
cd ../luadec
make LUAVER=5.1

生成的3个可执行文件luadec luaopswap luareplace就是全部,她就可以直接解码openwrt的luaC的Bytecode了 这里提供编译好的文件下载

Author Info : From:反向编译OpenWrt的Lua字节码 (Decompile Lua bytecode of OpenWRT) URL:https://blog.ihipop.info/2018/05/5110.html Please Reserve This Link,Thanks!

发布于 2018年05月29日2018年10月8日作者 ihipop分类 MyOriginalOpenWrt标签 BytecodeLualuaC反编译


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK