-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
48 lines (43 loc) · 1.14 KB
/
layers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from .protocols import types, ethernet, arp, ip, icmp, udp, tcp, dns, raw
from . import packet
import socket, os
Ethernet = ethernet.Ethernet
ARP = arp.ARP
IP = ip.IP
ICMP = icmp.ICMP
UDP = udp.UDP
TCP = tcp.TCP
DNSQ = dns.DNSQ
DNSA = dns.DNSA
Raw = raw.Raw
Packet = packet.Packet
class Types:
Proto_codes = types.ProtocolCode
Layers = types.Layers
def bind_socket(sock, iface):
try:
sock.bind((iface, 0))
return True
except:
return False
def send(packet:Packet, iface=None, sock=None):
"""
Sends a packet per given or detected network interface
"""
if not sock:
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
interfaces = os.listdir('/sys/class/net/')
is_bind = False
if not iface:
if Types.Layers.Ethernet in packet:
for iface in interfaces:
if iface != "lo":
is_bind = bind_socket(sock, iface)
if is_bind:
break
else:
is_bind = bind_socket(sock, iface)
if not is_bind:
raise Exception("Unable to bind socket")
else:
sock.send(packet.pack())