概要
アバストが自社のマシンコード逆コンパイラをオープンソース化
というavast blogを見た。
Ubuntu 16.04 LTSに導入した時の手順等を記載する。
このblogですが、そもそも「逆コンパイラとは」の話も書いてあり、マルウェア解析等の初学者には有用かと思われます。
What is RetDec?
Github: https://github.com/avast-tl/retdec
Project: https://retdec.com/decompilation/
オープンソース化された逆コンパイラで以下のサポートをしている。(githubより抜粋)
- Supported file formats
- ELF, PE, Mach-O, COFF, AR(archive), Intel HEX, and raw machine code.
- Supported architectures(32bit only)
- Intel x86, ARM, MIPS, PIC32 and PowerPC
以下の機能がある(from github, google translate)
- 詳細な情報を含む実行可能ファイルの静的解析
- コンパイラとパッカーの検出
- ロードおよび命令解読
- 静的にリンクされたライブラリコードのシグネチャベースの削除
- デバッグ情報(DWARF、PDB)の抽出と利用。
- 命令のイディオムの再構築。
- C ++クラス階層(RTTI、vtables)の検出と再構築。
- C ++バイナリ(GCC、MSVC、Borland)からのシンボルのデマンギング
- 関数、型、および上位レベルの構造の再構築。
- 統合ディスアセンブラ。
- 2つの高水準言語CとPythonライクな言語で出力します。
- コールグラフ、制御フローグラフ、各種統計の生成
これだけの機能があれば、とりあえずマルウェア解析を始める、という時にも使えそうです。
インストール
githubのInstallation and Use参照。
今回のターゲットはLinux(Ubuntu 16)なので、Linuxの項目を見る。
- pre-build packageでは配っていないから、セルフビルドをする
- 追加でパッケージ入れる
- 実行テストする
セルフビルドせよ
セルフビルドせよ、はBuild and Installationに記載がある。
- Ubuntuの場合は、と書いてある所のパッケージをインストールする
sudo apt-get install build-essential cmake git perl python3 bash bison flex autoconf automake libtool pkg-config m4 coreutils zlib1g-dev libtinfo-dev wget bc upx doxygen graphviz
- リポジトリのクローン
git clone https://github.com/avast-tl/retdec
- コンパイル
cd retdec
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=<install path>
-
make -jN
- Nは並列ビルドするときのCPUコア数
- make isntall
が、Ubuntu16のcmakeは version 3.5.1だが、コンパイルにはCMake 3.6以上が必要。
cmakeを最新版に更新する。
-
公式サイトで最新版を落とす
- "Binary distributions:"のPlatformがLinux X86_64の .sh の物とか
-
sudo sh ./cmake-3.10.2-Linux-x86_64.sh
とかでインストール
- パスの追加
- Unpackingされたbinにcmake等が要るので、PATHに追加する
PATH=<PAHT2cmake>:$PATH
- Unpackingされたbinにcmake等が要るので、PATHに追加する
- あとはコンパイルを始めるだけ
- 少し時間かかるみたい
あと、Dockerばんもどっかにあるようだ。そっち使った方が楽そうだけど、どのような環境で利用するか次第でしょう。
追加でパッケージ入れる
次に、Installation and UseのLinuxで明示されているパッケージを入れる
- パッケージ名は、apt-cache search <パッケージ名>とかで探して入れる
- 既に入っている、というものも半数以上あったので、環境による、としか。
実行テスト
適当にWindowsの.exeファイルをもってきて、テストする
- notepad.exeとかでよい
bash <install path>/bin/retdec-decompiler.sh <path/>notepad.exe
こんな感じ
@localhost:~/bin$ ./retdec-decompiler.sh /opt/samba/work/notepad.exe
##### Checking if file is a Mach-O Universal static library...
RUN: <install path>/bin/retdec-macho-extractor --list <file path>/notepad.exe
##### Checking if file is an archive...
RUN: <install path>/bin/retdec-ar-extractor --arch-magic <file path>/notepad.exe
Not an archive, going to the next step.
##### Gathering file information...
RUN: <install path>/bin/retdec-fileinfo -c <file path>/notepad.c.json --similarity <file path>/notepad.exe --no-hashes=all --crypto <install path>/bin/../share/retdec/support/generic/yara_patterns/signsrch/signsrch.yara
Input file : <file path>/notepad.exe
File format : PE
File class : 32-bit
File type : Executable file
Architecture : x86
Endianness : Little endian
Image base address : 0x1000000
Entry point address : 0x10031c9
Entry point offset : 0x25c9
Entry point section name : .text
Entry point section index: 0
Bytes on entry point : e8caf9ffff6a5868e0320001e87204000033db895de4895dfc8d459850ff1500110001c745fcfeffffffc745fc0100000064
Detected tool : Microsoft Linker (9.0) (linker), combined heuristic
Detected tool : MoleBox (2.0) (packer), 4 from 6 significant nibbles (66.6667%)
Rich header offset : 0x80
Rich header key : 0x31ace036
Rich header signature : 0095780900000003009378090000001d00010000000000f7008378090000001f008478090000000200947809
000000010091780900000001
##### Trying to unpack <file path>/notepad.exe into <file path>/notepad-unpacked.tmp by using generic unpacker...
RUN: <install path>/bin/retdec-unpacker <file path>/notepad.exe -o <file path>/notepad-unpacked.tmp
No matching plugins found for 'Microsoft Linker 9.0'
No matching plugins found for 'MoleBox 2.0'
##### Unpacking by using generic unpacker: nothing to do
##### Trying to unpack <file path>/notepad.exe into <file path>/notepad-unpacked.tmp by using UPX...
RUN: upx -d <file path>/notepad.exe -o <file path>/notepad-unpacked.tmp
upx: <file path>/notepad.exe: NotPackedException: not packed by UPX
##### Unpacking by using UPX: nothing to do
以下略
逆コンパイル後
notepad.exeを逆コンパイルしたら、以下の物が排出された
- notepad.c
- notepad.c.backend.bc
- notepad.c.backend.ll
- notepad.c.frontend.dsm
- notepad.c.json
notepad.cを見ると、#includeやstruct、functionなどが見える。バイナリよりとても見やすい。
著作権的にアレなので、ここでは中身をちら見して捨てます。
以上で利用できる状態になった。
あとは解析対象のバイナリを突っ込めばよい感じ。