-
Notifications
You must be signed in to change notification settings - Fork 6
/
fuzz.go
63 lines (51 loc) · 1.39 KB
/
fuzz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build gofuzz
// +build gofuzz
package fuzz
import (
"io/ioutil"
"log"
"os"
"github.com/colinnewell/pcap-cli/tcp"
"github.com/colinnewell/pcap2har-go/internal/reader"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
)
func Fuzz(data []byte) int {
streamFactory := &tcp.StreamFactory{
Reader: reader.New(),
}
streamPool := tcpassembly.NewStreamPool(streamFactory)
assembler := tcpassembly.NewAssembler(streamPool)
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(data); err != nil {
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
if handle, err := pcap.OpenOffline(tmpfile.Name()); err != nil {
return 0
} else {
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
// NOTE: just pushing all TCP through it on the basis it might
// be http.
if tcp, ok := packet.TransportLayer().(*layers.TCP); ok {
assembler.AssembleWithTimestamp(
packet.NetworkLayer().NetworkFlow(),
tcp, packet.Metadata().Timestamp)
}
}
}
assembler.FlushAll()
//fmt.Printf("Found %d connections\n", connections)
streamFactory.Reader.GetConversations()
return 0
}