-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexor.py
executable file
·285 lines (259 loc) · 9.11 KB
/
complexor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
# complexor
import os
import sys
import argparse
from operator import xor
DEBUG = True
charSet = {}
charSetCheck = {}
file_size = 0
max_key_size = 1024
class FileTypes:
def __init__(self):
self.types = []
self.typeLookup = {}
class FileType:
def __init__(self):
self.id = None
self.fileMagic = []
self.name = ""
"""
Main -
in_file, out_file, key, offset, write_offset
"""
def main(args):
try:
pos = 0
keyHint = ''
begin_offset = 0
offset_chars = ''
detection_offset = -1
key = ''
# Input file
try:
in_file = open(args.in_file, 'rb')
except:
print 'Fatal Error: Could not open input file!'
sys.exit(0)
# Output file
try:
out_file = open(args.out_file, 'wb')
except:
print 'Fatal Error: Could not open output file!'
sys.exit(0)
# Need file size to make sure we can't offset too far
file_size = os.path.getsize(args.in_file)
if args.k:
key = args.k
len_key = len(key)
if len_key > max_key_size:
key[0:max_key_size - 1]
len_key = len(key)
print('BAD KEY: Key Too Large. Reducing size to maximum of ' +
max_key_size + '.')
if len_key < 1:
print 'BAD KEY: Zero-sized Key. Please provide a valid key!'
sys.exit(0)
else:
if file_size % len_key != 0:
print('BAD KEY: Key size doesn\'t match length of file.'
'Please provide a valid key!')
sys.exit(0)
if args.b:
begin_offset = args.b
if begin_offset > file_size:
begin_offset = file_size
# Read offset bytes
if begin_offset > 0:
offset_chars = in_file.read(begin_offset)
if args.w is True:
out_file.write(offset_chars)
pos = pos + begin_offset
offset_chars = ''
if args.x and not args.k:
detection_offset = args.x
if detection_offset > file_size:
detection_offset = file_size
# Attempt to determine key
if detection_offset >= 0:
# Check against known characters for executable
keyHint = checkHead(in_file.read(2))
if DEBUG is True:
print 'KeyHint:"' + ''.join(keyHint) + '"'
in_file.seek(file_size - detection_offset)
charSet = findKeys(in_file, keyHint, detection_offset)
key = findRepeatKey(charSet, keyHint, detection_offset)
if DEBUG is True:
print 'RepeatedKey: ' + unicode(key)
key = findLongKey(charSet, keyHint, detection_offset)
if DEBUG is True:
print 'LongestKey: ' + unicode(key)
if key != '':
key = ''.join(keyHint) + key
print 'DETECT KEY: Found Key \'' + key + '\''
else:
print('DETECT KEY: Failed to detect key! '
'Try a larger detection offset.')
if key != '':
# Key as array of characters
bKey = list(key)
key_len = len(key)
in_file.seek(begin_offset)
byte = ''
byte = in_file.read(1)
pos = 0
while byte != '':
key_cur = bKey[pos % key_len]
# Compare
xor_out = chr(xor(ord(byte), ord(key_cur)))
out_file.write(xor_out)
# print 'Byte=' + byte + ';Key=' + key_cur + ';XOR:' + xor_out
byte = in_file.read(1)
pos += 1
print('Done! ' + str(pos) + ' bytes written to ' +
args.out_file + '.')
in_file.close
out_file.close
except Exception as e:
print('Error:' + str(type(e)) + ':' + str(e) +
' at line: {}'.format(sys.exc_info()[-1].tb_lineno))
in_file.close
out_file.close
sys.exit(0)
def checkHead(first_chars):
exeHead = ['M', 'Z']
keyStart = ['', '']
if DEBUG is True:
print 'FirstChars:' + first_chars
if len(first_chars) == 2:
keyStart[0] = chr(xor(ord(exeHead[0]), ord(first_chars[0])))
keyStart[1] = chr(xor(ord(exeHead[1]), ord(first_chars[1])))
return keyStart
def findKeys(in_file, keyStart, offset):
try:
file_size = os.path.getsize(args.in_file)
keyChar = in_file.read(1)
keyBuffer = ''
keyBuffer = keyBuffer + keyChar
if int(file_size/offset) % 2 == 0:
mod_factor = 2
else:
mod_factor = 1
in_file.seek(file_size - offset)
while keyChar != '':
keyLen = len(keyBuffer)
if keyLen >= 2:
if(keyBuffer[keyLen - 2:] == ''.join(keyStart) and keyLen %
mod_factor == 0):
# and file_size - (keyLen * int(offset/keyLen)) > 0:
keyBuffer = ''
if keyChar in charSet:
charSet[keyChar] += 1
else:
charSet[keyChar] = 1
if keyBuffer in charSet:
charSet[keyBuffer] += 1
else:
charSet[keyBuffer] = 1
keyChar = in_file.read(1)
keyBuffer = keyBuffer + keyChar
debugCharSet(charSet)
keys = charSet.keys()
values = charSet.values()
kLen = len(keys) - 1
for k in range(kLen, -1, -1):
key_length = len(keys[k])
if key_length > 0 and key_length <= offset:
seek = file_size - (key_length * int(offset/key_length))
if seek > 0:
in_file.seek(seek)
keyChar = in_file.read(key_length)
while keyChar != '':
if keys[k] == keyChar:
if keys[k] not in charSetCheck:
charSetCheck[keys[k]] = 1
else:
charSetCheck[keys[k]] += 1
keyChar = in_file.read(key_length)
debugCharSet(charSetCheck)
keyCheckKeys = charSetCheck.keys()
keyCheckValues = charSetCheck.values()
return charSetCheck
except Exception as e:
print('Error:' + str(type(e)) + ':' + str(e) +
' at line: {}'.format(sys.exc_info()[-1].tb_lineno))
def findLongKey(charSet, keyStart, offset):
keys = charSet.keys()
values = charSet.values()
kLen = len(keys) - 1
mostMatches = 0
longestKey = -1
if int(file_size/offset) % 2 == 0:
mod_factor = 2
else:
mod_factor = 1
for k in range(kLen, -1, -1):
len_key = len(keys[k])
if len_key > len(keys[longestKey]) and len_key % mod_factor == 0:
if values[k] >= int(file_size/offset):
if file_size % len_key == 0:
longestKey = k
return keys[longestKey]
def findRepeatKey(charSet, keyStart, offset):
keys = charSet.keys()
values = charSet.values()
kLen = len(keys) - 1
mostMatches = 0
repeatKey = -1
keyOut = ['']
if int(file_size/offset) % 2 == 0:
mod_factor = 2
else:
mod_factor = 1
for k in range(kLen, -1, -1):
len_key = len(keys[k])
if (len_key >= 1 and len_key % mod_factor == 0):
if values[k] > values[repeatKey]:
if values[k] >= int(file_size/offset):
if file_size % len_key == 0:
repeatKey = k
keyOut[0] = keys[repeatKey]
return keyOut
def debugCharSet(charSet):
if DEBUG is True:
keys = charSet.keys()
values = charSet.values()
print keys
print values
else:
return
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(
prog='complexor', usage='%(prog)s [in-file] [out-file] [options]',
description='XOR a file byte for byte based on'
' string key and output the result.'
)
parser.add_argument('in_file', type=str, help='File input')
parser.add_argument('out_file', type=str, help='File output')
parser.add_argument(
'-k', metavar='key', type=str,
help='Specify a key rather than detect it automatically'
)
parser.add_argument(
'-b', metavar='n', type=int,
help='Number of Bytes to skip as offset'
)
parser.add_argument(
'-w', metavar='bool', type=int, default=1,
help='Write offset bytes [0=off, 1=on(default)]'
)
parser.add_argument(
'-x', metavar='x', type=int, default=512,
help='Detect the key using this offset from the end of file'
)
args = parser.parse_args()
main(args)
except Exception as e:
sys.exit(0)