Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return packet_layer
# Converting packet_definition to pypacker.Packet.
packet = reduce(lambda a, b: a + b, map(_pypacker_layer, packet_definition))
# Handle Dot1Q layer and IP options in packet_definition
first = operator.itemgetter(0)
for layer in packet_definition:
with suppress(KeyError):
dot1q_definition = layer["Dot1Q"]
pypacker_vlan = struct.pack("!H", pypacker.layer12.ethernet.ETH_TYPE_8021Q) + \
struct.pack("!H", dot1q_definition["vlan"])
packet.vlan = pypacker_vlan
with suppress(KeyError):
opts = layer["IP"]["opts"]
packet.ip.opts = [ip.IPOptMulti(**opt) for opt in opts]
with suppress(KeyError):
tlvlist_def = layer["LLDP"]["tlvlist"]
packet.lldp.tlvlist = []
for tlv_dict in tlvlist_def:
tlv_type = next(iter(tlv_dict))
packet.lldp.tlvlist.append(_pypacker_layer(
{tlv_type: OrderedDict(sorted(tlv_dict[tlv_type].items(), key=first))},
))
with suppress(KeyError):
# Inner list of DCBXApplicationPriorityTable
apppriotables = tlv_dict['DCBXApplicationPriority']['apppriotable']
app_class_name = "DCBXApplicationPriorityTable"
app_tables_list = []
for app_table in apppriotables:
app_tables_list.append(_pypacker_layer(
{app_class_name: OrderedDict(sorted(app_table[app_class_name].items(), key=first))}
def test_headerupate(self):
print_header("Header update (most common protos)")
pkt1 = ethernet.Ethernet() + ip.IP() + tcp.TCP() + dns.DNS()
layers = [layer for layer in pkt1]
layers.reverse()
# make sure every header in stack A.B.C... is uptodate starting from highest
# Must be same as A.bin() but we are testing...
for layer in layers:
layer.bin()
sum_ip = pkt1.ip.sum
sum_tcp = pkt1.ip.tcp.sum
dns1 = pkt1.ip.tcp.dns
dns_amounts = dns1.questions_amount + dns1.answers_amount + dns1.authrr_amount + dns1.addrr_amount
self.assertEqual(dns_amounts, 0)
# change highest layer and check checksums
dns1.queries.append(dns.DNS.Query())
dns1.answers.append(dns.DNS.Answer())
dns1.auths.append(dns.DNS.Auth())
dns1.addrecords.append(dns.DNS.AddRecord())
thread_rcv = threading.Thread(target=rcv_cycler, args=[sock_rcv])
thread_rcv.start()
print("starting to scan")
for port in range(0, 0xFFFF + 1):
print("\rPinging TCP port %d" % port, end="")
while not open_ports.empty():
port_open = open_ports.get()
print("\nTCP Port %d on %s seems to be open" % (port_open, IP_DST))
pkt_send = ethernet.Ethernet(dst_s=MAC_DST, src_s=MAC_SRC) +\
ip.IP(src_s=IP_SRC, dst_s=IP_DST) +\
tcp.TCP(sport=PORT_SRC, dport=port, seq=TCP_SEQ)
sock_send.send(pkt_send.bin())
print()
print("waiting some seconds to receive delayed responses")
time.sleep(2)
sock_rcv.close()
sock_send.close()
def icmp_cb(pargs):
"""ICMP DoS"""
pkt_icmpreq = ethernet.Ethernet(dst_s=pargs.mac_dst, src_s=pargs.mac_src) +\
ip.IP(src_s=pargs.ip_src, dst_s=pargs.ip_dst, p=ip.IP_PROTO_ICMP) +\
icmp.ICMP(type=8) +\
icmp.ICMP.Echo(id=1, ts=123456789, body_bytes=b"A" * 1460)
psock = psocket.SocketHndl(iface_name=pargs.iface_name)
for cnt in range(pargs.count):
psock.send(pkt_icmpreq.bin())
psock.close()
def verdict_cb(ll_data, ll_proto_id, data, ctx):
ip1 = ip.IP(data)
icmp1 = ip1[icmp.ICMP]
if icmp1 is None or icmp1.type != icmp.ICMP_TYPE_ECHO_REQ:
return data, interceptor.NF_ACCEPT
echo1 = icmp1[icmp.ICMP.Echo]
if echo1 is None:
return data, interceptor.NF_ACCEPT
pp_bts = b"PYPACKER"
print("changing ICMP echo request packet")
echo1.body_bytes = echo1.body_bytes[:len(pp_bts)] + pp_bts
return ip1.bin(), interceptor.NF_ACCEPT
def ip_cb(pargs):
"""
IP fragment DOS
"""
eth_l = ethernet.Ethernet(dst_s=pargs.mac_dst, src_s=pargs.mac_src)
ip_l = ip.IP(src_s=pargs.ip_src, dst_s=pargs.ip_dst)
ip_l.body_bytes = b"A" * 4000
psock = psocket.SocketHndl(iface_name=pargs.iface_name)
ip_frags = ip_l.create_fragments(fragment_len=8)
for cnt in range(pargs.count):
for ip_frag in ip_frags:
eth_l.upper_layer = ip_frag
psock.send(eth_l.bin())
psock.close()
def _dissect(self, buf):
family = struct.unpack("@I", buf[0:4])
hndl = None
if family == 2:
hndl = ip.IP(buf[4:])
elif family == 0x02000000:
#self.family = 2
hndl = ip.IP(buf[4:])
elif family in (24, 28, 30):
hndl = ip.IP6(buf[2:])
elif family > 1500:
hndl = ethernet.Ethernet(buf[4:])
if hndl is not None:
self._set_bodyhandler(hndl)
# http://www.iana.org/assignments/ppp-numbers
PPP_IP = 0x21 # Internet Protocol
PPP_IP6 = 0x57 # Internet Protocol v6
# Protocol field compression
PFC_BIT = 0x01
class PPP(pypacker.Packet):
__hdr__ = (
("p", None, triggerlist.TriggerList),
)
__handler__ = {
PPP_IP: ip.IP,
PPP_IP6: ip6.IP6
}
def _dissect(self, buf):
offset = 1
ppp_type = buf[0]
if buf[0] & PFC_BIT == 0:
ppp_type = unpack_H(buf[:2])[0]
offset = 2
self.p.append(buf[0:2])
else:
self.p.append(buf[0:1])
self._init_handler(ppp_type, buf[offset:])
return offset
LLC_TYPE_IP = 0x0800 # IPv4 protocol
LLC_TYPE_ARP = 0x0806 # address resolution protocol
LLC_TYPE_IP6 = 0x86DD # IPv6 protocol
class LLC(pypacker.Packet):
__hdr__ = (
("dsap", "B", 0),
("ssap", "B", 0),
("ctrl", "B", 0),
("snap", "5s", b"\x00" * 5),
)
__handler__ = {
LLC_TYPE_IP: ip.IP,
LLC_TYPE_ARP: arp.ARP,
LLC_TYPE_IP6: ip6.IP6
}
def _dissect(self, buf):
if buf[0] == 170: # = 0xAA
# SNAP is following ctrl
htype = unpack_H(buf[5:7])[0]
self._init_handler(htype, buf[8:])
else:
# deactivate SNAP
self.snap = None
return 8
filter = lambda p: p[ntp.NTP] is not None and p[ip.IP].src_s == IP_SRC
answer = psock.recvp(filter_match_recv=filter)[0]