Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/extra/cloak/cloak.py
2992 views
1
#!/usr/bin/env python
2
3
"""
4
cloak.py - Simple file encryption/compression utility
5
6
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
7
See the file 'LICENSE' for copying permission
8
"""
9
10
from __future__ import print_function
11
12
import os
13
import struct
14
import sys
15
import zlib
16
17
from optparse import OptionError
18
from optparse import OptionParser
19
20
if sys.version_info >= (3, 0):
21
xrange = range
22
ord = lambda _: _
23
24
KEY = b"E6wRbVhD0IBeCiGJ"
25
26
def xor(message, key):
27
return b"".join(struct.pack('B', ord(message[i]) ^ ord(key[i % len(key)])) for i in range(len(message)))
28
29
def cloak(inputFile=None, data=None):
30
if data is None:
31
with open(inputFile, "rb") as f:
32
data = f.read()
33
34
return xor(zlib.compress(data), KEY)
35
36
def decloak(inputFile=None, data=None):
37
if data is None:
38
with open(inputFile, "rb") as f:
39
data = f.read()
40
try:
41
data = zlib.decompress(xor(data, KEY))
42
except Exception as ex:
43
print(ex)
44
print('ERROR: the provided input file \'%s\' does not contain valid cloaked content' % inputFile)
45
sys.exit(1)
46
finally:
47
f.close()
48
49
return data
50
51
def main():
52
usage = '%s [-d] -i <input file> [-o <output file>]' % sys.argv[0]
53
parser = OptionParser(usage=usage, version='0.2')
54
55
try:
56
parser.add_option('-d', dest='decrypt', action="store_true", help='Decrypt')
57
parser.add_option('-i', dest='inputFile', help='Input file')
58
parser.add_option('-o', dest='outputFile', help='Output file')
59
60
(args, _) = parser.parse_args()
61
62
if not args.inputFile:
63
parser.error('Missing the input file, -h for help')
64
65
except (OptionError, TypeError) as ex:
66
parser.error(ex)
67
68
if not os.path.isfile(args.inputFile):
69
print('ERROR: the provided input file \'%s\' is non existent' % args.inputFile)
70
sys.exit(1)
71
72
if not args.decrypt:
73
data = cloak(args.inputFile)
74
else:
75
data = decloak(args.inputFile)
76
77
if not args.outputFile:
78
if not args.decrypt:
79
args.outputFile = args.inputFile + '_'
80
else:
81
args.outputFile = args.inputFile[:-1]
82
83
f = open(args.outputFile, 'wb')
84
f.write(data)
85
f.close()
86
87
if __name__ == '__main__':
88
main()
89
90