tqbf / ruckus

A DOM-Inspired Ruby Smart Fuzzer

This URL has Read+Write access

ruckus / ruckus / mutator.rb
100644 319 lines (283 sloc) 9.87 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# === Mutator is a half-assed fuzzing framework intended to snap in to Ruckus.
# Mutator provides a decorator-style interface to fuzzed integers and strings,
# so that you can chain them to get strings that grow and include metacharacters
# with X probability, etc.
#
# There are two core objects in here:
# * <tt>Mutator</tt> is a base type, like a String or an Integer, wrapped
# in an object that provides a <tt>permute</tt> method to change the
# value, and a stack of <tt>Modifier</tt> objects.
# * <tt>Modifier</tt> is an object that takes a value and changes it
# to do evil. Modifiers chain.
#
# This stuff is really kind of an afterthought; the real work in building
# fuzzer is getting the encoding right, so you can reach more of the target
# code. Ruckus hides the details of how strings and integers are actually
# encoded, so most of the "fuzzing" part just comes down to making evil
# looking strings. The Parsel framework takes care of encoding those evil
# strings properly so that a CIFS stack will read them.
#
module Ruckus
    module Mutator
 
        # Create me with a String, and I wrap the string, forwarding method
        # invocations to it. Call "permute" and I'll run through my Modifier
        # chain to derive a new value (for instance, tripling the number of
        # characters).
        #
        class Mutator
 
            # <tt>base</tt> is usually a String (like "helu") or Fixnum.
            # Stack is a set of modifier objects (or class names, if you
            # just want to use the defaults). A Mutator with an empty
            # Modifier stack doesn't do anything and behaves just like an
            # ordinary String or Fixnum.
            #
            def initialize(base=nil, stack=[])
                @base = base
                @cur = @base
                @stack = stack.map do |o|
                    o = o.new if o.kind_of? Class
                    o
                end
            end
 
            def method_missing(meth, *args)
                @cur.send(meth, *args)
            end
 
            # A fuzzer clock tick; mess up the enclosed value.
            #
            def permute
                @cur = @stack.inject(@cur) {|cur, mod| mod << cur}
            end
        end
 
        # The guts; each Modifier class implements some way of screwing
        # with a value to catch bugs. Modifiers are all created with
        # keyword args; the base class catches:
        # now:: true/false (def: false) fire immediately, just once,
        # irrespective of probability, even if probability is
        # provided.
        # prob:: (def: 100) pctg chance this modifier will fire on this
        # tick
        # max_steps:: number of times to fire this modifier before it stops
        # and just starts acting like a no-op. -1 equals no max
        #
        class Modifier
            def initialize(opts={})
                @now = opts[:now] || false
                @prob = opts[:prob] || 100
                @max_steps = opts[:max_steps] || 700
                @cur = 0
                @opts = opts
            end
 
            # Should this fire, based on prob?
            #
            def go?
                if @now
                    @now = false
                    return true
                end
                rand(100) < @prob
            end
 
            # Base class stub does nothing. Subclasses override this method
            # to implement logic. Callers don't use this method, though: they
            # use operator<<.
            #
            def mod(x); x; end
 
            # This is what callers call. Subclasses do not override this
            # method. This implements probability and max-steps. How it
            # looks:
            #
            # str = modifier << str
            #
            def <<(x)
                return x if (@cur += 1) > @max_steps && @max_steps != -1
                return x if not go?
                mod(x)
            end
        end
 
        # Geometrically increases size.
        #
        # A
        # AA
        # AAAA
        # AAAAAAAA ... etc
        #
        class Multiplier < Modifier
 
            # Takes:
            # multiplier:: (def: 2) how much to multiply by.
            #
            def initialize(opts={})
                @step = opts[:multiplier] || 2
                super
            end
 
            def mod(x)
                x * @step
            end
        end
 
        # Adds fixed amounts of data.
        #
        # A
        # AA
        # AAA
        # AAAA ... etc
        #
        class Adder < Modifier
 
            # Takes:
            # base:: (def: "A") what to add
            # step:: (def: 100) how much of this to add at each step
            #
            def initialize(opts={})
                @base = opts[:base] || "A"
                @step = opts[:step] || 100
                super
            end
 
            def mod(x)
                if x.kind_of? String
                    x + (@base * @step)
                else
                    x + @step
                end
            end
        end
 
        # Path traversal metacharacters and keywords, cycled. Add new
        # ones to the STRINGS array.
        #
        class PathTraversal < Modifier
            STRINGS = [ "etc/passwd",
                        "etc/passwd\x00",
                        "etc/passwd%00",
                        "boot.ini",
                        "boot.ini\x00",
                        "boot.ini%00" ]
            def mod(x)
                x = x + ("../" * (@cur + 1)) + STRINGS[@cur % STRINGS.size]
                if (@cur % 2) == 0
                    x.gsub!("/", "\\")
                end
                return x
            end
        end
 
        # The format strings, cycled. Add new ones to the STRINGS array.
        #
        class FormatStrings < Modifier
            STRINGS = [ "%n", "%25n", "%s", "%x" ]
 
            def mod(x)
                x + STRINGS[@cur % STRINGS.size]
            end
        end
 
        # The most likely evil metachars, but if you're thorough, you
        # just try all non-isalnums.
        #
        class Metacharacters < Modifier
            STRINGS = [ ".", "/", "\\", "$", "-", "%", "$", ";",
                        "'", '"', "*", "\x00" ]
 
            def mod
                x = x + STRINGS[@cur % STRINGS.size]
                if opts[:hexify]
                    0.upto(x.size - 1) do |i|
                        x[i] = "%#{ x[i].to_s(16) }"
                    end
                end
                return x
            end
        end
 
        # Things that will break SQL queries.
        #
        class SQLStrings < Modifier
            STRINGS = [ "'sql0", "+sql1", "sql2;", "sql3 ;--", "(sql4)" ]
 
            def mod
                x + STRINGS[@cur % STRINGS.size]
            end
        end
 
        # Trivial XSS tickler.
        #
        class XSS < Modifier
            def mod
                x + "<script>alert(document.location);</script>"
            end
        end
 
        # Generate random numbers
        #
        class Random < Modifier
            def initialize(opts={})
                srand((@seed = opts[:seed])) if opts[:seed]
                super
                @max = opts[:max] || 0xFFFFFFFF
                if opts[:width]
                    @max = Numeric.mask_for(opts[:width])
                end
            end
 
            def mod(i)
                rand(@max)
            end
        end
 
        # Randomly sets the top bit of each byte, turning ASCII into
        # hi-ASCII.
        #
        class Hibit < Modifier
            def initialize(opts={})
                opts[:prob] ||= 50
                @width = opts[:width] || 32
                super
            end
 
            def mod(x)
                if x.kind_of? String
                    0.upto(x.size - 1) do |i|
                        x[i] |= 0x80 if go?
                    end
                else
                    x |= (0x80 << (@width - 8))
                end
                return x
            end
        end
 
        # Cache the starting value (this is meant to the be first modifier
        # in the chain, if you're using it) and randomly reset the string
        # back to that starting value.
        #
        class Reset < Modifier
            def initialize(opts={})
                opts[:prob] ||= 25
                super(opts.merge(:now => true))
            end
 
            def mod(x)
                (@orig ||= x.clone).clone
            end
        end
 
        # Randomize a string.
        #
        class Randomizer < Modifier
            def mod(x)
                0.upto(x.size-1) do |i|
                    x[i] = rand(0xff)
                end
                return x
            end
        end
 
        # Wrap Number with Mutator, make to_i work.
        #
        class Number < Mutator
            def initialize(base=0, stack=[]); super; end
            def to_i; @cur.to_i; end
        end
 
        # Wrap String with Mutator, make to_s work.
        #
        class Str < Mutator
            def initialize(base="", stack=[]); super; end
            def to_s(off=nil); @cur.to_s; end
        end
 
        class << self
            def random_int(opts={})
                Number.new(1, [Random.new(opts)])
            end
 
            def r8; random_int(:width => 8); end
            def r16; random_int(:width => 16); end
            def r32; random_int(:width => 32); end
            def r64; random_int(:width => 64); end
 
            def grostring(base="A", opts={})
                Str.new(base, [Reset, Adder.new(opts)])
            end
 
            def randstr(base="A")
                Str.new(base, [Reset, Randomizer])
            end
        end
    end
end