Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
5
Indexable
import os
import sys
import socket
import struct
import random

def send_ping_request(dest_ip):
    # Generate a random identifier and sequence number for ICMP packets
    identifier = random.randint(0, 65535)
    sequence_number = random.randint(0, 65535)

    # Create a raw socket using ICMP protocol
    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)

    # Set the time-to-live (TTL) field of the IP header
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_TTL, 64)

    # Craft the ICMP Echo Request packet
    icmp_packet = struct.pack('!BBHHH', 8, 0, 0, identifier, sequence_number)
    checksum = calculate_checksum(icmp_packet)
    icmp_packet = struct.pack('!BBHHH', 8, 0, checksum, identifier, sequence_number)

    try:
        # Send the ICMP Echo Request packet to the target IP address
        sock.sendto(icmp_packet, (dest_ip, 0))
        print(f"Sent ICMP Echo Request to {dest_ip}")
    except socket.error as e:
        print(f"Error: {e}")
        sys.exit()

def calculate_checksum(data):
    checksum = 0
    count_to = (len(data) // 2) * 2

    for count in range(0, count_to, 2):
        checksum += (data[count + 1] << 8) + data[count]

    if count_to < len(data):
        checksum += data[len(data) - 1]

    checksum = (checksum >> 16) + (checksum & 0xffff)
    checksum += (checksum >> 16)

    return ~checksum & 0xffff

# Replace 'target_ip' with the IP address of the target (e.g., your home network)
target_ip = "target_ip"

# Number of ICMP Echo Requests to send
num_requests = 10

for _ in range(num_requests):
    send_ping_request(target_ip)
Editor is loading...