[Lua] 使用 luadec 反組譯 luac 編譯後的檔案

[Lua] 使用 luadec 反組譯 luac 編譯後的檔案

最近遇到一個 Lua 的問題,

同事提供了一個用 luac 編譯過的模組檔案,

這個檔案可以讓主程式正常的執行。

但我自己同樣用 luac 編譯的,卻無法正常執行。

不禁懷疑,會不會同事編譯的原始碼和我的不同呢?

 

想要知道原始碼,就得用 Lua 的反組譯工具。

麻煩的是 Lua 有各種版本,像 5.1, 5.2, 5.3, 到現在的 5.4,

編譯出來的東西似乎也不一定相容,

因此有一些反組譯工具都只能適用於特定版本。

 

後來查到一個 luadec,這個 Github 專案支援 Lua 5.1, 5.2, 5.3 的反組譯,

雖然並沒有支援到 5.4,但至少可以通吃三種版本了~

來看一下要怎麼用它吧~

 

1. 安裝 luadec

先用 git clone 下載 luadec 的原始碼:

git clone https://github.com/viruscamp/luadec

 

2. 編譯 luadec

基本上每個 Lua 版本都得編譯對應的 luadec。

假設想編譯給 Lua 5.1 用的 luadec,就執行下面指令:

cd luadec
git submodule update --init lua-5.1
cd lua-5.1
make macosx
cd ../luadec
make LUAVER=5.1

 

注意因為我是在 Mac 上編譯的,所以中間下的是 make macosx。

如果你是在其他平台上的話,

就在 aix ansi bsd freebsd generic linux macosx mingw posix solaris 這些裡面選一個符合的。

像是如果在 Linux 上編譯的話,就用 make linux。

 

如果是想反組譯 Lua 5.2 或 5.3 的話,就把上面指令裡面的 5.1 代換成 5.2 或 5.3 就好。

編譯好之後,你同時會拿到 lua 和 luadec:

testuser@localhost ~/luadec $ lua-5.1/src/luac -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio

testuser@localhost ~/luadec $ luadec/luadec -v
LuaDec 2.2 rev: 895d923 for Lua 5.1

 

3. 使用 luadec 反組譯

先寫一個小 Lua 程式來當測試目標:

function foo(bar)
    print("this is foo: " .. bar)
end

foo("hello")

 

把這儲存成 test.lua,接著用 luac 來編譯:

./lua-5.1/src/luac -o test.lua.compiled test.lua

 

編譯好的檔案,用 magic file 是可以辨識出 Lua 版本的:

testuser@localhost ~/luadec $ file test.lua*
test.lua:           ASCII text
test.luac.compiled: Lua bytecode, version 5.1

 

接著用 luadec 來反組譯吧:

testuser@localhost ~/luadec $ ./luadec/luadec test.lua.compiled
-- Decompiled using luadec 2.2 rev: 895d923 for Lua 5.1 from https://github.com/viruscamp/luadec
-- Command line: test.lua.compiled

-- params : ...
-- function num : 0
foo = function(bar)
  -- function num : 0_0
  print("this is foo: " .. bar)
end

foo("hello")

 

可以看到反組譯的結果裡面,整個程式的架構是有出來的,

函式和參數的名稱也有出來,

不過當然沒辦法完整的回到最原始的模樣,

但和原始碼比較起來是差不多的~

 

這個 luadec 算是蠻不錯用的工具,

在拿不到 Lua 原始碼的情況下,用它來大致理解原始碼的內容,

是很方便的喔~^^

 

 

(本頁面已被瀏覽過 2,347 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料