Last active
December 3, 2019 14:31
-
-
Save tbhaxor/4c5df18eb8a62b08847e2000d68004b9 to your computer and use it in GitHub Desktop.
Simple ICMP Packet Sniffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import socket | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
# configuring the argument | |
parser = ArgumentParser(description="host discovery tool", | |
formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument("--host", | |
help="hostname to bind", | |
metavar="HOSTNAME/IP", | |
required=True) | |
# actually parsing the args | |
args = parser.parse_args() | |
# instancing the sniffer | |
# setting the protocol to capture only ICMP Packets | |
sniffer = socket.socket(socket.AF_INET, | |
socket.SOCK_RAW, | |
proto=socket.IPPROTO_ICMP) | |
# binding to the host:port | |
sniffer.bind((args.host, 0)) | |
# including the ip header in capture packet | |
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) | |
# getting the raw packet data along with ip and port | |
pkt = sniffer.recvfrom(65565) | |
# formatting | |
print("Remote IP:", pkt[1][0]) | |
print("Remote PORT:", pkt[1][1]) | |
print("Data:", pkt[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment