-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_xor.py
183 lines (169 loc) · 7.75 KB
/
smart_xor.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
#!/usr/bin/env python
__version__ = "0.0.1"
__date__ = "05.05.2012"
__author__ = "Joseph Zeranski"
__maintainer__ = "Joseph Zeranski"
__email__ = "madsc13ntist gmail.com"
__copyright__ = "Copyright 2012, " + __author__
__license__ = "MIT"
__status__ = "Prototype"
__credits__ = [""]
__description__= "Perform a conditional XOR on a file."
####################### MIT License #######################
# Copyright (c) 2012 Joseph Zeranski
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###########################################################
### Import Modules
import optparse
### Functions
def toint(data, convert_ascii=True):
'''
Convert str, hex, bin or int and return its numerical value.
'''
t = type(data)
#print("%s %s" % (t, data))
if t == int:
return data
if t == str and data.startswith('0x'):
return int(data, 16)
if t == str and data.startswith('0b'):
return int(data, 2)
try:
return int(ord(data))
except ValueError:
return ord(data)
return data
def smart_xor(filename, patched_filename, xor_key=0x0, ignore=[], offsets=[]):
'''
Perform an XOR on each byte whose value is not present in the 'ignore' list.
'''
with open(patched_filename, 'wb') as p:
with open(filename, 'rb') as fp:
byte = fp.read(1)
while byte != "":
i = toint(byte)
o = toint(fp.tell())
if i not in ignore:
if options.only_offset:
if o in offsets:
p.write(chr(i ^ xor_key))
elif o not in offsets:
p.write(chr(i ^ xor_key))
else:
p.write(byte)
if options.verbose:
if i not in ignore:
if options.only_offset:
if o in offsets:
print("XORing offset: %s" % hex(o))
byte = fp.read(1)
### If the script is being executed (not imported).
if __name__ == "__main__":
opt_parser = optparse.OptionParser()
opt_parser.usage = "%prog [options] args\n"
#''' Additional formatting for Meta-data ''''''''''''''''''
if __description__ not in ["", [""], None, False]:
opt_parser.usage += __description__ + "\n"
opt_parser.usage += "Copyright (c) 2012 " + __author__ + " <" + __email__ + ">"
if __credits__ not in ["", [""], None, False]:
opt_parser.usage += "\nThanks go out to "
if type(__credits__) == str:
opt_parser.usage += __credits__ + "."
elif type(__credits__) == list:
if len(__credits__) == 1:
opt_parser.usage += __credits__[0] + "."
else:
opt_parser.usage += ', '.join(__credits__[:-1]) + " and " + __credits__[-1] + "."
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
opt_parser.add_option("-v",
dest="verbose",
action = "store_true",
default = False,
help = "More verbose output regarding XORing.")
opt_parser.add_option("-f",
dest="outfile",
action = "store",
default = False,
help = "the filename to save the patched file to.")
opt_parser.add_option("-k",
dest="key",
action = "store",
default = False,
help = "the XOR key to use. example: 149, 0x95")
opt_parser.add_option("-o",
dest="offset",
action = "store",
default = False,
help = "offsets to skip regardless of byte value.\t\t\t\tSupports: comma seperated (no spaces). int, hex (ranges)\t\t\t\texample : 42,0x95,25-50,0x14-0x1A,250-0xFF")
opt_parser.add_option("-s",
dest="only_offset",
action = "store_true",
default = False,
help = "ONLY XOR offsets unless value is to be ignored. (used with '-o')")
opt_parser.add_option("-i",
dest="ignore",
action = "store",
default = False,
help = "byte values to ignore.\t\t\t\t\tSupports: comma seperated (no spaces). int, hex (ranges)\t\t\t\texample : 42,0x95,25-50,0x14-0x1A,250-0xFF")
(options, args) = opt_parser.parse_args()
if not args or not options.key:
opt_parser.print_help()
exit(1)
### Parse XOR key
if options.key:
options.key = toint(options.key)
if type(options.key) != int:
print("key value invalid: %s" % options.key)
exit(1)
### Parse byte values to ignore
if options.ignore:
ignore = []
for x in options.ignore.split(','):
if '-' in x:
y = x.split('-')
for n in range(toint(y[0]), toint(y[-1])):
ignore.append(n)
continue
if toint(x) is not None:
ignore.append(toint(x))
else:
print("ignore parameter invalid: %s" % x)
exit(1)
options.ignore = sorted(ignore)
### Parse offsets to skip
if options.offset:
offset = []
for x in options.offset.split(','):
if '-' in x:
y = x.split('-')
for n in range(toint(y[0]), toint(y[-1])):
offset.append(n)
continue
if toint(x) is not None:
offset.append(toint(x))
else:
print("offset parameter invalid: %s" % x)
exit(1)
options.offset = sorted(offset)
# Do things with your options and args.
for arg in args:
print("Using XOR key: %s" % hex(options.key))
if options.ignore:
print("Ignoring values: %s" % ", ".join([ hex(toint(x)) for x in options.ignore ]))
else:
options.ignore = []
if options.offset:
if not options.only_offset:
print("Ignoring offsets: %s" % ", ".join([ hex(toint(x)) for x in options.offset ]))
else:
print("Only XOR offsets: %s" % ", ".join([ hex(toint(x)) for x in options.offset ]))
else:
options.offset = []
if options.verbose:
print('-' * 30)
if options.outfile:
smart_xor(arg, options.outfile, options.key, options.ignore, options.offset)
else:
smart_xor(arg, arg + '.patched', options.key, options.ignore, options.offset)