sqlmap/extra/cloak/cloak.py

89 lines
2.2 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
2022-01-03 13:30:34 +03:00
Copyright (c) 2006-2022 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
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
2020-06-14 22:23:55 +03:00
ord = lambda _: _
2019-03-28 17:14:16 +03:00
2021-02-19 13:08:14 +03:00
KEY = b"ENWsCymUeJcXqSbD"
2020-06-14 22:23:55 +03:00
def xor(message, key):
return b"".join(struct.pack('B', ord(message[i]) ^ ord(key[i % len(key)])) for i in range(len(message)))
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()
2020-06-14 22:23:55 +03:00
return xor(zlib.compress(data), KEY)
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:
2020-06-14 22:23:55 +03:00
data = zlib.decompress(xor(data, KEY))
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]
2020-06-14 22:23:55 +03:00
parser = OptionParser(usage=usage, version='0.2')
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()