|
|
|
@ -1,21 +1,82 @@
|
|
|
|
|
#! /usr/bin/env python3 |
|
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
|
|
from random import randint |
|
|
|
|
from threading import Thread, Lock |
|
|
|
|
from time import sleep |
|
|
|
|
import binascii |
|
|
|
|
import ctypes |
|
|
|
|
import logging |
|
|
|
|
import os |
|
|
|
|
import socket |
|
|
|
|
import subprocess |
|
|
|
|
|
|
|
|
|
logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
class Sins: |
|
|
|
|
|
|
|
|
|
class Server: |
|
|
|
|
children = list() |
|
|
|
|
|
|
|
|
|
host = ('127.0.0.1', 5000) |
|
|
|
|
sock = socket.socket() |
|
|
|
|
sock.bind(host) |
|
|
|
|
sock.listen(1) |
|
|
|
|
client = None |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
self.lock = Lock() |
|
|
|
|
self.thread = Thread(target=self.recv) |
|
|
|
|
self.thread.start() |
|
|
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
|
self.client, address = self.sock.accept() |
|
|
|
|
logging.info('address [{}]'.format(address)) |
|
|
|
|
return self |
|
|
|
|
|
|
|
|
|
def __exit__(self, *args): |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
def __del__(self): |
|
|
|
|
self.sock.shutdown(socket.SHUT_RD) |
|
|
|
|
self.sock.close() |
|
|
|
|
|
|
|
|
|
def recv(self, size: int=int()) -> list: |
|
|
|
|
while self.client: |
|
|
|
|
try: |
|
|
|
|
child = self.client.recv(size) |
|
|
|
|
except OSError as error: |
|
|
|
|
logging.error('socket error [{}]'.format(error)) |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
if not child: |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
self.lock.acquire() |
|
|
|
|
self.children.append((child, datetime.utcnow())) |
|
|
|
|
self.lock.release() |
|
|
|
|
|
|
|
|
|
logging.info('recevied child') |
|
|
|
|
|
|
|
|
|
def send(self, data: bytes): |
|
|
|
|
if not self.client: |
|
|
|
|
raise BrokenPipeError('client connection missing') |
|
|
|
|
|
|
|
|
|
self.client.sendall(data) |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def children(self): |
|
|
|
|
if not self.children: |
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
children = self.children |
|
|
|
|
self.children = list() |
|
|
|
|
return children |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Genorator: |
|
|
|
|
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] |
|
|
|
|
self.parent = os.path.abspath(parent) |
|
|
|
|
|
|
|
|
|
if seed: |
|
|
|
|
with open(seed, 'rb') as seed_file: |
|
|
|
@ -30,15 +91,15 @@ class Sins:
|
|
|
|
|
|
|
|
|
|
self.run_dir = run_dir |
|
|
|
|
|
|
|
|
|
child = self.generation( |
|
|
|
|
parent=self.parent, |
|
|
|
|
scrap=self.seed, |
|
|
|
|
cwd=self.run_dir) |
|
|
|
|
parent_args = { |
|
|
|
|
'parent': self.parent, |
|
|
|
|
'scrap': self.seed, |
|
|
|
|
'cwd': self.run_dir} |
|
|
|
|
|
|
|
|
|
if child: |
|
|
|
|
logging.info(child) |
|
|
|
|
self.thread = Thread(target=self.generation, kwargs=parent_args) |
|
|
|
|
self.thread.start() |
|
|
|
|
|
|
|
|
|
def scrap_recent(self, *, run_dir: str)->str: |
|
|
|
|
def scrap_recent(self, *, run_dir: str) -> str: |
|
|
|
|
scraps = sorted(os.listdir(run_dir)) |
|
|
|
|
|
|
|
|
|
if scraps: |
|
|
|
@ -46,20 +107,30 @@ class Sins:
|
|
|
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
def generation(self, *, parent: ctypes.CDLL, scrap: bytes, cwd: str): |
|
|
|
|
def generation(self, *, parent: str, 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) |
|
|
|
|
cmd = [parent] |
|
|
|
|
logging.info(cmd) |
|
|
|
|
|
|
|
|
|
logging.info('status {}'.format(status)) |
|
|
|
|
logging.info('scrap_length [{}]'.format(scrap_len)) |
|
|
|
|
logging.info('scrap\n{}'.format(binascii.b2a_hex(scrap))) |
|
|
|
|
proc = subprocess.Popen( |
|
|
|
|
[parent], |
|
|
|
|
cwd=cwd) |
|
|
|
|
|
|
|
|
|
with Server() as server: |
|
|
|
|
server.send(bytes(len(scrap))) |
|
|
|
|
server.send(scrap) |
|
|
|
|
|
|
|
|
|
logging.info('child send') |
|
|
|
|
|
|
|
|
|
server.recv() |
|
|
|
|
children = server.children |
|
|
|
|
|
|
|
|
|
if children: |
|
|
|
|
raise Exception(children) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def hex_dumps(scrap_dir): |
|
|
|
@ -84,6 +155,19 @@ def hex_dumps(scrap_dir):
|
|
|
|
|
file_out.writelines('\'\\x{:02X}\',\n'.format(each)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def 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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build(): |
|
|
|
|
waf = [sys.executable, 'waf.py', 'build'] |
|
|
|
|
waf_proc = subprocess.run(waf) |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
import argparse |
|
|
|
|
import sys |
|
|
|
@ -98,7 +182,7 @@ if __name__ == '__main__':
|
|
|
|
|
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', |
|
|
|
|
parser.add_argument('-parent', default='build/generation.elf', |
|
|
|
|
help='path to generation lib.') |
|
|
|
|
parser.add_argument('-dir', default='sandbox', |
|
|
|
|
help='path to execution directory.') |
|
|
|
@ -107,7 +191,8 @@ if __name__ == '__main__':
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
|
logger = logging.getLogger() |
|
|
|
|
logger.setLevel(logging.DEBUG) if args.verbose else logger.setLevel(logging.INFO) |
|
|
|
|
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() |
|
|
|
@ -122,20 +207,13 @@ if __name__ == '__main__':
|
|
|
|
|
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) |
|
|
|
|
provision() |
|
|
|
|
|
|
|
|
|
elif args.build: |
|
|
|
|
waf = [sys.executable, 'waf.py', 'build'] |
|
|
|
|
waf_proc = subprocess.run(waf) |
|
|
|
|
logging.info(waf_proc.stdout) |
|
|
|
|
build() |
|
|
|
|
|
|
|
|
|
elif args.dumps: |
|
|
|
|
hex_dumps(args.dir) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
sins = Sins(parent=args.parent, seed=args.seed, run_dir=args.dir) |
|
|
|
|
gen = Genorator(parent=args.parent, seed=args.seed, run_dir=args.dir) |
|
|
|
|