Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ICMP_TCP_UDP.py #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ICMP_TCP_UDP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#using scapy library
# install scapy with cmd pip install scapy
from scapy.all import sniff

def icmp_parser(packet):
return {
"type": packet[1].type,
"code": packet[1].code,
"checksum": packet[1].chksum,
}

def tcp_parser(packet):
return {
"src_port": packet[1].sport,
"dst_port": packet[1].dport,
"seq": packet[1].seq,
"ack": packet[1].ack,
}

def udp_parser(packet):
return {
"src_port": packet[1].sport,
"dst_port": packet[1].dport,
"length": packet[1].len,
}

def packet_callback(packet):
protocol_data = None

if packet.haslayer("ICMP"):
protocol = "ICMP"
protocol_data = icmp_parser(packet)
elif packet.haslayer("TCP"):
protocol = "TCP"
protocol_data = tcp_parser(packet)
elif packet.haslayer("UDP"):
protocol = "UDP"
protocol_data = udp_parser(packet)
else:
protocol = "Unknown"

print(f"Protocol: {protocol}")
if protocol_data:
print("Parsed Data:", protocol_data)

# Start sniffing
sniff(prn=packet_callback, count=10)