qira/middleware/qira.py

90 lines
3.1 KiB
Python
Raw Normal View History

2014-08-05 13:14:03 +04:00
#!/usr/bin/env python2.7
import os
2014-08-10 23:21:26 +04:00
import sys
2014-07-31 19:20:12 +04:00
basedir = os.path.dirname(os.path.realpath(__file__))
import argparse
import ipaddr
import socket
import threading
import time
import qira_config
import qira_socat
import qira_program
import qira_webserver
if __name__ == '__main__':
# define arguments
2014-07-29 01:08:10 +04:00
parser = argparse.ArgumentParser(description = 'Analyze binary. Like "qira /bin/ls /"')
parser.add_argument('-s', "--server", help="bind on port 4000. like socat", action="store_true")
parser.add_argument('-t', "--tracelibraries", help="trace into all libraries", action="store_true")
parser.add_argument('binary', help="path to the binary")
parser.add_argument('args', nargs='*', help="arguments to the binary")
2014-07-31 03:24:34 +04:00
parser.add_argument("--flush-cache", help="flush all QIRA caches", action="store_true")
2014-07-31 19:20:12 +04:00
parser.add_argument("--dwarf", help="parse program dwarf data", action="store_true")
if os.path.isdir(basedir+"/../cda"):
parser.add_argument("--cda", help="use CDA to view source(implies dwarf)", action="store_true")
2014-08-10 23:10:52 +04:00
parser.add_argument("--cda-only", help="use CDA to view source ONLY", action="store_true")
2014-08-04 23:35:10 +04:00
parser.add_argument("--pin", help="use pin as the backend", action="store_true")
parser.add_argument("--web-host", help="listen address for web interface. 127.0.0.1 by default", default=qira_config.WEB_HOST)
parser.add_argument("--web-port", help="listen port for web interface. 3002 by default", type=int, default=qira_config.WEB_PORT)
# parse arguments
args = parser.parse_args()
# validate arguments
if args.web_port < 1 or args.web_port > 65535:
raise Exception("--web-port must be a valid port number (1-65535)")
try:
args.web_host = ipaddr.IPAddress(args.web_host).exploded
except ValueError:
raise Exception("--web-host must be a valid IPv4/IPv6 address")
# handle arguments
2014-08-10 23:39:59 +04:00
if sys.argv[0][-3:] == "cda":
2014-08-10 23:21:26 +04:00
print "*** called as cda, not running QIRA"
args.cda_only = True
qira_config.WEB_HOST = args.web_host
qira_config.WEB_PORT = args.web_port
2014-08-04 23:35:10 +04:00
qira_config.USE_PIN = args.pin
if args.tracelibraries:
qira_config.TRACE_LIBRARIES = True
if args.dwarf:
qira_config.WITH_DWARF = True
2014-08-01 02:27:12 +04:00
try:
2014-08-10 23:10:52 +04:00
if args.cda or args.cda_only:
2014-08-01 02:27:12 +04:00
qira_config.WITH_CDA = True
qira_config.WITH_DWARF = True
except:
pass
2014-07-31 03:24:34 +04:00
if args.flush_cache:
print "*** flushing caches"
os.system("rm -rfv /tmp/qira*")
# creates the file symlink, program is constant through server run
program = qira_program.Program(args.binary, args.args)
is_qira_running = 1
try:
socket.create_connection(('127.0.0.1', qira_config.WEB_PORT))
if args.server:
raise Exception("can't run as server if QIRA is already running")
except:
is_qira_running = 0
print "no qira server found, starting it"
program.clear()
# start the binary runner
2014-08-10 23:10:52 +04:00
if not args.cda_only:
if args.server:
qira_socat.start_bindserver(program, qira_config.SOCAT_PORT, -1, 1, True)
else:
print "**** running "+program.program
program.execqira(shouldfork=not is_qira_running)
if not is_qira_running:
# start the http server
2014-07-25 08:34:10 +04:00
qira_webserver.run_server(args, program)