From a61b4e6a55bbd1879756d8491d8f7c2e0244d6b2 Mon Sep 17 00:00:00 2001 From: Kareem El-Faramawi Date: Fri, 7 Jul 2017 16:10:22 -0400 Subject: [PATCH] Remove server and AppJailLauncher --- .gitmodules | 3 - tools/AppJailLauncher | 1 - tools/common.py | 88 ----------------------------- tools/server.py | 128 ------------------------------------------ 4 files changed, 220 deletions(-) delete mode 100644 .gitmodules delete mode 160000 tools/AppJailLauncher delete mode 100755 tools/server.py diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4d7e79bc4..000000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "tools/AppJailLauncher"] - path = tools/AppJailLauncher - url = https://github.com/trailofbits/AppJailLauncher diff --git a/tools/AppJailLauncher b/tools/AppJailLauncher deleted file mode 160000 index d2d6f18bd..000000000 --- a/tools/AppJailLauncher +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d2d6f18bd5e27d2936e66f84b118bdd5d6f47231 diff --git a/tools/common.py b/tools/common.py index 9a7d386d9..4b3f88690 100644 --- a/tools/common.py +++ b/tools/common.py @@ -8,94 +8,6 @@ IS_LINUX = 'linux' in sys.platform IS_WINDOWS = sys.platform == 'win32' -TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) -RUNNER = os.path.join(TOOLS_DIR, 'challenge_runner.py') -AJL = os.path.join(TOOLS_DIR, 'AppJailLauncher', 'Debug', 'AppJailLauncher.exe') - -# Key to get the server output fd -SERVER_OUT_KEY = 'SERVER_OUT_FD' - -# Keys to grab the replay sync pipe fds -_RP_R = 'REPLAY_PIPE_R' -_RP_W = 'REPLAY_PIPE_W' - -# Define all os-specific functions -if IS_WINDOWS: - import win32api - import win32pipe - import win32security - import win32file - - def rp_create(): - sattr = win32security.SECURITY_ATTRIBUTES() - sattr.bInheritHandle = 1 - r, w = win32pipe.CreatePipe(sattr, 0) - - os.putenv(_RP_R, str(int(r))) - os.putenv(_RP_W, str(int(w))) - return r, w - - def rp_send_sync(): - try: - fd = win32file._open_osfhandle(int(os.getenv(_RP_W)), os.O_APPEND) - os.write(fd, 'R') - os.close(fd) - except TypeError: - sys.stderr.write('Write end of sync pipe not specified\n') - except Exception as e: - print e - - def rp_recv_sync(): - try: - fd = win32file._open_osfhandle(int(os.getenv(_RP_R)), os.O_RDONLY) - while os.read(fd, 1) != 'R': - pass - os.close(fd) - except TypeError as e: - print e - sys.stderr.write('Read end of sync pipe not specified\n') - except Exception as e: - print e - - def rp_close(): - # Nothing to do here for windows - pass - - def terminate(proc): - try: - win32api.TerminateProcess(proc._handle, 1) - except: - # An exception is thrown if the process has already terminated - pass - -else: - PIPE_NAME = os.path.join(TOOLS_DIR, 'rpsync|') - - def rp_create(): - if not os.path.exists(PIPE_NAME): - os.mkfifo(PIPE_NAME) - - def rp_send_sync(): - try: - with open(PIPE_NAME, 'w+') as f: - f.write('R') - except TypeError: - sys.stderr.write('Write end of sync pipe not specified') - - def rp_recv_sync(): - try: - with open(PIPE_NAME, 'r') as f: - while f.read(1) != 'R': - pass - except TypeError: - sys.stderr.write('Read end of sync pipe not specified') - - def rp_close(): - os.remove(PIPE_NAME) - - def terminate(proc): - proc.terminate() - def debug(s): sys.stdout.write(str(s)) diff --git a/tools/server.py b/tools/server.py deleted file mode 100755 index 3c5adf9af..000000000 --- a/tools/server.py +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/env python2 - -import argparse -import os -import subprocess -import sys -import time -from threading import Thread -from SocketServer import StreamRequestHandler, ThreadingTCPServer - -from common import * - -if IS_WINDOWS: - import win32file - - -class ChallengeHandler(StreamRequestHandler): - challenges = [] - chal_timeout = 2 - use_signals = False - - def handle(self): - # Prepare the challenge runner - runner_cmd = [sys.executable, RUNNER, '-t', str(self.chal_timeout)] + self.challenges - if self.use_signals: - runner_cmd.append('--use-signals') - - # Start the challenge runner - sock = self.connection - subprocess.Popen(runner_cmd, stdin=sock, stdout=sock, stderr=sock).wait() - - -class LimitedConnectionServer(ThreadingTCPServer): - def __init__(self, server_address, handler, max_connections): - self.max_connections = max_connections - ThreadingTCPServer.__init__(self, server_address, handler) - - def process_request(self, request, client_address): - # Only the parent (server) will return from this - ThreadingTCPServer.process_request(self, request, client_address) - - # Check if we need to shutdown now - self.max_connections -= 1 - stdout_flush('Client connected! {} remaining\n'.format(self.max_connections)) - if self.max_connections <= 0: - stdout_flush('No more connections allowed, shutting down!\n') - Thread(target=self.shutdown).start() - - -def launch_ajl(args): - ajl_cmd = [ - AJL, - '/port:{}'.format(args.port), - '/nojail', '/timeout:20', - '"{}"'.format(' '.join([ - sys.executable, - RUNNER, - '-t', str(ChallengeHandler.chal_timeout) - ] + ChallengeHandler.challenges)) - ] - p = subprocess.Popen(' '.join(ajl_cmd)) - try: - with Timeout(args.timeout + 5): - while p.poll() is None: - time.sleep(0.1) - except TimeoutError: - terminate(p) - - -def main(): - parser = argparse.ArgumentParser() - - parser.add_argument('-p', '--port', required=True, type=int, - help='TCP port used for incoming connections') - parser.add_argument('-d', '--directory', required=True, - help='Directory containing the challenge binaries') - parser.add_argument('-m', '--max-connections', required=False, - type=int, default=0, - help='The number of connections this server will handle before shutting down') - parser.add_argument('-t', '--timeout', type=int, - help='The time in seconds that challenges are allowed to run before quitting' - ' (default is {} seconds)'.format(ChallengeHandler.chal_timeout)) - parser.add_argument('--use-signals', action='store_true', - help='Use signals to coordinate starting the challenges with another process') - parser.add_argument('challenge_binaries', nargs='+', - help='List of challenge binaries to run on the server') - - args = parser.parse_args(sys.argv[1:]) - - # Generate the full paths to all binaries in the request handler - cdir = os.path.abspath(args.directory) - for chal in args.challenge_binaries: - ChallengeHandler.challenges.append(os.path.join(cdir, chal)) - - # Set challenge timeout - if args.timeout and args.timeout > 0: - ChallengeHandler.chal_timeout = args.timeout - - # Set how the handler will start challenges - ChallengeHandler.use_signals = args.use_signals - - # Duplicate stdout for children to report back to - out_fd = os.dup(1) - if IS_WINDOWS: - # Get the HANDLE of the new stdout - out_fd = win32file._get_osfhandle(out_fd) - os.putenv(SERVER_OUT_KEY, str(out_fd)) - - # Start the challenge server - if IS_WINDOWS: - launch_ajl(args) - else: - ThreadingTCPServer.allow_reuse_address = True - if args.max_connections > 0: - srv = LimitedConnectionServer(('localhost', args.port), ChallengeHandler, args.max_connections) - else: - srv = ThreadingTCPServer(('localhost', args.port), ChallengeHandler) - try: - stdout_flush('Starting server at localhost:{}\n'.format(args.port)) - srv.serve_forever() - except KeyboardInterrupt: - pass - finally: - srv.server_close() - - -if __name__ == '__main__': - exit(main())