コード例 #1
0
ファイル: test_udp.py プロジェクト: umitproject/umpa
    def test_get_raw_with_payload(self):
        ip = IP(src='1.2.3.4', dst='5.6.7.8')
        udp = UDP(srcport=9, dstport=10)
        payload = Payload('12345678')

        # the next two lines are normally done by Packet.get_raw()
        payload.get_raw([ip, udp, payload], 8*len(payload.data))
        udp.__dict__['payload'] = payload

        assert udp._raw(0, 0, [ip, udp, payload], 8*len(payload.data)) == \
                                                       (0x9000a00101ed3L, 64)
コード例 #2
0
    def test_add_new_protocols__strict(self):
        py.test.raises(UMPAStrictException, Packet, TCP(), IP())
        py.test.raises(UMPAStrictException, Packet, TCP(), IP(), strict=True)
        py.test.raises(UMPAStrictException, Packet, TCP(), Payload(), IP())
        py.test.raises(UMPAStrictException, Packet, TCP(), IP(), Payload())
        py.test.raises(UMPAStrictException, Packet, Payload(), TCP())
        py.test.raises(UMPAStrictException, Packet, UDP(), TCP())

        p = Packet(TCP())
        py.test.raises(UMPAStrictException, p.include, IP())
        py.test.raises(UMPAStrictException, p.include, UDP())

        p = Packet(strict=False)
        p.strict = True
        py.test.raises(UMPAStrictException, p.include, UDP(), TCP())
コード例 #3
0
ファイル: test_sockets.py プロジェクト: umitproject/umpa
    def test_send_size_L2(self):
        if os.name == 'posix' and os.geteuid() != 0:
            py.test.skip('root-privileges are needed')

        p1 = Packet(Ethernet(src='00:11:22:33:44:55', dst='00:11:22:33:44:55'),
                    IP(src="127.0.0.1", dst="127.0.0.1"),
                    TCP(srcport=1234, dstport=4321), Payload('xyz'))
        p2 = Packet(Ethernet(src='00:11:22:33:44:55', dst='00:11:22:33:44:55'),
                    IP(src="127.0.0.1", dst="127.0.0.1"),
                    TCP(srcport=1234, dstport=4321), Payload('xyz'))

        s = SocketL2(iface='lo')
        size = s.send(p1, p2)

        assert size == [57, 57]
コード例 #4
0
    def test_get_destination(self):
        p = Packet(Ethernet(dst='00:11:22:33:44:55'), IP(dst='1.2.3.4'),
                   UDP(dstport=1234), Payload('UMPA'))

        py.test.raises(UMPAException, p._get_destination, 1)
        assert p._get_destination(2) == '00:11:22:33:44:55'
        assert p._get_destination(3) == '1.2.3.4'
        py.test.raises(UMPAAttributeException, p._get_destination, 4)
        py.test.raises(UMPAAttributeException, p._get_destination, 5)
        py.test.raises(UMPAException, p._get_destination, 6)
コード例 #5
0
    def test_protos_order(self):
        order = (IP, UDP, Payload)

        p = Packet(*[x() for x in order])
        for i in xrange(len(order)):
            assert isinstance(p.protos[i], order[i])

        p = Packet(IP())
        p.include(UDP(), Payload())
        for i in xrange(len(order)):
            assert isinstance(p.protos[i], order[i])
コード例 #6
0
    def test_get_raw(self):
        if sys.platform.find('linux') != -1:
            ttl = _consts.TTL_LINUX
        elif sys.platform.find('darwin') != -1:
            ttl = _consts.TTL_MACOS
        elif sys.platform.find('win') != -1:
            ttl = _consts.TTL_WINDOWS
        elif sys.platform.find('freebsd') != -1:
            ttl = _consts.TTL_FREEBSD
        elif sys.platform.find('os2') != -1:
            ttl = _consts.TTL_OS2
        elif sys.platform.find('sunos') != -1:
            ttl = _consts.TTL_SUNOS
        elif sys.platform.find('aix') != -1:
            ttl = _consts.TTL_AIX
        elif sys.platform.find('irix') != -1:
            ttl = _consts.TTL_IRIX
        elif sys.platform.find('solaris') != -1:
            ttl = _consts.TTL_SOLARIS
        elif sys.platform.find('ultrix') != -1:
            ttl = _consts.TTL_ULTRIX
        elif sys.platform.find('dec') != -1:
            ttl = _consts.TTL_DEC
        else:
            ttl = _consts.TTL_LINUX

        expected = ("\x45\x00\x00\x28\x03\xe8\x00\x00"
                    "%c\x06\x79\x00\x7f\x00\x00\x01"
                    "\x7f\x00\x00\x01") % chr(ttl)

        p = Packet()
        p.include(
            IP(src="127.0.0.1", dst="127.0.0.1", _id=1000, _checksum=0x7900))
        p.include(TCP(srcport=123, dstport=321, flags='psh'))

        expected += ("\x00\x7b\x01\x41\x00\x00\x00\x00"
                     "\x00\x00\x00\x01\x50\x08\x02\x00"
                     "\xae\x1d\x00\x00")
        assert expected == p.get_raw()

        p.include(Payload(data="UMPA"))

        expected = ("\x45\x00\x00\x2c\x03\xe8\x00\x00"
                    "%c\x06\x79\x00\x7f\x00\x00\x01"
                    "\x7f\x00\x00\x01"
                    "\x00\x7b\x01\x41\x00\x00\x00\x00"
                    "\x00\x00\x00\x01\x50\x08\x02\x00"
                    "\x08\x8b\x00\x00") % chr(ttl)

        expected += "\x55\x4d\x50\x41"
        assert expected == p.get_raw()
コード例 #7
0
ファイル: _decoder.py プロジェクト: umitproject/umpa
def decode(buffer, linktype):
    """
    Decode raw buffer of packet and return umit.umpa.Packet's object.
    
    @param buffer: raw buffer

    @type linktype: C{int}
    @param linktype: datalink of 2nd layer
    (return by datalink() method of pcap session)

    @rtype: C{umit.umpa.Packet}
    @return: decoded packet
    """

    protos = _prepare_protos()
    packet = umit.umpa.Packet(strict=False, warn=False)

    # XXX: currently there is no protocols in upper layers (above 4th layer)
    #      propably if they would be implemented - some changes are needed
    #      to detect what protocol it is
    #      statical version of decode was existing till r5043
    next_type = linktype
    layer = 2
    while next_type:
        for proto in protos[layer]:
            if next_type == proto.protocol_id:
                header = proto()
                buffer = header.load_raw(buffer)
                if header.payload_fieldname:
                    next_type = getattr(header, header.payload_fieldname)
                else:
                    next_type = None
                packet.include(header)
                break
        else:
            header = Payload()
            header.load_raw(buffer)
            packet.include(header)
            return packet
        layer += 1

    # payload
    if len(buffer) > 0:
        data = Payload()
        data.load_raw(buffer)
        packet.include(data)

    return packet
コード例 #8
0
    def test_sndrcv_loopback(self):
        packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="127.0.0.1"),
                                  TCP(srcport=99), Payload(data="foo bar"))
        th = SendPacket(packet)
        th.start()
        received = umit.umpa.sniffing.sniff_next(filter="src 1.2.3.4",
                                                 device="lo")
        th.join()

        assert received.ip.src == packet.ip.src
        assert received.ip.dst == packet.ip.dst
        assert received.tcp.srcport == packet.tcp.srcport
        assert received.tcp.dstport == packet.tcp.dstport
        assert received.payload.data == packet.payload.data
コード例 #9
0
ファイル: test_sockets.py プロジェクト: umitproject/umpa
    def test_send_sniff_tcp_L2(self):
        if os.name == 'posix' and os.geteuid() != 0:
            py.test.skip('root-privileges are needed')

        p = Packet(Ethernet(src='00:11:22:33:44:55', dst='00:11:22:33:44:55'),
                   IP(src="127.0.0.1", dst="127.0.0.1"),
                   TCP(srcport=1234, dstport=4321), Payload('xyz'))

        th = SendPacketL2(p, iface='lo')
        th.start()
        result = umit.umpa.sniffing.sniff(1,
                                          device='lo',
                                          filter="src port 1234")
        th.join()

        assert len(result) == 1
        assert result[0].ethernet.src == '00:11:22:33:44:55'
        assert result[0].ip.src == '127.0.0.1'
        assert result[0].tcp.srcport == 1234
        assert result[0].tcp.dstport == 4321
コード例 #10
0
ip = IP()
# setting some fields
ip.src = "127.0.0.1"
ip.dst = "67.205.14.183"

# the same for TCP
tcp = TCP()
# setting some fields
tcp.srcport = 2958
tcp.dstport = 0

# also, SYN flag will be set up. here SYN
tcp.set_flags('flags', syn=True)

# create payload object and set the data while calling constructor
payload = Payload(data="something here")

# create a new packet and include one protocol (ip)
packet = umit.umpa.Packet(ip)
# packing another protocol into our packet
packet.include(tcp, payload)

# creating new socket connection
# NOTE: we need to raise our priviliges.
# we can call super_priviliges() function and pass function which need these
# priviliges as an argument. after that, we drop priviliges automatically

# we can do the same with the code:
# umit.umpa.utils.security.super_priviliges()
# sock = umit.umpa.Socket()
# umit.umpa.utils.security.drop_priviliges()
コード例 #11
0
ファイル: models.py プロジェクト: umitproject/umpa
# obviously it's not necessary in real usecase
class SendPacket(threading.Thread):
    def __init__(self, packet, amount=1):
        super(SendPacket, self).__init__()
        self._packet = packet
        self._amount = amount

    def run(self):
        s = umit.umpa.Socket()
        for i in xrange(self._amount):
            time.sleep(2)
            s.send(self._packet)


packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="127.0.0.1"), TCP(srcport=99),
                          Payload(data="sniff me"))

# run thread and send 2 packets
th = SendPacket(packet, 2)
th.start()

# capture 2 packets and resend them with reverse src/dst ports
models.react(2, filter="host 1.2.3.4 and port 99", device="any", revports=True)

# collect terminated thread
th.join()

# do the same but resend it to a new destination
th = SendPacket(packet, 2)
th.start()
models.react(2,
コード例 #12
0
class TestExtensionXML(object):
    example_xml = """<?xml version="1.0" ?>
<UMPA>
        <packet id="0" strict="True">
                <protocol class="umit.umpa.protocols.IP.IP">
                        <_version type="int">
                                4
                        </_version>
                        <_hdr_len type="NoneType">
                                None
                        </_hdr_len>
                        <tos type="bits">
                                <precedence0 type="bool">
                                        False
                                </precedence0>
                                <precedence1 type="bool">
                                        False
                                </precedence1>
                                <precedence2 type="bool">
                                        False
                                </precedence2>
                                <delay type="bool">
                                        False
                                </delay>
                                <throughput type="bool">
                                        False
                                </throughput>
                                <reliability type="bool">
                                        False
                                </reliability>
                                <reserved0 type="bool">
                                        False
                                </reserved0>
                                <reserved1 type="bool">
                                        False
                                </reserved1>
                        </tos>
                        <_len type="NoneType">
                                None
                        </_len>
                        <_id type="int">
                                0
                        </_id>
                        <flags type="bits">
                                <rb type="bool">
                                        False
                                </rb>
                                <df type="bool">
                                        False
                                </df>
                                <mf type="bool">
                                        False
                                </mf>
                        </flags>
                        <_frag_offset type="int">
                                0
                        </_frag_offset>
                        <ttl type="NoneType">
                                None
                        </ttl>
                        <_proto type="NoneType">
                                None
                        </_proto>
                        <_checksum type="int">
                                0
                        </_checksum>
                        <src type="str">
                                127.0.0.1
                        </src>
                        <dst type="str">
                                67.205.14.183
                        </dst>
                        <options type="int">
                                0
                        </options>
                        <_padding type="int">
                                0
                        </_padding>
                </protocol>
                <protocol class="umit.umpa.protocols.TCP.TCP">
                        <srcport type="int">
                                2958
                        </srcport>
                        <dstport type="int">
                                0
                        </dstport>
                        <_seq type="NoneType">
                                None
                        </_seq>
                        <_ack type="NoneType">
                                None
                        </_ack>
                        <_hdr_len type="NoneType">
                                None
                        </_hdr_len>
                        <_reserved type="int">
                                0
                        </_reserved>
                        <flags type="bits">
                                <urg type="bool">
                                        False
                                </urg>
                                <ack type="bool">
                                        False
                                </ack>
                                <psh type="bool">
                                        False
                                </psh>
                                <rst type="bool">
                                        False
                                </rst>
                                <syn type="bool">
                                        True
                                </syn>
                                <fin type="bool">
                                        False
                                </fin>
                        </flags>
                        <_window_size type="NoneType">
                                None
                        </_window_size>
                        <_checksum type="NoneType">
                                None
                        </_checksum>
                        <_urgent_pointer type="NoneType">
                                None
                        </_urgent_pointer>
                        <options type="int">
                                0
                        </options>
                        <_padding type="int">
                                0
                        </_padding>
                </protocol>
                <protocol class="umit.umpa.protocols.Payload.Payload">
                        <data type="str">
                                this is umit.umpa!
                        </data>
                </protocol>
        </packet>
</UMPA>
"""
    example_xml2 = """        <packet id="1" strict="True">
                <protocol class="umit.umpa.protocols.TCP.TCP">
                        <srcport type="int">
                                123
                        </srcport>
                        <dstport type="int">
                                321
                        </dstport>
                        <_seq type="NoneType">
                                None
                        </_seq>
                        <_ack type="NoneType">
                                None
                        </_ack>
                        <_hdr_len type="NoneType">
                                None
                        </_hdr_len>
                        <_reserved type="int">
                                0
                        </_reserved>
                        <flags type="bits">
                                <urg type="bool">
                                        False
                                </urg>
                                <ack type="bool">
                                        False
                                </ack>
                                <psh type="bool">
                                        False
                                </psh>
                                <rst type="bool">
                                        False
                                </rst>
                                <syn type="bool">
                                        False
                                </syn>
                                <fin type="bool">
                                        False
                                </fin>
                        </flags>
                        <_window_size type="NoneType">
                                None
                        </_window_size>
                        <_checksum type="NoneType">
                                None
                        </_checksum>
                        <_urgent_pointer type="NoneType">
                                None
                        </_urgent_pointer>
                        <options type="int">
                                0
                        </options>
                        <_padding type="int">
                                0
                        </_padding>
                </protocol>
                <protocol class="umit.umpa.protocols.Payload.Payload">
                        <data type="str">
                                another umit.umpa packet!
                        </data>
                </protocol>
        </packet>
</UMPA>
"""

    ip = IP()
    ip.src = "127.0.0.1"
    ip.dst = "67.205.14.183"

    # TCP header
    tcp = TCP()
    tcp.srcport = 2958
    tcp.dstport = 0
    tcp.set_flags('flags', syn=True)

    # Payload
    data = Payload()
    data.data = "this is umit.umpa!"

    # packet
    example_packet = umit.umpa.Packet(ip, tcp, data)

    tcp2 = TCP()
    tcp2.srcport = 123
    tcp2.dstport = 321
    tcp2.set_flags('flags', syn=False)
    data2 = Payload()
    data2.data = "another umit.umpa packet!"
    example_packet2 = umit.umpa.Packet(tcp2, data2, strict=True)

    def test_packet_attrs(self):
        assert hasattr(umit.umpa.Packet, 'save_xml')
        assert hasattr(umit.umpa.Packet, 'load_xml')

    def test_xml_load(self):
        f = StringIO()
        f.write(self.example_xml)
        f.seek(0)

        packets = XML.load(f)
        for p in packets:
            for i, proto in enumerate(p.protos):
                for fieldname in proto.get_fields_keys():
                    assert proto.get_field(fieldname).fillout() == \
            self.example_packet.protos[i].get_field(fieldname).fillout()

    def test_xml_load_multiple(self):
        l_xml = (self.example_xml, self.example_xml2)
        l_packet = (self.example_packet, self.example_packet2)
        f = StringIO()
        f.write(l_xml[0][:-8])  # </UMPA>\n
        f.write(l_xml[1])
        f.seek(0)

        packets = XML.load(f)
        for j, p in enumerate(packets):
            for i, proto in enumerate(p.protos):
                for fieldname in proto.get_fields_keys():
                    assert proto.get_field(fieldname).fillout() == \
            l_packet[j].protos[i].get_field(fieldname).fillout()

    def test_xml_load_proto_only(self):
        f = StringIO()
        f.write(self.example_xml)
        f.seek(0)

        protos = XML.load(f, proto_only=True)
        for i, p in enumerate((IP, TCP, Payload)):
            assert isinstance(protos[i], p)

    def test_xml_save(self):
        output = StringIO()
        XML.save(output, (self.example_packet, ))
        a = output.getvalue()
        b = self.example_xml.replace('        ', '\t')
        assert a == b

    def test_xml_save_filename(self):
        if os.name != 'posix':
            py.test.skip('only for POSIX platforms')
        output = tempfile.NamedTemporaryFile(mode='w+t')
        XML.save(output.name, (self.example_packet, ))
        a = output.read()
        b = self.example_xml.replace('        ', '\t')
        assert a == b
        output.close()

    def test_xml_save_multiple(self):
        output = StringIO()
        XML.save(output, (self.example_packet, self.example_packet2))
        a = output.getvalue()
        b = self.example_xml[:-8]  # </UMPA>\n
        b += self.example_xml2
        b = b.replace('        ', '\t')
        assert a == b