-
Notifications
You must be signed in to change notification settings - Fork 21
/
json.go
148 lines (124 loc) · 3.64 KB
/
json.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
//go:build !ja3_disable_gopacket
/*
* JA3 - TLS Client Hello Hash
* Copyright (c) 2017, Salesforce.com, Inc.
* this code was created by Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ja3
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"strconv"
"time"
"github.com/google/gopacket"
)
// Record contains all information for a calculated JA3
type Record struct {
DestinationIP string `json:"destination_ip"`
DestinationPort int `json:"destination_port"`
JA3 string `json:"ja3"`
JA3Digest string `json:"ja3_digest"`
JA3S string `json:"ja3s"`
JA3SDigest string `json:"ja3s_digest"`
SourceIP string `json:"source_ip"`
SourcePort int `json:"source_port"`
Timestamp float64 `json:"timestamp"`
}
// ReadFileJSON reads the PCAP file at the given path
// and prints out all packets containing JA3 digests formatted as JSON to the supplied io.Writer
func ReadFileJSON(file string, out io.Writer, doJA3s bool) {
r, f, link, err := openPcap(file)
if err != nil {
panic(err)
}
defer f.Close()
var records []*Record
for {
// read packet data
data, ci, err := r.ReadPacketData()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
var (
// create gopacket
p = gopacket.NewPacket(data, link, gopacket.Lazy)
// get JA3 if possible
bare = BarePacket(p)
isServer bool
)
if doJA3s && len(bare) == 0 {
bare = BarePacketJa3s(p)
isServer = true
}
// check if we got a result
if len(bare) > 0 {
var (
nl = p.NetworkLayer()
tl = p.TransportLayer()
)
// stop if either network or transport layer is nil
if tl == nil || nl == nil {
fmt.Println("error: ", nl, tl, p.Dump())
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),
}
if isServer {
r.JA3S = string(bare)
r.JA3SDigest = BareToDigestHex(bare)
} else {
r.JA3 = string(bare)
r.JA3Digest = BareToDigestHex(bare)
}
// append record and populate all fields
records = append(records, r)
}
}
// make it pretty please
b, err := json.MarshalIndent(records, "", " ")
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)
}
}
if Debug {
fmt.Println(len(records), "fingerprints.")
}
}
// convert a time.Time to a string timestamp in the format seconds.microseconds
func timeToString(t time.Time) string {
micro := fmt.Sprintf("%06d", t.Nanosecond()/1000)
return strconv.FormatInt(t.Unix(), 10) + "." + micro
}
func timeStringToFloat64(t string) float64 {
f, err := strconv.ParseFloat(t, 64)
if err != nil {
fmt.Println("[ERROR] failed to convert", t, "to float64. error:", err)
}
return f
}
func timeToFloat(t time.Time) float64 {
return timeStringToFloat64(timeToString(t))
}