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)
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())
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)
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])
def test_send_sniff_tcp(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"), UDP(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].udp.srcport == 1234 assert result[0].udp.dstport == 4321
def test_get_raw(self): ip = IP(src='1.2.3.4', dst='5.6.7.8') udp = UDP(srcport=9, dstport=10) assert udp._raw(0, 0, [ip, udp], 0) == (0x9000A0008EFB7, 64)
# to create RAW_SOCKET we need SUID, but for normal usage it's not # necessary. we recommend to use umit.umpa.utils.security to make our programs # more safety. also dropping priviliges should be done at the same beginning # of the application # dropping unnecessary priviliges umit.umpa.utils.security.drop_priviliges() # create new IP object ip = IP() # setting some fields ip.src = "127.0.0.1" ip.dst = "67.205.14.183" # create new UDP object udp = UDP() # setting some fields udp.srcport = 0 udp.dstport = 7 # create a new packet and include protocols packet = umit.umpa.Packet(ip, udp) # 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()