Use gopacket to capture packets under windows

Prepare

  1. install gopacket
    go get github.com/google/gopacket/pcap
  2. Install gcc
    to http://tdm-gcc.tdragon.net/downloaddownload gcc, configure gcc will not go into details

  3. Install winpcapand winpcap develop package
    go to https://www.winpcap.org/devel.htmdownload winpacp4.1.3 and development package 4.1.2 (there is no development package for 4.1.3)

write picture description here
Unzip WpdPack_4_1_2.zip to C:\WpdPack, this directory is necessary, otherwise the code in the go library must be modified

test

The function of this code is to display all network card device information of this device

package main

import (
    "fmt"
    "log"

    "github.com/google/gopacket/pcap"
)

func main() {
    // Find all devices
    devices, err := pcap.FindAllDevs()
    if err != nil {
        log.Fatal(err)
    }

    // Print device information
    fmt.Println("Devices found:")
    for _, d := range devices {
        fmt.Println("\nName: ", d.Name)
        fmt.Println("Description: ", d.Description)
        fmt.Println("Devices addresses: ", d.Description)

        for _, address := range d.Addresses {
            fmt.Println("- IP address: ", address.IP)
            fmt.Println("- Subnet mask: ", address.Netmask)
        }
    }
}

go run test.go

warn

Don't go to this place to download the development package, https://www.winpcap.org/archive/ , I don't understand the version in it, and it's wrong to download several.

quote

https://blog.csdn.net/worldzhy/article/details/8234584
https://www.devdungeon.com/content/packet-capture-injection-and-analysis-gopacket
https://www.winpcap.org/devel.htm
https://godoc.org/github.com/google/gopacket

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811248&siteId=291194637
Recommended