Skip to content

Instantly share code, notes, and snippets.

@msymt
Last active May 31, 2024 04:49
Show Gist options
  • Save msymt/ced6d8c12cc7a4b1b104b0ff81ca219a to your computer and use it in GitHub Desktop.
Save msymt/ced6d8c12cc7a4b1b104b0ff81ca219a to your computer and use it in GitHub Desktop.
Ghidraでバイナリの基本ブロックを計算するスクリプト
# ref: https://reverseengineering.stackexchange.com/questions/23469/way-to-get-basic-blocks-of-a-binary-using-ghidra
from ghidra.program.model.block import BasicBlockModel
from ghidra.util.task import TaskMonitor
bbm = BasicBlockModel(currentProgram)
blocks = bbm.getCodeBlocks(TaskMonitor.DUMMY)
block = blocks.next()
bbl_cnt = 0
while block:
bbl_cnt += 1
print "Label: {}".format(block.name)
print "Min Address: {}".format(block.minAddress)
print "Max address: {}".format(block.maxAddress)
print
block = blocks.next()
print "total number of basic block: {}".format(bbl_cnt)
@msymt
Copy link
Author

msymt commented May 31, 2024

コマンドラインから実行する方法。ghidraのheadless modeで可能。

#!/bin/bash

#実際のパスに要変更
GHIDRA_DIR="/Users/hoge/ghidra_10.0_PUBLIC" 
# get current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}"  )" >/dev/null 2>&1 && pwd  )"
echo $DIR

PROJECT_DIR="${GHIDRA_DIR}/project"
PROJECT="fuzzing" #実際のプロジェクト名に要変更

FILE="target.elf"  #実際のプロジェクト名に要変更
SCRIPT="bbl_calc.py"

$GHIDRA_DIR/support/analyzeHeadless $PROJECT_DIR \
  $PROJECT \
  -noanalysis \
  -process $FILE \
  -scriptPath $DIR \
  -postscript $SCRIPT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment