sqlmap/extra/dbgtool/dbgtool.py

97 lines
2.4 KiB
Python
Raw Permalink Normal View History

2019-03-28 18:04:38 +03:00
#!/usr/bin/env python
"""
dbgtool.py - Portable executable to ASCII debug script converter
2023-01-03 01:24:59 +03:00
Copyright (c) 2006-2023 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
2019-01-22 03:20:27 +03:00
from __future__ import print_function
import os
import sys
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:
2019-01-22 03:20:27 +03:00
print("ERROR: the provided input file '%s' is too big for debug.exe" % inputFile)
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
2011-04-30 17:20:05 +04:00
fp = open(inputFile, "rb")
fileContent = fp.read()
for fileChar in fileContent:
2019-03-29 00:54:05 +03:00
unsignedFileChar = fileChar if sys.version_info >= (3, 0) else ord(fileChar)
if unsignedFileChar != 0:
counter2 += 1
if not scrString:
2011-04-30 17:20:05 +04:00
scrString = "e %0x %02x" % (counter, unsignedFileChar)
else:
scrString += " %02x" % unsignedFileChar
elif scrString:
2011-04-30 17:20:05 +04:00
script += "%s\n" % scrString
scrString = ""
2011-04-30 17:20:05 +04:00
counter2 = 0
counter += 1
if counter2 == 20:
2011-04-30 17:20:05 +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):
2019-01-22 03:20:27 +03: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:
2011-04-30 17:20:05 +04:00
fpOut = open(outputFile, "w")
sys.stdout = fpOut
sys.stdout.write(script)
sys.stdout.close()
else:
2019-01-22 03:20:27 +03:00
print(script)
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")
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) as ex:
parser.error(ex)
2011-04-30 17:20:05 +04:00
inputFile = args.inputFile
outputFile = args.outputFile
2009-01-20 00:26:02 +03:00
main(inputFile, outputFile)