Skip to content

Instantly share code, notes, and snippets.

@d0now
Created July 20, 2023 08:01
Show Gist options
  • Save d0now/791ec0350737adf1a13b5310d766d37e to your computer and use it in GitHub Desktop.
Save d0now/791ec0350737adf1a13b5310d766d37e to your computer and use it in GitHub Desktop.
BinaryNinja - LiteCov output coverage explorer snippet
from binaryninja.binaryview import BinaryView
from binaryninja.enums import HighlightStandardColor
from binaryninja.log import log_warn, log_info
from binaryninja.interaction import OpenFileNameField, get_form_input
from binaryninja.plugin import PluginCommand
from typing import List
class LiteCov:
highlighted: List[int] = []
@staticmethod
def parse_coverage_file(filepath: str):
ret = []
with open(filepath) as f:
line = f.readline()
while line:
splitted = line.strip().split("+")
if len(splitted) != 2:
raise Exception
ret.append(int(splitted[1], 16))
line = f.readline()
return ret
@classmethod
def highlight(cls, bv: BinaryView, coverage: List[int]):
for addr in coverage:
bbs = bv.get_basic_blocks_at(bv.start + addr)
if not bbs:
log_warn(f"no basic blocks at: 0x{addr:x}")
continue
for bb in bbs:
log_info(f"highlighting: 0x{bb.start:x}")
bb.set_auto_highlight(HighlightStandardColor.BlueHighlightColor)
cls.highlighted.append(bb.start)
@classmethod
def clear(cls, bv: BinaryView):
for addr in cls.highlighted:
for bb in bv.get_basic_blocks_at(addr):
log_info(f"clearing: 0x{addr:x}")
bb.set_auto_highlight(HighlightStandardColor.NoHighlightColor)
def litecov_cmd_highlight_coverage(bv: BinaryView):
ff = OpenFileNameField("coverage file")
if get_form_input([ff], "Highlight LiteCov Coverage"):
coverage = LiteCov.parse_coverage_file(ff.result)
LiteCov.highlight(bv, coverage)
def litecov_cmd_clear_coverage(bv: BinaryView):
LiteCov.clear(bv)
PluginCommand.register("LiteCov\highlight", "", litecov_cmd_highlight_coverage)
PluginCommand.register("LiteCov\clear", "", litecov_cmd_clear_coverage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment