Esempio n. 1
0
 def get_if_bcast(self, iff):
     for net, msk, gw, iface, addr in self.routes:
         if (iff == iface and net != 0):
             bcast = atol(addr) | (~msk & 0xffffffff)
             # FIXME: check error in atol()
             return ltoa(bcast)
     warning("No broadcast address found for iface %s\n" % iff)
Esempio n. 2
0
 def ifadd(self, iff, addr):
     self.invalidate_cache()
     the_addr, the_msk = (addr.split("/") + ["32"])[:2]
     the_msk = itom(int(the_msk))
     the_rawaddr = atol(the_addr)
     the_net = the_rawaddr & the_msk
     self.routes.append((the_net, the_msk, '0.0.0.0', iff, the_addr))
Esempio n. 3
0
    def route(self, dest, verbose=None):
        if type(dest) is list and dest:
            dest = dest[0]
        if dest in self.cache:
            return self.cache[dest]
        if verbose is None:
            verbose = conf.verb
        # Transform "192.168.*.1-5" to one IP of the set
        dst = dest.split("/")[0]
        dst = dst.replace("*", "0")
        while True:
            l = dst.find("-")
            if l < 0:
                break
            m = (dst[l:] + ".").find(".")
            dst = dst[:l] + dst[l + m:]

        dst = atol(dst)
        pathes = []
        for d, m, gw, i, a in self.routes:
            aa = atol(a)
            #Commented out after issue with virtual network with local address 0.0.0.0
            #if aa == dst:
            #    pathes.append((0xffffffff,(LOOPBACK_NAME,a,"0.0.0.0")))
            if (dst & m) == (d & m):
                pathes.append((m, (i, a, gw)))
        if not pathes:
            if verbose:
                warning("No route found (no default route?)")
            return LOOPBACK_NAME, "0.0.0.0", "0.0.0.0"  #XXX linux specific!
        # Choose the more specific route (greatest netmask).
        # XXX: we don't care about metrics
        pathes.sort()
        ret = pathes[-1][1]
        self.cache[dest] = ret
        return ret
Esempio n. 4
0
    def ifchange(self, iff, addr):
        self.invalidate_cache()
        the_addr, the_msk = (addr.split("/") + ["32"])[:2]
        the_msk = itom(int(the_msk))
        the_rawaddr = atol(the_addr)
        the_net = the_rawaddr & the_msk

        for i in range(len(self.routes)):
            net, msk, gw, iface, addr = self.routes[i]
            if iface != iff:
                continue
            if gw == '0.0.0.0':
                self.routes[i] = (the_net, the_msk, gw, iface, the_addr)
            else:
                self.routes[i] = (net, msk, gw, iface, the_addr)
        conf.netcache.flush()
Esempio n. 5
0
def read_routes():
    routes = []
    if_index = '(\d+)'
    dest = '(\d+\.\d+\.\d+\.\d+)'
    mask = '(\d+\.\d+\.\d+\.\d+)'
    next_hop = '(\d+\.\d+\.\d+\.\d+)'
    metric_pattern = "(\d+)"
    delim = "\s+"  # The columns are separated by whitespace
    netstat_line = delim.join([if_index, dest, mask, next_hop, metric_pattern])
    pattern = re.compile(netstat_line)
    # This way works on Windows 7+ (probably as well on older Windows systems). Note the | ft in the end to keep table
    # format, as powershell will change from table to line based format when it has to print more than 4 columns
    ps = sp.Popen([
        'powershell', 'Get-WMIObject', 'Win32_IP4RouteTable', '|',
        'select InterfaceIndex, Destination, Mask, NextHop, Metric1', '|', 'ft'
    ],
                  stdout=sp.PIPE,
                  universal_newlines=True)
    stdout, stdin = ps.communicate(timeout=10)
    for l in stdout.split('\n'):
        match = re.search(pattern, l)
        if match:
            try:
                iface = devname_from_index(int(match.group(1)))
                addr = ifaces[iface].ip
            except:
                continue
            dest = atol(match.group(2))
            mask = itom(
                sum([
                    len(bin(int(a)).replace("0", "")) - 1
                    for a in match.group(3).split(".")
                ]))
            gw = match.group(4)
            # try:
            #     intf = pcapdnet.dnet.intf().get_dst(pcapdnet.dnet.addr(type=2, addrtxt=dest))
            # except OSError:
            #     log_loading.warning("Building Scapy's routing table: Couldn't get outgoing interface for destination %s" % dest)
            #     continue
            routes.append((dest, mask, gw, iface, addr))
    return routes
Esempio n. 6
0
 def make_route(self, host=None, net=None, gw=None, dev=None):
     if host is not None:
         thenet, msk = host, 32
     elif net is not None:
         thenet, msk = net.split("/")
         msk = int(msk)
     else:
         raise Scapy_Exception(
             "make_route: Incorrect parameters. You should specify a host or a net"
         )
     if gw is None:
         gw = "0.0.0.0"
     if dev is None:
         if gw:
             nhop = gw
         else:
             nhop = thenet
         dev, ifaddr, x = self.route(nhop)
     else:
         ifaddr = get_if_addr(dev)
     return (atol(thenet), itom(msk), gw, dev, ifaddr)