#! /usr/bin/env python3 from random import randint import binascii import ctypes import logging import os import subprocess logging.getLogger(__name__) class Sins: seed = bytes() def __init__(self, *, parent: str, seed: str=str(), run_dir: str=str()): self.parent = ctypes.CDLL(parent) self.parent.gen_fork.restype = ctypes.c_int self.parent.gen_fork.argtypes = [ctypes.c_void_p, ctypes.c_void_p] if seed: with open(seed, 'rb') as seed_file: self.seed = seed_file.read() try: os.mkdir(run_dir) except FileNotFoundError or PermissionError: run_dir = os.path.dirname(self.seed) except FileExistsError: pass self.run_dir = run_dir child = self.generation( parent=self.parent, scrap=self.seed, cwd=self.run_dir) if child: logging.info(child) def scrap_recent(self, *, run_dir: str)->str: scraps = sorted(os.listdir(run_dir)) if scraps: return os.path.join(run_dir, scraps[-1]) return None def generation(self, *, parent: ctypes.CDLL, scrap: bytes, cwd: str): offset = randint(0, len(scrap)) flip = randint(0, 255) scrap_len = len(scrap) logging.info('scrap_length [{}]'.format(scrap_len)) logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap))) status = parent.gen_fork(scrap, scrap_len) logging.info('status {}'.format(status)) logging.info('scrap_length [{}]'.format(scrap_len)) logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap))) def hex_dumps(scrap_dir): scrap_dir = os.path.abspath(scrap_dir) dump_dir = os.path.join(scrap_dir, 'hex') if not os.path.isdir(dump_dir): os.mkdir(dump_dir) scraps = os.listdir(scrap_dir) for scrap in scraps: scrap_path = os.path.join(scrap_dir, scrap) hex_name = '{}.hex'.format(scrap) hex_path = os.path.join(dump_dir, hex_name) if os.path.isfile(scrap_path): with open(scrap_path, 'rb') as file_in: with open(hex_path, 'w') as file_out: binary = bytes(file_in.read()) for each in binary: file_out.writelines('\'\\x{:02X}\',\n'.format(each)) if __name__ == '__main__': import argparse import sys parser = argparse.ArgumentParser( description='position independent code (PIC) mutation experiment.') parser.add_argument('--verbose', '-v', action='count') parser.add_argument('-build', action='store_true', help='build parent and seed PIC image, exit.') parser.add_argument('-provision', action='store_true', help='provision ubuntu for run, exit.') parser.add_argument('-logfile', help='log to file.') parser.add_argument('-seed', default='build/seed.asm.2.o', help='path to PIC image.') parser.add_argument('-parent', default='build/libgeneration.so', help='path to generation lib.') parser.add_argument('-dir', default='sandbox', help='path to execution directory.') parser.add_argument('-dumps', action='store_true', help='dump hex values of scraps in directory, exit.') args = parser.parse_args() logger = logging.getLogger() logger.setLevel(logging.DEBUG) if args.verbose else logger.setLevel(logging.INFO) formatter = logging.Formatter('# %(filename)s:%(lineno)s\n%(message)s') stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) if args.logfile: file_handler = logging.FileHandler(args.logfile, 'a') file_handler.setFormatter(formatter) logger.addHandler(file_handler) logging.info(args) if args.provision: provision = ['sudo', 'sh', 'provision-ubuntu.sh'] prov_proc = subprocess.run(provision) os.environ['CC'] = 'clang' waf = [sys.executable, 'waf.py', 'configure'] waf_proc = subprocess.run(waf) elif args.build: waf = [sys.executable, 'waf.py', 'build'] waf_proc = subprocess.run(waf) logging.info(waf_proc.stdout) elif args.dumps: hex_dumps(args.dir) else: sins = Sins(parent=args.parent, seed=args.seed, run_dir=args.dir)