def test_get_raw__tcp(self): p = Packet() p.include(TCP(srcport=123, dstport=321, flags='psh')) assert ("\x00\x7b\x01\x41\x00\x00\x00\x00" "\x00\x00\x00\x01\x50\x08\x02\x00" "\xae\x1d\x00\x00") == p.get_raw()
def test_get_raw__tcp(self): p = Packet() p.include(TCP(srcport=123,dstport=321,flags='psh')) assert ("\x00\x7b\x01\x41\x00\x00\x00\x00" "\x00\x00\x00\x01\x50\x08\x02\x00" "\xae\x1d\x00\x00") == p.get_raw()
def test_get_raw__ip(self): p = Packet() p.include( IP(src="127.0.0.1", dst="127.0.0.1", _id=1000, _checksum=0x7900)) 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\x14\x03\xe8\x00\x00" "%c\x00\x79\x00\x7f\x00\x00\x01" "\x7f\x00\x00\x01") % chr(ttl) assert expected == p.get_raw()
def test_sent_size(self): if os.name == 'posix' and os.geteuid() != 0: py.test.skip('root-privileges are needed') p1 = Packet(IP(), TCP()) p2 = Packet(IP(), TCP()) s = Socket() size = s.send(p1, p2) assert size == [40, 40]
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_proto_access_edit(self): p = Packet(IP(src="1.2.3.4"), TCP()) assert p.ip.src == "1.2.3.4" p.ip.src = "10.0.0.1" assert p.ip.src == "10.0.0.1" ip = IP(src="127.0.0.1") p = Packet(ip) p.ip.src = "10.0.0.1" assert p.ip.src == "10.0.0.1" assert ip.src == "10.0.0.1"
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_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()
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_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]
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()
def test_get_raw__ip(self): p = Packet() p.include(IP(src="127.0.0.1", dst="127.0.0.1", _id=1000, _checksum=0x7900)) 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\x14\x03\xe8\x00\x00" "%c\x00\x79\x00\x7f\x00\x00\x01" "\x7f\x00\x00\x01") % chr(ttl) assert expected == p.get_raw()
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
#print "Structure Of Ipv6 Packet" #print first_packet #print "_____________________" sock = super_priviliges(INET6) #sock.send(first_packet) #udp = UDP(srcport=263, dstport=153) #second_packet = Packet(ip, udp) #print second_packet #sock.send(second_packet) icmp = ICMPV6(type=137, code=0) #icmp.data = '00:00:00:00:00:00' #icmp.ident = 4660 #icmp.seq = 22136 icmp.data = 'ABCD' second_packet = Packet(ip, icmp) print second_packet sock.send(second_packet) #i = IPV6() #i.load_raw("\x88\x00\x92\x84\xB2\xB4\x56\x78\x00\x04\x15\x28\x00\x06\x84\x12\x92\x84\x12\xB4\x56\xB4\x56\x78\x80\x04\x16\x28\x03\x06\x84\x12\x02\x84\x14\xB4\x53\xB4\x56\x78") #print i._version #print i.dscp #print i.ds #print i._flow_label #print i._payload #print i._nxt_hdr #print i._hop_limit #print i.src #print i.dst #########################################################
def test_add_new_protocols__warn(self): # TODO: how py.test handles with warnings? py.test.skip('how py.test handles with warnings?') p = Packet(strict=False) py.test.raises(StrictWarning, "p.include(TCP(), IP())")
def test_proto_access(self): p = Packet(IP(), TCP()) assert isinstance(p.protos[0], IP) assert isinstance(p.protos[1], TCP) assert isinstance(p.ip, IP) assert isinstance(p.tcp, TCP)
def test_overwrite_options(self): p = Packet(strict=False, warn=True) assert p.strict is False assert p.warn is True
def test_default_options(self): p = Packet() assert p.strict is True assert p.warn is True
from umit.umpa._sockets import INET from umit.umpa.utils.security import super_priviliges ip = IP(src='127.0.0.1', dst='127.0.0.1') #print "Value Of fielf key for Ipv6 is Here" #print(ip) #print(list(ip.get_fields_keys())) #print ICMP(type=8) tcp = TCP() tcp.srcport = 295 tcp.dstport = 255 tcp.set_flags('flags', syn=True) #payload = Payload() #payload.data = "this is umpa!" first_packet = Packet(ip, tcp) #first_packet.include(payload) #print "Structure Of Ipv4 Packet" #print first_packet #print "_____________________" sock = super_priviliges(INET) sock.send(first_packet) #print tcp._checksum ####################################################################### #udp = UDP(srcport=25, dstport=362) #ip.ttl = "windows" #second_packet = Packet(ip, udp) #print second_packet #sock.send(second_packet) #########################################################################