a

よろしくのキワミ

angrを使ってみた

angrを使ってみた

2019/05/25(土) ~ 05/26(日)に開催されていたctf4b 2019のReversingの問題を解いていると、フラグを生成する処理がとても長く、個人で出場していると(私の今の能力では)すべての問題を解くのが、時間内に間に合わないかもしれないということがあった。

f:id:toDo:20190527091041p:plain

単純な命令を繰り返していて、なおかつ規則性(1,2,3,1,2,3...)がない場合CTFなどの競技で時間が設けられていると、紙と鉛筆を用いた処理の解析に限界を感じた。

そこで他の人はどう解いているのだろうと考えWriteupを見ているとangrというツールを使用しているチームがあった

というわけで今回はangrというツールについて学習したいと思う。

学習に使用するサイトはここ↓

docs.angr.io

Google翻訳

angrとは

angr is a multi-architecture binary analysis toolkit, 
with the capability to perform dynamic symbolic execution 
(like Mayhem, KLEE, etc.) and various static analyses on binaries.

angrはマルチアーキテクチャバイナリ解析ツールキットで、
動的なシンボリック実行(Mayhem、KLEEなど)やバイナリに対する
さまざまな静的解析を実行することができます。

使用する

今回はctf4b 2019で手動で解析した中で大変だったLinear Operationを解くことにする。

Linear Operation

Simulation Managersという項目がある。

docs.angr.io

ここを参照するとフラグを取得する処理があるのでここを参考にしてコードをたたいていく。
まずはライブラリをインポートし、プロジェクトを作成。

>>> import angr
>>> proj = angr.Project('/linear_operation')

次にSimulationManagerを作成し

>>> simgr = proj.factory.simgr()

ある状態に一致する状態を見つけるまで実行する。
ここでは"correct"の文字列に一致するまで探索する。

"correct"は等ファイルに適当な文字列を渡して実行したとき、出力された文字列が"wrong"だったことから、stringsコマンドでELFファイルを見たときに正解の文字列が入力されたときに表示される文字列だろうと予想し使用した。

simgr.explore(find=lambda s: b"correct" in s.posix.dumps(1))

すると以下の結果が出力された。

WARNING | 2019-05-27 10:56:08,802 | angr.state_plugins.symbolic_memory | The program is accessing memory or registers with an unspecified value. This could indicate unwanted behavior.
WARNING | 2019-05-27 10:56:08,807 | angr.state_plugins.symbolic_memory | angr will cope with this by generating an unconstrained symbolic variable and continuing. You can resolve this by:
WARNING | 2019-05-27 10:56:08,811 | angr.state_plugins.symbolic_memory | 1) setting a value to the initial state
WARNING | 2019-05-27 10:56:08,812 | angr.state_plugins.symbolic_memory | 2) adding the state option ZERO_FILL_UNCONSTRAINED_{MEMORY,REGISTERS}, to make unknown regions hold null
WARNING | 2019-05-27 10:56:08,813 | angr.state_plugins.symbolic_memory | 3) adding the state option SYMBOL_FILL_UNCONSTRAINED_{MEMORY_REGISTERS}, to suppress these messages.
WARNING | 2019-05-27 10:56:08,814 | angr.state_plugins.symbolic_memory | Filling register r15 with 8 unconstrained bytes referenced from 0x40cfb0 (__libc_csu_init+0x0 in linear_operation (0x40cfb0))
WARNING | 2019-05-27 10:56:08,818 | angr.state_plugins.symbolic_memory | Filling register r14 with 8 unconstrained bytes referenced from 0x40cfb2 (__libc_csu_init+0x2 in linear_operation (0x40cfb2))
WARNING | 2019-05-27 10:56:08,825 | angr.state_plugins.symbolic_memory | Filling register r13 with 8 unconstrained bytes referenced from 0x40cfb7 (__libc_csu_init+0x7 in linear_operation (0x40cfb7))
WARNING | 2019-05-27 10:56:08,830 | angr.state_plugins.symbolic_memory | Filling register r12 with 8 unconstrained bytes referenced from 0x40cfb9 (__libc_csu_init+0x9 in linear_operation (0x40cfb9))
WARNING | 2019-05-27 10:56:08,838 | angr.state_plugins.symbolic_memory | Filling register rbx with 8 unconstrained bytes referenced from 0x40cfca (__libc_csu_init+0x1a in linear_operation (0x40cfca))
WARNING | 2019-05-27 10:56:08,900 | angr.state_plugins.symbolic_memory | Filling register cc_ndep with 8 unconstrained bytes referenced from 0x4005b1 (register_tm_clones+0x21 in linear_operation (0x4005b1))
<SimulationManager with 63 deadended, 1 found>

あとは出力してやるとフラグが入手できる。

flag = s.posix.dumps(0)
>>> print(flag)
b'ctf4b{5ymbol1c_3xecuti0n_1s_3ffect1ve_4ga1nst_l1n34r_0p3r4ti0n}'
プライバシーポリシー お問い合わせ