def main():

    # The whole process should be run as root.
    if os.getuid() != 0:
        print >> sys.stderr, 'Please run as root.'
        sys.exit(1)

    print '\n' * 100
    print """
        ===========================
          Princeton IoT Inspector
        ===========================

        View the IoT Inspector report at:
        https://inspector.cs.princeton.edu

        Close this window when you are done.

    """

    utils.log('[Main] Terminating existing processes.')
    if not kill_existing_inspector():
        utils.log('[Main] Unable to end existing process. Exiting.')
        return

    utils.log('[Main] Starting webserver.')
    webserver.start_thread()

    utils.log('[Main] Starting inspector.')
    inspector.enable_ip_forwarding()
    utils.safe_run(inspector.start, args=(webserver.context, ))

    while not webserver.context['quit']:
        host_state = webserver.context['host_state']
        if host_state:
            with host_state.lock:
                if host_state.quit:
                    break
        try:
            time.sleep(2)
        except KeyboardInterrupt:
            break

    utils.log('[Main] Restoring ARP...')

    host_state = webserver.context['host_state']
    if host_state:
        with host_state.lock:
            host_state.spoof_arp = False

    time.sleep(10)

    inspector.disable_ip_forwarding()

    utils.log('[Main] Quit.')

    print '\n' * 100
    print """
def main():
    sc.load_layer("http")
    # The whole process should be run as root.
    try:
        is_admin = os.getuid() == 0
    except AttributeError:
        is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

    if not is_admin:
        sys.stderr.write('Please run as root.\n')
        sys.exit(1)

    utils.log('[Main] Terminating existing processes.')
    if not kill_existing_inspector():
        utils.log('[Main] Unable to end existing process. Exiting.')
        return

    utils.log('[Main] Starting webserver.')
    webserver.start_thread()

    utils.log('[Main] Starting inspector.')
    inspector.enable_ip_forwarding()
    utils.safe_run(inspector.start, args=(webserver.context,))

    while not webserver.context['quit']:
        host_state = webserver.context['host_state']
        if host_state:
            with host_state.lock:
                if host_state.quit:
                    break
        try:
            time.sleep(2)
        except KeyboardInterrupt:
            print('')
            break

    utils.log('[Main] Restoring ARP...')

    host_state = webserver.context['host_state']
    if host_state:
        with host_state.lock:
            host_state.spoof_arp = False

    for t in range(10):
        print('Cleaning up ({})...'.format(10 - t))
        time.sleep(1)

    inspector.disable_ip_forwarding()

    utils.log('[Main] Quit.')

    print('\n' * 100)
    print("""
        Princeton IoT Inspector has terminated.

        Feel free to close this window.

    """)
Exemplo n.º 3
0
def exit_inspector():

    log_http_request('/exit')

    inspector.disable_ip_forwarding()

    context['quit'] = True

    return 'OK'
Exemplo n.º 4
0
def pause_inspecting_traffic():

    log_http_request('/pause_inspecting_traffic')

    inspector.disable_ip_forwarding()

    host_state = context['host_state']
    if host_state is not None:
        with host_state.lock:
            host_state.is_inspecting_traffic = False

    return 'OK'
Exemplo n.º 5
0
def main():

    # The whole process should be run as root.
    if os.getuid() != 0:
        print >> sys.stderr, 'Please run as root.'
        sys.exit(1)

    utils.log('[Main] Terminating existing processes.')
    if not kill_existing_inspector():
        utils.log('[Main] Unable to end existing process. Exiting.')
        return

    utils.log('[Main] Starting webserver.')
    webserver.start_thread()

    utils.log('[Main] Starting inspector.')
    inspector.enable_ip_forwarding()
    utils.safe_run(inspector.start, args=(webserver.context, ))

    while not webserver.context['quit']:
        host_state = webserver.context['host_state']
        if host_state:
            with host_state.lock:
                if host_state.quit:
                    break
        try:
            time.sleep(2)
        except KeyboardInterrupt:
            print ''
            break

    utils.log('[Main] Restoring ARP...')

    host_state = webserver.context['host_state']
    if host_state:
        with host_state.lock:
            host_state.spoof_arp = False

    for t in range(10):
        print 'Cleaning up ({})...'.format(10 - t)
        time.sleep(1)

    inspector.disable_ip_forwarding()

    utils.log('[Main] Quit.')

    print '\n' * 100
    print """
def main():
    sc.load_layer("http")
    # The whole process should be run as root.
    try:
        is_admin = os.getuid() == 0
    except AttributeError:
        is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

    if not is_admin:
        sys.stderr.write('Please run as root.\n')
        sys.exit(1)

    # Check for Windows
    if utils.get_os() == 'windows':

        # Check Npcap installation
        npcap_path = os.path.join(os.environ['WINDIR'], 'System32', 'Npcap')
        if not os.path.exists(npcap_path):
            sys.stderr.write(
                "IoT Inspector cannot run without installing Npcap.\n")
            sys.stderr.write("For details, visit " +
                             server_config.NPCAP_ERROR_URL)
            utils.open_browser_on_windows(server_config.NPCAP_ERROR_URL)
            sys.exit(1)

        # Check presence of multiple interfaces (e.g., VPN)
        if len(utils.get_network_ip_range()) == 0:
            sys.stderr.write(
                "IoT Inspector cannot run with multiple network interfaces running.\n"
            )
            sys.stderr.write("For details, visit " +
                             server_config.NETMASK_ERROR_URL)
            utils.open_browser_on_windows(server_config.NETMASK_ERROR_URL)
            sys.exit(1)

    utils.log('[Main] Terminating existing processes.')
    if not kill_existing_inspector():
        utils.log('[Main] Unable to end existing process. Exiting.')
        return

    utils.log('[Main] Starting inspector.')
    inspector.enable_ip_forwarding()

    # We don't wrap the function below in safe_run because, well, if it crashes,
    # it crashes.
    host_state = inspector.start()

    # Waiting for termination
    while True:
        with host_state.lock:
            if host_state.quit:
                break
        try:
            time.sleep(2)
        except KeyboardInterrupt:
            print('')
            break

    utils.log('[Main] Restoring ARP...')

    with host_state.lock:
        host_state.spoof_arp = False

    for t in range(10):
        print('Cleaning up ({})...'.format(10 - t))
        time.sleep(1)

    inspector.disable_ip_forwarding()

    utils.log('[Main] Quit.')

    print('\n' * 100)
    print("""
        Princeton IoT Inspector has terminated.

        Feel free to close this window.

    """)

    # Remove PID file
    try:
        os.remove(get_pid_file())
    except Exception:
        pass