Created
June 10, 2015 23:53
-
-
Save david415/d38936fd3e93168ae221 to your computer and use it in GitHub Desktop.
FreeBSD Golang sniffer using BPF
This file contains hidden or 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 ( | |
"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