コード例 #1
0
    def __init__(self, program, original_file, log_file_name, mutation_type, directory):
        print 'FuzzerFactory(...) started'
        self.mutation_generator = MutationGenerator(mutation_type)  # create the list of mutations
        self.mutator            = None                              # to create the files that reportedly cause crashes
        self.mutations          = self.mutation_generator.getValues()
        self.mutations_range    = range(len(self.mutations))
        self.file_name          = split(original_file)[1]       # just the filename
        self.program            = program
        self.log_file_name      = log_file_name                 # just the name
        self.directory          = directory                     # directory to save crash causing files
        self.log_file           = None                          # the opened instance
        self.contents           = None
        self.contents_range     = None
        self.generator          = self.createGenerator()
        self.clients            = []                            # list of clients
        self.crashes            = []                            # list of crashes
        self.mutations_executed = 0                             # number of mutations executed so far
        self.paused             = False

        # make sure we can read the original target file
        try:
            self.contents = open(original_file, 'rb').read()
            self.contents_range = range(len(self.contents))
        except Exception as e:
            quit('Unable to open "%s" and read the contents. Error: %s' % (original_file, e))
    
        # make sure we can write to the logfile
        try:
            self.log_file = open(self.log_file_name, 'w')
        except Exception as e:
            quit('Unable to open logfile "%s". Error: %s' % (self.log_file_name, self.log_file))

        # we have all the pieces for the mutator now
        self.mutator = Mutator(self.contents, self.mutations, self.file_name, self.directory)

        # a thread to handle user input and print statistics
        menu_thread = Thread(target=self.menu)
        menu_thread.start()
コード例 #2
0
class FuzzerFactory(ServerFactory):
    protocol = FuzzerServerProtocol

    def __init__(self, program, original_file, log_file_name, mutation_type, directory):
        print 'FuzzerFactory(...) started'
        self.mutation_generator = MutationGenerator(mutation_type)  # create the list of mutations
        self.mutator            = None                              # to create the files that reportedly cause crashes
        self.mutations          = self.mutation_generator.getValues()
        self.mutations_range    = range(len(self.mutations))
        self.file_name          = split(original_file)[1]       # just the filename
        self.program            = program
        self.log_file_name      = log_file_name                 # just the name
        self.directory          = directory                     # directory to save crash causing files
        self.log_file           = None                          # the opened instance
        self.contents           = None
        self.contents_range     = None
        self.generator          = self.createGenerator()
        self.clients            = []                            # list of clients
        self.crashes            = []                            # list of crashes
        self.mutations_executed = 0                             # number of mutations executed so far
        self.paused             = False

        # make sure we can read the original target file
        try:
            self.contents = open(original_file, 'rb').read()
            self.contents_range = range(len(self.contents))
        except Exception as e:
            quit('Unable to open "%s" and read the contents. Error: %s' % (original_file, e))
    
        # make sure we can write to the logfile
        try:
            self.log_file = open(self.log_file_name, 'w')
        except Exception as e:
            quit('Unable to open logfile "%s". Error: %s' % (self.log_file_name, self.log_file))

        # we have all the pieces for the mutator now
        self.mutator = Mutator(self.contents, self.mutations, self.file_name, self.directory)

        # a thread to handle user input and print statistics
        menu_thread = Thread(target=self.menu)
        menu_thread.start()
            
    def createGenerator(self):
        for offset in self.contents_range:
            for mutation_index in self.mutations_range:
                yield {'offset':offset, 'mutation_index':mutation_index, 'stop':False, 'pause':False}

    def getNextMutation(self):
        if self.paused:
            return {'offset':0, 'mutation_index':0, 'stop':False, 'pause':True}
        try:
            n = self.generator.next()
            self.mutations_executed += 1
            return n
        except StopIteration:
            # no more mutations, close the logfile
            if not self.log_file.closed:
                self.log_file.close()
            # tell any clients to 'stop'
            return {'offset':0, 'mutation_index':0, 'stop':True, 'pause':False}

    def printStatistics(self, mutations=False, clients=False, crashes=False):
        ''' print some statistics information '''

        print ''
        if mutations:
            total_mutations = len(self.contents) * len(self.mutations) 
            print 'Mutations:'
            print '  - File size                    :', len(self.contents)
            print '  - Number of possible mutations :', len(self.mutations)
            print '  - Total number of mutations    :', total_mutations
            print '  - Total executed so far        : %d (%d%%)' % (self.mutations_executed, float(self.mutations_executed)/total_mutations*100)
        if clients:
            print 'Clients:'
            for client in self.clients:
                print '  - %s:%d' % (client.host, client.port)
        if crashes:
            print 'Crashes:'
            for crash in self.crashes:
                print '  - Offset         :',   crash['offset']
                print '  - Mutation Index :',   crash['mutation_index']
                print '  - Filename       :',   crash['filename']
                print '  - Output         :\n', crash['output']

    def menu(self):
        while True:
            print '\n'
            if self.paused: print '---- PAUSED ----'
            print 'Menu :'
            print '1. Show clients'
            print '2. Show crashes'
            print '3. Mutations'
            print '4. Show all'
            print '5. Pause/Resume'
            selection = raw_input('Enter Selection : ').rstrip()
            if selection == '1':
                self.printStatistics(clients=True)
            if selection == '2':
                self.printStatistics(crashes=True)
            if selection == '3':
                self.printStatistics(mutations=True)
            if selection == '4':
                self.printStatistics(clients=True, crashes=True, mutations=True)
            if selection == '5':
                self.paused = False if self.paused else True