|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +$Id$ |
| 5 | +
|
| 6 | +This file is part of the sqlmap project, http://sqlmap.sourceforge.net. |
| 7 | +
|
| 8 | +Copyright (c) 2010 Miroslav Stampar <[email protected]> |
| 9 | +
|
| 10 | +sqlmap is free software; you can redistribute it and/or modify it under |
| 11 | +the terms of the GNU General Public License as published by the Free |
| 12 | +Software Foundation version 2 of the License. |
| 13 | +
|
| 14 | +sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | +details. |
| 18 | +
|
| 19 | +You should have received a copy of the GNU General Public License along |
| 20 | +with sqlmap; if not, write to the Free Software Foundation, Inc., 51 |
| 21 | +Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | +""" |
| 23 | + |
| 24 | +import codecs |
| 25 | +import os |
| 26 | +import cProfile |
| 27 | + |
| 28 | +from lib.core.data import conf |
| 29 | +from lib.core.data import logger |
| 30 | +from lib.core.data import paths |
| 31 | + |
| 32 | +def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None): |
| 33 | + try: |
| 34 | + from extra.gprof2dot import gprof2dot |
| 35 | + from extra.xdot import xdot |
| 36 | + import gobject |
| 37 | + import gtk |
| 38 | + import pydot |
| 39 | + except ImportError, e: |
| 40 | + errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e) |
| 41 | + logger.error(errMsg) |
| 42 | + return |
| 43 | + |
| 44 | + if profileOutputFile is None: |
| 45 | + profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw") |
| 46 | + |
| 47 | + if dotOutputFile is None: |
| 48 | + dotOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.dot") |
| 49 | + |
| 50 | + if imageOutputFile is None: |
| 51 | + imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png") |
| 52 | + |
| 53 | + if os.path.exists(profileOutputFile): |
| 54 | + os.remove(profileOutputFile) |
| 55 | + |
| 56 | + if os.path.exists(dotOutputFile): |
| 57 | + os.remove(dotOutputFile) |
| 58 | + |
| 59 | + if os.path.exists(imageOutputFile): |
| 60 | + os.remove(imageOutputFile) |
| 61 | + |
| 62 | + infoMsg = "profiling the execution into file %s" % profileOutputFile |
| 63 | + logger.info(infoMsg) |
| 64 | + |
| 65 | + # Start sqlmap main function and generate a raw profile file |
| 66 | + cProfile.run("start()", profileOutputFile) |
| 67 | + |
| 68 | + infoMsg = "converting profile data into a dot file '%s'" % dotOutputFile |
| 69 | + logger.info(infoMsg) |
| 70 | + |
| 71 | + # Create dot file by using extra/gprof2dot/gprof2dot.py |
| 72 | + # http://code.google.com/p/jrfonseca/wiki/Gprof2Dot |
| 73 | + dotFilePointer = codecs.open(dotOutputFile, 'wt', conf.dataEncoding) |
| 74 | + parser = gprof2dot.PstatsParser(profileOutputFile) |
| 75 | + profile = parser.parse() |
| 76 | + profile.prune(0.5/100.0, 0.1/100.0) |
| 77 | + dot = gprof2dot.DotWriter(dotFilePointer) |
| 78 | + dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP) |
| 79 | + dotFilePointer.close() |
| 80 | + |
| 81 | + infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile |
| 82 | + logger.info(infoMsg) |
| 83 | + |
| 84 | + # Create graph image (png) by using pydot (python-pydot) |
| 85 | + # http://code.google.com/p/pydot/ |
| 86 | + pydotGraph = pydot.graph_from_dot_file(dotOutputFile) |
| 87 | + pydotGraph.write_png(imageOutputFile) |
| 88 | + |
| 89 | + infoMsg = "displaying interactive graph with xdot library" |
| 90 | + logger.info(infoMsg) |
| 91 | + |
| 92 | + # Display interactive Graphviz dot file by using extra/xdot/xdot.py |
| 93 | + # http://code.google.com/p/jrfonseca/wiki/XDot |
| 94 | + win = xdot.DotWindow() |
| 95 | + win.connect('destroy', gtk.main_quit) |
| 96 | + win.set_filter("dot") |
| 97 | + win.open_file(dotOutputFile) |
| 98 | + gobject.timeout_add(1000, win.update, dotOutputFile) |
| 99 | + gtk.main() |
0 commit comments