Skip to content

Instantly share code, notes, and snippets.

@david415
Created June 10, 2015 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david415/d38936fd3e93168ae221 to your computer and use it in GitHub Desktop.
Save david415/d38936fd3e93168ae221 to your computer and use it in GitHub Desktop.
FreeBSD Golang sniffer using BPF
package main
import (
"encoding/hex"
"fmt"
"syscall"
)
func main() {
enable := 1
fd, err := syscall.Open("/dev/bpf0", syscall.O_RDWR, syscall.S_IRUSR|syscall.S_IWUSR)
if err != nil {
panic(err)
}
err = syscall.SetBpfInterface(fd, "vtnet0")
if err != nil {
panic(err)
}
err = syscall.SetBpfImmediate(fd, enable)
if err != nil {
panic(err)
}
err = syscall.SetBpfHeadercmpl(fd, enable)
if err != nil {
panic(err)
}
var buf_len int
buf_len, err = syscall.BpfBuflen(fd)
if err != nil {
panic(err)
}
fmt.Printf("buflen %d\n", buf_len)
err = syscall.SetBpfPromisc(fd, enable)
if err != nil {
panic(err)
}
var n int
for {
buf := make([]byte, buf_len)
n, err = syscall.Read(fd, buf)
if err != nil {
panic(err)
}
//fmt.Printf("% X\n", buf[:n])
fmt.Printf("\npacket of size %d captured\n", n)
fmt.Print(hex.Dump(buf[:n]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment