Example #1
0
 def createRequest(self,m,username=None,auth=None,cid=None,cseq=1,fromaddr=None,toaddr=None,contact=None):
     from base64 import b64encode
     from libs.svhelper import makeRequest
     from libs.svhelper import createTag
     if cid is None:
         cid='%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     localtag=createTag(username)
     if not contact:
         contact = 'sip:%s@%s' % (username,self.domain)
     if not fromaddr: 
         fromaddr = '"%s"<sip:%s@%s>' % (username,username,self.domain)
     if not toaddr: 
         toaddr = '"%s"<sip:%s@%s>' % (username,username,self.domain)        
     request = makeRequest(
                             m,
                             fromaddr,
                             toaddr,
                             self.domain,
                             self.dstport,
                             cid,
                             self.externalip,
                             branchunique,
                             cseq,
                             auth,
                             localtag,
                             self.compact,
                             contact=contact,
                             localport=self.localport,
                             extension=username
                           )
     return request
Example #2
0
 def Register(self,extension,remotehost,auth=None,cid=None):
     from libs.svhelper import makeRequest
     from libs.svhelper import createTag
     m = 'REGISTER'
     if cid is None:
         cid='%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     cseq = 1
     localtag=str(random.getrandbits(32))
     contact = 'sip:%s@%s' % (extension,remotehost)
     if auth is not None:
         cseq = 2
         localtag=createTag('%s:%s' % (self.auth['username'],self.auth['password']))
     register = makeRequest(
                                 m,
                                 '"%s" <sip:%s@%s>' % (extension,extension,self.domain),
                                 '"%s" <sip:%s@%s>' % (extension,extension,self.domain),
                                 self.domain,
                                 self.dstport,
                                 callid=cid,
                                 srchost=self.externalip,
                                 branchunique=branchunique,
                                 cseq=cseq,
                                 auth=auth,
                                 localtag=localtag,
                                 compact=self.compact,
                                 localport=self.localport
                               )
     return register
Example #3
0
 def Register(self, extension, remotehost, auth=None, cid=None):
     from libs.svhelper import makeRequest
     from libs.svhelper import createTag
     m = 'REGISTER'
     if cid is None:
         cid = '%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     cseq = 1
     localtag = str(random.getrandbits(32))
     contact = 'sip:%s@%s' % (extension, remotehost)
     if auth is not None:
         cseq = 2
         localtag = createTag(
             '%s:%s' % (self.auth['username'], self.auth['password']))
     register = makeRequest(
         m,
         '"%s" <sip:%s@%s>' % (extension, extension, self.domain),
         '"%s" <sip:%s@%s>' % (extension, extension, self.domain),
         self.domain,
         self.dstport,
         callid=cid,
         srchost=self.externalip,
         branchunique=branchunique,
         cseq=cseq,
         auth=auth,
         localtag=localtag,
         compact=self.compact,
         localport=self.localport)
     return register
Example #4
0
    def createRequest(self,m,username=None,auth=None,cid=None,cseq=1,fromaddr=None,toaddr=None,contact=None):
        """
        Method that create Reuest.

        :param m: string
        :param username: string
        :param auth: string
        :param cid: string
        :param cseq: integer
        :param fromaddr: string
        :param toaddr: string
        :param contact: string
        :return: request object
        """
        from base64 import b64encode
        from libs.svhelper import makeRequest
        from libs.svhelper import createTag
        # Generate new cid, by default it not set
        # Returns a python long int with k random bits.
        if cid is None:
            cid='%s' % str(random.getrandbits(32))
        # Do the same thing for variable branchunique
        branchunique = '%s' % random.getrandbits(32)
        localtag=createTag(username)
        if not contact:
            contact = 'sip:%s@%s' % (username,self.domain)
        if not fromaddr: 
            fromaddr = '"%s"<sip:%s@%s>' % (username,username,self.domain)
        if not toaddr: 
            toaddr = '"%s"<sip:%s@%s>' % (username,username,self.domain)
        # Make a registration request.
        # More about SIP registration http://tools.ietf.org/search/rfc3261#page-56
        request = makeRequest(
                                m,
                                fromaddr,
                                toaddr,
                                self.domain,
                                self.dstport,
                                cid,
                                self.externalip,
                                branchunique,
                                cseq,
                                auth,
                                localtag,
                                self.compact,
                                contact=contact,
                                localport=self.localport,
                                extension=username
                              )
        return request
Example #5
0
 def Register(self,extension,remotehost,auth=None,cid=None):
     '''
     Method for registration
     :param extension: string
     :param remotehost: string
     :param auth: string, default = None
     :param cid: string, default = None
     :return object
     '''
     from libs.svhelper import makeRequest
     from libs.svhelper import createTag
     m = 'REGISTER'
     # Generate new cid, by default it not set
     # Returns a python long int with k random bits.
     if cid is None:
         cid='%s' % str(random.getrandbits(32))
     # Do the same thing for variable branchunique
     branchunique = '%s' % random.getrandbits(32)
     cseq = 1
     localtag=str(random.getrandbits(32))
     contact = 'sip:%s@%s' % (extension,remotehost)
     if auth is not None:
         cseq = 2
         localtag=createTag('%s:%s' % (self.auth['username'],self.auth['password']))
     # Make a registration request.
     # More about SIP registration http://tools.ietf.org/search/rfc3261#page-56
     register = makeRequest(
                                 m,
                                 '"%s" <sip:%s@%s>' % (extension,extension,self.domain),
                                 '"%s" <sip:%s@%s>' % (extension,extension,self.domain),
                                 self.domain,
                                 self.dstport,
                                 callid=cid,
                                 srchost=self.externalip,
                                 branchunique=branchunique,
                                 cseq=cseq,
                                 auth=auth,
                                 localtag=localtag,
                                 compact=self.compact,
                                 localport=self.localport
                               )
     return register
Example #6
0
 def createRequest(self,
                   m,
                   username=None,
                   auth=None,
                   cid=None,
                   cseq=1,
                   fromaddr=None,
                   toaddr=None,
                   contact=None):
     from base64 import b64encode
     from libs.svhelper import makeRequest
     from libs.svhelper import createTag
     from libs.svhelper import check_ipv6
     if cid is None:
         cid = '%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     localtag = createTag(username)
     domain = self.domain
     if self.ipv6 and check_ipv6(domain):
         domain = '[' + self.domain + ']'
     if not contact:
         contact = 'sip:%s@%s' % (username, domain)
     if not fromaddr:
         fromaddr = '"%s"<sip:%s@%s>' % (username, username, domain)
     if not toaddr:
         toaddr = '"%s"<sip:%s@%s>' % (username, username, domain)
     request = makeRequest(m,
                           fromaddr,
                           toaddr,
                           domain,
                           self.dstport,
                           cid,
                           self.externalip,
                           branchunique,
                           cseq,
                           auth,
                           localtag,
                           self.compact,
                           contact=contact,
                           localport=self.localport,
                           extension=username)
     return request
Example #7
0
    def Register(self, extension, remotehost, auth=None, cid=None):
        from libs.svhelper import makeRequest
        from libs.svhelper import createTag
        from libs.svhelper import check_ipv6

        m = self.method
        if cid is None:
            cid = '%s' % str(random.getrandbits(32))
        branchunique = '%s' % random.getrandbits(32)
        cseq = 1
        localtag = str(random.getrandbits(32))
        if self.ipv6 and check_ipv6(remotehost):
            remotehost = '[' + remotehost + ']'
        contact = 'sip:%s@%s' % (extension, remotehost)
        if auth is not None:
            cseq = 2
            localtag = createTag(
                '%s:%s' % (self.auth['username'], self.auth['password']))
        domain = self.domain
        if self.ipv6 and check_ipv6(domain):
            domain = '[' + self.domain + ']'
        register = makeRequest(
            m,
            '"%s" <sip:%s@%s>' % (extension, extension, domain),
            '"%s" <sip:%s@%s>' % (extension, extension, domain),
            domain,
            self.dstport,
            callid=cid,
            srchost=self.externalip,
            branchunique=branchunique,
            cseq=cseq,
            auth=auth,
            localtag=localtag,
            compact=self.compact,
            localport=self.localport,
            requesturi=self.requesturi,
        )
        return register
Example #8
0
    def start(self):
        from libs.svhelper import makeRequest, createTag
        from libs.svhelper import mysendto
        import socket
        # bind to 5060 - the reason is to maximize compatability with
        # devices that disregard the source port and send replies back
        # to port 5060
        self.log.debug("binding to %s:%s" % (self.bindingip, self.localport))
        while 1:
            if self.localport > 65535:
                self.log.critical("Could not bind to any port")
                return
            try:
                self.sock.bind((self.bindingip, self.localport))
                break
            except socket.error:
                self.log.debug("could not bind to %s" % self.localport)
                self.localport += 1
        if self.originallocalport != self.localport:
            self.log.warn(
                "could not bind to %s:%s - some process might already be listening on this port. Listening on port %s instead"
                % (self.bindingip, self.originallocalport, self.localport))
            self.log.info(
                "Make use of the -P option to specify a port to bind to yourself"
            )
        while 1:
            r, w, e = select.select(self.rlist, self.wlist, self.xlist,
                                    self.selecttime)
            if r:
                # we got stuff to read off the socket
                try:
                    buff, srcaddr = self.sock.recvfrom(8192)
                    self.log.debug('got data from %s:%s' % srcaddr)
                    self.log.debug('data: %s' % ` buff `)
                    if self.printdebug:
                        print srcaddr
                        print buff
                except socket.error:
                    continue
                self.getResponse(buff, srcaddr)
            else:
                # no stuff to read .. its our turn to send back something
                if self.nomoretoscan:
                    try:
                        # having the final sip
                        self.log.debug("Making sure that no packets get lost")
                        self.log.debug("Come to daddy")
                        while 1:
                            buff, srcaddr = self.sock.recvfrom(8192)
                            if self.printdebug:
                                print srcaddr
                                print buff
                            self.getResponse(buff, srcaddr)
                    except socket.error:
                        break
                try:
                    nextscan = self.scaniter.next()
                except StopIteration:
                    self.log.debug('no more hosts to scan')
                    self.nomoretoscan = True
                    continue
                dstip, dstport, method = nextscan
                self.nextip = dstip
                dsthost = (dstip, dstport)
                branchunique = '%s' % random.getrandbits(32)

                localtag = createTag('%s%s' % (''.join(
                    map(lambda x: '%02x' % int(x),
                        dsthost[0].split('.'))), '%04x' % dsthost[1]))
                cseq = 1
                fromaddr = '"%s"<%s>' % (self.fromname, self.fromaddr)
                toaddr = fromaddr
                callid = '%s' % random.getrandbits(80)
                contact = None
                if method != 'REGISTER':
                    contact = 'sip:%s@%s:%s' % (
                        self.extension, self.externalip, self.localport)
                data = makeRequest(method,
                                   fromaddr,
                                   toaddr,
                                   dsthost[0],
                                   dsthost[1],
                                   callid,
                                   self.externalip,
                                   branchunique,
                                   compact=self.compact,
                                   localtag=localtag,
                                   contact=contact,
                                   accept='application/sdp',
                                   localport=self.localport,
                                   extension=self.extension)
                try:
                    self.log.debug("sending packet to %s:%s" % dsthost)
                    self.log.debug("packet: %s" % ` data `)
                    mysendto(self.sock, data, dsthost)
                    self.sentpackets += 1
                    #self.sock.sendto(data,dsthost)
                    if self.sessionpath is not None:
                        if self.packetcount.next():
                            try:
                                f = open(
                                    os.path.join(self.sessionpath,
                                                 'lastip.pkl'), 'w')
                                pickle.dump(self.nextip, f)
                                f.close()
                                self.log.debug('logged last ip %s' %
                                               self.nextip)
                            except IOError:
                                self.log.warn(
                                    'could not log the last ip scanned')
                    if self.first is not None:
                        if self.sentpackets >= self.first:
                            self.log.info(
                                'Reached the limit to scan the first %s packets'
                                % self.first)
                            self.nomoretoscan = True
                except socket.error, err:
                    self.log.error(
                        "socket error while sending to %s:%s -> %s" %
                        (dsthost[0], dsthost[1], err))
                    pass
Example #9
0
 def start(self):
     from libs.svhelper import makeRequest, createTag
     from libs.svhelper import mysendto
     import socket
     # bind to 5060 - the reason is to maximize compatability with
     # devices that disregard the source port and send replies back
     # to port 5060
     self.log.debug("binding to %s:%s" % (self.bindingip,self.localport))
     while 1:
         # Check if there any ports are available, print error message if not.
         if self.localport > 65535:
             self.log.critical("Could not bind to any port")
             return
         try:
         # Bind the socket to address. The socket must not already be bound.
             self.sock.bind((self.bindingip,self.localport))
             break
         # This exception is raised for socket-related errors. The accompanying value is either a string telling
         # what went wrong or a pair (errno, string) representing an error returned by a system call, similar
         # to the value accompanying os.error.
         except socket.error:
             self.log.debug("could not bind to %s" % self.localport)
             self.localport += 1            
     if self.originallocalport != self.localport:
         self.log.warn("could not bind to %s:%s - some process might already be listening on this port. Listening on port %s instead" % (self.bindingip,self.originallocalport, self.localport))
         self.log.info("Make use of the -P option to specify a port to bind to yourself")
     while 1:
         r, w, e = select.select(
             self.rlist,
             self.wlist,
             self.xlist,
             self.selecttime
             )
         if r:
             # we got stuff to read off the socket
             try:
                 buff,srcaddr = self.sock.recvfrom(8192)
                 self.log.debug('got data from %s:%s' % srcaddr)
                 self.log.debug('data: %s' % `buff`)
                 if self.printdebug:
                     print srcaddr
                     print buff
             except socket.error:
                 continue
             self.getResponse(buff,srcaddr)
         else:
             # no stuff to read .. its our turn to send back something
             if self.nomoretoscan:                    
                 try:
                     # having the final sip 
                     self.log.debug("Making sure that no packets get lost")
                     self.log.debug("Come to daddy")
                     while 1:
                         buff,srcaddr = self.sock.recvfrom(8192)
                         if self.printdebug:
                             print srcaddr
                             print buff
                         self.getResponse(buff,srcaddr)
                 except socket.error:
                     break
             try:
                 nextscan = self.scaniter.next()
             except StopIteration:
                 self.log.debug('no more hosts to scan')
                 self.nomoretoscan = True
                 continue
             dstip,dstport,method = nextscan
             self.nextip = dstip
             # Set destination IP and port
             dsthost = (dstip,dstport)
             # Generate new branchunique, by default it not set
             # Returns a python long int with k random bits.
             branchunique = '%s' % random.getrandbits(32)
             localtag = createTag('%s%s' % (''.join(map(lambda x: '%02x' % int(x), dsthost[0].split('.'))),'%04x' % dsthost[1]))
             cseq = 1
             fromaddr = '"%s"<%s>' % (self.fromname,self.fromaddr)
             toaddr = fromaddr
             callid = '%s' % random.getrandbits(80)
             contact = None
             if method != 'REGISTER':
                 contact = 'sip:%s@%s:%s' % (self.extension,self.externalip,self.localport)
             # Make a registration request.
             # More about SIP registration http://tools.ietf.org/search/rfc3261#page-56
             data = makeRequest(
                             method,
                             fromaddr,
                             toaddr,
                             dsthost[0],
                             dsthost[1],
                             callid,
                             self.externalip,
                             branchunique,
                             compact=self.compact,
                             localtag=localtag,
                             contact=contact,
                             accept='application/sdp',
                             localport=self.localport,
                             extension=self.extension
                             )
             try:
                 self.log.debug("sending packet to %s:%s" % dsthost)
                 self.log.debug("packet: %s" % `data`)
                 mysendto(self.sock,data,dsthost)
                 self.sentpackets += 1
                 #self.sock.sendto(data,dsthost)    
                 if self.sessionpath is not None:
                     if self.packetcount.next():
                         try:
                             # Open file for write
                             f=open(os.path.join(self.sessionpath,'lastip.pkl'),'w')
                             # Write a pickled representation of obj to the open file object file.
                             pickle.dump(self.nextip,f)
                             # Close file
                             f.close()
                             self.log.debug('logged last ip %s' % self.nextip)
                         except IOError:
                             self.log.warn('could not log the last ip scanned')
                 if self.first is not None:
                     if self.sentpackets >= self.first:
                         self.log.info('Reached the limit to scan the first %s packets' % self.first)
                         self.nomoretoscan = True
             except socket.error,err:
                 self.log.error( "socket error while sending to %s:%s -> %s" % (dsthost[0],dsthost[1],err))
                 pass
Example #10
0
 def start(self):
     from libs.svhelper import makeRequest, createTag
     from libs.svhelper import mysendto
     import socket
     # bind to 5060 - the reason is to maximize compatability with
     # devices that disregard the source port and send replies back
     # to port 5060
     self.log.debug("binding to %s:%s" % (self.bindingip,self.localport))
     while 1:
         if self.localport > 65535:
             self.log.critical("Could not bind to any port")
             return
         try:            
             self.sock.bind((self.bindingip,self.localport))
             break
         except socket.error:
             self.log.debug("could not bind to %s" % self.localport)
             self.localport += 1            
     if self.originallocalport != self.localport:
         self.log.warn("could not bind to %s:%s - some process might already be listening on this port. Listening on port %s instead" % (self.bindingip,self.originallocalport, self.localport))
         self.log.info("Make use of the -P option to specify a port to bind to yourself")
     while 1:
         r, w, e = select.select(
             self.rlist,
             self.wlist,
             self.xlist,
             self.selecttime
             )
         if r:
             # we got stuff to read off the socket
             try:
                 buff,srcaddr = self.sock.recvfrom(8192)
                 self.log.debug('got data from %s:%s' % srcaddr)
                 self.log.debug('data: %s' % `buff`)
                 if self.printdebug:
                     print srcaddr
                     print buff
             except socket.error:
                 continue
             self.getResponse(buff,srcaddr)
         else:
             # no stuff to read .. its our turn to send back something
             if self.nomoretoscan:                    
                 try:
                     # having the final sip 
                     self.log.debug("Making sure that no packets get lost")
                     self.log.debug("Come to daddy")
                     while 1:
                         buff,srcaddr = self.sock.recvfrom(8192)
                         if self.printdebug:
                             print srcaddr
                             print buff
                         self.getResponse(buff,srcaddr)
                 except socket.error:
                     break
             try:
                 nextscan = self.scaniter.next()
             except StopIteration:
                 self.log.debug('no more hosts to scan')
                 self.nomoretoscan = True
                 continue
             dstip,dstport,method = nextscan
             self.nextip = dstip
             dsthost = (dstip,dstport)
             branchunique = '%s' % random.getrandbits(32)
             
             localtag = createTag('%s%s' % (''.join(map(lambda x: '%02x' % int(x), dsthost[0].split('.'))),'%04x' % dsthost[1]))
             cseq = 1
             fromaddr = '"%s"<%s>' % (self.fromname,self.fromaddr)
             toaddr = fromaddr
             callid = '%s' % random.getrandbits(80)
             contact = None
             if method != 'REGISTER':
                 contact = 'sip:%s@%s:%s' % (self.extension,self.externalip,self.localport)
             data = makeRequest(
                             method,
                             fromaddr,
                             toaddr,
                             dsthost[0],
                             dsthost[1],
                             callid,
                             self.externalip,
                             branchunique,
                             compact=self.compact,
                             localtag=localtag,
                             contact=contact,
                             accept='application/sdp',
                             localport=self.localport,
                             extension=self.extension
                             )
             try:
                 self.log.debug("sending packet to %s:%s" % dsthost)
                 self.log.debug("packet: %s" % `data`)
                 mysendto(self.sock,data,dsthost)
                 self.sentpackets += 1
                 #self.sock.sendto(data,dsthost)    
                 if self.sessionpath is not None:
                     if self.packetcount.next():
                         try:
                             f=open(os.path.join(self.sessionpath,'lastip.pkl'),'w')
                             pickle.dump(self.nextip,f)
                             f.close()
                             self.log.debug('logged last ip %s' % self.nextip)
                         except IOError:
                             self.log.warn('could not log the last ip scanned')
                 if self.first is not None:
                     if self.sentpackets >= self.first:
                         self.log.info('Reached the limit to scan the first %s packets' % self.first)
                         self.nomoretoscan = True
             except socket.error,err:
                 self.log.error( "socket error while sending to %s:%s -> %s" % (dsthost[0],dsthost[1],err))
                 pass