Skip to content

Commit

Permalink
Merge pull request #14 from mraerino/feat/gopacket-opt-out
Browse files Browse the repository at this point in the history
Allow opt-out of building with gopacket
  • Loading branch information
dreadl0ck authored Sep 14, 2022
2 parents d928b0a + e460030 commit 0c3c870
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 73 deletions.
2 changes: 2 additions & 0 deletions csv.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !ja3_disable_gopacket

/*
* JA3 - TLS Client Hello Hash
* Copyright (c) 2017, Salesforce.com, Inc.
Expand Down
2 changes: 2 additions & 0 deletions gopacket.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !ja3_disable_gopacket

/*
* JA3 - TLS Client Hello Hash
* Copyright (c) 2017, Salesforce.com, Inc.
Expand Down
71 changes: 0 additions & 71 deletions ja3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,82 +17,11 @@ package ja3
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"strconv"
"strings"

"github.com/dreadl0ck/tlsx"
"github.com/google/gopacket"
)

// ReadFileJa3s reads the PCAP file at the given path
// and prints out all packets containing JA3S digests to the supplied io.Writer
func ReadFileJa3s(file string, out io.Writer) {

r, f, link, err := openPcap(file)
if err != nil {
panic(err)
}
defer f.Close()

count := 0
for {
// read packet data
data, _, err := r.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, link, gopacket.Lazy)
// get JA3 if possible
digest = DigestHexPacketJa3s(p)
)

// check if we got a result
if digest != "" {

count++

var (
b strings.Builder
nl = p.NetworkLayer()
tl = p.TransportLayer()
)

// got an a digest but no transport or network layer
if tl == nil || nl == nil {
if Debug {
fmt.Println("got a nil layer: ", nl, tl, p.Dump(), digest)
}
continue
}

b.WriteString("[")
b.WriteString(nl.NetworkFlow().Dst().String())
b.WriteString(":")
b.WriteString(tl.TransportFlow().Dst().String())
b.WriteString("] JA3S: ")
b.WriteString(string(BarePacketJa3s(p)))
b.WriteString(" --> ")
b.WriteString(digest)
b.WriteString("\n")

_, err := out.Write([]byte(b.String()))
if err != nil {
panic(err)
}
}
}
}

// BareToDigestHex converts a bare []byte to a hex string.
func BareToDigestHexJa3s(bare []byte) string {
sum := md5.Sum(bare)
Expand Down
78 changes: 78 additions & 0 deletions ja3s_pcap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//go:build !ja3_disable_gopacket

package ja3

import (
"fmt"
"io"
"strings"

"github.com/google/gopacket"
)

// ReadFileJa3s reads the PCAP file at the given path
// and prints out all packets containing JA3S digests to the supplied io.Writer
func ReadFileJa3s(file string, out io.Writer) {

r, f, link, err := openPcap(file)
if err != nil {
panic(err)
}
defer f.Close()

count := 0
for {
// read packet data
data, _, err := r.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, link, gopacket.Lazy)
// get JA3 if possible
digest = DigestHexPacketJa3s(p)
)

// check if we got a result
if digest != "" {

count++

var (
b strings.Builder
nl = p.NetworkLayer()
tl = p.TransportLayer()
)

// got an a digest but no transport or network layer
if tl == nil || nl == nil {
if Debug {
fmt.Println("got a nil layer: ", nl, tl, p.Dump(), digest)
}
continue
}

b.WriteString("[")
b.WriteString(nl.NetworkFlow().Dst().String())
b.WriteString(":")
b.WriteString(tl.TransportFlow().Dst().String())
b.WriteString("] JA3S: ")
b.WriteString(string(BarePacketJa3s(p)))
b.WriteString(" --> ")
b.WriteString(digest)
b.WriteString("\n")

_, err := out.Write([]byte(b.String()))
if err != nil {
panic(err)
}
}
}
}
2 changes: 2 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !ja3_disable_gopacket

/*
* JA3 - TLS Client Hello Hash
* Copyright (c) 2017, Salesforce.com, Inc.
Expand Down
4 changes: 3 additions & 1 deletion live.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//go:build !ja3_disable_gopacket

package ja3

import (
"encoding/binary"
"encoding/json"
"fmt"
"github.com/google/gopacket/pcapgo"
"io"
"os"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
"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
Expand Down
4 changes: 3 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//go:build !ja3_disable_gopacket

package ja3

import (
"fmt"
"github.com/google/gopacket/layers"
"os"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)

Expand Down

0 comments on commit 0c3c870

Please sign in to comment.