forked from JohnTroony/Scriptology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorry.py
99 lines (67 loc) · 2.6 KB
/
xorry.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
#!/usr/bin/python
import optparse
import sys
class xorOperation():
''' Xor Operations for Obfuscation and deObfuscation of strings.'''
def __init__(self, stringInput, keyId):
self.stringInput = stringInput
self.keyId = keyId
def obfuscate(self):
'''XOR Operation on a plain-text using the key id supplied.'''
plaintext = self.stringInput
keyid = self.keyId
encoded = ""
for i in range(0, len(plaintext), len(keyid)):
start = i
end = i + len(keyid)
for x, y in zip(plaintext[start:end], keyid):
encoded += chr(ord(x) ^ ord(y))
return encoded
def deobfuscate(self):
'''Reverse XOR Operation on an obfuscated string
using key id supplied.'''
obfuscated = self.stringInput
keyid = self.keyId
decoded = ""
for i in range(0, len(obfuscated), len(keyid)):
start = i
end = i + len(keyid)
for x, y in zip(obfuscated[start:end], keyid):
decoded += chr(ord(x) ^ ord(y))
return decoded
def main():
'''Main function to run if xorry.py is executed. Otherwise if imported,
there is no need to run this function.'''
parser = optparse.OptionParser(
'xorry.py -m de/ob -s string -k key ')
parser.add_option('-m', dest='mode',
type='string',
help='Mode of Operation (ob or de)')
parser.add_option('-s', dest='plaintext', type='string',
help='String for manipulation')
parser.add_option('-k', dest='keyid', type='string',
help='Key for XOR Operation')
(options, args) = parser.parse_args()
mode = options.mode
plaintext = options.plaintext
keyid = options.keyid
if (mode is None) | (plaintext is None) | (keyid is None):
print(parser.usage)
sys.exit(0)
elif (mode == "de"):
hiddenBin = plaintext.decode("hex")
keyBin = keyid.decode("hex")
xorry = xorOperation(hiddenBin, keyBin)
decoded = xorry.deobfuscate()
deobufString = decoded.encode("hex")
print("Deobfuscated String : %s" % deobufString.decode("hex"))
print("Key Used : %s " % keyBin.encode("hex"))
print("Key String : %s " % keyBin)
elif (mode == "ob"):
xorry = xorOperation(plaintext, keyid)
encoded = xorry.obfuscate()
obufString = encoded.encode("hex")
print("Obfuscated String : %s " % obufString)
print("Key Used : %s " % keyid.encode("hex"))
if __name__ == '__main__':
main()