From 099f07c4ca30d4629e36624bad1c5c50695d584e Mon Sep 17 00:00:00 2001 From: Andrew Lamoureux Date: Mon, 20 Apr 2020 14:16:26 -0400 Subject: Add python example: pe_stat.py --- python/examples/pe_stat.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 python/examples/pe_stat.py (limited to 'python') diff --git a/python/examples/pe_stat.py b/python/examples/pe_stat.py new file mode 100755 index 00000000..aa8cc5d9 --- /dev/null +++ b/python/examples/pe_stat.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# +# BinaryNinja multiplatform version of Z0MBIE's PE_STAT for opcode frequency +# statistics http://z0mbie.dreamhosters.com/opcodes.html +# +# Copyright (c) 2020 Vector 35 Inc +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +import sys +import binaryninja +from collections import defaultdict + +opc2count = defaultdict(lambda:0) +target = sys.argv[1] + +print('opening %s' % target) +bv = binaryninja.BinaryViewType.get_view_of_file(target) +print('analyzing') +bv.update_analysis_and_wait() + +print('looping over functions') +for func in bv.functions: + print('disassembling %s()' % func.symbol.full_name) + for block in func: + for (toks, length) in block: + opc = toks[0].text + opc2count[opc] += 1 + #print('incremented %s, is now: %d' % (opc, opc2count[opc])) + +total = sum([x[1] for x in opc2count.items()]) + +print('op frequency %') +print('-- --------- -') +for opc in sorted(opc2count.keys(), key=lambda x:opc2count[x], reverse=True): + print(opc.ljust(8), str(opc2count[opc]).ljust(16), '%.1f%%'%(100.0*opc2count[opc]/total)) + -- cgit v1.3.1