Ejemplo n.º 1
0
 def getResponse(self):
     # we got stuff to read off the socket
     buff, _ = self.sock.recvfrom(8192)
     buff = buff.decode('utf-8', 'ignore')
     if buff.startswith(self.PROXYAUTHREQ):
         self.dstisproxy = True
     elif buff.startswith(self.AUTHREQ):
         self.dstisproxy = False
     if buff.startswith(self.PROXYAUTHREQ) or buff.startswith(self.AUTHREQ):
         authheader = getAuthHeader(buff)
         if authheader is None:
             self.log.critical(
                 "no authentication header found in response, cannot proceed"
             )
             exit(1)
         nonce = getNonce(authheader)
         opaque = getOpaque(authheader)
         algorithm = getAlgorithm(authheader)
         qop = getQop(authheader)
         cid = getCID(buff)
         if self.realm is None:
             self.realm = getRealm(buff)
         if None not in (nonce, self.realm):
             if self.reusenonce:
                 if len(self.challenges) > 0:
                     return
                 else:
                     self.staticnonce = nonce
                     self.staticcid = cid
             self.challenges.append([nonce, cid, qop, algorithm, opaque])
     elif buff.startswith(self.OKEY):
         self.passwordcracked = True
         _tmp = getCredentials(buff)
         if (_tmp is not None) and (len(_tmp) == 2):
             crackeduser, crackedpasswd = _tmp
             self.log.info("The password for %s is %s" %
                           (crackeduser.decode(), crackedpasswd.decode()))
             self.resultpasswd[crackeduser] = crackedpasswd
             if self.sessionpath is not None and self.dbsyncs:
                 self.resultpasswd.sync()
         else:
             self.log.info("Does not seem to require authentication")
             self.noauth = True
             self.resultpasswd[self.username] = '[no password]'
     elif buff.startswith(self.NOTFOUND):
         self.log.warning("User not found")
         self.noauth = True
     elif buff.startswith(self.INVALIDPASS):
         pass
     elif buff.startswith(self.TRYING):
         pass
     else:
         self.log.error("We got an unknown response")
         self.log.debug(buff.__repr__())
         self.nomore = True
Ejemplo n.º 2
0
    def getResponse(self):
        # we got stuff to read off the socket
        buff, srcaddr = self.sock.recvfrom(8192)
        if self.printdebug:
            print(srcaddr)
            print(buff)
        buff = buff.decode('utf-8')
        try:
            extension = getTag(buff).decode('utf-8', 'ignore')
        except (TypeError, AttributeError):
            self.log.error('could not decode to tag')
            extension = None
        if extension is None:
            self.nomore = True
            return
        try:
            firstline = buff.splitlines()[0]
        except (ValueError, IndexError, AttributeError):
            self.log.error("could not get the 1st line")
            return
        if self.enableack:
            # send an ack to any responses which match
            _tmp = parseHeader(buff)
            if not (_tmp and 'code' in _tmp):
                return
            if 699 > _tmp['code'] >= 200:
                self.log.debug('will try to send an ACK response')
                if 'headers' not in _tmp:
                    self.log.debug('no headers?')
                    return
                if 'from' not in _tmp['headers']:
                    self.log.debug('no from?')
                    return
                if 'cseq' not in _tmp['headers']:
                    self.log.debug('no cseq')
                    return
                if 'call-id' not in _tmp['headers']:
                    self.log.debug('no caller id')
                    return
                try:
                    # _tmp['headers']['from'][0].split('"')[1]
                    username = getTag(buff)
                except IndexError:
                    self.log.warning('could not parse the from address %s' % _tmp[
                                  'headers']['from'])
                    username = '******'
                cseq = _tmp['headers']['cseq'][0]
                cseqmethod = cseq.split()[1]
                if 'INVITE' == cseqmethod:
                    cid = _tmp['headers']['call-id'][0]
                    fromaddr = _tmp['headers']['from'][0]
                    toaddr = _tmp['headers']['to'][0]
                    ackreq = self.createRequest('ACK',
                                                cid=cid,
                                                cseq=cseq.replace(
                                                    cseqmethod, ''),
                                                fromaddr=fromaddr,
                                                toaddr=toaddr,
                                                )
                    self.log.debug('here is your ack request: %s' % ackreq)
                    mysendto(self.sock, ackreq, (self.dsthost, self.dstport))
                    # self.sock.sendto(ackreq,(self.dsthost,self.dstport))
                    if _tmp['code'] == 200:
                        byemsg = self.createRequest('BYE',
                                                    cid=cid,
                                                    cseq='2',
                                                    fromaddr=fromaddr,
                                                    toaddr=toaddr,
                                                    )
                        self.log.debug(
                            'sending a BYE to the 200 OK for the INVITE')
                        mysendto(self.sock, byemsg,
                                 (self.dsthost, self.dstport))

        if firstline != self.BADUSER:
            if buff.startswith(self.PROXYAUTHREQ) \
                    or buff.startswith(self.INVALIDPASS) \
                    or buff.startswith(self.AUTHREQ):
                if self.realm is None:
                    self.realm = getRealm(buff)
                self.log.info(
                    "extension '%s' exists - requires authentication" % extension)
                self.resultauth[extension] = 'reqauth'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            elif buff.startswith(self.TRYING):
                pass
            elif buff.startswith(self.RINGING):
                pass
            elif buff.startswith(self.OKEY):
                self.log.info(
                    "extension '%s' exists - authentication not required" % extension)
                self.resultauth[extension] = 'noauth'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
            else:
                self.log.warning(
                    "extension '%s' probably exists but the response is unexpected" % extension)
                self.log.debug("response: %s" % firstline)
                self.resultauth[extension] = 'weird'
                if self.sessionpath is not None and self.dbsyncs:
                    self.resultauth.sync()
        elif buff.startswith(self.NOTFOUND):
            self.log.debug("User '%s' not found" % extension)
        elif buff.startswith(self.INEXISTENTTRANSACTION):
            pass

        # Prefix not found, lets go to the next one. Should we add a warning
        # here???
        elif buff.startswith(self.SERVICEUN):
            pass
        elif buff.startswith(self.TRYING):
            pass
        elif buff.startswith(self.RINGING):
            pass
        elif buff.startswith(self.OKEY):
            pass
        elif buff.startswith(self.DECLINED):
            pass
        elif buff.startswith(self.NOTALLOWED):
            self.log.warning("method not allowed")
            self.nomore = True
            return
        elif buff.startswith(self.BADREQUEST):
            self.log.error(
                "Protocol / interopability error! The remote side most probably has problems with parsing your SIP messages!")
            self.nomore = True
            return
        else:
            self.log.warning("We got an unknown response")
            self.log.error("Response: %s" % buff.__repr__())
            self.log.debug("1st line: %s" % firstline.__repr__())
            self.log.debug("Bad user: %s" % self.BADUSER.__repr__())
            self.nomore = True