Пример #1
0
 def _start_search(self, target="NOTHING",service=0,private=False,password=None,bitSize=5):
     """
     Search the specified target, ie. do a scan through wpa_supplicant.
     """
     self.startbuildScanTime = time.time()
     if(self.state == IDLE or self.state == CONNECTED):
         self._remove_old_nets()
         if(private==False):
             self.request = self._trippleHash(self._sanitize(target))
             logging.info("Publicly searching for: "+ target)
         else:
             logging.debug("Privately searching for: %s with password: %s" %(target ,password))
             if(password != None):
                 logging.info("Privately searching for: %s" %target)
                 self.request = self._trippleHash(self._sanitize(target))
                 self.request[2] = sofi_crypt.xorDecode(self.request[2],password)
                 self.request[1] = sofi_crypt.xorDecode(self.request[1],password)
                 self.request[0] = sofi_crypt.xorDecode(self.request[0],password)
                 #Encrypt Hash with Secret
                 #Set MAC known to requested User FIXME in future VERSION
             else:
                 logging.error("No Password specified in private search!")
         
         self.ComId = random.randint(0,255)
         self.start_time = time.time()
         ssid = sofi_ssid.ssid(hashString=self.request[2],private=private,request=True,service=service,Comid=self.ComId)
         self.nId = self.wpa.wpa_supplicant_connect(ssid.toString(), pwd=None)
         self.state = WAITING_FOR_RESPONSE
         self._start_timeout()
         self.endbuildScanTime = time.time()
         self.startScanDiscoverTime = time.time()
         return True
     else:
         logging.info("Already searching for another file. Please wait for timeout...")
         return False
Пример #2
0
 def _start_connect(self,
                    target,
                    service=0,
                    private=False,
                    password=None,
                    bitSize=5):
     """
     Get the specified target, ie. establish a connection through wpa_supplicant.
     """
     self.startbuildConnectTime = time.time()
     if (self.state == WAITING_FOR_RESPONSE):
         ssid = sofi_ssid.ssid(hashString=target,
                               private=private,
                               request=True,
                               reply=True,
                               service=service,
                               Comid=self.ComId,
                               bitSize=bitSize)
         self.ssid = ssid.toString()
         passwd = sofi_crypt.computePSK_hex(ssid.toString(), password)
         logging.info("Connecting to Network with WPA Key: %s" % passwd)
         self.nId = self.wpa.wpa_supplicant_connect(self.ssid, pwd=passwd)
         self.state = ANSWERED_PUZZLE
         self._start_timeout()
         self.endbuildConnectTime = time.time()
         self.startConnectDiscover = time.time()
         return True
     else:
         logging.info(
             "Already searching for another file. Please wait for timeout..."
         )
         return False
Пример #3
0
    def _start_search(self,
                      target="NOTHING",
                      service=0,
                      private=False,
                      password=None,
                      bitSize=5):
        """
        Search the specified target, ie. do a scan through wpa_supplicant.
        """
        self.startbuildScanTime = time.time()
        if (self.state == IDLE or self.state == CONNECTED):
            self._remove_old_nets()
            if (private == False):
                self.request = self._trippleHash(self._sanitize(target))
                logging.info("Publicly searching for: " + target)
            else:
                logging.debug("Privately searching for: %s with password: %s" %
                              (target, password))
                if (password != None):
                    logging.info("Privately searching for: %s" % target)
                    self.request = self._trippleHash(self._sanitize(target))
                    self.request[2] = sofi_crypt.xorDecode(
                        self.request[2], password)
                    self.request[1] = sofi_crypt.xorDecode(
                        self.request[1], password)
                    self.request[0] = sofi_crypt.xorDecode(
                        self.request[0], password)
                    #Encrypt Hash with Secret
                    #Set MAC known to requested User FIXME in future VERSION
                else:
                    logging.error("No Password specified in private search!")

            self.ComId = random.randint(0, 255)
            self.start_time = time.time()
            ssid = sofi_ssid.ssid(hashString=self.request[2],
                                  private=private,
                                  request=True,
                                  service=service,
                                  Comid=self.ComId)
            self.nId = self.wpa.wpa_supplicant_connect(ssid.toString(),
                                                       pwd=None)
            self.state = WAITING_FOR_RESPONSE
            self._start_timeout()
            self.endbuildScanTime = time.time()
            self.startScanDiscoverTime = time.time()
            return True
        else:
            logging.info(
                "Already searching for another file. Please wait for timeout..."
            )
            return False
Пример #4
0
    def listen(self):
        """
        Listen for incoming service requests and provide as necessary.
        """
        self.init_sockets()
        self.dnsmasq = pwifi_dnsmasq.DnsmasqCTRL()
        self.dnsmasq.start()
        self.webserver = sofi_web.sofi_web(self.database,self.stateIndex)
        
        while True:
            logging.info("Waiting for messages...")
            data, addr = self.receive_msg()
            s = addr
            if s is None:
                s = "hostapd"
            logging.info("Received message from %s: %s" % (s, data))

            split_data = data.split()
            ssid = split_data[-1]
            request = split_data[0]
            source_address = sofi_iftools.sanitizeMac(split_data[1]) #source_address MAC
            # Requests from legacy clients only contain the request keyword
            # and SSID. Requests from hostapd provide the IP address which
            # will be used for the BSS as well.
            ip = None
            if len(split_data) == 4:
                ip = split_data[2]

            if(sofi_ssid.isSofiSSID(ssid)):
                ssidObject = sofi_ssid.ssid(SSID=ssid)
                if request == IPC.PWIFI_IPC_REQUEST:
                    reply = self.handle_request(ssidObject,source_address, ip=ip)
                    if len(reply) != 0:
                        if addr is None:
                            logging.info("Sending reply to hostapd: %s" %reply)
                            logging.info("Hex encoded reply: %s" %(sofi_crypt.hex(reply))) 
                            self.sock_hostapd.sendto(reply, IPC.PWIFI_HOSTAPD_INTERFACE)
                        else:
                            logging.info("Sending reply to legacy client " + addr)
                            self.sock_legacy.sendto(reply, addr)
                    else:
                        logging.info("Empty reply. Not sending a reply.")
                elif request == IPC.PWIFI_IPC_DISBAND:
                    reply = self.handle_disband(ssidObject,source_address)
                    # hostapd does not expect a reply on disband messages -> reply is empty String
                else:
                    logging.error("Invalid message format. Ignored: %s" %data)
                    continue
            else:
                logging.error("Not a valid SSID! Ignored!")
                continue
Пример #5
0
 def _start_connect(self,target,service=0,private=False,password=None,bitSize=5):
     """
     Get the specified target, ie. establish a connection through wpa_supplicant.
     """
     self.startbuildConnectTime = time.time()
     if(self.state == WAITING_FOR_RESPONSE):            
         ssid = sofi_ssid.ssid(hashString=target,private=private,request=True,reply=True,service=service,Comid=self.ComId,bitSize=bitSize)
         self.ssid = ssid.toString()
         passwd = sofi_crypt.computePSK_hex(ssid.toString(),password)
         logging.info("Connecting to Network with WPA Key: %s" %passwd)
         self.nId = self.wpa.wpa_supplicant_connect(self.ssid, pwd=passwd)
         self.state = ANSWERED_PUZZLE
         self._start_timeout()
         self.endbuildConnectTime = time.time()
         self.startConnectDiscover = time.time()
         return True
     else:
         logging.info("Already searching for another file. Please wait for timeout...")
         return False
Пример #6
0
    def _eval_events(self, event):
        event = event[3:].split()
        if (event[0] == "CTRL-EVENT-BSS-ADDED"):
            apList = self.wpa.wpa_supplicant_get_scan()
            apList = apList.split('\n')
            apList = apList[1:-1]
            for line in apList:
                item = line.split()
                if (item[0] == event[2]):
                    if (len(item) < 5):
                        logging.error("Strange AP added: %s" % item)
                        return
                    logging.info("AP added: %s with SSID: %s" %
                                 (item[0], item[4]))
                    if (sofi_ssid.isSofiSSID(item[4])
                            and (self.state == WAITING_FOR_RESPONSE
                                 or self.state == ANSWERED_PUZZLE)):
                        ssid = sofi_ssid.ssid(SSID=item[4])
                        if (ssid.getID() == self.ComId):
                            logging.info(
                                "Received matching response with ComID: %s" %
                                self.ComId)
                            #This is a reply to a request we sent.
                            if (self.state == WAITING_FOR_RESPONSE):
                                logging.info(
                                    "Waited for response and response received!"
                                )
                                self._stop_timeout()
                                self.endScanDiscoverTime = time.time()
                                solution = self._start_puzzle(ssid)
                                logging.info(
                                    "Puzzle solved and setting up answer!")
                                self.wpa.wpa_supplicant_remove_net(self.nId)
                                self._start_connect(solution,
                                                    service=ssid.getService(),
                                                    private=ssid.isPrivate(),
                                                    password=self.request[0],
                                                    bitSize=ssid.getBitSize())
                            if (self.state == ANSWERED_PUZZLE):
                                logging.info(
                                    "Answered puzzle and response received: connecting to Network!"
                                )

        elif (event[0] == "CTRL-EVENT-CONNECTED"):
            logging.debug("Connected event received: %s" % event)
            if (event[7][4:] == self.nId):
                if (self.state != CONNECTED):
                    logging.info("Connected to network!")
                    self._stop_timeout()
                    self.connectedBSSID = event[4]
                    self.ie = self._get_ie(self.connectedBSSID, ForceSSID=True)
                    self.end_time = time.time()
                    self.endConnectDiscover = self.end_time
                    if (IS_EVAL):
                        self.state = CONNECTED
                        return
                    while (self.ie == None):
                        time.sleep(DELAY)
                        self.lastScan = self.wpa.wpa_supplicant_get_scan()
                        self.ie = self._get_ie(self.connectedBSSID,
                                               ForceSSID=True)
                    logging.info("Received IE: %s" % self.ie)
                    ie_ssid, ie_sofi = self._parse_ie(self.ie)
                    logging.info("Parsed IE: SSID: %s  So-Fi: %s" %
                                 (ie_ssid, ie_sofi))
                    self._eval_sofi_ie(ie_sofi)
                    self.state = CONNECTED
                else:
                    logging.info("Reauth to Network with Id: %s" % self.nId)
            else:
                self.ie = self._get_ie(event[4], ForceSSID=False)
                logging.info("Received IE: %s" % self.ie)
        elif (event[0] == "CTRL-EVENT-DISCONNECTED"):
            logging.debug("Disconnect event received: %s" % event)
            if (self.state == CONNECTED):
                bssid = event[1][6:]
                logging.info("Disconnected from Network with BBSID: %s" %
                             bssid)
                if (bssid == self.connectedBSSID):
                    logging.info("Disconnected from So-Fi Network!")
                    self.wpa.wpa_supplicant_remove_net(self.nId)
        elif (event[0] == "CTRL-EVENT-SCAN-RESULTS"):
            self.lastScan = self.wpa.wpa_supplicant_get_scan()
        else:
            logging.debug(event)
Пример #7
0
    def _eval_events(self,event):
        event = event[3:].split()
        if(event[0]=="CTRL-EVENT-BSS-ADDED"):
            apList = self.wpa.wpa_supplicant_get_scan()
            apList = apList.split('\n')
            apList = apList[1:-1]
            for line in apList:
                item = line.split()
                if(item[0]==event[2]):
                    if(len(item)<5):
                        logging.error("Strange AP added: %s" %item)
                        return
                    logging.info("AP added: %s with SSID: %s" %(item[0],item[4]))                    
                    if(sofi_ssid.isSofiSSID(item[4]) and (self.state == WAITING_FOR_RESPONSE or self.state == ANSWERED_PUZZLE)):
                        ssid = sofi_ssid.ssid(SSID=item[4])
                        if(ssid.getID()==self.ComId):
                            logging.info("Received matching response with ComID: %s" %self.ComId)
                            #This is a reply to a request we sent.
                            if(self.state == WAITING_FOR_RESPONSE):
                                logging.info("Waited for response and response received!")
                                self._stop_timeout()
                                self.endScanDiscoverTime = time.time()
                                solution = self._start_puzzle(ssid)
                                logging.info("Puzzle solved and setting up answer!")
                                self.wpa.wpa_supplicant_remove_net(self.nId)
                                self._start_connect(solution, service=ssid.getService(), private=ssid.isPrivate(), password=self.request[0], bitSize=ssid.getBitSize())
                            if(self.state == ANSWERED_PUZZLE):
                                logging.info("Answered puzzle and response received: connecting to Network!")

        elif(event[0]=="CTRL-EVENT-CONNECTED"):
            logging.debug("Connected event received: %s" %event)
            if(event[7][4:]==self.nId):
                if(self.state != CONNECTED):
                    logging.info("Connected to network!")
                    self._stop_timeout()
                    self.connectedBSSID = event[4]
                    self.ie = self._get_ie(self.connectedBSSID,ForceSSID=True)
                    self.end_time = time.time()
                    self.endConnectDiscover = self.end_time
                    if(IS_EVAL):
                        self.state = CONNECTED
                        return
                    while(self.ie == None):
                        time.sleep(DELAY)
                        self.lastScan = self.wpa.wpa_supplicant_get_scan()
                        self.ie = self._get_ie(self.connectedBSSID,ForceSSID=True)
                    logging.info("Received IE: %s" %self.ie)
                    ie_ssid,ie_sofi = self._parse_ie(self.ie)
                    logging.info("Parsed IE: SSID: %s  So-Fi: %s" %(ie_ssid,ie_sofi))
                    self._eval_sofi_ie(ie_sofi)
                    self.state = CONNECTED
                else:
                    logging.info("Reauth to Network with Id: %s" %self.nId)
            else:
                self.ie = self._get_ie(event[4],ForceSSID=False)
                logging.info("Received IE: %s" %self.ie)
        elif(event[0]=="CTRL-EVENT-DISCONNECTED"):
            logging.debug("Disconnect event received: %s" %event)
            if(self.state == CONNECTED):
                bssid = event[1][6:]
                logging.info("Disconnected from Network with BBSID: %s" %bssid)
                if(bssid == self.connectedBSSID):
                    logging.info("Disconnected from So-Fi Network!")
                    self.wpa.wpa_supplicant_remove_net(self.nId)
        elif(event[0]=="CTRL-EVENT-SCAN-RESULTS"):
            self.lastScan = self.wpa.wpa_supplicant_get_scan()      
        else:
            logging.debug(event)