forked from flabbergastedbd/nightfury
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhack_parser.py
More file actions
172 lines (152 loc) · 6.8 KB
/
Copy pathhack_parser.py
File metadata and controls
172 lines (152 loc) · 6.8 KB
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
import re
import hack_actions
from HTMLParser import HTMLParser
from bs4 import BeautifulSoup
class CustomHTMLParser(HTMLParser):
def __init__(self, taint):
self.stack = []
self.found = None
self.found_helper = None
self.taint = taint
self.trace = ''
HTMLParser.__init__(self)
def get_context(self):
return(self.found or 0, self.found_helper or 0)
def feed(self, data):
self._sink = data
temp_data = re.sub('[^\'"]', '', data)
temp_data = temp_data.replace('""', '')
temp_data = temp_data.replace("''", '')
if len(temp_data) > 0:
data += temp_data[::-1]
temp_data = re.sub('[^<>]', '', data)
temp_data = temp_data.replace('<>', '')
if temp_data == '<':
data += '>'
HTMLParser.feed(self, data)
def get_stack(self):
return(self.stack)
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
def handle_starttag(self, tag, attrs):
if not self.found:
temp_tag = None
if tag.startswith(self.taint):
self.found = 'start_tag_name'
self.trace = tag
elif self.taint in tag:
self.found = 'start_tag_attr'
self.trace = tag
if tag.replace(self.taint, '') in hack_actions.TAGS:
temp_tag = tag.replace(self.taint, '')
# New tag found, so override attributes
temp_attrs = []
for param, value in attrs:
if param.startswith(self.taint):
self.found = 'attr_param'
self.trace = param
elif self.taint in param:
self.found ='equal_delim'
if param in hack_actions.ATTR_PARAMS: self.found_helper = param
self.trace = param
if value and self.taint in value:
if value.startswith(self.taint):
if self._sink[self._sink.index(value) - 1] == '=':
# NOTE: Temporarily disabling to avoid string mess
# self.found = 'attr_value'
self.found = 'attr_value_start_delim'
else:
self.found = 'attr_value'
elif self.taint in value:
if self._sink[self._sink.index(value) - 1] == '=':
self.found = 'attr_delim'
else:
self.found = 'attr_value_end_delim'
self.trace = value
if param in hack_actions.ATTR_PARAMS: self.found_helper = param
self.trace = value
param = param.replace(self.taint, '')
value = value.replace(self.taint, '') if value else value
if param in hack_actions.ATTR_PARAMS and param not in [i[0] for i in temp_attrs]: # Only once can a param occur
if value and value in hack_actions.ATTR_VALUES:
temp_attrs.append([param, value])
else:
temp_attrs.append([param, None])
if temp_tag:
temp_attrs.sort(key=lambda x: x[0])
self.stack.append([temp_tag, temp_attrs])
def handle_endtag(self, tag):
if not self.found:
if tag.startswith(self.taint):
self.found = 'end_tag_name'
elif self.taint in tag:
self.found = 'end_tag_attr'
temp_tag = tag.replace(self.taint, '')
if temp_tag in hack_actions.TAGS:
self.stack.append([temp_tag, [['end', 1]]])
def handle_data(self, data):
if not self.found:
if self.taint in data:
self.trace = data
self.found = 'data'
def get_control_chars(self):
c_chars = ''
if self.found in ['attr_param', 'start_tag_attr', 'end_tag_attr', 'equal_delim', 'attr_value_start_delim', 'attr_delim']:
c_chars = c_chars + '>'
elif self.found == 'attr_value_end_delim':
c_chars = c_chars + '>'
possible_delim = self._sink[self._sink.index(self.trace) - 1]
if possible_delim in hack_actions.MASTER_CONTROL_CHARS and possible_delim != '=':
c_chars = possible_delim + c_chars
elif self.found in ['attr_value']:
c_chars = c_chars + '>'
possible_delim = self._sink[self._sink.index(self.trace) - 1]
if possible_delim in hack_actions.MASTER_CONTROL_CHARS and possible_delim != '=':
c_chars = possible_delim + c_chars
# Tested on "test(')', \"'\", {'taint':1}, \"(\", '\\'')"
# Following four loops will remove all the arguments except the ones with the trace
t = self.taint
s = self.trace
for m in re.findall(r"'(?:[^\\'\"]|(?:\\'))*'", s):
if t not in m:
s = s.replace(m, '')
for m in re.findall(r'"(?:[^\\"\']|(?:\\"))*"', s):
if t not in m:
s = s.replace(m, '')
if s.count('"') % 2 == 1:
for m in re.findall(r"'(?:[^\\']|(?:\\'))*'", s):
if t not in m:
s = s.replace(m, '')
for m in re.findall(r'"(?:[^\\"]|(?:\\"))*"', s):
if t not in m:
s = s.replace(m, '')
elif s.count("'") % 2 == 1:
for m in re.findall(r'"(?:[^\\"]|(?:\\"))*"', s):
if t not in m:
s = s.replace(m, '')
for m in re.findall(r"'(?:[^\\']|(?:\\'))*'", s):
if t not in m:
s = s.replace(m, '')
for c_plus, c_minus in hack_actions.COUNTER_CONTROL_CHARS.items():
for m in re.findall(r"\\%c[^\\%c]*\\%c" % (c_plus, c_minus, c_minus), s):
if t not in m:
s = s.replace(m, '')
temp_trace = s
temp_trace = temp_trace[temp_trace.index(self.taint)+len(self.taint):]
temp_control_chars = dict(hack_actions.COUNTER_CONTROL_CHARS)
temp_control_chars.update(hack_actions.MASTER_COUNTER_CONTROL_CHARS)
s = temp_trace
temp_trace = ''
for c in s:
if c in temp_control_chars.values():
temp_trace += c
c_chars = temp_trace + c_chars
return(c_chars)
if __name__ == '__main__':
sink = u'<keygen ><iframe/onload=popup=1;>'
parser = CustomHTMLParser('abcdef')
parser.feed(sink)
print(sink)
print(parser.get_control_chars())
print(parser.get_stack())
print(parser.get_context())