2016-07-15 04:16:25 +00:00
|
|
|
#! /usr/bin/env python3
|
2017-07-20 21:16:59 +00:00
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
from datetime import datetime
|
2017-07-20 21:16:59 +00:00
|
|
|
from random import randint
|
2017-07-21 19:19:23 +00:00
|
|
|
from time import sleep
|
2017-07-20 21:16:59 +00:00
|
|
|
import binascii
|
2016-07-15 04:16:25 +00:00
|
|
|
import logging
|
2016-07-18 02:19:05 +00:00
|
|
|
import os
|
2017-07-21 19:19:23 +00:00
|
|
|
import socket
|
2016-07-15 04:16:25 +00:00
|
|
|
import subprocess
|
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
|
|
|
|
class Server:
|
2017-08-27 00:33:49 +00:00
|
|
|
_children = list()
|
2017-07-21 19:19:23 +00:00
|
|
|
|
|
|
|
host = ('127.0.0.1', 5000)
|
|
|
|
sock = socket.socket()
|
|
|
|
sock.bind(host)
|
2017-07-28 21:24:37 +00:00
|
|
|
sock.listen()
|
2017-07-21 19:19:23 +00:00
|
|
|
client = None
|
2017-07-28 21:24:37 +00:00
|
|
|
logging.info('binding [{}]'.format(host))
|
2017-07-21 19:19:23 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
return
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.sock.shutdown(socket.SHUT_RD)
|
|
|
|
self.sock.close()
|
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
def accept(self):
|
|
|
|
self.client, address = self.sock.accept()
|
|
|
|
logging.info('address accpeted [{}]'.format(address))
|
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
def recv(self, size: int=int()) -> list:
|
2017-07-28 21:24:37 +00:00
|
|
|
if not self.client:
|
|
|
|
raise BrokenPipeError('client connection missing')
|
2017-07-21 19:19:23 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
recvd = self.client.recv(size)
|
2017-07-21 19:19:23 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
if recvd:
|
|
|
|
logging.info('recvd [{}]'.format(len(recvd)))
|
2017-07-21 19:19:23 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
return recvd
|
2017-07-21 19:19:23 +00:00
|
|
|
|
|
|
|
def send(self, data: bytes):
|
2017-07-28 21:24:37 +00:00
|
|
|
logging.info('send [{}]'.format(binascii.b2a_hex(data)))
|
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
if not self.client:
|
|
|
|
raise BrokenPipeError('client connection missing')
|
|
|
|
|
|
|
|
self.client.sendall(data)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def children(self):
|
2017-08-27 00:33:49 +00:00
|
|
|
children = self._children
|
|
|
|
self._children = list()
|
2017-07-21 19:19:23 +00:00
|
|
|
return children
|
|
|
|
|
|
|
|
|
|
|
|
class Genorator:
|
2017-07-20 21:16:59 +00:00
|
|
|
seed = bytes()
|
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
def __init__(self, *, server: Server, parent: str, seed: str=str(), run_dir: str=str()):
|
|
|
|
self.server = server
|
2017-07-21 19:19:23 +00:00
|
|
|
self.parent = os.path.abspath(parent)
|
2017-07-20 21:16:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
parent_args = {
|
|
|
|
'parent': self.parent,
|
|
|
|
'scrap': self.seed,
|
|
|
|
'cwd': self.run_dir}
|
2017-07-20 21:16:59 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
self.parenting(parent=self.parent, cwd=self.run_dir)
|
|
|
|
self.seeding(scrap=self.seed)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self.proc:
|
|
|
|
self.proc.terminate()
|
2017-07-20 21:16:59 +00:00
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
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-28 21:24:37 +00:00
|
|
|
def parenting(self, *, parent: str, cwd: str):
|
|
|
|
cmd = [parent]
|
|
|
|
logging.info('parent {}'.format(cmd))
|
|
|
|
|
|
|
|
self.proc = subprocess.Popen(
|
|
|
|
[parent],
|
|
|
|
cwd=cwd)
|
|
|
|
|
|
|
|
def seeding(self, *, scrap: bytes):
|
2017-07-20 21:16:59 +00:00
|
|
|
offset = randint(0, len(scrap))
|
|
|
|
flip = randint(0, 255)
|
2016-07-21 02:39:43 +00:00
|
|
|
|
2017-07-20 21:16:59 +00:00
|
|
|
logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap)))
|
2016-07-21 02:39:43 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
self.server.accept()
|
2016-07-21 02:08:33 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
scrap_len = bytes([len(scrap)])
|
|
|
|
logging.info('send child len [{}]'.format(scrap_len))
|
2017-07-21 19:19:23 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
self.server.send(scrap_len)
|
|
|
|
# self.server.send(scrap)
|
2017-07-21 19:19:23 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
logging.info('child send')
|
2016-07-21 02:39:43 +00:00
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
child = self.server.recv()
|
|
|
|
if child:
|
|
|
|
raise Exception(child)
|
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))
|
|
|
|
|
|
|
|
|
2017-07-21 19:19:23 +00:00
|
|
|
def provision():
|
|
|
|
provision = ['sudo', 'sh', 'provision-ubuntu.sh']
|
|
|
|
prov_proc = subprocess.run(provision)
|
|
|
|
|
|
|
|
waf = [sys.executable, 'waf.py', 'configure']
|
|
|
|
waf_proc = subprocess.run(waf)
|
|
|
|
|
|
|
|
|
|
|
|
def build():
|
|
|
|
waf = [sys.executable, 'waf.py', 'build']
|
|
|
|
waf_proc = subprocess.run(waf)
|
|
|
|
|
2016-07-15 04:16:25 +00:00
|
|
|
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.')
|
2017-07-28 21:24:37 +00:00
|
|
|
parser.add_argument('-log', default='log_sins', help='log to file.')
|
2016-07-21 02:08:33 +00:00
|
|
|
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-21 19:19:23 +00:00
|
|
|
parser.add_argument('-parent', default='build/generation.elf',
|
2017-07-20 21:16:59 +00:00
|
|
|
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-21 19:19:23 +00:00
|
|
|
logger.setLevel(
|
|
|
|
logging.DEBUG) if args.verbose else logger.setLevel(logging.INFO)
|
2017-07-20 21:16:59 +00:00
|
|
|
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
|
|
|
|
2017-07-28 21:24:37 +00:00
|
|
|
file_handler = logging.FileHandler(args.log, '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
|
|
|
|
2016-07-18 02:04:39 +00:00
|
|
|
if args.provision:
|
2017-07-21 19:19:23 +00:00
|
|
|
provision()
|
2016-07-18 02:19:05 +00:00
|
|
|
|
2017-08-31 22:20:30 +00:00
|
|
|
if args.build:
|
2017-07-21 19:19:23 +00:00
|
|
|
build()
|
2016-07-15 05:20:57 +00:00
|
|
|
|
2017-08-31 22:20:30 +00:00
|
|
|
if args.dumps:
|
2016-07-15 04:16:25 +00:00
|
|
|
hex_dumps(args.dir)
|
2017-07-20 21:16:59 +00:00
|
|
|
|
2017-08-31 22:20:30 +00:00
|
|
|
with Server() as server:
|
|
|
|
gen = Genorator(server=server, parent=args.parent,
|
|
|
|
seed=args.seed, run_dir=args.dir)
|