public
Last active

Random string generator using regex-like expressions

  • Download Gist
fuzzex.py
Python
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
#!/usr/bin/env python
# Copyright (C) 2010, Scott W. Dunlop <swdunlop at gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
'''
Generating random strings from regex-like expressions.
 
Fuzzex generates sequences of random characters derived from a production
expression with a grammar approximately identical to that used by Regular
Expressions. For the majority of regular expressions, R, any product
generated by a Fuzzex for R would be a satisfactory match.
 
This is intended for data-driven testing (fuzzing) of input validators and
parsers that use regex or regex-like semantics.
 
Example, Generates a Random Email Address:
-- fuzzex.generate( "[a-zA-Z0-9._]+@[a-zA-Z0-9._]+\\\\.[a-z]+" )
 
Where Fuzzex Breaks From Regex:
-- Fuzzex does not support "non-greedy" repetition. (e.g. "a*?")
-- Fuzzex does not currently support "non-capturing" groups.
-- Fuzzex is derived from the "Extended Regex" dialect most frequently used.
-- Fuzzex is 8-bit ASCII-oriented; this matches Python's RE, but not some others.
 
Where Fuzzex Is Not The Right Tool: (Go use Sulley or Peach)
-- Fuzzing semantic logic; e.g. it might generate data that looks like BASE-64,
but it might not decode.
 
Special Thanks to Ryan O'Horo and Mattias Brutti for pointing out various
alternate implementations of this idea in Perl and C#.
'''
 
import re, sys, random
 
def op_dat( data ):
def op_dat_fn( vm ):
vm.write( data )
return op_dat_fn
 
def op_seq( seq ):
def op_seq_fn( vm ):
for op in seq:
op( vm )
return True
return op_seq_fn
 
def op_alt( alt ):
def op_alt_fn( vm ):
vm.choose( alt )( vm )
return op_alt_fn
 
def op_set( ix, op ):
def op_set_fn( vm ):
with vm.capture( ix ): op( vm )
return op_set_fn
 
def op_ref( ix ):
def op_ref_fn( vm ):
vm.write( vm.group( ix ) )
return op_ref_fn
 
def op_nul( ):
def op_nul_fn( vm ):
pass
return op_nul_fn
 
def op_rpt( op, m, n ):
if m == n:
def op_fix_rpt_fn( vm ):
for i in range( 0, m ):
op( vm )
return op_fix_rpt_fn
if m > n: n, m = m, n
r = n - m
 
def op_rpt_fn( vm ):
for i in range( 0, m + vm.choose( r ) ):
op( vm )
 
return op_rpt_fn
 
def op_opt( op ):
def op_opt_fn( vm ):
if vm.choose( 2 ): op( vm )
return op_opt_fn
 
def op_rng( s ):
s = ''.join( s )
def op_rng_fn( vm ):
vm.write( vm.choose( s ) )
return op_rng_fn
 
any_op = op_rng( map( chr, range( 0, 255 ) ) )
 
def mk_seq_op( seq ):
"unifies a sequence of 0 to N requirements into a minimal operation"
if not seq: return op_nul( )
if callable( seq ): return seq
if isinstance( seq, str ): return op_dat( seq )
 
out = []; buf = ''
for op in seq:
if isinstance( op, str ):
buf += op
else:
if buf:
out.append( op_dat( buf ) )
buf = ''
if op:
out.append( op )
if buf:
out.append( op_dat( buf ) )
 
if len( out ) == 1:
return out[0]
else:
return op_seq( out )
 
class Error( Exception ):
pass
 
escapes = {
'n' : '\n',
'r' : '\r',
't' : '\t',
'v' : '\v',
'0' : '\0'
}
 
rx_octet = re.compile( '([0-8][0-8][0-8]|[0])' )
rx_refer = re.compile( '([0-9][0-9]?)')
rx_range = re.compile( '[^\\\\]')
rx_repeat = re.compile( '([0-9]+)(,[0-9]+)?\\}' )
 
class Compiler:
def __init__( self, src ):
self.src = src
self.ofs = 0
self.opens = 0
self.closes = 0
self.limit = 4
 
def parse_rpt( self, op ):
m = rx_repeat.match( self.src, self.ofs )
if not m:
raise Error( '"{" followed without matching "}"') #TODO: Python ignores this silently.
self.ofs = m.end( )
if not m.group(2):
n = int( m.group( 1 ) )
return op_rpt( op, n, n )
n = int( m.group(2)[1:] )
m = int( m.group(1) )
return op_rpt( op, m, n )
 
def parse_rng( self ):
end = len( self.src )
if self.ofs >= end:
raise Error( '"[" at end of expression' )
 
if self.src[self.ofs] == '^':
self.ofs += 1
x = self.parse_rng_items( )
s = set( )
for i in range( 0, 127 ): #TODO: configurable
c = chr( i )
if c not in x: s.add( c )
else:
s = self.parse_rng_items( )
 
return op_rng( s )
 
def parse_rng_items( self ):
end = len( self.src )
s = set()
while self.ofs < end:
ch = self.src[ self.ofs ]
if ch == ']':
self.ofs += 1
return s
 
a = self.parse_rng_item( )
if self.src[ self.ofs ] == '-':
self.ofs += 1
b = self.parse_rng_item( )
for i in range( ord( a ), ord( b ) + 1 ):
ch = chr( i )
s.add( ch )
else:
s.add( a )
 
raise Error( 'unmatched "["' )
 
def parse_rng_item( self ):
end = len( self.src )
if self.ofs >= end: raise Error( '"[" at end of expression' )
ch = self.src[ self.ofs ]
self.ofs += 1
if ch == '\\':
if self.ofs >= end: raise Error( '"\\" at end of expression' )
 
m = rx_octet.match( self.src, self.ofs )
if m:
self.ofs = m.end( )
return chr( int( m.group(1), 8 ) )
else:
return ch
 
def parse_esc( self ):
if self.ofs >= len( self.src ):
raise Error( '"\\" at end of expression' )
 
m = rx_octet.match( self.src, self.ofs )
if m:
self.ofs = m.end( )
return op_data( chr( int( m.group(1), 8 ) ) )
m = rx_refer.match( self.src, self.ofs )
if m:
self.ofs = m.end( )
ix = int( m.group( 1 ), 8 )
if ix > self.closes:
raise Error( 'illegal forward reference' ) # not that there are any legal ones.
return op_ref( ix )
ch = self.src[self.ofs]
self.ofs += 1
return escapes.get( ch, ch )
 
def parse_expr( self, inner = False ):
"parses a possibly branched expression yielding none or an operation"
opt = [] # We start with an empty sequence of requirements.
opts = [opt] # And a empty list of branches.
end = len( self.src )
 
while self.ofs < end:
ch = self.src[ self.ofs ]
self.ofs += 1
 
if ch == '|':
opt = []
opts.append( opt )
elif ch =='(':
self.opens += 1
opt.append( op_set( self.opens, self.parse_expr( True ) ) )
self.closes += 1
elif ch == ')':
if not inner: raise Error( 'unmatched ")"' )
inner = False
break
elif ch == '\\':
opt.append( self.parse_esc( ) )
elif ch == '.':
opt.append( any_op )
elif ch == '*':
if not opt: raise Error( 'nothing to repeat' )
#TODO: Catch and Report multiple-repeat.
opt[-1] = op_rpt( mk_seq_op( opt[-1] ), 0, self.limit )
elif ch == '+':
if not opt: raise Error( 'nothing to repeat' )
#TODO: Catch and Report multiple-repeat.
opt[-1] = op_rpt( mk_seq_op( opt[-1] ), 1, self.limit )
elif ch == '?':
if not opt: raise Error( 'nothing to repeat' )
#TODO: Catch and Report multiple-repeat.
opt[-1] = op_opt( mk_seq_op( opt[-1] ) )
elif ch == '[':
opt.append( self.parse_rng( ) )
elif ch == '{':
if not opt: raise Error( 'nothing to repeat' )
#TODO: Catch and Report multiple-repeat.
opt[-1] = self.parse_rpt( mk_seq_op( opt[-1] ) )
else:
opt.append( ch )
if inner:
raise Error( 'unmatched "("' )
 
opts = filter( lambda x:x, opts )
if not opts:
return op_nul( )
elif len( opts ) == 1:
return mk_seq_op( opt )
else:
return op_alt( map( mk_seq_op, opts ) )
class Generator:
def __init__( self, root ):
if not root: raise Exception #TODO
self.root = root
 
def generate( self, seed = None, vm = None ):
if vm is None: vm = Vm( seed )
self.root( vm )
return str( vm )
 
class Vm:
def __init__( self, seed = None ):
self.rng = random.WichmannHill( seed )
self.out = []
self.groups = {}
def choose( self, field ):
"selects a random value from 0 .. field - 1, or the list"
if isinstance( field, int ):
return self.rng.randint( 0, field )
else:
return self.rng.choice( field )
def write( self, data ):
self.out.append( data )
 
def group( self, tag ):
return self.groups.get( tag, '' )
 
def capture( self, tag ):
return Capture( self, tag )
 
def __str__( self ):
return ''.join( self.out )
 
def __repr__( self ):
return '<fuzzex.vm %r>' % str( self )
 
class Capture:
def __init__( self, vm, ref ):
self.vm = vm
self.ref = ref
def __enter__( self ):
self.out = self.vm.out
self.vm.out = []
 
def __exit__( self, errtyp, errval, errtb ):
if errtyp or errval: return False
 
vm = self.vm
data = ''.join( vm.out )
vm.out = self.out
vm.write( data )
vm.groups[self.ref] = data
 
def generate( fx, seed = None, vm = None ):
if isinstance( fx, str ):
fx = compile( fx )
return fx.generate( seed, vm )
 
def compile( data ):
return Generator( Compiler( data ).parse_expr( ) )
 
def test( ex, seed = None ):
r = re.compile( '^' + ex + '$' )
data = generate( ex, seed )
k = r.match( data )
print >>sys.stderr, 'TESTING: %s, RESULT: %r, %s' % (
ex, data, "PASSED" if k else "FAILED"
)
return k
 
def test_batch( seed = None ):
return ( test( 'a' )
and test( 'a|b', seed )
and test( 'a|b|c', seed )
and test( 'aa', seed )
and test( 'aa|b', seed )
and test( 'aa|bb', seed )
and test( 'aa|bb|c', seed )
and test( 'a|bb|c', seed )
and test( 'a|bb|cc', seed )
and test( '(a|bb|cc)z\\1', seed )
and test( 'a?b', seed )
and test( 'a*b', seed )
and test( 'a+b', seed )
and test( '[a]', seed )
and test( '[-a-c]', seed )
and test( '[^a-zA-Z0-9]', seed )
and test( "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)([?]([^#]*))?(#(.*))?", seed )
and test( "[a-zA-Z0-9._]+@[a-z0-9._]+\\.[a-z]+", seed )
and test( "[a-z]{3,8}", seed )
and test( ".{3,8}", seed )
)
 
__all__ = [
'compile', 'generate', 'Generator'
]
 
if __name__ == '__main__':
test_batch( )
 
#TODO: report error for []

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.