-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeobfuscate_Web_Log.py
171 lines (148 loc) · 6.6 KB
/
Deobfuscate_Web_Log.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
#Input a log file that contains encoded text
#this script will replace hex (0x), ASCII, Unicode percent encoded characters, and interpreted CHAR() commands.
#Copyright (c) 2021 Ryan Boyle randomrhythm@rhythmengineering.com.
#Copyright (c) 2012 Jenny Qian.
#All rights reserved.
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import string
import sys
import binascii
inputEncoding = "utf-8"
def build_cli_parser():
parser = OptionParser(usage="%prog [options]", description="Deobfuscate URL in log file")
parser.add_option("-i", "--input", action="store", default=None, dest="strinputfpath",
help="Path to log file that will be deobfuscated")
parser.add_option("-o", "--output", action="store", default=None, dest="stroutputfpath",
help="Deobfuscated log output file path")
parser.add_option("-l", "--loginteresting", action="store_true", default=False, dest="boolInteresting",
help="True or False value if interesting encoding should be logged")
return parser
def urldecode(url):
"""
Decode an encoded url. https://github.com/jennyq/urldecode
Usage:
url = 'http://www.example.com/this%20is%20my%20test%20%26%20%23%24'
decoded_url = urldecode(url)
"""
if url.find(" ") == -1:
url = url.replace("+", " ")
p = re.compile("%(?=[0-9A-F]{2})")
plist = p.split(url)
if len(plist) > 1:
for i in range(1, len(plist)):
plist[i] = '%s%s' % (chr(int((plist[i])[:2], 16)), (plist[i])[2:])
decoded_url = ''.join(plist)
return decoded_url
return url
def replaceUnicodeChar(strUurl):
boolFirstLine = True
strReturnWithChars = ""
if '%u' in strUurl:
urllist = strUurl.split('%u')
for s in urllist:
sTmpUnicode = s[:4]
if all(c in string.hexdigits for c in sTmpUnicode):
seq = ('\\u', sTmpUnicode )
strFunicode = ''.join(seq)
if len(s) > 4: #include everything else in string
seq = (strReturnWithChars,strFunicode.encode().decode('unicode_escape'), s[len(s)-4:])
else: #string was only the unicode char
seq = (strReturnWithChars,strFunicode.encode().decode('unicode_escape'))
strReturnWithChars = ''.join(seq)
else: #was not hex ... could be the first line or perhaps not a properly formated URL?
if boolFirstLine == True:
seq = (strReturnWithChars, s )
boolFirstLine = False
else:
seq = (strReturnWithChars, '%u', s )
strReturnWithChars = ''.join(seq )
else:
strReturnWithChars = strUurl
return strReturnWithChars
def HexDecode(strhURL, strHexIdentifier):
"""
replace hex variable with characters
"""
boolFirstLine = True
strHexDecoded = ""
if 'declare' in strhURL.lower() and '@' in strhURL and '=' in strhURL and 'set' in strhURL.lower() and strHexIdentifier in strhURL:
strTmpHex = ""
if strHexIdentifier in strhURL:
urllist = strhURL.split(strHexIdentifier)
for strTmpUlistItem in urllist:
strTmpHex = ""
if boolFirstLine == True: #first entry has no hex value
boolFirstLine = False
else: #check and parse hex value
for strTmpChar in strTmpUlistItem:
if all(c in string.hexdigits for c in strTmpChar):
strTmpHex = strTmpHex + strTmpChar
else:
break
if strTmpHex != "":
#print (strTmpUlistItem, strTmpHex)
intHexLen = len(strTmpHex)
if intHexLen > 1 and (intHexLen % 2) == 0: #ensure proper length for conversion
strHexDecoded = strHexDecoded + binascii.unhexlify(strTmpHex).decode('unicode_escape') + strhURL.replace(strHexDecoded + strHexIdentifier + strTmpHex, "", 1)
else:
strHexDecoded = strHexDecoded + strTmpUlistItem
else:
strHexDecoded = strHexDecoded + strTmpUlistItem
else:
strHexDecoded = strhURL
return strHexDecoded
def replaceChar(strEURL):
"""
replace CHAR() with character
"""
strCHARACTER = ""
if 'CHR(' in strEURL:
strCHARACTER = 'CHR('
elif 'CHAR(' in strEURL:
strCHARACTER = 'CHAR('
if strCHARACTER != "":
urllist = strEURL.split(strCHARACTER)
strReturnWithChars = ""
for s in urllist:
strTmpPart = ""
#print(s)
if ")" in s or strCHARACTER in s:
if len(s) > s.index(')') + 1:
tmpChrCode = s[:s.index(')')]
if tmpChrCode.isdigit():
if int(s[:s.index(')')]) < 257:
seq = (strReturnWithChars, chr(int(s[:s.index(')')])),s[s.index(')') +1 -len(s):])
strReturnWithChars = strTmpPart.join( seq )
else:
#print (s[:s.index(')')] + " is not a valid char number:" + strEURL)
seq = (strReturnWithChars,strCHARACTER,s[:s.index(')')] ,s[s.index(')') -len(s):])
strReturnWithChars = strTmpPart.join(seq )
else:
seq = (strReturnWithChars, s )
strReturnWithChars = strTmpPart.join(seq )
else:
seq = (strReturnWithChars, s )
strReturnWithChars = strTmpPart.join(seq )
else:
seq = (strReturnWithChars, s )
strReturnWithChars = strTmpPart.join(seq )
else:
strReturnWithChars = strEURL
return strReturnWithChars
def replaceString(strInputLine, strMatchText, strReplace):
strReplaceReturn = strInputLine
strManipulate = strInputLine[:-2]#dont match EOL chars
if strMatchText in strManipulate:
strReplaceReturn = strManipulate.replace(strMatchText, strReplace)
strReplaceReturn = strReplaceReturn + strInputLine[-2:] #add back EOL chars
return strReplaceReturn