forked from mandiant/Ghidrathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghidrathon_example.py
31 lines (25 loc) · 1.43 KB
/
ghidrathon_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Print function basic block and instruction counts.
# @author Mike Hunhoff ([email protected])
# @category Python 3
# Copyright (C) 2022 Mandiant, Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
from ghidra.program.model.block import SimpleBlockIterator
from ghidra.program.model.block import BasicBlockModel
for func in currentProgram().getListing().getFunctions(True):
block_count = 0
# find basic block count for the current function
block_itr = SimpleBlockIterator(BasicBlockModel(currentProgram()), func.getBody(), monitor())
while block_itr.hasNext():
block_count += 1
block_itr.next()
# find instruction count for the current function
insn_count = len(tuple(currentProgram().getListing().getInstructions(func.getBody(), True)))
# print counts to user
print(
f"Function {func.getName()} @ {hex(func.getEntryPoint().getOffset())}: {block_count} blocks, {insn_count} instructions"
)