二進制代碼的分析是計算機科學和軟件工程學科的許多領域的關鍵活動,從軟件安全和程序分析到反向工程。手動二進制分析是一項艱鉅且耗時的任務,有一些軟件工具試圖自動化或協助人類分析師。但是,這些工具中的大多數都有幾種技術和商業限制,這些限制限制了大部分學術和從業者社區的訪問和使用。 Barf是一個開源二進制分析框架,旨在支持信息安全紀律中常見的廣泛二進制代碼分析任務。它是一個可腳本的平台,它支持從多個體系結構,二進制翻譯為中間表示形式,代碼分析插件的可擴展框架以及與外部工具(例如辯論者,SMT求解器和儀器工具)進行互操作的指令。該框架主要用於人類輔助分析,但可以完全自動化。
Barf項目包括Barf和相關工具和軟件包。到目前為止,該項目由以下項目組成:
有關更多信息,請參見:
當前狀態:
| 最新版本 | V0.6.0 |
|---|---|
| URL | https://github.com/programa-stic/barf-project/releases/tag/v0.6.0 |
| 更改日誌 | https://github.com/programa-stic/barf-project/blob/v0.6.0/changelog.md |
在Ubuntu 16.04(X86_64)上測試了所有軟件包。
Barf是用於二進制分析和反向工程的Python軟件包。它可以:
ELF , PE等)加載二進製程序,它目前正在開發中。
BARF取決於以下SMT求解器:
以下命令在系統上安裝barf :
$ sudo python setup.py install您也可以在本地安裝它:
$ sudo python setup.py install --usersudo pip install pyasmjitsudo apt-get install graphviz這是一個非常簡單的示例,它顯示瞭如何打開二進製文件並打印每個指令及其轉換為中間語言( REIL )。
from barf import BARF
# Open binary file.
barf = BARF ( "examples/misc/samples/bin/branch4.x86" )
# Print assembly instruction.
for addr , asm_instr , reil_instrs in barf . translate ():
print ( "{:#x} {}" . format ( addr , asm_instr ))
# Print REIL translation.
for reil_instr in reil_instrs :
print ( " t {}" . format ( reil_instr ))我們還可以恢復CFG並將其保存到.dot文件中。
# Recover CFG.
cfg = barf . recover_cfg ()
# Save CFG to a .dot file.
cfg . save ( "branch4.x86_cfg" )我們可以使用SMT求解器檢查代碼的限制。例如,假設您有以下代碼:
80483ed: 55 push ebp
80483ee: 89 e5 mov ebp,esp
80483f0: 83 ec 10 sub esp,0x10
80483f3: 8b 45 f8 mov eax,DWORD PTR [ebp-0x8]
80483f6: 8b 55 f4 mov edx,DWORD PTR [ebp-0xc]
80483f9: 01 d0 add eax,edx
80483fb: 83 c0 05 add eax,0x5
80483fe: 89 45 fc mov DWORD PTR [ebp-0x4],eax
8048401: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
8048404: c9 leave
8048405: c3 ret
而且,您想知道必須將哪些值分配給內存位置ebp-0x4 , ebp-0x8和ebp-0xc以便在執行代碼後在eax寄存器中獲取特定值。
首先,我們將說明添加到分析儀組件中。
from barf import BARF
# Open ELF file
barf = BARF ( "examples/misc/samples/bin/constraint1.x86" )
# Add instructions to analyze.
for addr , asm_instr , reil_instrs in barf . translate ( 0x80483ed , 0x8048401 ):
for reil_instr in reil_instrs :
barf . code_analyzer . add_instruction ( reil_instr )然後,我們為感興趣的每個變量生成表達式,並添加對它們的所需限制。
ebp = barf . code_analyzer . get_register_expr ( "ebp" , mode = "post" )
# Preconditions: set range for variable a and b
a = barf . code_analyzer . get_memory_expr ( ebp - 0x8 , 4 , mode = "pre" )
b = barf . code_analyzer . get_memory_expr ( ebp - 0xc , 4 , mode = "pre" )
for constr in [ a >= 2 , a <= 100 , b >= 2 , b <= 100 ]:
barf . code_analyzer . add_constraint ( constr )
# Postconditions: set desired value for the result
c = barf . code_analyzer . get_memory_expr ( ebp - 0x4 , 4 , mode = "post" )
for constr in [ c >= 26 , c <= 28 ]:
barf . code_analyzer . add_constraint ( constr )最後,我們檢查是我們建立的限制可以解決。
if barf . code_analyzer . check () == 'sat' :
print ( "[+] Satisfiable! Possible assignments:" )
# Get concrete value for expressions
a_val = barf . code_analyzer . get_expr_value ( a )
b_val = barf . code_analyzer . get_expr_value ( b )
c_val = barf . code_analyzer . get_expr_value ( c )
# Print values
print ( "- a: {0:#010x} ({0})" . format ( a_val ))
print ( "- b: {0:#010x} ({0})" . format ( b_val ))
print ( "- c: {0:#010x} ({0})" . format ( c_val ))
assert a_val + b_val + 5 == c_val
else :
print ( "[-] Unsatisfiable!" )您可以在示例目錄中看到這些和更多示例。
該框架分為三個主要組成部分:核心,拱門和分析。
該組件包含必需模塊:
REIL :提供REIL語言的定義。它還實現了模擬器和解析器。SMT :提供與Z3和CVC4 SMT求解器接口的手段。此外,它提供了將REIL指令轉換為SMT表達式的功能。BI :二進制接口模塊負責加載二進製文件進行處理(它使用Pefile和Pyelftools。)。 每個支持的體系結構都作為子組件提供,其中包含以下模塊。
Architecture :描述體系結構,即,寄存器,內存地址大小。Translator :為每個受支持的說明提供翻譯以供電。Disassembler :提供拆卸功能(它使用Capstone。)。Parser :將指令從字符串轉換為對象形式。 到目前為止,該組件由模塊組成:控制流圖,呼叫圖和代碼分析儀。前兩個分別為CFG和CG恢復提供了功能。後者是SMT求解器相關功能的高級接口。
BARFgadgets是一個建立在Barf上的Python腳本,可讓您在二進製程序中搜索,分類和驗證ROP小工具。搜索階段找到了二進制中的所有ret , jmp和調用 - call的小工具。分類階段根據以下類型進行了先前發現的小工具分類:
這是通過教學仿真完成的。最後,驗證階段包括使用SMT求解器在第二階段驗證分配給每個小工具的語義。
usage: BARFgadgets [-h] [--version] [--bdepth BDEPTH] [--idepth IDEPTH] [-u]
[-c] [-v] [-o OUTPUT] [-t] [--sort {addr,depth}] [--color]
[--show-binary] [--show-classification] [--show-invalid]
[--summary SUMMARY] [-r {8,16,32,64}]
filename
Tool for finding, classifying and verifying ROP gadgets.
positional arguments:
filename Binary file name.
optional arguments:
-h, --help show this help message and exit
--version Display version.
--bdepth BDEPTH Gadget depth in number of bytes.
--idepth IDEPTH Gadget depth in number of instructions.
-u, --unique Remove duplicate gadgets (in all steps).
-c, --classify Run gadgets classification.
-v, --verify Run gadgets verification (includes classification).
-o OUTPUT, --output OUTPUT
Save output to file.
-t, --time Print time of each processing step.
--sort {addr,depth} Sort gadgets by address or depth (number of
instructions) in ascending order.
--color Format gadgets with ANSI color sequences, for output
in a 256-color terminal or console.
--show-binary Show binary code for each gadget.
--show-classification
Show classification for each gadget.
--show-invalid Show invalid gadget, i.e., gadgets that were
classified but did not pass the verification process.
--summary SUMMARY Save summary to file.
-r {8,16,32,64} Filter verified gadgets by operands register size.
有關更多信息,請參見Readme。
BARFcfg是建立在Barf上的Python腳本,可讓您恢復二進製程序的控制流圖。
usage: BARFcfg [-h] [-s SYMBOL_FILE] [-f {txt,pdf,png,dot}] [-t]
[-d OUTPUT_DIR] [-b] [--show-reil]
[--immediate-format {hex,dec}] [-a | -r RECOVER]
filename
Tool for recovering CFG of a binary.
positional arguments:
filename Binary file name.
optional arguments:
-h, --help show this help message and exit
-s SYMBOL_FILE, --symbol-file SYMBOL_FILE
Load symbols from file.
-f {txt,pdf,png,dot}, --format {txt,pdf,png,dot}
Output format.
-t, --time Print process time.
-d OUTPUT_DIR, --output-dir OUTPUT_DIR
Output directory.
-b, --brief Brief output.
--show-reil Show REIL translation.
--immediate-format {hex,dec}
Output format.
-a, --recover-all Recover all functions.
-r RECOVER, --recover RECOVER
Recover specified functions by address (comma
separated).
BARFcg是一個基於Barf的Python腳本,可讓您恢復二進製程序的呼叫圖。
usage: BARFcg [-h] [-s SYMBOL_FILE] [-f {pdf,png,dot}] [-t] [-a | -r RECOVER]
filename
Tool for recovering CG of a binary.
positional arguments:
filename Binary file name.
optional arguments:
-h, --help show this help message and exit
-s SYMBOL_FILE, --symbol-file SYMBOL_FILE
Load symbols from file.
-f {pdf,png,dot}, --format {pdf,png,dot}
Output format.
-t, --time Print process time.
-a, --recover-all Recover all functions.
-r RECOVER, --recover RECOVER
Recover specified functions by address (comma
separated).
Pyasmjit是用於X86_64/ARM組裝代碼生成和執行的Python軟件包。
開發此軟件包是為了測試從X86_64/ARM到REIL的Barf指令翻譯。主要思想是能夠本地運行代碼的片段。然後,將相同的片段翻譯成reil並在Reil VM中執行。最後,這兩個最終上下文(通過本機執行獲得的一個和仿真的上下文)都在與差異進行比較。
有關更多信息,請參見Pyasmjit。
BSD 2-CAREASE許可證。有關更多信息,請參見許可證。