2011-08-04 23:59:32 +00:00
|
|
|
from boto.s3.connection import S3Connection
|
2011-08-19 21:54:24 +00:00
|
|
|
from boto.s3.key import Key
|
2011-08-04 00:00:02 +00:00
|
|
|
from optparse import OptionParser
|
2011-08-05 18:42:33 +00:00
|
|
|
from boto import UserAgent
|
2011-08-04 00:00:02 +00:00
|
|
|
from . import common
|
|
|
|
|
|
|
|
import traceback
|
2011-08-10 18:27:06 +00:00
|
|
|
import itertools
|
2011-08-04 00:00:02 +00:00
|
|
|
import random
|
|
|
|
import string
|
2011-08-10 21:39:25 +00:00
|
|
|
import struct
|
2011-08-08 23:51:10 +00:00
|
|
|
import yaml
|
2011-08-04 00:00:02 +00:00
|
|
|
import sys
|
2011-08-11 18:32:18 +00:00
|
|
|
import re
|
2011-08-04 00:00:02 +00:00
|
|
|
|
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
class DecisionGraphError(Exception):
|
|
|
|
""" Raised when a node in a graph tries to set a header or
|
|
|
|
key that was previously set by another node
|
|
|
|
"""
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
|
|
|
|
|
|
|
class RecursionError(Exception):
|
|
|
|
"""Runaway recursion in string formatting"""
|
|
|
|
|
|
|
|
def __init__(self, msg):
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{0.__doc__}: {0.msg!r}'.format(self)
|
|
|
|
|
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
def assemble_decision(decision_graph, prng):
|
|
|
|
""" Take in a graph describing the possible decision space and a random
|
|
|
|
number generator and traverse the graph to build a decision
|
2011-08-04 23:59:32 +00:00
|
|
|
"""
|
2011-08-09 18:56:38 +00:00
|
|
|
return descend_graph(decision_graph, 'start', prng)
|
|
|
|
|
|
|
|
|
|
|
|
def descend_graph(decision_graph, node_name, prng):
|
|
|
|
""" Given a graph and a particular node in that graph, set the values in
|
|
|
|
the node's "set" list, pick a choice from the "choice" list, and
|
|
|
|
recurse. Finally, return dictionary of values
|
|
|
|
"""
|
2011-08-09 22:44:25 +00:00
|
|
|
node = decision_graph[node_name]
|
|
|
|
|
2011-08-09 18:56:38 +00:00
|
|
|
try:
|
2011-08-10 20:26:00 +00:00
|
|
|
choice = make_choice(node['choices'], prng)
|
2011-08-11 19:25:13 +00:00
|
|
|
if choice == '':
|
|
|
|
decision = {}
|
|
|
|
else:
|
|
|
|
decision = descend_graph(decision_graph, choice, prng)
|
2011-08-09 18:56:38 +00:00
|
|
|
except IndexError:
|
|
|
|
decision = {}
|
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
for key, choices in node['set'].iteritems():
|
|
|
|
if key in decision:
|
|
|
|
raise DecisionGraphError("Node %s tried to set '%s', but that key was already set by a lower node!" %(node_name, key))
|
|
|
|
decision[key] = make_choice(choices, prng)
|
2011-08-11 18:32:18 +00:00
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
if 'headers' in node:
|
|
|
|
decision.setdefault('headers', [])
|
2011-08-11 18:32:18 +00:00
|
|
|
|
|
|
|
for desc in node['headers']:
|
2011-08-15 21:16:40 +00:00
|
|
|
try:
|
|
|
|
(repetition_range, header, value) = desc
|
|
|
|
except ValueError:
|
|
|
|
(header, value) = desc
|
|
|
|
repetition_range = '1'
|
|
|
|
|
|
|
|
try:
|
|
|
|
size_min, size_max = repetition_range.split('-', 1)
|
|
|
|
except ValueError:
|
|
|
|
size_min = size_max = repetition_range
|
|
|
|
|
|
|
|
size_min = int(size_min)
|
|
|
|
size_max = int(size_max)
|
|
|
|
|
2011-08-11 18:32:18 +00:00
|
|
|
num_reps = prng.randint(size_min, size_max)
|
2011-08-15 21:16:40 +00:00
|
|
|
if header in [h for h, v in decision['headers']]:
|
|
|
|
raise DecisionGraphError("Node %s tried to add header '%s', but that header already exists!" %(node_name, header))
|
2011-08-11 18:32:18 +00:00
|
|
|
for _ in xrange(num_reps):
|
|
|
|
decision['headers'].append([header, value])
|
|
|
|
|
2011-08-09 18:56:38 +00:00
|
|
|
return decision
|
2011-08-04 23:59:32 +00:00
|
|
|
|
|
|
|
|
2011-08-10 20:26:00 +00:00
|
|
|
def make_choice(choices, prng):
|
2011-08-11 18:32:18 +00:00
|
|
|
""" Given a list of (possibly weighted) options or just a single option!,
|
|
|
|
choose one of the options taking weights into account and return the
|
|
|
|
choice
|
|
|
|
"""
|
2011-08-10 22:10:24 +00:00
|
|
|
if isinstance(choices, str):
|
|
|
|
return choices
|
2011-08-10 20:26:00 +00:00
|
|
|
weighted_choices = []
|
|
|
|
for option in choices:
|
2011-08-11 19:25:13 +00:00
|
|
|
if option is None:
|
|
|
|
weighted_choices.append('')
|
|
|
|
continue
|
2011-08-15 21:16:40 +00:00
|
|
|
try:
|
|
|
|
(weight, value) = option.split(None, 1)
|
2011-08-19 21:54:24 +00:00
|
|
|
weight = int(weight)
|
2011-08-15 21:16:40 +00:00
|
|
|
except ValueError:
|
2011-08-19 21:54:24 +00:00
|
|
|
weight = 1
|
2011-08-15 21:16:40 +00:00
|
|
|
value = option
|
|
|
|
|
|
|
|
if value == 'null' or value == 'None':
|
|
|
|
value = ''
|
|
|
|
|
2011-08-10 20:26:00 +00:00
|
|
|
for _ in xrange(weight):
|
|
|
|
weighted_choices.append(value)
|
|
|
|
|
|
|
|
return prng.choice(weighted_choices)
|
|
|
|
|
|
|
|
|
2011-08-18 19:34:56 +00:00
|
|
|
def expand_headers(decision, prng):
|
2011-08-19 21:54:24 +00:00
|
|
|
expanded_headers = {}
|
2011-08-15 21:16:40 +00:00
|
|
|
for header in decision['headers']:
|
2011-08-18 19:34:56 +00:00
|
|
|
h = expand(decision, header[0], prng)
|
|
|
|
v = expand(decision, header[1], prng)
|
2011-08-19 21:54:24 +00:00
|
|
|
expanded_headers[h] = v
|
2011-08-15 21:16:40 +00:00
|
|
|
return expanded_headers
|
2011-08-10 18:27:06 +00:00
|
|
|
|
|
|
|
|
2011-08-18 19:34:56 +00:00
|
|
|
def expand(decision, value, prng):
|
2011-08-10 18:27:06 +00:00
|
|
|
c = itertools.count()
|
2011-08-18 19:34:56 +00:00
|
|
|
fmt = RepeatExpandingFormatter(prng)
|
2011-08-15 21:16:40 +00:00
|
|
|
new = fmt.vformat(value, [], decision)
|
|
|
|
return new
|
|
|
|
|
|
|
|
|
|
|
|
class RepeatExpandingFormatter(string.Formatter):
|
2011-08-18 19:34:56 +00:00
|
|
|
charsets = {
|
|
|
|
'printable': string.printable,
|
|
|
|
'punctuation': string.punctuation,
|
|
|
|
'whitespace': string.whitespace,
|
|
|
|
'digits': string.digits
|
|
|
|
}
|
2011-08-15 21:16:40 +00:00
|
|
|
|
2011-08-18 19:34:56 +00:00
|
|
|
def __init__(self, prng, _recursion=0):
|
2011-08-15 21:16:40 +00:00
|
|
|
super(RepeatExpandingFormatter, self).__init__()
|
|
|
|
# this class assumes it is always instantiated once per
|
|
|
|
# formatting; use that to detect runaway recursion
|
2011-08-18 19:34:56 +00:00
|
|
|
self.prng = prng
|
2011-08-15 21:16:40 +00:00
|
|
|
self._recursion = _recursion
|
|
|
|
|
|
|
|
def get_value(self, key, args, kwargs):
|
2011-08-18 19:34:56 +00:00
|
|
|
fields = key.split(None, 1)
|
|
|
|
fn = getattr(self, 'special_{name}'.format(name=fields[0]), None)
|
|
|
|
if fn is not None:
|
|
|
|
if len(fields) == 1:
|
|
|
|
fields.append('')
|
|
|
|
return fn(fields[1])
|
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
val = super(RepeatExpandingFormatter, self).get_value(key, args, kwargs)
|
|
|
|
if self._recursion > 5:
|
|
|
|
raise RecursionError(key)
|
2011-08-18 19:34:56 +00:00
|
|
|
fmt = self.__class__(self.prng, _recursion=self._recursion+1)
|
2011-08-15 21:16:40 +00:00
|
|
|
# must use vformat not **kwargs so our SpecialVariables is not
|
|
|
|
# downgraded to just a dict
|
|
|
|
n = fmt.vformat(val, args, kwargs)
|
|
|
|
return n
|
|
|
|
|
2011-08-09 22:44:25 +00:00
|
|
|
def special_random(self, args):
|
|
|
|
arg_list = args.split()
|
|
|
|
try:
|
2011-08-15 21:16:40 +00:00
|
|
|
size_min, size_max = arg_list[0].split('-', 1)
|
|
|
|
except ValueError:
|
|
|
|
size_min = size_max = arg_list[0]
|
2011-08-09 22:44:25 +00:00
|
|
|
except IndexError:
|
2011-08-15 21:16:40 +00:00
|
|
|
size_min = '0'
|
|
|
|
size_max = '1000'
|
|
|
|
|
|
|
|
size_min = int(size_min)
|
|
|
|
size_max = int(size_max)
|
|
|
|
length = self.prng.randint(size_min, size_max)
|
|
|
|
|
2011-08-09 22:44:25 +00:00
|
|
|
try:
|
2011-08-15 21:16:40 +00:00
|
|
|
charset_arg = arg_list[1]
|
2011-08-09 22:44:25 +00:00
|
|
|
except IndexError:
|
2011-08-15 21:16:40 +00:00
|
|
|
charset_arg = 'printable'
|
2011-08-09 22:44:25 +00:00
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
if charset_arg == 'binary':
|
2011-08-10 21:39:25 +00:00
|
|
|
num_bytes = length + 8
|
|
|
|
tmplist = [self.prng.getrandbits(64) for _ in xrange(num_bytes / 8)]
|
|
|
|
tmpstring = struct.pack((num_bytes / 8) * 'Q', *tmplist)
|
2011-08-18 19:34:56 +00:00
|
|
|
return tmpstring[0:length]
|
2011-08-10 21:39:25 +00:00
|
|
|
else:
|
2011-08-15 21:16:40 +00:00
|
|
|
charset = self.charsets[charset_arg]
|
2011-08-18 19:34:56 +00:00
|
|
|
return ''.join([self.prng.choice(charset) for _ in xrange(length)]) # Won't scale nicely
|
2011-08-09 22:44:25 +00:00
|
|
|
|
|
|
|
|
2011-08-04 00:00:02 +00:00
|
|
|
def parse_options():
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option('-O', '--outfile', help='write output to FILE. Defaults to STDOUT', metavar='FILE')
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.add_option('--seed', dest='seed', type='int', help='initial seed for the random number generator', metavar='SEED')
|
2011-08-04 00:00:02 +00:00
|
|
|
parser.add_option('--seed-file', dest='seedfile', help='read seeds for specific requests from FILE', metavar='FILE')
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.add_option('-n', dest='num_requests', type='int', help='issue NUM requests before stopping', metavar='NUM')
|
2011-08-19 21:54:24 +00:00
|
|
|
parser.add_option('-v', '--verbose', dest='verbose', action="store_true", help='turn on verbose output')
|
|
|
|
parser.add_option('-d', '--debug', dest='debug', action="store_true", help='turn on debugging (very verbose) output')
|
2011-08-08 23:51:10 +00:00
|
|
|
parser.add_option('--decision-graph', dest='graph_filename', help='file in which to find the request decision graph', metavar='NUM')
|
2011-08-04 00:00:02 +00:00
|
|
|
|
2011-08-04 23:59:32 +00:00
|
|
|
parser.set_defaults(num_requests=5)
|
2011-08-08 23:51:10 +00:00
|
|
|
parser.set_defaults(graph_filename='request_decision_graph.yml')
|
2011-08-04 00:00:02 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2011-08-15 21:16:40 +00:00
|
|
|
def randomlist(seed=None):
|
|
|
|
""" Returns an infinite generator of random numbers
|
2011-08-04 00:00:02 +00:00
|
|
|
"""
|
2011-08-15 21:16:40 +00:00
|
|
|
rng = random.Random(seed)
|
|
|
|
while True:
|
2011-08-04 00:00:02 +00:00
|
|
|
yield rng.random()
|
|
|
|
|
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
def populate_buckets(conn, alt):
|
|
|
|
""" Creates buckets and keys for fuzz testing and sets appropriate
|
|
|
|
permissions. Returns a dictionary of the bucket and key names.
|
|
|
|
"""
|
|
|
|
breadable = common.get_new_bucket(alt)
|
|
|
|
bwritable = common.get_new_bucket(alt)
|
|
|
|
bnonreadable = common.get_new_bucket(alt)
|
|
|
|
|
|
|
|
oreadable = Key(breadable)
|
|
|
|
owritable = Key(bwritable)
|
|
|
|
ononreadable = Key(breadable)
|
|
|
|
oreadable.set_contents_from_string('oreadable body')
|
|
|
|
owritable.set_contents_from_string('owritable body')
|
|
|
|
ononreadable.set_contents_from_string('ononreadable body')
|
|
|
|
|
|
|
|
breadable.set_acl('public-read')
|
|
|
|
bwritable.set_acl('public-read-write')
|
|
|
|
bnonreadable.set_acl('private')
|
|
|
|
oreadable.set_acl('public-read')
|
|
|
|
owritable.set_acl('public-read-write')
|
|
|
|
ononreadable.set_acl('private')
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
bucket_readable=breadable.name,
|
|
|
|
bucket_writable=bwritable.name,
|
|
|
|
bucket_not_readable=bnonreadable.name,
|
|
|
|
bucket_not_writable=breadable.name,
|
|
|
|
object_readable=oreadable.key,
|
|
|
|
object_writable=owritable.key,
|
|
|
|
object_not_readable=ononreadable.key,
|
|
|
|
object_not_writable=oreadable.key,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2011-08-04 00:00:02 +00:00
|
|
|
def _main():
|
|
|
|
""" The main script
|
|
|
|
"""
|
|
|
|
(options, args) = parse_options()
|
|
|
|
random.seed(options.seed if options.seed else None)
|
2011-08-04 23:59:32 +00:00
|
|
|
s3_connection = common.s3.main
|
2011-08-19 21:54:24 +00:00
|
|
|
alt_connection = common.s3.alt
|
|
|
|
|
|
|
|
if options.outfile:
|
|
|
|
OUT = open(options.outfile, 'w')
|
|
|
|
else:
|
|
|
|
OUT = sys.stderr
|
|
|
|
|
|
|
|
VERBOSE = DEBUG = open('/dev/null', 'w')
|
|
|
|
if options.verbose:
|
|
|
|
VERBOSE = OUT
|
|
|
|
if options.debug:
|
|
|
|
DEBUG = OUT
|
|
|
|
VERBOSE = OUT
|
2011-08-04 00:00:02 +00:00
|
|
|
|
2011-08-04 23:59:32 +00:00
|
|
|
request_seeds = None
|
2011-08-04 00:00:02 +00:00
|
|
|
if options.seedfile:
|
|
|
|
FH = open(options.seedfile, 'r')
|
2011-08-19 21:54:24 +00:00
|
|
|
request_seeds = [float(line) for line in FH.readlines()]
|
|
|
|
print>>OUT, 'Seedfile: %s' %options.seedfile
|
|
|
|
print>>OUT, 'Number of requests: %d' %len(request_seeds)
|
2011-08-04 00:00:02 +00:00
|
|
|
else:
|
2011-08-19 21:54:24 +00:00
|
|
|
if options.seed:
|
|
|
|
print>>OUT, 'Initial Seed: %d' %options.seed
|
|
|
|
print>>OUT, 'Number of requests: %d' %options.num_requests
|
2011-08-15 21:16:40 +00:00
|
|
|
random_list = randomlist(options.seed)
|
|
|
|
request_seeds = itertools.islice(random_list, options.num_requests)
|
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
print>>OUT, 'Decision Graph: %s' %options.graph_filename
|
2011-08-04 00:00:02 +00:00
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
graph_file = open(options.graph_filename, 'r')
|
|
|
|
decision_graph = yaml.safe_load(graph_file)
|
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
constants = populate_buckets(s3_connection, alt_connection)
|
|
|
|
print>>VERBOSE, "Test Buckets/Objects:"
|
|
|
|
for key, value in constants.iteritems():
|
|
|
|
print>>VERBOSE, "\t%s: %s" %(key, value)
|
2011-08-08 23:51:10 +00:00
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
print>>OUT, "Begin Fuzzing..."
|
|
|
|
print>>VERBOSE, '='*80
|
2011-08-04 23:59:32 +00:00
|
|
|
for request_seed in request_seeds:
|
2011-08-19 21:54:24 +00:00
|
|
|
print>>OUT, request_seed
|
|
|
|
|
2011-08-08 23:51:10 +00:00
|
|
|
prng = random.Random(request_seed)
|
|
|
|
decision = assemble_decision(decision_graph, prng)
|
|
|
|
decision.update(constants)
|
|
|
|
|
2011-08-18 19:34:56 +00:00
|
|
|
method = expand(decision, decision['method'], prng)
|
2011-08-19 21:54:24 +00:00
|
|
|
path = expand(decision, decision['urlpath'], prng)
|
2011-08-18 19:34:56 +00:00
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
try:
|
|
|
|
body = expand(decision, decision['body'], prng)
|
|
|
|
except KeyError:
|
|
|
|
body = ''
|
2011-08-08 23:51:10 +00:00
|
|
|
|
2011-08-19 21:54:24 +00:00
|
|
|
try:
|
|
|
|
headers = expand_headers(decision, prng)
|
|
|
|
except KeyError:
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
response = s3_connection.make_request(method, path, data=body, headers=headers, override_num_retries=0)
|
|
|
|
|
|
|
|
print>>VERBOSE, "%s %s" %(method[:100], path[:100])
|
|
|
|
for h, v in headers.iteritems():
|
|
|
|
print>>VERBOSE, "%s: %s" %(h[:50], v[:50])
|
|
|
|
print>>VERBOSE, "%s\n" % body[:100]
|
|
|
|
|
|
|
|
print>>DEBUG, 'FULL REQUEST'
|
|
|
|
print>>DEBUG, 'Method: %r' %method
|
|
|
|
print>>DEBUG, 'Path: %r' %path
|
|
|
|
print>>DEBUG, 'Headers:'
|
|
|
|
for h, v in headers.iteritems():
|
|
|
|
print>>DEBUG, "\t%r: %r" %(h, v)
|
|
|
|
print>>DEBUG, 'Body: %r' %body
|
|
|
|
|
|
|
|
print>>VERBOSE, 'Response status code: %d %s' %(response.status, response.reason)
|
|
|
|
print>>DEBUG, 'Body:\n%s' %response.read()
|
2011-08-08 23:51:10 +00:00
|
|
|
if response.status == 500 or response.status == 503:
|
2011-08-19 21:54:24 +00:00
|
|
|
print>>OUT, 'FAILED:\n%s' %request
|
|
|
|
print>>VERBOSE, '='*80
|
|
|
|
print>>OUT, '...done fuzzing'
|
|
|
|
common.teardown()
|
2011-08-04 00:00:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
common.setup()
|
|
|
|
try:
|
|
|
|
_main()
|
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
common.teardown()
|
|
|
|
|