def lauch_hostapd(self): """ The method lauch_hostapd kill old instance of hostapd and launch a new one as a background process. :return: bool - if hostapd sucessfully launched. """ # Kill potential zombies of hostapd terminate_process("hostapd") sp.Popen(["ifconfig", self.iface_in, "up"]).wait() sp.Popen("/usr/sbin/hostapd /tmp/hostapd.conf > /tmp/hostapd.log", shell=True) while True: if path.isfile("/tmp/hostapd.log"): with open("/tmp/hostapd.log", "r") as f: log = f.read() err = [ "Could not configure driver mode", "driver initialization failed" ] if not any(e in log for e in err): if "AP-ENABLED" in log: return True else: return False time.sleep(1)
def stop_hostapd(self): """ Stop hostapd instance. :return: dict - a little message for debug. """ if terminate_process("hostapd"): return {"status": True, "message": "AP stopped"} else: return {"status": False, "message": "No AP running"}
def stop_capture(self): """ Stop tshark if any instance present & ask create_capinfos. :return: dict as a small confirmation. """ if terminate_process("tshark"): self.create_capinfos() return {"status": True, "message": "Capture stopped"} else: return {"status": False, "message": "No active capture"}
def wifi_connect(self): """ Connect to one of the WiFi networks present in the wpa_supplicant.conf. :return: dict containing the TinyCheck <-> AP status. """ # Kill wpa_supplicant instances, if any. terminate_process("wpa_supplicant") # Launch a new instance of wpa_supplicant. sp.Popen([ "wpa_supplicant", "-B", "-i", self.iface_out, "-c", "/etc/wpa_supplicant/wpa_supplicant.conf" ]).wait() # Check internet status for _ in range(1, 40): if self.check_internet(): return {"status": True, "message": "Wifi connected"} time.sleep(1) return {"status": False, "message": "Wifi not connected"}
def stop_capture(self): """ Stoping tshark if any instance present. :return: dict as a small confirmation. """ # Kill instance of tshark if any. if terminate_process("tshark"): return {"status": True, "message": "Capture stopped"} else: return {"status": False, "message": "No active capture"}
def start_capture(self): """ Start a tshark capture on the created AP interface and save it in a temporary directory under /tmp/. :return: dict containing capture token and status. """ # Kill potential tshark zombies instances, if any. terminate_process("tshark") # Few context variable assignment self.capture_token = "".join( [random.choice(self.random_choice_alphabet) for i in range(8)]) self.capture_dir = "/tmp/{}/".format(self.capture_token) self.assets_dir = "/tmp/{}/assets/".format(self.capture_token) self.pcap = self.capture_dir + "capture.pcap" self.iface = read_config(("network", "in")) # For packets monitoring self.list_pkts = [] self.last_pkts = 0 # Make the capture and the assets directory mkdir(self.capture_dir) mkdir(self.assets_dir) try: sp.Popen([ "tshark", "-i", self.iface, "-w", self.pcap, "-f", "tcp or udp" ]) return { "status": True, "message": "Capture started", "capture_token": self.capture_token } except: return { "status": False, "message": "Unexpected error: %s" % sys.exc_info()[0] }