Beispiel #1
0
 def __init__(self):
     self.hwtype = self.HW_TYPE_ETHERNET
     self.opcode = 0
     self.prototype  = arp.PROTO_TYPE_IP
     self.hwsrc = ETHER_ANY
     self.hwdst = ETHER_ANY
     self.protosrc = IpAddress()
     self.protodst = IpAddress()
Beispiel #2
0
 def __init__(self):
     self.hwtype = self.HW_TYPE_ETHERNET
     self.opcode = 0
     self.prototype = arp.PROTO_TYPE_IP
     self.hwsrc = ETHER_ANY
     self.hwdst = ETHER_ANY
     self.protosrc = IpAddress()
     self.protodst = IpAddress()
Beispiel #3
0
 def __init__(self):
     self.srcip = IpAddress()
     self.dstip = IpAddress()
     self.protocol = 0
     self.tos = 0
     self.id = 0xDEADBEEF
     self.flags = 0
     self.frag = 0
     self.ttl = 64
     self.csum = 0
Beispiel #4
0
 def __init__(self):
     self.srcip = IpAddress()
     self.dstip = IpAddress()
     self.protocol = 0
     self.tos = 0
     self.id = 0xDEADBEEF
     self.flags = 0
     self.frag = 0
     self.ttl = 64
     self.csum  = 0
    def add_range(self, value, rule_number):
        split_ip = value.split('-')
        curr_ip, upper_ip = IpAddress(split_ip[0]), IpAddress(split_ip[1])

        while curr_ip.is_less_than_or_equal_to(upper_ip):
            curr_ip_str = str(curr_ip)
            if curr_ip_str not in self.rule_store:
                self.rule_store[curr_ip_str] = set()

            self.rule_store[curr_ip_str].add(rule_number)
            curr_ip.increment()
Beispiel #6
0
class ipv4(packet_base):
    IPv4 = 4
    ICMP_PROTOCOL = 1
    TCP_PROTOCOL = 6
    UDP_PROTOCOL = 17

    def __init__(self):
        self.srcip = IpAddress()
        self.dstip = IpAddress()
        self.protocol = 0
        self.tos = 0
        self.id = 0xDEADBEEF
        self.flags = 0
        self.frag = 0
        self.ttl = 64
        self.csum = 0

    def __repr__(self):
        if self.next != None:
            payload = " <- " + repr(self.next)
        else:
            payload = ""
        return "ipv4: " + str(
            (self.protocol, self.srcip, self.dstip)) + payload

    def copy(self):
        cpy = ipv4()
        cpy.srcip = self.srcip.copy()
        cpy.dstip = self.dstip.copy()
        cpy.protocol = self.protocol
        cpy.tos = self.tos
        cpy.id = self.id
        cpy.flags = self.flags
        cpy.frag = self.frag
        cpy.ttl = self.ttl
        cpy.csum = self.csum
        if self.next != None:
            cpy.set_payload(self.next.copy())
        return cpy

    def dump_equivalent_state(self):
        filtered_dict = packet_base.dump_equivalent_state(self)
        filtered_dict["srcip"] = utils.copy_state(self.srcip)
        filtered_dict["dstip"] = utils.copy_state(self.dstip)
        filtered_dict["protocol"] = utils.copy_state(self.protocol)
        filtered_dict["tos"] = utils.copy_state(self.tos)
        filtered_dict["id"] = utils.copy_state(self.id)
        filtered_dict["flags"] = utils.copy_state(self.flags)
        filtered_dict["frag"] = utils.copy_state(self.frag)
        filtered_dict["ttl"] = utils.copy_state(self.ttl)
        filtered_dict["csum"] = utils.copy_state(self.csum)
        if next != None:
            filtered_dict["next"] = utils.copy_state(self.next)
        return filtered_dict

    def toScapy(self):
        from scapy.layers.inet import IP

        tos = self.tos
        id = self.id
        flags = self.flags
        frag = self.frag
        ttl = self.ttl
        proto = self.protocol
        chksum = None if self.csum == 0 else self.csum  # TODO: reconsider automatic chksum computation
        src = str(self.srcip)
        dst = str(self.dstip)
        # left are: version - default 4, ihl?, len, options

        p = IP(tos=tos,
               id=id,
               flags=flags,
               frag=frag,
               ttl=ttl,
               proto=proto,
               src=src,
               dst=dst)

        if self.next == None:
            return IP(str(p)), None
        elif isinstance(self.next, packet_base):
            p2, data = self.next.toScapy()
            return IP(str(p / p2)), data
        elif isinstance(self.next, str):
            return IP(str(p)), self.next
Beispiel #7
0
class arp(packet_base):
    HW_TYPE_ETHERNET = 1
    PROTO_TYPE_IP = 0x0800

    # opcodes:
    REQUEST = 1  # ARP
    REPLY = 2  # ARP
    REV_REQUEST = 3  # RARP
    REV_REPLY = 4  # RARP

    def __init__(self):
        self.hwtype = self.HW_TYPE_ETHERNET
        self.opcode = 0
        self.prototype = arp.PROTO_TYPE_IP
        self.hwsrc = ETHER_ANY
        self.hwdst = ETHER_ANY
        self.protosrc = IpAddress()
        self.protodst = IpAddress()

    def __repr__(self):
        if self.next != None:
            payload = " <- " + repr(self.next)
        else:
            payload = ""

        if self.hwtype == self.HW_TYPE_ETHERNET:
            hwt = "HW_TYPE_ETHERNET"
        else:
            utils.crash("Unknown ARP hwtype: 0x%x" % self.hwtype)

        if self.opcode == self.REQUEST:
            opc = "REQUEST"
        elif self.opcode == self.REPLY:
            opc = "REPLY"
        elif self.opcode == self.REV_REQUEST:
            opc = "REV_REQUEST"
        elif self.opcode == self.REV_REPLY:
            opc = "REV_REPLY"
        else:
            utils.crash("ARP opcode %d not known" % self.opcode)

        return "arp: " + str((hwt, opc, self.hwsrc, self.hwdst, self.protosrc,
                              self.protodst)) + payload

    def copy(self):
        cpy = arp()
        cpy.hwtype = self.hwtype
        cpy.opcode = self.opcode
        cpy.prototype = self.prototype
        cpy.hwsrc = self.hwsrc.copy()
        cpy.hwdst = self.hwdst.copy()
        cpy.protosrc = self.protosrc.copy()
        cpy.protodst = self.protodst.copy()
        if self.next != None:
            self.set_payload(self.next.copy())
        return cpy

    def toScapy(self):
        from scapy.layers.l2 import ARP

        hwtype = self.hwtype
        ptype = self.prototype
        op = self.opcode
        hwsrc = str(self.hwsrc)
        hwdst = str(self.hwdst)
        psrc = str(self.protosrc)
        pdst = str(self.protodst)

        p = ARP(hwtype=hwtype,
                ptype=ptype,
                op=op,
                hwsrc=hwsrc,
                hwdst=hwdst,
                psrc=psrc,
                pdst=pdst)

        if self.next == None:
            return ARP(str(p)), None
        elif isinstance(self.next, packet_base):
            p2, data = self.next.toScapy()
            return ARP(str(p / p2)), data
        elif isinstance(self.next, str):
            return ARP(str(p)), self.next

    def dump_equivalent_state(self):
        filtered_dict = packet_base.dump_equivalent_state(self)
        filtered_dict["hwtype"] = utils.copy_state(self.hwtype)
        filtered_dict["opcode"] = utils.copy_state(self.opcode)
        filtered_dict["prototype"] = utils.copy_state(self.prototype)
        filtered_dict["hwsrc"] = utils.copy_state(self.hwsrc)
        filtered_dict["hwdst"] = utils.copy_state(self.hwdst)
        filtered_dict["protosrc"] = utils.copy_state(self.protosrc)
        filtered_dict["protodst"] = utils.copy_state(self.protodst)
Beispiel #8
0
class arp(packet_base):
    HW_TYPE_ETHERNET = 1
    PROTO_TYPE_IP    = 0x0800

    # opcodes:
    REQUEST     = 1 # ARP
    REPLY       = 2 # ARP
    REV_REQUEST = 3 # RARP
    REV_REPLY   = 4 # RARP

    def __init__(self):
        self.hwtype = self.HW_TYPE_ETHERNET
        self.opcode = 0
        self.prototype  = arp.PROTO_TYPE_IP
        self.hwsrc = ETHER_ANY
        self.hwdst = ETHER_ANY
        self.protosrc = IpAddress()
        self.protodst = IpAddress()

    def __repr__(self):
        if self.next != None:
            payload = " <- " + repr(self.next)
        else:
            payload = ""

        if self.hwtype == self.HW_TYPE_ETHERNET:
            hwt = "HW_TYPE_ETHERNET"
        else:
            utils.crash("Unknown ARP hwtype: 0x%x" % self.hwtype)

        if self.opcode == self.REQUEST:
            opc = "REQUEST"
        elif self.opcode == self.REPLY:
            opc = "REPLY"
        elif self.opcode == self.REV_REQUEST:
            opc = "REV_REQUEST"
        elif self.opcode == self.REV_REPLY:
            opc = "REV_REPLY"
        else:
            utils.crash("ARP opcode %d not known" % self.opcode)

        return "arp: " + str((hwt, opc, self.hwsrc, self.hwdst, self.protosrc, self.protodst)) + payload

    def copy(self):
        cpy = arp()
        cpy.hwtype = self.hwtype
        cpy.opcode = self.opcode
        cpy.prototype = self.prototype
        cpy.hwsrc = self.hwsrc.copy()
        cpy.hwdst = self.hwdst.copy()
        cpy.protosrc = self.protosrc.copy()
        cpy.protodst = self.protodst.copy()
        if self.next != None:
            self.set_payload(self.next.copy())
        return cpy

    def toScapy(self):
        from scapy.layers.l2 import ARP

        hwtype = self.hwtype
        ptype = self.prototype
        op = self.opcode
        hwsrc = str(self.hwsrc)
        hwdst = str(self.hwdst)
        psrc = str(self.protosrc)
        pdst = str(self.protodst)

        p = ARP(hwtype = hwtype, ptype = ptype, op = op, hwsrc = hwsrc, hwdst = hwdst, psrc = psrc, pdst = pdst)

        if self.next == None:
            return ARP(str(p)), None
        elif isinstance(self.next, packet_base):
            p2, data = self.next.toScapy()
            return ARP(str(p/p2)), data
        elif isinstance(self.next, str):
            return ARP(str(p)), self.next

    def dump_equivalent_state(self):
        filtered_dict = packet_base.dump_equivalent_state(self)
        filtered_dict["hwtype"] = utils.copy_state(self.hwtype)
        filtered_dict["opcode"] = utils.copy_state(self.opcode)
        filtered_dict["prototype"] = utils.copy_state(self.prototype)
        filtered_dict["hwsrc"] = utils.copy_state(self.hwsrc)
        filtered_dict["hwdst"] = utils.copy_state(self.hwdst)
        filtered_dict["protosrc"] = utils.copy_state(self.protosrc)
        filtered_dict["protodst"] = utils.copy_state(self.protodst)
from ip_address import IpAddress

zero = '0.0.0.0'
carry_over = '9.255.255.255'
one_to_max = '255.255.255.254'

zero_ip = IpAddress(zero)
carry_over_ip = IpAddress(carry_over)
one_to_max_ip = IpAddress(one_to_max)

def test_ip_address():
    print('TEST IP ADDRESS:')
    test_str()
    test_is_less_than_or_equal_to()
    test_increment()

def test_str():
    print('Test initialization:')
    print(zero == str(zero_ip))
    print(carry_over == str(carry_over_ip))
    print(one_to_max == str(one_to_max_ip))

def test_is_less_than_or_equal_to():
    print('Test is_less_than_or_equal_to():')
    print(zero_ip.is_less_than_or_equal_to(zero_ip) == True)
    print(zero_ip.is_less_than_or_equal_to(carry_over_ip) == True)
    print(zero_ip.is_less_than_or_equal_to(one_to_max_ip) == True)

    print(carry_over_ip.is_less_than_or_equal_to(zero_ip) == False)
    print(carry_over_ip.is_less_than_or_equal_to(carry_over_ip) == True)
    print(carry_over_ip.is_less_than_or_equal_to(one_to_max_ip) == True)
Beispiel #10
0
class ipv4(packet_base):
    IPv4 = 4
    ICMP_PROTOCOL = 1
    TCP_PROTOCOL  = 6
    UDP_PROTOCOL  = 17

    def __init__(self):
        self.srcip = IpAddress()
        self.dstip = IpAddress()
        self.protocol = 0
        self.tos = 0
        self.id = 0xDEADBEEF
        self.flags = 0
        self.frag = 0
        self.ttl = 64
        self.csum  = 0

    def __repr__(self):
        if self.next != None:
            payload = " <- " + repr(self.next)
        else:
            payload = ""
        return "ipv4: " + str((self.protocol, self.srcip, self.dstip)) + payload

    def copy(self):
        cpy = ipv4()
        cpy.srcip = self.srcip.copy()
        cpy.dstip = self.dstip.copy()
        cpy.protocol = self.protocol
        cpy.tos = self.tos
        cpy.id = self.id
        cpy.flags = self.flags
        cpy.frag = self.frag
        cpy.ttl = self.ttl
        cpy.csum  = self.csum
        if self.next != None:
            cpy.set_payload(self.next.copy())
        return cpy

    def dump_equivalent_state(self):
        filtered_dict = packet_base.dump_equivalent_state(self)
        filtered_dict["srcip"] = utils.copy_state(self.srcip)
        filtered_dict["dstip"] = utils.copy_state(self.dstip)
        filtered_dict["protocol"] = utils.copy_state(self.protocol)
        filtered_dict["tos"] = utils.copy_state(self.tos)
        filtered_dict["id"] = utils.copy_state(self.id)
        filtered_dict["flags"] = utils.copy_state(self.flags)
        filtered_dict["frag"] = utils.copy_state(self.frag)
        filtered_dict["ttl"] = utils.copy_state(self.ttl)
        filtered_dict["csum"] = utils.copy_state(self.csum)
        if next != None:
            filtered_dict["next"] = utils.copy_state(self.next)
        return filtered_dict

    def toScapy(self):
        from scapy.layers.inet import IP

        tos = self.tos
        id = self.id
        flags = self.flags
        frag = self.frag
        ttl = self.ttl
        proto = self.protocol
        chksum = None if self.csum == 0 else self.csum      # TODO: reconsider automatic chksum computation
        src = str(self.srcip)
        dst = str(self.dstip)
        # left are: version - default 4, ihl?, len, options

        p = IP(tos = tos, id = id, flags = flags, frag = frag, ttl = ttl, proto = proto, src = src, dst = dst)

        if self.next == None:
            return IP(str(p)), None
        elif isinstance(self.next, packet_base):
            p2, data = self.next.toScapy()
            return IP(str(p/p2)), data
        elif isinstance(self.next, str):
            return IP(str(p)), self.next
Beispiel #11
0
def ipstr_to_int(a):
    octets = a.split('.')
    octets = map(lambda x: int(x), octets)
    return IpAddress(tuple(octets))