ログイン
編集不可のページディスカッション情報添付ファイル

2014-01-04 23:44:28時点のリビジョン1

メッセージを消す
CTF/Toolkit/METASM

MMA

METASM

METASMはPure Rubyで記述されている、アセンブラ、逆アセンブラ、コンパイラ、リンカー、デバッガーである。

対応アーキテクチャ
x86_64,ia32,mips,ppc (unstable: arc,arm,bpf,cy16,dalvik,python,sh4,z80)
対応フォーマット
a.out,elf,pe,raw,Mach-O,nds,xcoffなど

http://metasm.cr0.org/

ドキュメントはソースコード

インストール

リポジトリをクローンする。

$ hg clone https://www.cr0.org/progs/metasm/hg/ ~/local/metasm

環境変数RUBYLIBにmetasmのディレクトリを追加する。.bashrcや.zshenvなどに以下を記述。

[[ -s "$HOME/local/metasm" ]] && export RUBYLIB="$HOME/local/metasm"

関連gemをインストールする(GUI逆アセンブラーを使う場合のみ)

$ gem install gtk2

Tools

samplesディレクトリに利用例としてツールが入っている。(大体はRubyから扱うためのSampleである)

有用性が微妙なもの

逆アセンブラ

基本的な利用方法

$ ruby disassmble.rb --cpu <CPU名> --exe <ファイルフォーマット> [その他オプション] 実行ファイル

--cpu--exeを省略しても自動的に適切なものを選んでくれる

逆アセンブル(CUI)

デフォルトでちゃんとbacktraceしてくれる。

関数の呼び出し元なども表示してくれるのが便利。

$ ruby disassemble.rb --cpu x86_64 --exe ELF a.out                              
// ELF segment at 400000h, flags = R, X
db 7fh, "ELF", 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; @400000h
db 2, 0, 3eh, 0, 1, 0, 0, 0, 40h, 4, 40h, 0, 0, 0, 0, 0 ; @400010h
db 40h, 0, 0, 0, 0, 0, 0, 0, 38h, 0bh, 0, 0, 0, 0, 0, 0 ; @400020h
(中略)
// Xrefs: __libc_start_main
main:
// function binding: r10,r11,r8,r9,rcx,rdi,rdx -> unknown, rax -> 0, rsp -> rsp+8
// function ends at 400539h
    push rbp                                     ; @40050ch  55  
    mov rbp, rsp                                 ; @40050dh  4889e5  
    sub rsp, 10h                                 ; @400510h  4883ec10  
    mov [rbp-4], edi                             ; @400514h  897dfc  
    cmp dword ptr [rbp-4], 1                     ; @400517h  837dfc01  
    jnz loc_400529h                              ; @40051bh  750c  x:loc_400529h

    mov edi, 4005ech                             ; @40051dh  bfec054000  
    call thunk_puts                              ; @400522h  e8b9feffff  x:thunk_puts
    jmp loc_400533h                              ; @400527h  eb0a  x:loc_400533h


// Xrefs: 40051bh
loc_400529h:
    mov edi, 4005f2h                             ; @400529h  bff2054000  
    call thunk_puts                              ; @40052eh  e8adfeffff  x:thunk_puts


// Xrefs: 400527h
loc_400533h:
    mov eax, 0                                   ; @400533h  b800000000  
    leave                                        ; @400538h  c9  
    ret                                          ; @400539h  c3  endsub main
db 90h, 90h, 90h, 90h, 90h, 90h                  ; @40053ah

// Xrefs: __libc_start_main
(以下省略)

逆コンパイル

Boomerangと違って64bit ELFファイルの逆コンパイルもしてくれるが実用性はほとんどない。いろいろと間違ってるし。

   1 $ ruby disassemble.rb --decompile --cpu x86_64 --exe ELF a.out
   2 (中略)
   3 
   4 int main __attribute__((stackoff:-8))(int rbp __attribute__((register(rbp))), int rdi __attribute__((register(rdi))))
   5 {
   6         register int eax __attribute__((register(eax)));
   7         register int rbp_a0 __attribute__((register(rbp)));
   8         register int rsp __attribute__((register(rsp)));
   9         register int rsp_a0 __attribute__((register(rsp)));
  10         rsp_a0 = (rsp - 8);
  11         *(int*)(rsp_a0 - 8) = rbp;
  12         rbp_a0 = rsp_a0;
  13         *(int*)(rbp_a0 - 4) = rdi;
  14         *(__int32*)(rbp_a0 - 4) != 1;
  15         puts();
  16         return eax;
  17 }

シェルコード

RubyのExploit上で直接Shellcodeを書くことが可能になる。

   1 require 'metasm'
   2 
   3 print Metasm::Shellcode.assemble(Metasm::Ia32.new, <<SOURCE).encode_string
   4 mov eax, 1
   5 mov ebx, 42
   6 int 80h         // exit
   7 SOURCE
   8