|
| 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) 2006-2009 Bernardo Damele A. G. <[email protected]> |
| 9 | + and Daniele Bellucci <[email protected]> |
| 10 | +
|
| 11 | +sqlmap is free software; you can redistribute it and/or modify it under |
| 12 | +the terms of the GNU General Public License as published by the Free |
| 13 | +Software Foundation version 2 of the License. |
| 14 | +
|
| 15 | +sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY |
| 16 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 18 | +details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU General Public License along |
| 21 | +with sqlmap; if not, write to the Free Software Foundation, Inc., 51 |
| 22 | +Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +import os |
| 28 | +import sys |
| 29 | +import struct |
| 30 | + |
| 31 | +from optparse import OptionError |
| 32 | +from optparse import OptionParser |
| 33 | + |
| 34 | + |
| 35 | +def convert(inputFile, outputFile): |
| 36 | + fileStat = os.stat(inputFile) |
| 37 | + fileSize = fileStat.st_size |
| 38 | + |
| 39 | + if fileSize > 65280: |
| 40 | + print 'ERROR: the provided input file \'%s\' is too big for debug.exe' % inputFile |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + script = 'n %s\r\nr cx\r\n' % os.path.basename(inputFile.replace('.', '_')) |
| 44 | + script += "%x\r\nf 0100 ffff 00\r\n" % fileSize |
| 45 | + scrString = "" |
| 46 | + counter = 256 |
| 47 | + counter2 = 0 |
| 48 | + |
| 49 | + fp = open(inputFile, 'rb') |
| 50 | + fileContent = fp.read() |
| 51 | + |
| 52 | + for fileChar in fileContent: |
| 53 | + unsignedFileChar = struct.unpack('B', fileChar)[0] |
| 54 | + |
| 55 | + if unsignedFileChar != 0: |
| 56 | + counter2 += 1 |
| 57 | + |
| 58 | + if not scrString: |
| 59 | + scrString = "e %0x %02x" % (counter, unsignedFileChar) |
| 60 | + else: |
| 61 | + scrString += " %02x" % unsignedFileChar |
| 62 | + elif scrString: |
| 63 | + script += "%s\r\n" % scrString |
| 64 | + scrString = "" |
| 65 | + counter2 = 0 |
| 66 | + |
| 67 | + counter += 1 |
| 68 | + |
| 69 | + if counter2 == 20: |
| 70 | + script += "%s\r\n" % scrString |
| 71 | + scrString = "" |
| 72 | + counter2 = 0 |
| 73 | + |
| 74 | + script += "w\r\nq\r\n" |
| 75 | + |
| 76 | + return script |
| 77 | + |
| 78 | + |
| 79 | +def main(inputFile, outputFile): |
| 80 | + if not os.path.isfile(inputFile): |
| 81 | + print 'ERROR: the provided input file \'%s\' is not a regular file' % inputFile |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + script = convert(inputFile, outputFile) |
| 85 | + |
| 86 | + if outputFile: |
| 87 | + fpOut = open(outputFile, 'w') |
| 88 | + sys.stdout = fpOut |
| 89 | + sys.stdout.write(script) |
| 90 | + sys.stdout.close() |
| 91 | + else: |
| 92 | + print script |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + usage = '%s -i <input file> [-o <output file>]' % sys.argv[0] |
| 97 | + parser = OptionParser(usage=usage, version='0.1') |
| 98 | + |
| 99 | + try: |
| 100 | + parser.add_option('-i', dest='inputFile', help='Input binary file') |
| 101 | + |
| 102 | + parser.add_option('-o', dest='outputFile', help='Output debug.exe text file') |
| 103 | + |
| 104 | + (args, _) = parser.parse_args() |
| 105 | + |
| 106 | + if not args.inputFile: |
| 107 | + parser.error('Missing the input file, -h for help') |
| 108 | + |
| 109 | + except (OptionError, TypeError), e: |
| 110 | + parser.error(e) |
| 111 | + |
| 112 | + inputFile = args.inputFile |
| 113 | + outputFile = args.outputFile |
| 114 | + |
| 115 | + script = main(inputFile, outputFile) |
0 commit comments