2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
"""
|
2010-10-14 18:41:14 +04:00
|
|
|
dbgtool.py - Portable executable to ASCII debug script converter
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2014-01-13 21:24:49 +04:00
|
|
|
Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2009-01-13 02:02:02 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
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):
|
2009-01-13 02:02:02 +03:00
|
|
|
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
|
2009-01-13 02:02:02 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
2011-04-30 17:20:05 +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
|
2009-01-13 02:02:02 +03:00
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
fp = open(inputFile, "rb")
|
2009-01-13 02:02:02 +03:00
|
|
|
fileContent = fp.read()
|
|
|
|
|
|
|
|
for fileChar in fileContent:
|
2010-04-07 01:57:15 +04:00
|
|
|
unsignedFileChar = struct.unpack("B", fileChar)[0]
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
if unsignedFileChar != 0:
|
|
|
|
counter2 += 1
|
|
|
|
|
|
|
|
if not scrString:
|
2011-04-30 17:20:05 +04:00
|
|
|
scrString = "e %0x %02x" % (counter, unsignedFileChar)
|
2009-01-13 02:02:02 +03:00
|
|
|
else:
|
|
|
|
scrString += " %02x" % unsignedFileChar
|
|
|
|
elif scrString:
|
2011-04-30 17:20:05 +04:00
|
|
|
script += "%s\n" % scrString
|
2009-01-13 02:02:02 +03:00
|
|
|
scrString = ""
|
2011-04-30 17:20:05 +04:00
|
|
|
counter2 = 0
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
if counter2 == 20:
|
2011-04-30 17:20:05 +04:00
|
|
|
script += "%s\n" % scrString
|
|
|
|
scrString = ""
|
|
|
|
counter2 = 0
|
2009-01-13 02:02:02 +03:00
|
|
|
|
2010-04-07 01:57:15 +04:00
|
|
|
script += "w\nq\n"
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
return script
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2009-01-13 02:02:02 +03:00
|
|
|
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
|
2009-01-13 02:02:02 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
2009-01-13 02:59:07 +03:00
|
|
|
script = convert(inputFile)
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
if outputFile:
|
2011-04-30 17:20:05 +04:00
|
|
|
fpOut = open(outputFile, "w")
|
2009-01-13 02:02:02 +03:00
|
|
|
sys.stdout = fpOut
|
|
|
|
sys.stdout.write(script)
|
|
|
|
sys.stdout.close()
|
|
|
|
else:
|
|
|
|
print script
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2010-04-07 01:57:15 +04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
usage = "%s -i <input file> [-o <output file>]" % sys.argv[0]
|
2011-04-30 17:20:05 +04:00
|
|
|
parser = OptionParser(usage=usage, version="0.1")
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
try:
|
2010-04-07 01:57:15 +04:00
|
|
|
parser.add_option("-i", dest="inputFile", help="Input binary file")
|
2009-01-13 02:02:02 +03:00
|
|
|
|
2010-04-07 01:57:15 +04:00
|
|
|
parser.add_option("-o", dest="outputFile", help="Output debug.exe text file")
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
(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")
|
2009-01-13 02:02:02 +03:00
|
|
|
|
|
|
|
except (OptionError, TypeError), e:
|
|
|
|
parser.error(e)
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
inputFile = args.inputFile
|
2009-01-13 02:02:02 +03:00
|
|
|
outputFile = args.outputFile
|
|
|
|
|
2009-01-20 00:26:02 +03:00
|
|
|
main(inputFile, outputFile)
|