forked from packing-box/python-exeplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
46 lines (37 loc) · 2.17 KB
/
Copy pathgraph.py
File metadata and controls
46 lines (37 loc) · 2.17 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
from .__common__ import Binary, CACHE_DIR, COLORS, MIN_ZONE_WIDTH
from ..__conf__ import *
check_imports("angr", "networkx", "pygraphviz")
_DEFAULT_ALGORITHM, _DEFAULT_ENGINE = "fast", "default"
_ENGINES = ["default", "pcode", "vex"]
def arguments(parser):
parser.add_argument("executable", help="executable sample to be plotted")
parser.add_argument("-a", "--algorithm", default=_DEFAULT_ALGORITHM, choices=["emulated", "fast"],
help="engine for CFG extraction by Angr")
parser.add_argument("-e", "--engine", default=_DEFAULT_ENGINE, choices=_ENGINES,
help="engine for CFG extraction by Angr")
return parser
@save_figure
def plot(executable, algorithm=_DEFAULT_ALGORITHM, engine=_DEFAULT_ENGINE, **kwargs):
""" plot the Control Flow Graph (CFG) of an executable """
import angr
import networkx as nx
import pygraphviz as pgv
from math import ceil, log2
# ------------------------------------------------- DRAW THE PLOT --------------------------------------------------
logger.debug("> computing the executable's CFG")
engine = {k: getattr(angr.engines, "UberEngine" if k != "pcode" else f"UberEngine{k.capitalize()}") \
for k in _ENGINES}[engine]
project = angr.Project(executable, auto_load_libs=False, engine=engine)
cfg = getattr(project.analyses, f"CFG{algorithm.capitalize()}")()
labels, node_colors = {}, []
for node in cfg.graph.nodes():
labels[node] = f"{node.name}\n0x{node.addr:x}" if hasattr(node, "name") and node.name else f"0x{node.addr:x}"
node_colors.append("red" if node.function_address == node.addr else "lightblue")
# ---------------------------------------------- CONFIGURE THE FIGURE ----------------------------------------------
logger.debug("> configuring the figure")
n = max(10, min(30, ceil(log2(n_nodes := len(cfg.graph.nodes()) + 1) * 2)))
fig = plt.figure(figsize=(n, n))
nx.draw(cfg.graph, nx.kamada_kawai_layout(cfg.graph), fig.gca(), font_size=8, with_labels=True, labels=labels,
node_size=max(300, 15000 // n_nodes), node_color=node_colors)