Example #1
0
    def tryInterfaces(self, ifaces):
        try:
            from scapy.all import sr1   ## we want this check to be blocking
        except:
            log.msg("This test requires scapy: www.secdev.org/projects/scapy")
            raise SystemExit

        ifup = {}
        while ifaces:
            for ifname, ifaddr in ifaces:
                log.debug("Currently testing network capabilities of interface"
                          + "%s  by sending a packet to our address %s"
                          % (ifname, ifaddr))
                try:
                    pkt = IP(dst=ifaddr)/ICMP()
                    ans, unans = sr(pkt, iface=ifname, timeout=self.timeout)
                except Exception, e:
                    raise PermissionsError if e.find("Errno 1") else log.err(e)
                else:
                    ## xxx i think this logic might be wrong
                    log.debug("Interface test packet\n%s\n\n%s"
                              % (pkt.summary(), pkt.show2()))
                    if ans.summary():
                        log.info("Received answer for test packet on interface"
                                 +"%s :\n%s" % (ifname, ans.summary()))
                        ifup.update(ifname, ifaddr)
                    else:
                        log.info("Our interface test packet was unanswered:\n%s"
                                 % unans.summary())
Example #2
0
    def setUp(self, *a, **kw):
        '''
        :ivar ifaces:
            Struct returned from getifaddrs(3) and turned into a tuple in the
            form (*ifa_name, AF_FAMILY, *ifa_addr)
        '''

        if self.localOptions:
            log.debug("%s: local_options found" % self.name)
            for key, value in self.localOptions.items():
                log.debug("setting self.%s = %s" % (key, value))
                setattr(self, key, value)

        ## xxx is this now .subOptions?
        #self.inputFile = self.localOptions['file']
        self.timeout *= 1000            ## convert to milliseconds

        if not self.interface:
            log.msg("No network interface specified!")
            log.debug("OS detected: %s" % sys.platform)
            if LINUX or OPENBSD or NETBSD or FREEBSD or DARWIN or SOLARIS:
                from twisted.internet.test import _posixifaces
                log.msg("Attempting to discover network interfaces...")
                ifaces = _posixifaces._interfaces()
            elif WINDOWS:
                from twisted.internet.test import _win32ifaces
                log.msg("Attempting to discover network interfaces...")
                ifaces = _win32ifaces._interfaces()
            else:
                log.debug("Client OS %s not accounted for!" % sys.platform)
                log.debug("Unable to discover network interfaces...")
                ifaces = [('lo', '')]

            ## found = {'eth0': '1.1.1.1'}
            found = [{i[0]: i[2]} for i in ifaces if i[0] != 'lo']
            log.info("Found interfaces:\n%s" % pprint(found))
            self.interfaces = self.tryInterfaces(found)
        else:
            ## xxx need a way to check that iface exists, is up, and
            ## we have permissions on it
            log.debug("Our interface has been set to %s" % self.interface)

        if self.pcap:
            try:
                self.pcapfile = open(self.pcap, 'a+')
            except:
                log.msg("Unable to write to pcap file %s" % self.pcap)
                self.pcapfile = None

        try:
            assert os.path.isfile(self.file)
            fp = open(self.file, 'r')
        except Exception, e:
            hosts = ['8.8.8.8', '38.229.72.14']
            log.err(e)