Browse Source

initial commit with a working poc

master
Christopher Talib 2 years ago
commit
4dfa109fde
  1. 2
      .gitignore
  2. 5
      go.mod
  3. 9
      go.sum
  4. 84
      main.go

2
.gitignore

@ -0,0 +1,2 @@
resgo
*.pcap

5
go.mod

@ -0,0 +1,5 @@
module git.postblue.info/chris/resgo
go 1.12
require github.com/google/gopacket v1.1.17

9
go.sum

@ -0,0 +1,9 @@
github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY=
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 h1:1Fzlr8kkDLQwqMP8GxrhptBLqZG/EDpiATneiZHY998=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

84
main.go

@ -0,0 +1,84 @@
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
)
func main() {
// find devices and return them
devices := findDevices()
// try to capture on devices
for _, d := range devices {
fmt.Println("trying: ", d.Name)
err := openDeviceForCapture(d.Name)
if err != nil {
fmt.Println(err)
}
}
}
func pcapName(deviceName string) *os.File {
currentTime := time.Now()
fileName := currentTime.Format("2006-01-02 15:04:05") + deviceName
f, _ := os.Create(fileName)
return f
}
func openDeviceForCapture(deviceName string) error {
var snapshotLen int32
snapshotLen = 1024
promiscuous := false
var timeout time.Duration
timeout = 30 * time.Second
limit := 0
handle, err := pcap.OpenLive(deviceName, snapshotLen, promiscuous, timeout)
if err != nil {
return err
}
defer handle.Close()
pcapName := pcapName(deviceName)
w := pcapgo.NewWriter(pcapName)
w.WriteFileHeader(1024, layers.LinkTypeEthernet)
defer pcapName.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
fmt.Println(packet)
w.WritePacket(packet.Metadata().CaptureInfo, packet.Data())
limit++
if limit == 10 {
break
}
}
return nil
}
func findDevices() []pcap.Interface {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
var res []pcap.Interface
for _, d := range devices {
res = append(res, d)
}
return res
}
Loading…
Cancel
Save