sqlmap/extra/cloak/cloak.py

91 lines
2.3 KiB
Python
Raw Normal View History

2019-03-28 18:04:38 +03:00
#!/usr/bin/env python
"""
2010-01-27 17:27:11 +03:00
cloak.py - Simple file encryption/compression utility
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://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
2019-03-28 18:04:38 +03:00
import struct
import sys
import zlib
from optparse import OptionError
from optparse import OptionParser
2019-03-29 00:54:05 +03:00
if sys.version_info >= (3, 0):
2019-03-28 17:14:16 +03:00
xrange = range
def hideAscii(data):
2019-03-28 18:04:38 +03:00
retVal = b""
for i in xrange(len(data)):
2019-03-28 18:04:38 +03:00
value = data[i] if isinstance(data[i], int) else ord(data[i])
retVal += struct.pack('B', value ^ (127 if value < 128 else 0))
return retVal
2015-08-31 15:27:47 +03:00
def cloak(inputFile=None, data=None):
if data is None:
with open(inputFile, "rb") as f:
data = f.read()
2015-08-31 15:27:47 +03:00
return hideAscii(zlib.compress(data))
2015-08-31 15:27:47 +03:00
def decloak(inputFile=None, data=None):
if data is None:
with open(inputFile, "rb") as f:
data = f.read()
try:
2015-08-31 15:27:47 +03:00
data = zlib.decompress(hideAscii(data))
2019-03-28 18:04:38 +03:00
except Exception as ex:
print(ex)
2019-01-22 03:20:27 +03:00
print('ERROR: the provided input file \'%s\' does not contain valid cloaked content' % inputFile)
sys.exit(1)
finally:
f.close()
return data
def main():
usage = '%s [-d] -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:
parser.add_option('-d', dest='decrypt', action="store_true", help='Decrypt')
parser.add_option('-i', dest='inputFile', help='Input file')
parser.add_option('-o', dest='outputFile', help='Output file')
(args, _) = parser.parse_args()
if not args.inputFile:
parser.error('Missing the input file, -h for help')
except (OptionError, TypeError) as ex:
parser.error(ex)
2010-01-27 17:27:11 +03:00
if not os.path.isfile(args.inputFile):
2019-01-22 03:20:27 +03:00
print('ERROR: the provided input file \'%s\' is non existent' % args.inputFile)
sys.exit(1)
if not args.decrypt:
data = cloak(args.inputFile)
else:
data = decloak(args.inputFile)
if not args.outputFile:
if not args.decrypt:
args.outputFile = args.inputFile + '_'
else:
args.outputFile = args.inputFile[:-1]
f = open(args.outputFile, 'wb')
f.write(data)
f.close()
if __name__ == '__main__':
2010-06-10 01:43:22 +04:00
main()