Skip to content

Instantly share code, notes, and snippets.

@xputerax
Created September 17, 2024 09:21
Show Gist options
  • Select an option

  • Save xputerax/0b14d1476ead66da9b357b7c852fb691 to your computer and use it in GitHub Desktop.

Select an option

Save xputerax/0b14d1476ead66da9b357b7c852fb691 to your computer and use it in GitHub Desktop.

Revisions

  1. xputerax created this gist Sep 17, 2024.
    59 changes: 59 additions & 0 deletions ping.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/usr/bin/env python3

    # send ICMP using Impacket (PoC)
    # setup:
    # $ python3 -m venv .venv
    # $ source .venv/bin/activate
    # (venv)$ pip3 install impacket
    # (venv)$ sudo ping.py 192.168.1.1 100

    import socket
    from impacket import ImpactPacket, ImpactDecoder
    import traceback
    import sys

    DEBUG = False

    def send_icmp(host):
    icmp = ImpactPacket.ICMP()
    icmp.set_icmp_type(icmp.ICMP_ECHO)
    icmp.set_icmp_id(0x1337)

    if DEBUG:
    print("raw ICMP packet (len={}):".format(len(icmp.get_packet())))
    print(icmp.get_packet())

    d = ImpactDecoder.ICMPDecoder()
    decoded = d.decode(icmp.get_packet())
    print("decoded ICMP packet:")
    print(decoded)

    try:
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    ret = s.sendto(icmp.get_packet(), (host, 0))
    print("sent {} bytes".format(ret))
    except PermissionError:
    print("PermissionError: failed to open socket. please run as sudo")
    except:
    print("unexpected error:")
    print(traceback.format_exc())
    pass

    if __name__ == "__main__":
    if len(sys.argv) < 2:
    print("usage: ping.py <host ip> <count>")
    sys.exit(-1)

    host = sys.argv[1]
    count = 1

    if len(sys.argv) == 3:
    count = int(sys.argv[2])

    print("[+] sending {} ICMP packet(s) to {}".format(count, host))

    i = 0
    while (i < count):
    send_icmp(host)
    print("ping {} sent".format(i+1))
    i += 1