sqlmap/extra/dbgtool/dbgtool.py

98 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
dbgtool.py - Portable executable to ASCII debug script converter
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file doc/COPYING for copying permission.
"""
import os
import sys
import struct
from optparse import OptionError
from optparse import OptionParser
2009-01-13 02:59:07 +03:00
def convert(inputFile):
fileStat = os.stat(inputFile)
fileSize = fileStat.st_size
if fileSize > 65280:
2010-04-07 01:57:15 +04:00
print "ERROR: the provided input file '%s' is too big for debug.exe" % inputFile
sys.exit(1)
2010-04-07 01:57:15 +04:00
script = "n %s\nr cx\n" % os.path.basename(inputFile.replace(".", "_"))
script += "%x\nf 0100 ffff 00\n" % fileSize
scrString = ""
counter = 256
counter2 = 0
2010-04-07 01:57:15 +04:00
fp = open(inputFile, "rb")
fileContent = fp.read()
for fileChar in fileContent:
2010-04-07 01:57:15 +04:00
unsignedFileChar = struct.unpack("B", fileChar)[0]
if unsignedFileChar != 0:
counter2 += 1
if not scrString:
scrString = "e %0x %02x" % (counter, unsignedFileChar)
else:
scrString += " %02x" % unsignedFileChar
elif scrString:
2010-04-07 01:57:15 +04:00
script += "%s\n" % scrString
scrString = ""
counter2 = 0
counter += 1
if counter2 == 20:
2010-04-07 01:57:15 +04:00
script += "%s\n" % scrString
scrString = ""
counter2 = 0
2010-04-07 01:57:15 +04:00
script += "w\nq\n"
return script
def main(inputFile, outputFile):
if not os.path.isfile(inputFile):
2010-04-07 01:57:15 +04:00
print "ERROR: the provided input file '%s' is not a regular file" % inputFile
sys.exit(1)
2009-01-13 02:59:07 +03:00
script = convert(inputFile)
if outputFile:
2010-04-07 01:57:15 +04:00
fpOut = open(outputFile, "w")
sys.stdout = fpOut
sys.stdout.write(script)
sys.stdout.close()
else:
print script
2010-04-07 01:57:15 +04:00
if __name__ == "__main__":
usage = "%s -i <input file> [-o <output file>]" % sys.argv[0]
parser = OptionParser(usage=usage, version="0.1")
try:
2010-04-07 01:57:15 +04:00
parser.add_option("-i", dest="inputFile", help="Input binary file")
2010-04-07 01:57:15 +04:00
parser.add_option("-o", dest="outputFile", help="Output debug.exe text file")
(args, _) = parser.parse_args()
if not args.inputFile:
2010-04-07 01:57:15 +04:00
parser.error("Missing the input file, -h for help")
except (OptionError, TypeError), e:
parser.error(e)
inputFile = args.inputFile
outputFile = args.outputFile
2009-01-20 00:26:02 +03:00
main(inputFile, outputFile)