Skip to content

Instantly share code, notes, and snippets.

@affix
Last active January 5, 2023 15:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save affix/51daf036faf68593fb6d87af9eba1f0f to your computer and use it in GitHub Desktop.
Save affix/51daf036faf68593fb6d87af9eba1f0f to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"log"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
var (
iface = "docker0"
buffer = int32(1600)
filter = "tcp and port 21"
)
func main() {
fmt.Println("--= GoSniff =--")
fmt.Println("A simple packet sniffer in golang")
if !deviceExists(iface) {
log.Fatal("Unable to open device ", iface)
}
handler, err := pcap.OpenLive(iface, buffer, false, pcap.BlockForever)
if err != nil {
log.Fatal(err)
}
defer handler.Close()
if err := handler.SetBPFFilter(filter); err != nil {
log.Fatal(err)
}
source := gopacket.NewPacketSource(handler, handler.LinkType())
for packet := range source.Packets() {
harvestFTPCreds(packet)
}
}
func harvestFTPCreds(packet gopacket.Packet) {
app := packet.ApplicationLayer()
if app != nil {
payload := app.Payload()
dst := packet.NetworkLayer().NetworkFlow().Dst()
if bytes.Contains(payload, []byte("USER")) {
fmt.Print(dst, " -> ", string(payload))
} else if bytes.Contains(payload, []byte("PASS")) {
fmt.Print(dst, " -> ", string(payload))
}
}
}
func deviceExists(name string) bool {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Panic(err)
}
for _, device := range devices {
if device.Name == name {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment