News

Disabling pygraphviz

Written on 12.12.2017 18:49 by Rahul Gopinath

For those on Ubuntu 16.xx that are unable to install pygraphviz, it is possible to make do with a pure python library called pydot. Once you have installed pydot with pip3 install pydot, these are the changes in pycfg.py that can get you the dot file. Once you have the dot file created with a command such as python3 pycfg.py cgidecode.py -d -y example.cov, you can open the dotfile, and paste its contents in `http://viz-js.com/` to view the generated graph.

 

diff --git a/07-Search-Based Fuzzing/code/pycfg.py b/07-Search-Based Fuzzing/code/pycfg.py
index 298f85d..a4e6d5a 100755
--- a/07-Search-Based Fuzzing/code/pycfg.py
+++ b/07-Search-Based Fuzzing/code/pycfg.py
@@ -9,7 +9,7 @@ Use http://viz-js.com/ to view digraph output
 import ast
 import re
 import astunparse
-import pygraphviz
+import pydot

 class CFGNode(dict):
     registry = 0
@@ -68,24 +68,25 @@ class CFGNode(dict):
             for i in ['if', 'while', 'for', 'elif']:
                 v = re.sub(r'^_%s:' % i, '%s:' % i, v)
             return v
-        G = pygraphviz.AGraph(directed=True)
+        G = pydot.Graph('mygrap', graph_type='digraph')
         cov_lines = [i for i,j in arcs]
         for nid, cnode in CFGNode.cache.items():
-            G.add_node(cnode.rid)
+            node = pydot.Node(cnode.rid)
+            G.add_node(node)
             n = G.get_node(cnode.rid)
             lineno = cnode.lineno()
-            n.attr['label'] = "%d: %s" % (lineno, unhack(cnode.source()))
+            node.set('label', "%d: %s" % (lineno, unhack(cnode.source())))
             for pn in cnode.parents:
                 plineno = pn.lineno()
                 if arcs:
                     if  (plineno, lineno) in arcs:
-                        G.add_edge(pn.rid, cnode.rid, color='blue')
+                         G.add_edge(pydot.Edge(pn.rid, cnode.rid, color='blue'))
                     elif plineno == lineno and lineno in cov_lines:
-                        G.add_edge(pn.rid, cnode.rid, color='blue')
+                         G.add_edge(pydot.Edge(pn.rid, cnode.rid, color='blue'))
                     else:
-                        G.add_edge(pn.rid, cnode.rid, color='red')
+                         G.add_edge(pydot.Edge(pn.rid, cnode.rid, color='red'))
                 else:
-                    G.add_edge(pn.rid, cnode.rid)
+                    G.add_edge(pydot.Edge(pn.rid, cnode.rid))
         return G

 class PyCFG:
@@ -421,8 +422,7 @@ if __name__ == '__main__':
         cfg = PyCFG()
         cfg.gen_cfg(slurp(args.pythonfile).strip())
         g = CFGNode.to_graph(arcs)
-        g.draw('out.png', prog='dot')
-        print(g.string(), file=sys.stderr)
+        print(g.to_string(), file=sys.stderr)
     elif args.cfg:
         cfg,first,last = get_cfg(args.pythonfile)
         for i in sorted(cfg.keys()):

Privacy Policy | Legal Notice
If you encounter technical problems, please contact the administrators.