forked from mgeeky/Stracciatella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
83 lines (63 loc) · 2.13 KB
/
encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
import sys
import base64
import argparse
from pathlib import Path
config = {
'xorkey' : 0,
'command' : '',
}
def parseOptions(argv):
global config
parser = argparse.ArgumentParser(prog = argv[0], usage='%(prog)s [options] <command|file>')
parser.add_argument('command', nargs='?', help = 'Specifies either a command or script file\'s path for encoding')
parser.add_argument('-x', '--xor', dest='xor', metavar = 'KEY', default = '0x00', type=str, help = 'Specifies command/file XOR encode key (one byte)')
parser.add_argument('-o', '--output', dest='output', metavar = 'PATH', type=str, help = '(optional) Output file. If not given - will echo output to stdout')
opts = parser.parse_args()
if len(argv) < 2:
parser.print_help()
sys.exit(1)
try:
if opts.xor:
config['xorkey'] = int(opts.xor, 16)
except:
print('[-] Incorrect xor key number format. Must be in Hex.')
sys.exit(1)
config['command'] = opts.command
if config['xorkey'] and config['xorkey'] != 0 and config['xorkey'] < 0 or config['xorkey'] > 0xff:
print('[-] XOR key must be in range <0, 0xff>')
sys.exit(1)
return (opts)
def getData():
my_file = Path(config['command'])
try:
if my_file.is_file():
with open(my_file, 'rb') as f:
return f.read()
except:
pass
return config['command']
def base64Encode(x):
return base64.b64encode(x)
def xorEncode(data, key):
xored = []
for byte in data:
xored.append(byte ^ ord(key))
return bytearray(xored)[:len(data)]
def main(argv):
(opts) = parseOptions(argv)
if not opts:
print('Options parsing failed.')
return False
data = getData()
out = data
if config['xorkey'] != 0:
out = base64Encode(xorEncode(out, ""+chr(config['xorkey'])))
if (opts.output):
with open(opts.output, 'wb') as f:
f.write(out)
print('[+] Written {} bytes to: {}'.format(len(out), opts.output))
else:
sys.stdout.write(out.decode())
if __name__ == '__main__':
main(sys.argv)