Created
February 16, 2021 11:17
-
-
Save r10r/3bd69e5153de8a156b24b0203687e8f4 to your computer and use it in GitHub Desktop.
Capture HTTP network packages with gopacket/pcap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/google/gopacket" | |
//"github.com/google/gopacket/layers" | |
"flag" | |
"fmt" | |
"github.com/google/gopacket/pcap" | |
) | |
func main() { | |
var hostname string | |
flag.StringVar(&hostname, "hostname", "localhost", "target host name") | |
flag.Parse() | |
handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever) | |
if err != nil { | |
panic(err) | |
} | |
expr := fmt.Sprintf("host %s and tcp and dst port 80", hostname) | |
fmt.Printf("Handling expression %q\n", expr) | |
err = handle.SetBPFFilter(expr) | |
if err != nil { | |
panic(err) | |
} | |
packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) | |
for packet := range packetSource.Packets() { | |
// Get the TCP layer from this packet | |
if app := packet.ApplicationLayer(); app != nil { | |
println(string(app.Payload())) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment