def interact(manipulatee): """ Handle interactive command input """ mutant = Mutator(manipulatee) start = "Input commands to interact with your object, for example GET name, SET name=Will, GET *, exit" end = "\nExiting interactive shell\n" print(start) # Prompt, Read, Parse, Validate, Execute, in a while loop while True: outcome = '' txt = input("") if txt == 'exit' or txt == 'exit()': break try: outcome = mutant.command(txt) except ValueError: print('Invalid command syntax.') except AttributeError: print( 'Getting an attribute that does not exist yet does not allow me a valid response.' ) print(outcome) print(end) return mutant.render()
def main(): global POPULATION_COUNT, ITERATIONS, MAX_FUNC_DEPTH file = open('data.txt', 'w') if len(sys.argv) == 3: ITERATIONS = int(sys.argv[1]) if len(sys.argv) == 3: POPULATION_COUNT = int(sys.argv[2]) f, data = create_test_data() write_init_data(file, f, data) population = create_population(POPULATION_COUNT, MAX_FUNC_DEPTH) normalizator = Normalizator(data, mistake) functions = FunctoionContainer(choose_parent_pairs2, choose_to_kill, create_children) options = Options(0.4) mutator = Mutator(population, normalizator, options, functions) print 'population: ' #for s in population: print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f for i in range(ITERATIONS): print 'population ' + str(i) + ' : ' for j in range(5): s = population[(j + 1) * (-1)] print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f if j == 1: # ITERATION_NUMBER:MISTAKE:FUNCTION write_population_data(file, i, s, mistake(s.f, data)) mutator.make_children() mutator.kill_unused() # mutator.change_random() mutator.change_age() mutator.mutate() normalizator.compute_distribuante(population) population.sort() file.close()
def main(): from mutator import Mutator from helpers.helpers_data import prepare_data m = Mutator(population_size=4) # , generator_f=prepare_data, generator_args=['testing', False]) (x, y), (x_val, y_val) = prepare_data('colorflow - Hgg_vs_Hqq 65', first_time=True) print(m.evolve(x, y, validation_data=(x_val, y_val), batch_size=100, verbose=1, generations=20, use_generator=False))
def wait_for_devices(): adb.stop_adb() print "\n" adb.start_adb() print "\n" print "waiting for emulator instances to come online..." time.sleep(10) while True: devices = adb.check_devices() if devices == None: time.sleep(1) elif len(devices) < NUM_EMULATORS: time.sleep(1) else: break print "kicking off fuzzer threads..." dirlist = os.listdir(FUZZ_SAMPLE_DIR) queue = Queue.Queue() file_list = [] for dirname, dirnames, filenames in os.walk(FUZZ_SAMPLE_DIR): if filenames: for filename in filenames: if filename.find("FUZZFILE") > -1: os.remove(os.path.join(dirname, filename)) else: file_list.append(os.path.join(dirname, filename)) print "using corpus:" for file in file_list: print "\t", file tmp = open(file, "rb") fuzz_data = tmp.read() tmp.close() mut = Mutator(None, fuzz_data) for primitive in PRIMITIVES: max_mut = mut.get_max_mutations(primitive) tmp = [] part = max_mut / MAX_ITERATION if part < 1: part = 1 for i in range(0, part): tmp.append((max_mut / part) * i) #for i in range(0, NUM_EMULATORS): # tmp.append((max_mut/NUM_EMULATORS)*i) if tmp[-1] < max_mut: tmp.append(max_mut) for i in range(0, len(tmp) - 1): queue.put([file, tmp[i], tmp[i + 1], primitive]) print "total instance iterations: ", len(list(queue.queue)) for device in devices: thread = FuzzThread(device, queue) thread.setDaemon(True) thread.start() queue.join()
def worker_mutate(solveable, mutateable, log): m = Mutator() while True: fractals = [] for n in range(8): fractals.append(mutateable.get(block=True)) fractals = m.mutate(fractals) for f in fractals: solveable.put(f)
def main(): global POPULATION_COUNT, ITERATIONS, MAX_FUNC_DEPTH file = open('data.txt', 'w') if len(sys.argv) == 3: ITERATIONS = int(sys.argv[1]) if len(sys.argv) == 3: POPULATION_COUNT = int(sys.argv[2]) f, data = create_test_data() write_init_data(file, f, data) population = create_population(POPULATION_COUNT, MAX_FUNC_DEPTH) normalizator = Normalizator(data, mistake) functions = FunctoionContainer(choose_parent_pairs2, choose_to_kill, create_children) options = Options(0.4) mutator = Mutator(population, normalizator, options, functions) print 'population: ' #for s in population: print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f for i in range(ITERATIONS): print 'population ' + str(i) + ' : ' for j in range(5): s = population[(j+1)*(-1)] print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f if j == 1: # ITERATION_NUMBER:MISTAKE:FUNCTION write_population_data(file, i, s, mistake(s.f, data)) mutator.make_children() mutator.kill_unused() # mutator.change_random() mutator.change_age() mutator.mutate() normalizator.compute_distribuante(population) population.sort() file.close()
def run_epochs(self, to_mutate, to_keep): self.init_agents() for i in xrange(NUM_EPOCHS): # set the gameplaying groups self.init_agent_gameplaying_groups() # maps from player to list player scores in games played during # the epoch self.epoch_results = {} p = Pool(args.nthreads) all_game_scores = p.map(play_epoch, self.game_groups) # get the results of each epoch for each gameplaying group # don't record scores for benchmark for game_scores in all_game_scores: for player, scores in game_scores.iteritems(): if player != "benchmark": self.epoch_results[player] = self.epoch_results.get( player, []) + scores # sort the agents in order of rank self.agents = self.rank_agents() # get which agents to mutate self.mutate_agents = self.agents[:TO_MUTATE] # mutate the agents for the next generation self.mutator = Mutator(self.mutate_agents, AGENT_DIR) NUM_AGENTS_in_mutate_groups = (NUM_AGENTS - TO_KEEP) / 3 crossovers = self.mutator.crossover(NUM_AGENTS_in_mutate_groups) mutated = self.mutator.mutate(NUM_AGENTS_in_mutate_groups) combinations = self.mutator.combo(NUM_AGENTS - TO_KEEP - 2 * NUM_AGENTS_in_mutate_groups) # get the top to_keep agents self.top_agents = self.agents[:TO_KEEP] for aid in self.top_agents: print "---------------------------------------\n" print "Evaluating top agent %s from epoch %d: " % (aid, i) print "\tTotal Winnings: %d" % sum(self.epoch_results[aid]) print "\tGames Won: %d/%d" % (len( filter(lambda x: x >= 0, self.epoch_results[aid])), len(self.epoch_results[aid])) sys.stdout.flush() # set the agents for the next epoch self.agents = crossovers + mutated + combinations + self.top_agents self.epoch_results = {}
class Evolution: mutator = Mutator() def __init__(self): pass def update(self, agent_info): # Sort by fitness = index 0 sorted_info = sorted(agent_info, key=lambda x: x[0], reverse=True) all_params = [params for _, params in sorted_info] # Remove half half = int(len(all_params) / 2) all_params = all_params[:-half] # Mutate before duplicating for i, params in enumerate(all_params): # Choose a random layer layers = len(params['weights']) layer = np.random.randint(1, layers) # Generate new weight or biases and replace the information weights, biases = self.mutator.mutate(layer, params['weights'], params['biases']) all_params[i]['weights'] = weights all_params[i]['biases'] = biases # Duplicate the best all_params *= 2 return all_params
def test_can_pass_person(self): # Simple dummy class class Dummy: def __init__(self): self.name = 'Martha' mini = MiniRepl(Mutator(Dummy())) assert len(str(mini)) > 20
def run(self): while (self.time_to_die == False): args = self.queue.get() self.fuzz_file = args[0] self.mutation_start = args[1] self.mutation_stop = args[2] self.mutation_method = args[3] self.update_fuzz_data() #block on remount print "[%s] waiting for adb to detect the emulator...." % self.getName( ) while True: #if we do this to many times and we dont have the correct # of emulators #we need to kill and restart adb devices = adb.check_devices() if type(devices) == type(None): time.sleep(2) continue for dev in devices: if dev.find(self.device.split("\t")[0]) > -1: self.device = dev if adb.remount(self.device, "/mnt/sdcard"): break else: #print "[%s] %s sleeping..." % (self.device, self.getName()) time.sleep(2) #unlock the screen print "[%s] unlocking screen..." % self.getName() adb.unlock_screen(self.device) #mutate file (pass in data, max iteration, mutation method, this class) print "[%s]\n\tdevice: %s\n\tfuzz file: %s\n\tmethod: %s\n\titeration: %d %d" % ( self.getName(), self.device.split("\t")[0], self.fuzz_file, self.mutation_method, self.mutation_start, self.mutation_stop) mut = Mutator(self.fuzz_loop, self.fuzz_data, self.fuzz_file.split("/")[-1], self.getName()) mut.run(self.mutation_method, self.mutation_start, self.mutation_stop) print "[%s]\n\tdevice: %s\n\tcompleted mutations for file: %s\n\tmethod: %s" % ( self.getName(), self.device.split("\t")[0], self.fuzz_file, self.mutation_method) self.queue.task_done()
def mutation_test(file_name, tests): status = Status.UNEXPECTED local_changes_present = subprocess.call('git diff --quiet {0}'.format(file_name), shell=True) if local_changes_present == 1: status = Status.SKIPPED logging.warning("{0} has local changes, please commit/remove changes before running the test".format(file_name)) else: strategies = list(get_strategies()) while len(strategies): strategy = random.choice(strategies) strategies.remove(strategy) mutator = Mutator(strategy()) mutated_line = mutator.mutate(file_name) if mutated_line != -1: logging.info("Mutated {0} at line {1}".format(file_name, mutated_line)) logging.info("compiling mutant {0}:{1}".format(file_name, mutated_line)) test_command = "python mach build --release" if subprocess.call(test_command, shell=True, stdout=DEVNULL): logging.error("Compilation Failed: Unexpected error") logging.error("Failed: while running `{0}`".format(test_command)) subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True) status = Status.UNEXPECTED else: for test in tests: test_command = "python mach test-wpt {0} --release".format(test.encode('utf-8')) logging.info("running `{0}` test for mutant {1}:{2}".format(test, file_name, mutated_line)) test_status = subprocess.call(test_command, shell=True, stdout=DEVNULL) if test_status != 0: logging.error("Failed: while running `{0}`".format(test_command)) logging.error("mutated file {0} diff".format(file_name)) subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True) status = Status.SURVIVED else: logging.info("Success: Mutation killed by {0}".format(test.encode('utf-8'))) status = Status.KILLED break logging.info("reverting mutant {0}:{1}\n".format(file_name, mutated_line)) subprocess.call('git checkout {0}'.format(file_name), shell=True) break elif not len(strategies): # All strategies are tried logging.info("\nCannot mutate {0}\n".format(file_name)) return status
def review(): seed_from_form = request.form.get('seed_file') seed_from_url = request.args.get('seed_file') if seed_from_form: seed_file = seed_from_form elif seed_from_url: seed_file = seed_from_url else: raise AttributeError( 'You must specify the seed file via POST or GET parameter.') # alter to allow submittal of seed files that already have relative path file_handler = FileHandler() seed_file_with_full_path = file_handler.find_seed_file_on_disk() seed_file_with_relative_path = file_handler.full_to_relative_path( seed_file_with_full_path) seed_melody = file_handler.filename_to_list(filename=seed_file) i = 0 created_melodies = [] created_files = [] output_directory = file_handler.setup_output_directory() output_directory_seed_folder = os.path.join(output_directory, 'seed') shutil.copy2(seed_file_with_full_path, output_directory_seed_folder) mutator = Mutator(seed_melody=seed_melody, mutation_percentage=5) while i < 10: mutated_melody = mutator.mutate() if mutated_melody != seed_melody and mutated_melody not in created_melodies: midi_maker = MidiMaker(output_directory=output_directory, melody=mutated_melody) mutated_file = midi_maker.write() created_melodies.append(mutated_melody) created_files.append( file_handler.full_to_relative_path(mutated_file)) i += 1 # print(created_files) return render_template( 'review.html', seed_file_no_path=seed_file, seed_file_with_relative_path=seed_file_with_relative_path, created_files=created_files)
def mutation_test(file_name, tests): status = Status.UNEXPECTED local_changes_present = subprocess.call('git diff --quiet {0}'.format(file_name), shell=True) if local_changes_present == 1: status = Status.SKIPPED logging.warning("{0} has local changes, please commit/remove changes before running the test".format(file_name)) else: strategies = list(get_strategies()) while len(strategies): strategy = random.choice(strategies) strategies.remove(strategy) mutator = Mutator(strategy()) mutated_line = mutator.mutate(file_name) if mutated_line != -1: logging.info("Mutated {0} at line {1}".format(file_name, mutated_line)) logging.info("compiling mutant {0}:{1}".format(file_name, mutated_line)) if subprocess.call('python mach build --release', shell=True, stdout=DEVNULL): logging.error("Compilation Failed: Unexpected error") status = Status.UNEXPECTED else: for test in tests: test_command = "python mach test-wpt {0} --release".format(test.encode('utf-8')) logging.info("running `{0}` test for mutant {1}:{2}".format(test, file_name, mutated_line)) test_status = subprocess.call(test_command, shell=True, stdout=DEVNULL) if test_status != 0: logging.error("Failed: while running `{0}`".format(test_command)) logging.error("mutated file {0} diff".format(file_name)) subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True) status = Status.SURVIVED else: logging.info("Success: Mutation killed by {0}".format(test.encode('utf-8'))) status = Status.KILLED break logging.info("reverting mutant {0}:{1}\n".format(file_name, mutated_line)) subprocess.call('git checkout {0}'.format(file_name), shell=True) break elif not len(strategies): # All strategies are tried logging.info("\nCannot mutate {0}\n".format(file_name)) return status
def main(): global VERBOSE parser=argparse.ArgumentParser(description="Simple PCAP fuzzer based on radamsa mutation engine") requiredNamed = parser.add_argument_group('required named arguments') requiredNamed.add_argument("-D",required=True, action="store", dest="dest", help="Destionation host to fuzz application on") requiredNamed.add_argument("-P",required=True, action="store", dest="port", help="Destionation port to fuzz application on") requiredNamed.add_argument("-S",required=True, action="store", dest="source", help="Sorce host to fuzz messages from") requiredNamed.add_argument("-f",required=True, action="store", dest="file", help="Input pcap file") requiredNamed.add_argument("-z",required=True, action="store", dest="fuzz", help="fuzz factor between 0 and 100") requiredNamed.add_argument("-l",required=True, action="store", dest="layer", help="packet layer to fuzz") parser.add_argument("-v","--verbose", dest="verbose", action="store_true", help="verbose output") opts=parser.parse_args() if opts.dest: DEST=opts.dest if opts.source: SOURCE=opts.source if opts.port: PORT=opts.port if opts.file: FILE=opts.file if opts.fuzz: FUZZ=float(opts.fuzz) if opts.layer: LAYER=opts.layer assert isinstance(LAYER,str), "layer must be specified as layer in scapy's syntax" if opts.verbose: VERBOSE=opts.verbose reader=Reader(FILE, LAYER, FUZZ) # Cients and Servers are lists clients, servers = reader.get_cparts() if VERBOSE: for idx, elem in enumerate(clients): print "Client %s - %s" % (idx, elem) for idx, elem in enumerate(servers): print "Server %s - %s" % (idx, elem) # Grab mutable packets out of pcap with specified layer mutable_packets=reader.extract_mutable_packets() m=Mutator(DEST,PORT,LAYER) if VERBOSE: print u"\033[1;32m\u2714 Found %s mutable packets (factor %s)\033[0m" % (len(mutable_packets),FUZZ) if VERBOSE: for idx, p in enumerate(mutable_packets): print "%s -> %s" % (idx, type(p)) print "%s -> %s" % (idx, binascii.hexlify(p.load)) print "MUT: %s -> %s" % (idx, binascii.hexlify(m.prepare_payload(p.load))) for idx, p in enumerate(mutable_packets): print "%s" % (p.load)
class EvoAgent(object): def __init__(self): self.benchmark_agents = [] self.agents = [] self.epoch_results = [] if not os.path.exists(AGENT_DIR): os.makedirs(AGENT_DIR) ''' Run by the main function to produce the top agents. Function that runs NUM_EPOCHS epochs and writes the parameter results of the top three evolved agents to file ''' def produce_agents(self, nagents): print "Producing %d top agents of evolution:\n\ %d epochs\n\ %d agents\n\ %d players per game group\n\ coevolution: %d\n" % \ (nagents, NUM_EPOCHS, NUM_AGENTS, NUM_GAME_PLAYERS, COEVOLVE) sys.stdout.flush() self.run_epochs(TO_MUTATE, TO_KEEP) # get the parameters of the top agent for i in xrange(min(nagents, TO_KEEP)): print "Top agent %d ID: %s" % (i, self.top_agents[i]) sys.stdout.flush() ''' Runs num_epoch epochs, each of which plays num_game games. After the end of each epoch, agents are ranked in order of their success in winning games. The top to_mutate agents are then mutated/crossed-over to generate the next population of agents for the next epoch. The top to_keep agents from each generation are kept in the next generation as benchmarks for the next generation and to ensure that the evolution never loses progress. ''' def run_epochs(self, to_mutate, to_keep): self.init_agents() for i in xrange(NUM_EPOCHS): # set the gameplaying groups self.init_agent_gameplaying_groups() # maps from player to list player scores in games played during # the epoch self.epoch_results = {} p = Pool(args.nthreads) all_game_scores = p.map(play_epoch, self.game_groups) # get the results of each epoch for each gameplaying group # don't record scores for benchmark for game_scores in all_game_scores: for player, scores in game_scores.iteritems(): if player != "benchmark": self.epoch_results[player] = self.epoch_results.get( player, []) + scores # sort the agents in order of rank self.agents = self.rank_agents() # get which agents to mutate self.mutate_agents = self.agents[:TO_MUTATE] # mutate the agents for the next generation self.mutator = Mutator(self.mutate_agents, AGENT_DIR) NUM_AGENTS_in_mutate_groups = (NUM_AGENTS - TO_KEEP) / 3 crossovers = self.mutator.crossover(NUM_AGENTS_in_mutate_groups) mutated = self.mutator.mutate(NUM_AGENTS_in_mutate_groups) combinations = self.mutator.combo(NUM_AGENTS - TO_KEEP - 2 * NUM_AGENTS_in_mutate_groups) # get the top to_keep agents self.top_agents = self.agents[:TO_KEEP] for aid in self.top_agents: print "---------------------------------------\n" print "Evaluating top agent %s from epoch %d: " % (aid, i) print "\tTotal Winnings: %d" % sum(self.epoch_results[aid]) print "\tGames Won: %d/%d" % (len( filter(lambda x: x >= 0, self.epoch_results[aid])), len(self.epoch_results[aid])) sys.stdout.flush() # set the agents for the next epoch self.agents = crossovers + mutated + combinations + self.top_agents self.epoch_results = {} ''' Sorts agents based upon the results from the epoch Top agents are placed first ''' def rank_agents(self): sum_scores = [] for aid, scores in self.epoch_results.iteritems(): sum_scores.append((sum(scores), aid)) sum_scores.sort(reverse=True) sorted_list = [aid for (_, aid) in sum_scores] return sorted_list ''' Sets self.agents to be the initial list of agents as a dictionary mapping from an agent ID to the agent parameters ''' def init_agents(self): self.agents = [] noise = 0.001 for i in xrange(NUM_AGENTS): aid = str(uuid.uuid4()) initial_params = [] with np.load(os.path.join(AGENT_DIR, "initial-poker-params.npz")) as data: for i in range(len(data.keys())): initial_params.append(data["arr_%d" % i] + np.random.normal(0, noise, 1)[0]) agent_params = Params(aid=aid, agent_dir=AGENT_DIR, params_list=initial_params) self.agents.append(aid) ''' Inits the game_groups list of lists indicating (by agent ID) which agents are playing which other agents If not coevolving the agents, game_groups indicates (by agent ID) the agent in the population that is playing the game ''' def init_agent_gameplaying_groups(self): agent_game_groups = [] if COEVOLVE: agents_indices = range(len(self.agents)) for _ in range(4): random.shuffle(agents_indices) i = 0 while (i < len(self.agents)): group = [] for _ in xrange(NUM_GAME_PLAYERS): group.append(self.agents[i]) i += 1 agent_game_groups.append(group) else: for agent in self.agents: agent_game_groups.append([agent]) # the other agents will be benchmark agent programs self.game_groups = agent_game_groups
from mutator import Mutator #sample_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1] sample_data = 'abcdefghijklmnopqrstuvwxyz' m1 = Mutator(sample_data) m1.randomize_n_bytes(5) print(m1.data) m1 = Mutator(sample_data) m1. remove_n_bytes(8) print(m1.data) m1 = Mutator(sample_data) m1.remove_n_bytes_at(7, 3) print(m1.data) m1 = Mutator(sample_data) m1.append_n_random_bytes(5) print(m1.data) bytes_list = bytearray(b'abc') m1 = Mutator(sample_data) m1.append_bytes(bytes_list) print(m1.data) m1 = Mutator(sample_data) m1.insert_bytes(4, bytes_list) print(m1.data) print('===================================')
def test_mutated_length(): LENGTH = 10 ind1 = Individual(LENGTH) mutator = Mutator(2) ind2 = mutator.reproduce_asexually(ind1) assert_true(len(ind1.chars) == len(ind2.chars))
from individual import Individual from mutator import Mutator num_tries = 0 num_moves = 0 TARGET = "Methinks it is like a weasel" mutator = Mutator(2) ind = Individual(len(TARGET)) fitness = ind.evaluate_against(TARGET) while fitness < len(TARGET): new_ind = mutator.reproduce_asexually(ind) new_fitness = new_ind.evaluate_against(TARGET) num_tries = num_tries + 1 if new_fitness > fitness: ind = new_ind fitness = new_fitness num_moves = num_moves + 1 print(new_ind) print("Found in",num_tries,"tries and",num_moves,"moves.")