-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transfuzz.py
235 lines (207 loc) · 11.6 KB
/
Transfuzz.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
#
# Transfuzz Extension for PortSwigger Burp Suite
#
# Transform payloads for fuzzing.
#
# How to use:
# 0. Install Jython in Burp Suite and load this extension in Burp Suite.
# 1. In Intruder > Positions, clear all markers and surround one character
# of payload to test with markers. Choose a Request that usually returns
# a valid/expected/working response.
# 2. In Intruder > Payloads, select Payload type = Extension generated,
# and then select this extension.
# 3. Launch the attack.
#
# Note: For fuzzing JSON payloads, disable URL encoding and you might want
# to add these two payload processing rules so that the JSON body does not break.
# - Match/replace: \\ with \\\\
# - Match/replace: " with \\"
#
# Copyright (c) 2019, Alexandre Hamelin <[email protected]>
#
# Distributed under the Apache 2.0 License.
try:
from burp import IBurpExtender, IIntruderPayloadGeneratorFactory, IIntruderPayloadGenerator
except ImportError:
print 'sorry this file must be loaded in Burp Suite'
raise SystemExit(1)
class FuzzEncodingsGenerator(IIntruderPayloadGenerator):
def __init__(self, callbacks):
self._morePayloads = True
self._generator = None
self._baseValue = None
self._nextValue = None
self._callbacks = callbacks
self.debug = False
self.log = self._callbacks.printOutput if self.debug else lambda x: None
self.log('Generator initialized')
def hasMorePayloads(self):
self.log('g.hasMorePayloads: more={0}, g={1}, nextvalue={2}'.format(
self._morePayloads, self._generator, self._nextValue))
if self._generator is None:
# special case on first call
# has not been initialized with the base value yet
return True
try:
self._nextValue = next(self._generator)
self.log('queued next value to return = ' + str(self._nextValue))
except StopIteration:
self._morePayloads = False
self.log('no next value!')
return self._morePayloads
def __iter__(self):
self.log('g.iter initialized with ' + str(self._baseValue))
def key(name):
key.count = getattr(key, 'count', 0) + 1
return '{0:03}'.format(key.count)
def num2aschex(val):
# 0..15 -> '30'..'66' (hex value in ascii)
return str(30 + val) if val < 10 else str(60 + val - 9)
# Ideally we'd use basic transformations and find a nice way to
# mix/chain/stack them for more flexibility.
#
# Chaining could be dumb (\u0041 -> %5c%30%30%34%31)
# or smarter (\u0041 -> %5cu0041 i.e. not all chars).
transforms = {
'url' : lambda val: ''.join(['%{0:02x}'.format(ord(c)) for c in val]),
'urlnib' : lambda val: ''.join(['%%{0}%{1}'.format(*map(num2aschex, divmod(ord(c), 16))) for c in val]),
'url+url' : lambda val: ''.join(['%25{0:02x}'.format(ord(c)) for c in val]),
'url+urlnib1' : lambda val: ''.join(['%25%{0}%{1}'.format(*map(num2aschex, divmod(ord(c), 16))) for c in val]),
'url+urlnib2' : lambda val: ''.join(['%%25{0}%25{1}'.format(*map(num2aschex, divmod(ord(c), 16))) for c in val]),
'urluni' : lambda val: ''.join(['%u{0:04x}'.format(ord(c)) for c in val]),
'qprint' : lambda val: ''.join(['={0:02x}'.format(ord(c)) for c in val]),
'hexexpr' : lambda val: ''.join(['0x{0:02x}'.format(ord(c)) for c in val]),
'octexpr' : lambda val: ''.join(['{0:03o}'.format(ord(c)) for c in val]),
'hexesc' : lambda val: ''.join(['\\x{0:02x}'.format(ord(c)) for c in val]),
'hexescesc' : lambda val: ''.join(['\\\\x{0:02x}'.format(ord(c)) for c in val]),
'jsuni' : lambda val: ''.join(['\\u{0:04x}'.format(ord(c)) for c in val]),
'jsuniesc' : lambda val: ''.join(['\\\\u{0:04x}'.format(ord(c)) for c in val]),
'jsunilong' : lambda val: ''.join(['\\U{0:08x}'.format(ord(c)) for c in val]),
'jsunilongesc' : lambda val: ''.join(['\\\\U{0:08x}'.format(ord(c)) for c in val]),
'xmlhex' : lambda val: ''.join(['&#x{0:x};'.format(ord(c)) for c in val]),
'xmldec' : lambda val: ''.join(['&#{0};'.format(ord(c)) for c in val]),
'xmldecamp' : lambda val: ''.join(['&#{0};'.format(ord(c)) for c in val]),
'xmldec+url' : lambda val: ''.join(['%26#{0};'.format(ord(c)) for c in val]),
'cdata' : lambda val: '<![CDATA[' + val + ']]>',
'cdatarev' : lambda val: ']]>' + val + '<![CDATA[',
'cdata+xmlhex' : lambda val: '<![CDATA[' + ''.join(['&#x{0:x};'.format(ord(c)) for c in val]) + ']]>',
'cdata+url' : lambda val: '<![CDATA[' + ''.join(['%{0:02x}'.format(ord(c)) for c in val]) + ']]>',
'cdata+urluni' : lambda val: '<![CDATA[' + ''.join(['%u{0:04x}'.format(ord(c)) for c in val]) + ']]>',
'cdata+jsuni' : lambda val: '<![CDATA[' + ''.join(['\\u{0:04x}'.format(ord(c)) for c in val]) + ']]>',
'oct' : lambda val: ''.join(['\\{0:03o}'.format(ord(c)) for c in val]),
'octesc' : lambda val: ''.join(['\\\\{0:03o}'.format(ord(c)) for c in val]),
'chrplus' : lambda val: '+'.join(['chr({0})'.format(ord(c)) for c in val]),
'chrplusq' : lambda val: "'+" + '+'.join(['chr({0})'.format(ord(c)) for c in val]) + "+'",
'chrplusqq' : lambda val: '"+' + '+'.join(['chr({0})'.format(ord(c)) for c in val]) + '+"',
'chrdot' : lambda val: '.'.join(['chr({0})'.format(ord(c)) for c in val]),
'chrdotq' : lambda val: "'." + '.'.join(['chr({0})'.format(ord(c)) for c in val]) + ".'",
'chrdotqq' : lambda val: '".' + '.'.join(['chr({0})'.format(ord(c)) for c in val]) + '."',
key('str') : lambda val: '"+"' + val + '"+"',
key('str') : lambda val: "'+'" + val + "'+'",
key('str') : lambda val: "'||'" + val + "'||'",
key('str') : lambda val: '"' + val + '"',
key('str') : lambda val: "'" + val + "'",
key('str') : lambda val: '`' + val + '`',
key('str') : lambda val: '/' + val + '/',
key('tmpl') : lambda val: '{{"' + val + '"}}',
key('tmpl') : lambda val: '${{"' + val + '"}}',
key('tmpl') : lambda val: '{{$"' + val + '"}}',
key('tmpl') : lambda val: '`${"' + val + '"}`',
key('tmpl') : lambda val: '{"' + val + '"}',
key('tmpl') : lambda val: '${"' + val + '"}',
key('tmpl') : lambda val: '{$"' + val + '"}',
key('tmpl') : lambda val: '[["' + val + '"]]',
key('tmpl') : lambda val: '$[["' + val + '"]]',
key('tmpl') : lambda val: '$["' + val + '"]',
key('tmpl') : lambda val: '<%="' + val + '"%>',
key('tmpl') : lambda val: '<@="' + val + '"@>',
key('tmpl') : lambda val: '<!="' + val + '"!>',
key('tmpl') : lambda val: '<#="' + val + '"#>',
key('tmpl') : lambda val: '<?="' + val + '"?>',
key('tmpl') : lambda val: '@{' + val + '}',
key('tmpl') : lambda val: '@{"' + val + '"}',
key('tmpl') : lambda val: '@{{' + val + '}}',
key('tmpl') : lambda val: '@{{"' + val + '"}}',
key('tmpl') : lambda val: '{#' + val + '#}',
key('tmpl') : lambda val: '{#=' + val + '#}',
key('tmpl') : lambda val: '{#"' + val + '"#}',
key('tmpl') : lambda val: '{#="' + val + '"#}',
key('tmpl') : lambda val: '{%' + val + '%}',
key('tmpl') : lambda val: '{%=' + val + '%}',
key('tmpl') : lambda val: '{%"' + val + '"%}',
key('tmpl') : lambda val: '{%="' + val + '"%}',
key('tmpl') : lambda val: '##' + val + '##',
key('tmpl') : lambda val: '@@' + val + '@@',
key('tmpl') : lambda val: '%%' + val + '%%',
key('tmpl') : lambda val: '!!' + val + '!!',
key('tmpl') : lambda val: '$$' + val + '$$',
}
try:
n = int(self._baseValue)
transforms.update({
key('int') : lambda val: 'Integer(' + val + ')',
key('int') : lambda val: 'int(' + val + ')',
key('int') : lambda val: 'Integer(' + val + ')',
key('int') : lambda val: 'int(' + val + ')',
key('int') : lambda val: '(' + val + ')',
key('int') : lambda val: val + '.0',
key('int') : lambda val: '+' + val,
key('int') : lambda val: '0x{0:x}'.format(n),
key('int') : lambda val: '0{0:o}'.format(n),
key('int') : lambda val: '0b{0:b}'.format(n),
key('int') : lambda val: '+0x{0:x}'.format(n),
key('int') : lambda val: '+0{0:o}'.format(n),
key('int') : lambda val: '+0b{0:b}'.format(n),
key('int') : lambda val: '16#{0:x}#'.format(n),
key('int') : lambda val: 'x"{0:x}"'.format(n),
key('int') : lambda val: '${0:x}'.format(n),
key('int') : lambda val: '16r{0:x}'.format(n),
key('int') : lambda val: '&H{0:x}'.format(n),
})
except ValueError:
pass
'''
try:
n = float(self._baseValue)
transforms.update({
'tmpl1' : lambda val: '{{1+' + str(n-1) + '}}',
'tmpl2' : lambda val: '{1+' + str(n-1) + '}',
'tmpl3' : lambda val: '${{1+' + str(n-1) + '}}',
'tmpl4' : lambda val: '${1+' + str(n-1) + '}',
})
except ValueError:
pass
'''
for tr in transforms.values():
yield tr(self._baseValue)
def getNextPayload(self, baseValue):
self.log('g.getnext: basevalue={0}, g={1}'.format(baseValue, self._generator))
if self._generator is None:
# special case on first call
self._baseValue = str(bytearray(baseValue)) # remember base value for iter()
self._generator = iter(self)
return next(self._generator) # return the first value immediately
return self._nextValue # otherwise, return the one that was queued
def reset(self):
self.log('g.reset')
self._morePayloads = True
del self._generator
del self._nextValue
del self._baseValue
class FuzzEncodingsGeneratorFactory(IIntruderPayloadGeneratorFactory):
def __init__(self, callbacks):
self._callbacks = callbacks
def getGeneratorName(self):
return 'Transfuzz'
def createNewInstance(self, intruderAttack):
# return a IIntruderPayloadGenerator object
# intruderAttack is a IIntruderAttack object
g = FuzzEncodingsGenerator(self._callbacks)
return g
class BurpExtender(IBurpExtender):
def registerExtenderCallbacks(self, callbacks):
# 'entrypoint'
callbacks.setExtensionName('Transfuzz')
factory = FuzzEncodingsGeneratorFactory(callbacks)
callbacks.registerIntruderPayloadGeneratorFactory(factory)
return