Пример #1
0
#
# Simple recipe for using the code.  Example uses the ipaddr
# module, but that isn't a requirement (just makes things
# nicer).
#

import ipaddr
import ipaddrgen

net = ipaddr.IPv4Network('127.0.0.0/8')
print int(net),net.prefixlen

t = ipaddrgen.initialize_trie(int(net), net.prefixlen, 0.61)
for i in xrange(10000):
    a = ipaddr.IPv4Address(ipaddrgen.generate_addressv4(t))
    print a

n = ipaddrgen.count_nodes(t)
print "nodes",n
ipaddrgen.release_trie(t)

Пример #2
0
    def __init__(self, srcnode, ipsrc='0.0.0.0', ipdst='0.0.0.0', sport=0, dport=0, flowsize=1500, pktsize=1500, flowstart=0, ipproto=socket.IPPROTO_TCP, lossrate=0.001, mss=1460, iptos=0x0, xopen=True, tcpmodel='csa00'):
        TrafficGenerator.__init__(self, srcnode)
        self.logger = get_logger('fs.harpoon')
        self.srcnet = ipaddr.IPNetwork(ipsrc)
        self.dstnet = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.srcnet), self.srcnet.prefixlen, 0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.dstnet), self.dstnet.prefixlen, 0.61)

        if isinstance(ipproto, (str,unicode)):
            self.ipproto = eval(ipproto)
        else: 
            self.ipproto = randomchoice(ipproto)

        if isinstance(sport, (str,unicode)):
            self.srcports = eval(sport)
        else:
            self.srcports = randomchoice(sport)

        if isinstance(dport, (str,unicode)):
            self.dstports = eval(dport)
        else:
            self.dstports = randomchoice(dport)

        if isinstance(flowsize, (str,unicode)):
            self.flowsizerv = eval(flowsize)
        else:
            self.flowsizerv = randomchoice(flowsize)

        if isinstance(pktsize, (str,unicode)):
            self.pktsizerv = eval(pktsize)
        else:
            self.pktsizerv = randomchoice(pktsize)

        if isinstance(flowstart, (str,unicode)):
            self.flowstartrv = eval(flowstart)
        else:
            self.flowstartrv = randomchoice(flowstart)

        if isinstance(lossrate, (str,unicode)):
            self.lossraterv = eval(lossrate)
        else:
            self.lossraterv = randomchoice(lossrate)

        if isinstance(mss, (str,unicode)):
            self.mssrv = eval(mss)
        else:
            self.mssrv = randomchoice(mss)

        if isinstance(iptos, (str,unicode)):
            self.iptosrv = eval(iptos)
        else:
            self.iptosrv = randomchoice(iptos)

        self.xopen = xopen
        self.activeflows = {}

        try:
            self.tcpmodel = import_module("tcpmodels.{}".format(tcpmodel))
        except ImportError,e:
            raise InvalidFlowConfiguration('Unrecognized tcp model for harpoon: {} (Error on import: {})'.format(tcpmodel, str(e)))
Пример #3
0
    def __init__(self, sim, srcnode, ipsrc='0.0.0.0', ipdst='0.0.0.0', sport=0, dport=0, flowsize=1500, pktsize=1500, flowstart=0, ipproto=socket.IPPROTO_TCP, lossrate=0.001, mss=1460, emitprocess='randomchoice(x)', iptos=0x0, xopen=True, tcpmodel='csa00'):
        GeneratorNode.__init__(self, sim, srcnode)
        self.srcnet = ipaddr.IPNetwork(ipsrc)
        self.dstnet = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.srcnet), self.srcnet.prefixlen, 0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.dstnet), self.dstnet.prefixlen, 0.61)

        if isinstance(ipproto, str):
            self.ipproto = eval(ipproto)
        else:
            self.ipproto = randomchoice(ipproto)

        if isinstance(sport, str):
            self.srcports = eval(sport)
        else:
            self.srcports = randomchoice(sport)

        if isinstance(dport, str):
            self.dstports = eval(dport)
        else:
            self.dstports = randomchoice(dport)

        if isinstance(flowsize, str):
            self.flowsizerv = eval(flowsize)
        else:
            self.flowsizerv = randomchoice(flowsize)

        if isinstance(pktsize, str):
            self.pktsizerv = eval(pktsize)
        else:
            self.pktsizerv = randomchoice(pktsize)

        if isinstance(flowstart, str):
            self.flowstartrv = eval(flowstart)
        else:
            self.flowstartrv = randomchoice(flowstart)

        if isinstance(lossrate, str):
            self.lossraterv = eval(lossrate)
        else:
            self.lossraterv = randomchoice(lossrate)

        if isinstance(mss, str):
            self.mssrv = eval(mss)
        else:
            self.mssrv = randomchoice(mss)

        if isinstance(iptos, str):
            self.iptosrv = eval(iptos)
        else:
            self.iptosrv = randomchoice(iptos)

        self.emitrvstr = emitprocess

        self.xopen = xopen
        self.activeflows = {}
        self.tcpmodel = tcpmodel
        if self.tcpmodel not in ['mathis','msmo97','csa00']:
            raise InvalidFlowConfiguration('Unrecognized tcp model for harpoon:'+str(tcpmodel))

        self.anoFlag = False
        # import ipdb;ipdb.set_trace()
        if EXPORT_ABNORMAL_FLOW == 'TRUE':
            self.set_ano_flag(flowsize, flowstart, ipsrc)
Пример #4
0
    def __init__(self, sim, srcnode, ipsrc=None, ipdst=None, ipproto=None,
                 dport=None, sport=None, continuous=True, flowlets=None, tcpflags=None, iptos=None,
                 fps=None, pps=None, bps=None, pkts=None, bytes=None, pktsize=None,
                 icmptype=None, icmpcode=None, interval=None, autoack=False):
        GeneratorNode.__init__(self, sim, srcnode)
        # assume that all keyword params arrive as strings
        # print ipsrc,ipdst
        self.ipsrc = ipaddr.IPNetwork(ipsrc)
        self.ipdst = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.ipsrc), self.ipsrc.prefixlen, 0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.ipdst), self.ipdst.prefixlen, 0.61)


        self.sport = self.dport = None
        self.icmptype = self.icmpcode = None
        self.autoack = False
        if autoack:
            self.autoack = eval(autoack)
        try:
            self.ipproto = int(ipproto)
        except:
            if ipproto == 'tcp':
                self.ipproto = socket.IPPROTO_TCP
            elif ipproto == 'udp':
                self.ipproto = socket.IPPROTO_UDP
            elif ipproto == 'icmp':
                self.ipproto = socket.IPPROTO_ICMP
            else:
                raise InvalidFlowConfiguration('Unrecognized protocol:'+str(ipproto))

        if not iptos:
            self.iptos = randomchoice(0x0)
        else:
            self.iptos = eval(iptos)
            if isinstance(self.iptos, int):
                self.iptos = randomchoice(self.iptos)

        if self.ipproto == socket.IPPROTO_ICMP:
            xicmptype = xicmpcode = 0
            if icmptype:
                xicmptype = eval(icmptype)
            if icmpcode:
                xicmpcode = eval(icmpcode)
            if isinstance(xicmptype, int):
                xicmptype = randomchoice(xicmptype)
            if isinstance(xicmpcode, int):
                xicmpcode = randomchoice(xicmpcode)
            self.icmptype = xicmptype
            self.icmpcode = xicmpcode
        elif self.ipproto == socket.IPPROTO_UDP or self.ipproto == socket.IPPROTO_TCP:
            self.dport = eval(dport)
            if isinstance(self.dport, int):
                self.dport = randomchoice(self.dport)
            self.sport = eval(sport)
            if isinstance(self.sport, int):
                self.sport = randomchoice(self.sport)
            # print 'sport,dport',self.sport, self.dport
            if self.ipproto == socket.IPPROTO_TCP:
                self.tcpflags = randomchoice('')
                if tcpflags:
                    if re.search('\(\S+\)', tcpflags):
                        self.tcpflags = eval(tcpflags)
                    else:
                        self.tcpflags = randomchoice(tcpflags)
        else:
            self.dport = self.sport = 0

        self.continuous = None
        self.nflowlets = None
        if continuous:
            self.continuous = eval(continuous)

        if flowlets:
            self.nflowlets = eval(flowlets)
            if isinstance(self.nflowlets, (int, float)):
                self.nflowlets = randomchoice(self.nflowlets)

        if not self.nflowlets:
            self.nflowlets = randomchoice(1)


        if not fps and not interval:
            raise InvalidFlowConfiguration('Need one of fps or interval in rawflow configuration.')

        self.fps = self.interval = None
        if fps:
            fps = eval(fps)
            if isinstance(fps, int):
                fps = randomchoice(fps)
            self.fps = fps
        elif interval:
            self.interval = eval(interval)
            if isinstance(self.interval, (int, float)):
                self.interval = randomchoice(self.interval)

        assert(bytes)
        self.bytes = eval(bytes)
        if isinstance(self.bytes, int):
            self.bytes = randomchoice(self.bytes)

        self.pkts = self.pktsize = None

        if pkts:
            self.pkts = eval(pkts)
            if isinstance(self.pkts, int):
                self.pkts = randomchoice(self.pkts)

        if pktsize:
            self.pktsize = eval(pktsize)
            if isinstance(self.pktsize, int):
                self.pktsize = randomchoice(self.pktsize)

        assert(self.fps or self.interval)
        assert(self.pkts or self.pktsize)
Пример #5
0
    def __init__(self,
                 sim,
                 srcnode,
                 ipsrc=None,
                 ipdst=None,
                 ipproto=None,
                 dport=None,
                 sport=None,
                 continuous=True,
                 flowlets=None,
                 tcpflags=None,
                 iptos=None,
                 fps=None,
                 pps=None,
                 bps=None,
                 pkts=None,
                 bytes=None,
                 pktsize=None,
                 icmptype=None,
                 icmpcode=None,
                 interval=None,
                 autoack=False):
        GeneratorNode.__init__(self, sim, srcnode)
        # assume that all keyword params arrive as strings
        # print ipsrc,ipdst
        self.ipsrc = ipaddr.IPNetwork(ipsrc)
        self.ipdst = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.ipsrc),
                                                      self.ipsrc.prefixlen,
                                                      0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.ipdst),
                                                      self.ipdst.prefixlen,
                                                      0.61)

        self.sport = self.dport = None
        self.icmptype = self.icmpcode = None
        self.autoack = False
        if autoack:
            self.autoack = eval(autoack)
        try:
            self.ipproto = int(ipproto)
        except:
            if ipproto == 'tcp':
                self.ipproto = socket.IPPROTO_TCP
            elif ipproto == 'udp':
                self.ipproto = socket.IPPROTO_UDP
            elif ipproto == 'icmp':
                self.ipproto = socket.IPPROTO_ICMP
            else:
                raise InvalidFlowConfiguration('Unrecognized protocol:' +
                                               str(ipproto))

        if not iptos:
            self.iptos = randomchoice(0x0)
        else:
            self.iptos = eval(iptos)
            if isinstance(self.iptos, int):
                self.iptos = randomchoice(self.iptos)

        if self.ipproto == socket.IPPROTO_ICMP:
            xicmptype = xicmpcode = 0
            if icmptype:
                xicmptype = eval(icmptype)
            if icmpcode:
                xicmpcode = eval(icmpcode)
            if isinstance(xicmptype, int):
                xicmptype = randomchoice(xicmptype)
            if isinstance(xicmpcode, int):
                xicmpcode = randomchoice(xicmpcode)
            self.icmptype = xicmptype
            self.icmpcode = xicmpcode
        elif self.ipproto == socket.IPPROTO_UDP or self.ipproto == socket.IPPROTO_TCP:
            self.dport = eval(dport)
            if isinstance(self.dport, int):
                self.dport = randomchoice(self.dport)
            self.sport = eval(sport)
            if isinstance(self.sport, int):
                self.sport = randomchoice(self.sport)
            # print 'sport,dport',self.sport, self.dport
            if self.ipproto == socket.IPPROTO_TCP:
                self.tcpflags = randomchoice('')
                if tcpflags:
                    if re.search('\(\S+\)', tcpflags):
                        self.tcpflags = eval(tcpflags)
                    else:
                        self.tcpflags = randomchoice(tcpflags)
        else:
            self.dport = self.sport = 0

        self.continuous = None
        self.nflowlets = None
        if continuous:
            self.continuous = eval(continuous)

        if flowlets:
            self.nflowlets = eval(flowlets)
            if isinstance(self.nflowlets, (int, float)):
                self.nflowlets = randomchoice(self.nflowlets)

        if not self.nflowlets:
            self.nflowlets = randomchoice(1)

        if not fps and not interval:
            raise InvalidFlowConfiguration(
                'Need one of fps or interval in rawflow configuration.')

        self.fps = self.interval = None
        if fps:
            fps = eval(fps)
            if isinstance(fps, int):
                fps = randomchoice(fps)
            self.fps = fps
        elif interval:
            self.interval = eval(interval)
            if isinstance(self.interval, (int, float)):
                self.interval = randomchoice(self.interval)

        assert (bytes)
        self.bytes = eval(bytes)
        if isinstance(self.bytes, int):
            self.bytes = randomchoice(self.bytes)

        self.pkts = self.pktsize = None

        if pkts:
            self.pkts = eval(pkts)
            if isinstance(self.pkts, int):
                self.pkts = randomchoice(self.pkts)

        if pktsize:
            self.pktsize = eval(pktsize)
            if isinstance(self.pktsize, int):
                self.pktsize = randomchoice(self.pktsize)

        assert (self.fps or self.interval)
        assert (self.pkts or self.pktsize)
Пример #6
0
    def __init__(self,
                 sim,
                 srcnode,
                 ipsrc='0.0.0.0',
                 ipdst='0.0.0.0',
                 sport=0,
                 dport=0,
                 flowsize=1500,
                 pktsize=1500,
                 flowstart=0,
                 ipproto=socket.IPPROTO_TCP,
                 lossrate=0.001,
                 mss=1460,
                 emitprocess='randomchoice(x)',
                 iptos=0x0,
                 xopen=True,
                 tcpmodel='csa00'):
        GeneratorNode.__init__(self, sim, srcnode)
        self.srcnet = ipaddr.IPNetwork(ipsrc)
        self.dstnet = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.srcnet),
                                                      self.srcnet.prefixlen,
                                                      0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.dstnet),
                                                      self.dstnet.prefixlen,
                                                      0.61)

        if isinstance(ipproto, str):
            self.ipproto = eval(ipproto)
        else:
            self.ipproto = randomchoice(ipproto)

        if isinstance(sport, str):
            self.srcports = eval(sport)
        else:
            self.srcports = randomchoice(sport)

        if isinstance(dport, str):
            self.dstports = eval(dport)
        else:
            self.dstports = randomchoice(dport)

        if isinstance(flowsize, str):
            self.flowsizerv = eval(flowsize)
        else:
            self.flowsizerv = randomchoice(flowsize)

        if isinstance(pktsize, str):
            self.pktsizerv = eval(pktsize)
        else:
            self.pktsizerv = randomchoice(pktsize)

        if isinstance(flowstart, str):
            self.flowstartrv = eval(flowstart)
        else:
            self.flowstartrv = randomchoice(flowstart)

        if isinstance(lossrate, str):
            self.lossraterv = eval(lossrate)
        else:
            self.lossraterv = randomchoice(lossrate)

        if isinstance(mss, str):
            self.mssrv = eval(mss)
        else:
            self.mssrv = randomchoice(mss)

        if isinstance(iptos, str):
            self.iptosrv = eval(iptos)
        else:
            self.iptosrv = randomchoice(iptos)

        self.emitrvstr = emitprocess

        self.xopen = xopen
        self.activeflows = {}
        self.tcpmodel = tcpmodel
        if self.tcpmodel not in ['mathis', 'msmo97', 'csa00']:
            raise InvalidFlowConfiguration(
                'Unrecognized tcp model for harpoon:' + str(tcpmodel))

        self.anoFlag = False
        # import ipdb;ipdb.set_trace()
        if EXPORT_ABNORMAL_FLOW == 'TRUE':
            self.set_ano_flag(flowsize, flowstart, ipsrc)
Пример #7
0
    def __init__(self, srcnode, ipsrc='0.0.0.0', ipdst='0.0.0.0', sport=0, dport=0, flowstarttime=0, flowsize=1500, pktsize=1500, flowstart=0, ipproto=socket.IPPROTO_TCP, lossrate=0.0045, mss=1460, iptos=0x0, xopen=True, tcpmodel='csa00',matrix='../conf/input.matrix'):
        TrafficGenerator.__init__(self, srcnode)
        self.logger = get_logger('fs.matrix')

        trafficmatrix = open(matrix, 'r')
        flowList = []
        flowStartList = []
        srcipList = []
        dstipList = []
        flowSizeList = []

        for line in trafficmatrix:
            if line.strip()!= "":
                flowList.append(line.strip().split(" "))

        for item in  flowList:
            try:
                if nodename_to_ipaddr(srcnode)==item[1]:
                    flowStartList.append(item[0])
                    srcipList.append(item[1])
                    dstipList.append(item[2])
                    #srcipList.append(nodename_to_ipaddr(item[1]))
                    #dstipList.append(nodename_to_ipaddr(item[2]))
                    flowSizeList.append(item[3])
            except:
                print "Invalid Flow"
                print item
                pass

        self.srcipList=srcipList

        self.srcnet = eval('sequentialchoice(srcipList)')
        self.dstnet = eval('sequentialchoice(dstipList)')
        self.flowstarttimerv = eval('sequentialchoice(flowStartList)')
        self.flowsizerv = eval('sequentialchoice(flowSizeList)')

        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.srcnet), self.srcnet.prefixlen, 0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.dstnet), self.dstnet.prefixlen, 0.61)

        if isinstance(ipproto, (str,unicode)):
            #print eval(ipproto)
            self.ipproto = eval(ipproto)
        else: 
            self.ipproto = randomchoice(ipproto)

        if isinstance(sport, (str,unicode)):
            self.srcports = eval(sport)
        else:
            self.srcports = randomchoice(sport)

        if isinstance(dport, (str,unicode)):
            self.dstports = eval(dport)
        else:
            self.dstports = randomchoice(dport)

        if isinstance(pktsize, (str,unicode)):
            self.pktsizerv = eval(pktsize)
        else:
            self.pktsizerv = randomchoice(pktsize)

        if isinstance(flowstart, (str,unicode)):
            self.flowstartrv = eval(flowstart)
        else:
            self.flowstartrv = randomchoice(flowstart)

        if isinstance(lossrate, (str,unicode)):
            self.lossraterv = eval(lossrate)
        else:
            self.lossraterv = randomchoice(lossrate)

        if isinstance(mss, (str,unicode)):
            self.mssrv = eval(mss)
        else:
            self.mssrv = randomchoice(mss)

        if isinstance(iptos, (str,unicode)):
            self.iptosrv = eval(iptos)
        else:
            self.iptosrv = randomchoice(iptos)

        self.xopen = xopen
        self.activeflows = {}

        try:
            self.tcpmodel = import_module("tcpmodels.{}".format(tcpmodel))
        except ImportError,e:
            raise InvalidFlowConfiguration('Unrecognized tcp model for matrix: {} (Error on import: {})'.format(tcpmodel, str(e)))
Пример #8
0
    def __init__(self, srcnode, ipsrc='0.0.0.0', ipdst='0.0.0.0', sport=0, dport=0, flowsize=1500, pktsize=1500, flowstart=0, ipproto=socket.IPPROTO_TCP, lossrate=0.001, mss=1460, iptos=0x0, xopen=True, tcpmodel='csa00'):
        TrafficGenerator.__init__(self, srcnode)
        self.logger = get_logger('fs.harpoon')
        self.srcnet = ipaddr.IPNetwork(ipsrc)
        self.dstnet = ipaddr.IPNetwork(ipdst)
        if haveIPAddrGen:
            self.ipsrcgen = ipaddrgen.initialize_trie(int(self.srcnet), self.srcnet.prefixlen, 0.61)
            self.ipdstgen = ipaddrgen.initialize_trie(int(self.dstnet), self.dstnet.prefixlen, 0.61)

        if isinstance(ipproto, (str,unicode)):
            self.ipproto = eval(ipproto)
        else: 
            self.ipproto = randomchoice(ipproto)

        if isinstance(sport, (str,unicode)):
            self.srcports = eval(sport)
        else:
            self.srcports = randomchoice(sport)

        if isinstance(dport, (str,unicode)):
            self.dstports = eval(dport)
        else:
            self.dstports = randomchoice(dport)

        if isinstance(flowsize, (str,unicode)):
            self.flowsizerv = eval(flowsize)
        else:
            self.flowsizerv = randomchoice(flowsize)

        if isinstance(pktsize, (str,unicode)):
            self.pktsizerv = eval(pktsize)
        else:
            self.pktsizerv = randomchoice(pktsize)

        if isinstance(flowstart, (str,unicode)):
            self.flowstartrv = eval(flowstart)
        else:
            self.flowstartrv = randomchoice(flowstart)

        if isinstance(lossrate, (str,unicode)):
            self.lossraterv = eval(lossrate)
        else:
            self.lossraterv = randomchoice(lossrate)

        if isinstance(mss, (str,unicode)):
            self.mssrv = eval(mss)
        else:
            self.mssrv = randomchoice(mss)

        if isinstance(iptos, (str,unicode)):
            self.iptosrv = eval(iptos)
        else:
            self.iptosrv = randomchoice(iptos)

        self.xopen = xopen
        self.activeflows = {}

        try:
            self.tcpmodel = import_module("tcpmodels.{}".format(tcpmodel))
        except ImportError,e:
            raise InvalidFlowConfiguration('Unrecognized tcp model for harpoon: {} (Error on import: {})'.format(tcpmodel, str(e)))
Пример #9
0
#
# Simple recipe for using the code.  Example uses the ipaddr
# module, but that isn't a requirement (just makes things
# nicer).
#

import ipaddr
import ipaddrgen

net = ipaddr.IPv4Network('127.0.0.0/8')
print int(net), net.prefixlen

t = ipaddrgen.initialize_trie(int(net), net.prefixlen, 0.61)
for i in xrange(10000):
    a = ipaddr.IPv4Address(ipaddrgen.generate_addressv4(t))
    print a

n = ipaddrgen.count_nodes(t)
print "nodes", n
ipaddrgen.release_trie(t)