예제 #1
0
 def createRequest(self, m, username=None, auth=None, cid=None, 
                     cseq=1, fromaddr=None, toaddr=None, contact=None):
     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
예제 #2
0
 def Register(self, extension, remotehost, auth=None, cid=None):
     m = self.method
     if cid is None:
         cid = '%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     cseq = 1
     # Embedding value so as to not run into errors
     localtag = '3206210844'.encode()
     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
예제 #3
0
 def start(self):
     # bind to 5060 - the reason is to maximize compatability with
     # devices that disregard the source port and send replies back
     # to port 5060
     if self.bindingip == '':
         bindingip = 'any'
     else:
         bindingip = self.bindingip
     self.log.debug("binding to %s:%s" % (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.warning("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, _, _ = 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)
                 host, port, *_ = srcaddr
                 self.log.debug('got data from %s:%s' % (str(host), str(port)))
                 self.log.debug('data: %s' % buff.__repr__())
                 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 = next(self.scaniter)
             except StopIteration:
                 self.log.debug('no more hosts to scan')
                 self.nomoretoscan = True
                 continue
             dstip,dstport,method = nextscan
             self.nextip = dstip
             dsthost = (dstip,dstport)
             domain = dsthost[0]
             branchunique = '%s' % random.getrandbits(32)
             if self.ipv6 and check_ipv6(dsthost[0]):
                 domain = '[' + dsthost[0] + ']'
                 localtag = createTag('%s%s' % (''.join(map(lambda x:
                     '%s' % x, dsthost[0].split(':'))), '%04x' % dsthost[1]))
             else:
                 localtag = createTag('%s%s' % (''.join(map(lambda x:
                     '%02x' % int(x), dsthost[0].split('.'))),'%04x' % dsthost[1]))
             if self.ipv6:
                 fromaddr = '"%s"<sip:100@%s>' % (self.fromname, domain)
             else:
                 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,
                             domain,
                             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.__repr__())
                 mysendto(self.sock,data,dsthost)
                 self.sentpackets += 1
                 #self.sock.sendto(data,dsthost)
                 if self.sessionpath is not None:
                     if next(self.packetcount):
                         try:
                             f=open(os.path.join(self.sessionpath,'lastip.pkl'),'wb+')
                             pickle.dump(self.nextip,f)
                             f.close()
                             self.log.debug('logged last ip %s' % self.nextip)
                         except IOError:
                             self.log.warning('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 as err:
                 self.log.error( "socket error while sending to %s:%s -> %s" % (dsthost[0],dsthost[1],err))
                 pass