netshovel/netshovel.go

61 lines
1.8 KiB
Go
Raw Normal View History

2018-07-24 17:44:11 -06:00
/*
Package netshovel provides utilities to assist in creating of application-layer protocol decoders.
examples/simple/simple.go contains a full decoder which does nothing but dump every utterance.
It can be used as a template for new work.
*/
package netshovel
2018-07-23 09:58:31 -06:00
import (
"flag"
2020-09-24 16:57:02 -06:00
"log"
2018-07-23 09:58:31 -06:00
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
)
2020-09-24 16:57:02 -06:00
// Shovel handles dispatching of PCAP files from the command line.
// It's intended that you invoke this from your main function.
2018-07-24 17:32:08 -06:00
// This parses the command line arguments,
// and for each PCAP file specified on the command line,
// invokes a TCP assembler that sends streams to whatever is returned from factory.
2018-07-23 09:58:31 -06:00
func Shovel(factory tcpassembly.StreamFactory) {
//verbose := flag.Bool("verbose", false, "Write lots of information out")
2018-07-23 09:58:31 -06:00
flag.Parse()
2018-07-24 17:53:06 -06:00
2018-07-23 09:58:31 -06:00
streamPool := tcpassembly.NewStreamPool(factory)
assembler := tcpassembly.NewAssembler(streamPool)
2018-07-24 17:53:06 -06:00
2018-07-23 09:58:31 -06:00
for _, fn := range flag.Args() {
2020-09-24 20:23:43 -06:00
ShovelFile(fn, assembler)
}
assembler.FlushAll()
}
2018-07-24 17:53:06 -06:00
2020-09-24 20:23:43 -06:00
// ShovelFile shovels a single file.
// You must call assembler.FlushAll() at the end of this!
func ShovelFile(filename string, assembler *tcpassembly.Assembler) {
handle, err := pcap.OpenOffline(filename)
if err != nil {
log.Fatal(err)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
packets := packetSource.Packets()
for packet := range packets {
if packet == nil {
break
2018-07-23 09:58:31 -06:00
}
2020-09-24 20:23:43 -06:00
if packet.NetworkLayer() == nil || packet.TransportLayer() == nil || packet.TransportLayer().LayerType() != layers.LayerTypeTCP {
continue
}
tcp := packet.TransportLayer().(*layers.TCP)
assembler.AssembleWithTimestamp(packet.NetworkLayer().NetworkFlow(), tcp, packet.Metadata().Timestamp)
2018-07-23 09:58:31 -06:00
}
}