-
Notifications
You must be signed in to change notification settings - Fork 21
/
live.go
162 lines (140 loc) · 3.72 KB
/
live.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//go:build !ja3_disable_gopacket
package ja3
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
)
// ReadInterface reads packets from the named interface
// if asJSON is true the results will be dumped as newline separated JSON objects
// otherwise CSV will be printed to the supplied io.Writer.
func ReadInterface(iface, bpfFilter, dumpPkg string, out io.Writer, separator string, ja3s bool, asJSON bool, snaplen int, promisc bool, timeout time.Duration) {
h, err := pcap.OpenLive(iface, int32(snaplen), promisc, timeout)
if err != nil {
panic(err)
}
defer h.Close()
if strings.TrimSpace(bpfFilter) != "" {
if err := h.SetBPFFilter(bpfFilter); err != nil {
panic(err)
}
}
var dumpFileHandle *os.File
var pcapWriter *pcapgo.Writer
if dumpPkg != "" {
dumpFileHandle, err = os.OpenFile(dumpPkg, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer dumpFileHandle.Close()
pcapWriter = pcapgo.NewWriter(dumpFileHandle)
pcapWriter.WriteFileHeader(uint32(snaplen), h.LinkType())
}
if !asJSON {
columns := []string{"timestamp", "source_ip", "source_port", "destination_ip", "destination_port", "ja3_digest"}
_, err = out.Write([]byte(strings.Join(columns, separator) + "\n"))
if err != nil {
panic(err)
}
}
count := 0
for {
// read packet data
data, ci, err := h.ReadPacketData()
if err == io.EOF {
if Debug {
fmt.Println(count, "fingerprints.")
}
return
} else if err != nil {
panic(err)
}
var (
// create gopacket
p = gopacket.NewPacket(data, layers.LinkTypeEthernet, gopacket.Lazy)
bare = BarePacket(p)
isServer bool
)
if ja3s && len(bare) == 0 {
bare = BarePacketJa3s(p)
isServer = true
}
// check if we got a result
if len(bare) > 0 {
count++
if pcapWriter != nil {
pcapWriter.WritePacket(ci, data)
}
var (
b strings.Builder
nl = p.NetworkLayer()
tl = p.TransportLayer()
)
// got a bare but no transport or network layer
if tl == nil || nl == nil {
if Debug {
fmt.Println("got a nil layer: ", nl, tl, p.Dump(), string(bare))
}
continue
}
r := &Record{
DestinationIP: nl.NetworkFlow().Dst().String(),
DestinationPort: int(binary.BigEndian.Uint16(tl.TransportFlow().Dst().Raw())),
SourceIP: nl.NetworkFlow().Src().String(),
SourcePort: int(binary.BigEndian.Uint16(tl.TransportFlow().Src().Raw())),
Timestamp: timeToFloat(ci.Timestamp),
}
digest := BareToDigestHex(bare)
if isServer {
r.JA3S = string(bare)
r.JA3SDigest = digest
} else {
r.JA3 = string(bare)
r.JA3Digest = digest
}
if asJSON {
// make it pretty please
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
panic(err)
}
if string(b) != "null" { // no matches will result in "null" json
// write to output io.Writer
_, err = out.Write(b)
if err != nil {
panic(err)
}
_, err = out.Write([]byte("\n"))
if err != nil {
panic(err)
}
}
} else { // CSV
b.WriteString(timeToString(ci.Timestamp))
b.WriteString(separator)
b.WriteString(nl.NetworkFlow().Src().String())
b.WriteString(separator)
b.WriteString(tl.TransportFlow().Src().String())
b.WriteString(separator)
b.WriteString(nl.NetworkFlow().Dst().String())
b.WriteString(separator)
b.WriteString(tl.TransportFlow().Dst().String())
b.WriteString(separator)
b.WriteString(digest)
b.WriteString("\n")
_, err := out.Write([]byte(b.String()))
if err != nil {
panic(err)
}
}
}
}
}