scrap-is-not-scrap/run.py

142 lines
4.4 KiB
Python
Raw Normal View History

2016-07-15 04:16:25 +00:00
#! /usr/bin/env python3
2017-07-20 21:16:59 +00:00
from random import randint
import binascii
import ctypes
2016-07-15 04:16:25 +00:00
import logging
2016-07-18 02:19:05 +00:00
import os
2016-07-15 04:16:25 +00:00
import subprocess
2017-07-20 21:16:59 +00:00
logging.getLogger(__name__)
2016-07-15 04:16:25 +00:00
2017-07-20 21:16:59 +00:00
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
2016-07-15 04:16:25 +00:00
2017-07-20 21:16:59 +00:00
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:
2016-07-15 04:16:25 +00:00
scraps = sorted(os.listdir(run_dir))
if scraps:
return os.path.join(run_dir, scraps[-1])
return None
2017-07-20 21:16:59 +00:00
def generation(self, *, parent: ctypes.CDLL, scrap: bytes, cwd: str):
offset = randint(0, len(scrap))
flip = randint(0, 255)
scrap_len = len(scrap)
2016-07-21 02:39:43 +00:00
2017-07-20 21:16:59 +00:00
logging.info('scrap_length [{}]'.format(scrap_len))
logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap)))
2016-07-21 02:39:43 +00:00
2017-07-20 21:16:59 +00:00
status = parent.gen_fork(scrap, scrap_len)
2017-07-20 21:16:59 +00:00
logging.info('status {}'.format(status))
logging.info('scrap_length [{}]'.format(scrap_len))
logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap)))
2016-07-21 02:39:43 +00:00
2016-07-15 04:16:25 +00:00
def hex_dumps(scrap_dir):
scrap_dir = os.path.abspath(scrap_dir)
2016-07-18 04:16:41 +00:00
dump_dir = os.path.join(scrap_dir, 'hex')
2016-07-15 05:20:57 +00:00
2016-07-18 04:16:41 +00:00
if not os.path.isdir(dump_dir):
2016-07-15 05:20:57 +00:00
os.mkdir(dump_dir)
2016-07-15 04:16:25 +00:00
scraps = os.listdir(scrap_dir)
2016-07-15 05:20:57 +00:00
2016-07-15 04:16:25 +00:00
for scrap in scraps:
2016-07-15 05:20:57 +00:00
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:
2016-07-15 04:16:25 +00:00
binary = bytes(file_in.read())
for each in binary:
file_out.writelines('\'\\x{:02X}\',\n'.format(each))
if __name__ == '__main__':
import argparse
2016-07-18 02:19:05 +00:00
import sys
2016-07-15 04:16:25 +00:00
2016-07-18 03:28:21 +00:00
parser = argparse.ArgumentParser(
2017-07-20 21:16:59 +00:00
description='position independent code (PIC) mutation experiment.')
2016-07-18 03:28:21 +00:00
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',
2016-07-18 03:28:21 +00:00
help='path to PIC image.')
2017-07-20 21:16:59 +00:00
parser.add_argument('-parent', default='build/libgeneration.so',
help='path to generation lib.')
2016-07-18 03:28:21 +00:00
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()
2016-07-15 05:20:57 +00:00
2016-07-18 03:28:21 +00:00
logger = logging.getLogger()
2017-07-20 21:16:59 +00:00
logger.setLevel(logging.DEBUG) if args.verbose else logger.setLevel(logging.INFO)
formatter = logging.Formatter('# %(filename)s:%(lineno)s\n%(message)s')
2016-07-18 03:28:21 +00:00
2017-07-20 21:16:59 +00:00
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
2016-07-18 03:28:21 +00:00
if args.logfile:
file_handler = logging.FileHandler(args.logfile, 'a')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
2016-07-15 04:16:25 +00:00
2017-07-20 21:16:59 +00:00
logging.info(args)
2016-07-21 02:39:43 +00:00
if args.provision:
provision = ['sudo', 'sh', 'provision-ubuntu.sh']
prov_proc = subprocess.run(provision)
os.environ['CC'] = 'clang'
2016-07-18 02:19:05 +00:00
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)
2017-07-20 21:16:59 +00:00
logging.info(waf_proc.stdout)
2016-07-15 05:20:57 +00:00
elif args.dumps:
2016-07-15 04:16:25 +00:00
hex_dumps(args.dir)
2017-07-20 21:16:59 +00:00
2016-07-15 04:16:25 +00:00
else:
sins = Sins(parent=args.parent, seed=args.seed, run_dir=args.dir)