def update_ip_thread(self): while True: try: self.gateway_ip, _, self.host_ip = utils.get_default_route() except Exception: pass time.sleep(15)
def start(webserver_context): # Read from home directory the user_key. If non-existent, get one from # cloud. config_dict = utils.get_user_config() utils.log('[MAIN] Starting.') # Set up environment state = HostState() state.user_key = config_dict['user_key'] state.secret_salt = config_dict['secret_salt'] state.host_mac = utils.get_my_mac() state.gateway_ip, _, state.host_ip = utils.get_default_route() webserver_context['host_state'] = state assert utils.is_ipv4_addr(state.gateway_ip) assert utils.is_ipv4_addr(state.host_ip) state.packet_processor = PacketProcessor(state) utils.log('Initialized:', state.__dict__) # Continously discover devices arp_scan_thread = ArpScan(state) arp_scan_thread.start() # Continuously gather SSDP data netdisco_thread = NetdiscoWrapper(state) netdisco_thread.start() # Continuously capture packets packet_capture_thread = PacketCapture(state) packet_capture_thread.start() # Continously spoof ARP if '--no_spoofing' not in sys.argv: arp_spoof_thread = ArpSpoof(state) arp_spoof_thread.start() # Continuously upload data data_upload_thread = DataUploader(state) data_upload_thread.start()
def update_ip_thread(self): prev_gateway_ip = None prev_host_ip = None while True: try: self.gateway_ip, _, self.host_ip = utils.get_default_route() except Exception: pass # Upon network changes, clear ARP cache. if self.gateway_ip != prev_gateway_ip or \ self.host_ip != prev_host_ip: with self.lock: self.ip_mac_dict = {} prev_gateway_ip = self.gateway_ip prev_host_ip = self.host_ip time.sleep(15)
def main(): # Read from home directory the user_key. If non-existent, get one from # cloud. config_dict = utils.get_user_config() # Where user would see report url = server_config.REPORT_URL.format(user_key=config_dict['user_key']) # Open a web browser only if non-root if not is_root() and LAUNCH_WEB_BROWSER_UPON_START: if 'no_browser' not in sys.argv: webbrowser.open_new_tab(url) os_platform = sys.platform # Run as root if os_platform.startswith('linux'): elevate(graphical=False) else: elevate() assert is_root() utils.log('[MAIN] Starting.') # Set up environment state = HostState() state.user_key = config_dict['user_key'] state.secret_salt = config_dict['secret_salt'] state.host_mac = utils.get_my_mac() state.gateway_ip, _, state.host_ip = utils.get_default_route() assert utils.is_ipv4_addr(state.gateway_ip) assert utils.is_ipv4_addr(state.host_ip) state.packet_processor = PacketProcessor(state) utils.log('Initialized:', state.__dict__) # Enable kernal forwarding. if os_platform.startswith('darwin'): cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=1'] elif os_platform.startswith('linux'): cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1'] else: raise RuntimeError('Unsupported platform.') assert subprocess.call(cmd) == 0 # Continously discover devices arp_scan_thread = ArpScan(state) arp_scan_thread.start() # Continuously capture packets packet_capture_thread = PacketCapture(state) packet_capture_thread.start() # Continously spoof ARP arp_spoof_thread = ArpSpoof(state) arp_spoof_thread.start() # Continuously upload data data_upload_thread = DataUploader(state) data_upload_thread.start() # UI try: ui.start_main_ui(url, state) except KeyboardInterrupt: pass # Disable kernal forwarding. if os_platform.startswith('darwin'): cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=0'] elif os_platform.startswith('linux'): cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=0'] assert subprocess.call(cmd) == 0 utils.log('[MAIN] Done.')
def start(webserver_context): # Read from home directory the user_key. If non-existent, get one from # cloud. config_dict = utils.get_user_config() utils.log('[MAIN] Starting.') # Set up environment state = HostState() state.user_key = config_dict['user_key'].replace('-', '') state.secret_salt = config_dict['secret_salt'] state.host_mac = utils.get_my_mac() state.gateway_ip, _, state.host_ip = utils.get_default_route() webserver_context['host_state'] = state assert utils.is_ipv4_addr(state.gateway_ip) assert utils.is_ipv4_addr(state.host_ip) state.packet_processor = PacketProcessor(state) utils.log('Initialized:', state.__dict__) # Continously discover devices arp_scan_thread = ArpScan(state) arp_scan_thread.start() # Continuously gather SSDP data netdisco_thread = NetdiscoWrapper(state) netdisco_thread.start() # Continuously capture packets packet_capture_thread = PacketCapture(state) packet_capture_thread.start() # Continously spoof ARP if '--no_spoofing' not in sys.argv: arp_spoof_thread = ArpSpoof(state) arp_spoof_thread.start() # Continuously upload data data_upload_thread = DataUploader(state) data_upload_thread.start() # Suppress scapy warnings try: logging.getLogger("scapy.runtime").setLevel(logging.ERROR) except Exception: pass # Suppress flask messages try: logging.getLogger('werkzeug').setLevel(logging.ERROR) except Exception: pass if state.persistent_mode: # Insert a dash every four characters to make user-key easier to type pretty_user_key = '' for (ix, char) in enumerate(state.user_key): if (ix > 0) and (ix % 4 == 0): pretty_user_key += '-' pretty_user_key += char path = 'persistent/' + pretty_user_key caution = 'This is your private link. Open it only on trusted computers.' # noqa else: path = '' caution = '' print('\n' * 100) print(""" =========================== Princeton IoT Inspector =========================== View the IoT Inspector report at: https://inspector.cs.princeton.edu/{0} {1} Hit Control + C to terminate this process and stop data collection. """.format(path, caution))
def start(): """ Initializes inspector by spawning a number of background threads. Returns the host state once all background threats are started. """ # Read from home directory the user_key. If non-existent, get one from # cloud. config_dict = utils.get_user_config() utils.log('[MAIN] Starting.') # Set up environment state = HostState() state.user_key = config_dict['user_key'].replace('-', '') state.secret_salt = config_dict['secret_salt'] state.host_mac = utils.get_my_mac() state.gateway_ip, _, state.host_ip = utils.get_default_route() # Read special command-line arguments if '--raspberry_pi_mode' in sys.argv: state.raspberry_pi_mode = True assert utils.is_ipv4_addr(state.gateway_ip) assert utils.is_ipv4_addr(state.host_ip) state.packet_processor = PacketProcessor(state) utils.log('Initialized:', state.__dict__) # Continously discover devices arp_scan_thread = ArpScan(state) arp_scan_thread.start() # Continously discover ports via SYN scans syn_scan_thread = SynScan(state) syn_scan_thread.start() # Continuously gather SSDP data netdisco_thread = NetdiscoWrapper(state) netdisco_thread.start() # Continuously capture packets packet_capture_thread = PacketCapture(state) packet_capture_thread.start() # Continously spoof ARP if '--no_spoofing' not in sys.argv: arp_spoof_thread = ArpSpoof(state) arp_spoof_thread.start() # Continuously upload data data_upload_thread = DataUploader(state) data_upload_thread.start() # Suppress scapy warnings try: logging.getLogger("scapy.runtime").setLevel(logging.ERROR) except Exception: pass # Suppress flask messages try: logging.getLogger('werkzeug').setLevel(logging.ERROR) except Exception: pass # Insert a dash every four characters to make user-key easier to type pretty_user_key = '' for (ix, char) in enumerate(state.user_key): if (ix > 0) and (ix % 4 == 0): pretty_user_key += '-' pretty_user_key += char print('\n' * 100) os_platform = utils.get_os() print(WINDOWS_STARTUP_TEXT.format(server_config.BASE_URL, pretty_user_key)) # Open a browser window on Windows 10. Note that a new webpage will be # opened in a non-privileged mode. TODO: Not sure how to do the same # for macOS, as the "open" call on macOS will open a browser window # in privileged mode. if os_platform == 'windows': utils.open_browser_on_windows('{0}/user/{1}'.format( server_config.BASE_URL, pretty_user_key)) return state
def start(): """ Initializes inspector by spawning a number of background threads. Returns the host state once all background threats are started. """ # Read from home directory the user_key. If non-existent, get one from # cloud. config_dict = utils.get_user_config() utils.log('[MAIN] Starting.') # Set up environment state = HostState() state.user_key = config_dict['user_key'].replace('-', '') state.secret_salt = config_dict['secret_salt'] state.host_mac = utils.get_my_mac() state.gateway_ip, _, state.host_ip = utils.get_default_route() assert utils.is_ipv4_addr(state.gateway_ip) assert utils.is_ipv4_addr(state.host_ip) state.packet_processor = PacketProcessor(state) utils.log('Initialized:', state.__dict__) # Start web API webserver.start_thread(state) # Continously discover devices arp_scan_thread = ArpScan(state) arp_scan_thread.start() # Continously discover ports via SYN scans syn_scan_thread = SynScan(state) syn_scan_thread.start() # # Continuously gather SSDP data # netdisco_thread = NetdiscoWrapper(state) # netdisco_thread.start() # Continuously capture packets packet_capture_thread = PacketCapture(state) packet_capture_thread.start() # Continously spoof ARP if '--no_spoofing' not in sys.argv: arp_spoof_thread = ArpSpoof(state) arp_spoof_thread.start() # Suppress scapy warnings try: logging.getLogger("scapy.runtime").setLevel(logging.ERROR) except Exception: pass # Suppress flask messages try: logging.getLogger('werkzeug').setLevel(logging.ERROR) except Exception: pass # Insert a dash every four characters to make user-key easier to type pretty_user_key = '' for (ix, char) in enumerate(state.user_key): if (ix > 0) and (ix % 4 == 0): pretty_user_key += '-' pretty_user_key += char print( 'Ready. To test if the API works, visit http://127.0.0.1:46241/get_device_list' ) return state