google / gopacket

Provides packet processing capabilities for Go
BSD 3-Clause "New" or "Revised" License
6.36k stars 1.14k forks source link

learning ping by intercepting the response #1081

Open amlwwalker opened 1 year ago

amlwwalker commented 1 year ago

I am new to tunnels and network level coding, however there are some great examples using gopacket specifically i found this one Decoding Packet Layers here.

I am interested in understanding PING specifically so I slightly adjusted the code to

    ipLayer := packet.Layer(layers.LayerTypeIPv4)
    if ipLayer != nil {
        fmt.Println("IPv4 layer detected.")
        ip, _ := ipLayer.(*layers.IPv4)

        if ip.Protocol == layers.IPProtocolICMPv4 {
            fmt.Println("icmp")
            fmt.Println("payload ", string(ip.Payload))
            fmt.Printf("From %s to %s\n", ip.SrcIP, ip.DstIP)
            fmt.Println()
            fmt.Printf("packet %+v\r\n", packet.Dump())
        }
        return
    }

What I'm interested in is manipulating the ping response in this case. I.e when my computer is pinged from another device on the network (or the internet if thats configured on the router) can I hijack the response that my machine makes, i.e maybe by changing the payload or the latency (time it takes to respond). I.e can I get the pinger to receive data that is incorrect.

I tried adding a time.Sleep(1 * time.Second) in the above, however it doesn't slow the response down - so what I figure is I am reading the incoming data, but not hijacking the network to manipulate the returned data.

Can gopacket do this? What would the interception of the response look like so that I can add in an arbitrary delay and get ping latency of 1s +

Thanks for all/any help.

ureykane commented 1 year ago

@amlwwalker gopacket only monitors the network traffic, and does not take control any packet

if you want learn something about network, suggest to use cisco packet tracer, you can use it to observe how ping packets are transmitted frame by frame