Skip to content

use structured logging to enable easier log parsing #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"log"
"log/slog"
"os"
"time"

"github.com/google/gopacket"
Expand All @@ -14,6 +14,7 @@ import (
)

func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// Load FingerPrints
ja4Map, ja4sMap, err := parser.LoadFingerPrints("fingerprints.json")
Expand All @@ -25,14 +26,16 @@ func main() {
handle, err := pcap.OpenLive("enp0s3", 65535, true, pcap.BlockForever)
//handle, err := pcap.OpenLive("eth0", 65535, true, pcap.BlockForever)
if err != nil {
log.Fatal(err)
logger.Error("fatal error occurred", "err", err.Error())
os.Exit(1)
}
defer handle.Close()

//err = handle.SetBPFFilter("tcp port 443")
err = handle.SetBPFFilter("tcp")
if err != nil {
log.Fatal(err)
logger.Error("fatal error occurred", "err", err.Error())
os.Exit(1)
}

// Till here is normal flow
Expand All @@ -58,7 +61,7 @@ func main() {
// End of pcap files

packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
fmt.Println("DLT: ", handle.LinkType())
logger.Debug("process started", "DLT", handle.LinkType())
for {
select {
case packet := <-packetSource.Packets():
Expand All @@ -82,7 +85,7 @@ func main() {
// Avoid resource leak by cleaning up
cutoff := time.Now().Add(-2 * time.Minute)
flushedConn, closedConn := assembler.FlushWithOptions(tcpassembly.FlushOptions{CloseAll: false, T: cutoff})
fmt.Printf("FlushWithOptions - flushedConn: %v closedConn: %v\n", flushedConn, closedConn)
logger.Debug("flush", "flushed", flushedConn, "closed", closedConn)
}
}
}
39 changes: 21 additions & 18 deletions internal/parser/fingerprint_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"encoding/json"
"fmt"
"log/slog"
"os"

"github.com/voukatas/go-ja4/internal/model"
Expand Down Expand Up @@ -73,24 +74,26 @@ func LoadFingerPrints(fileName string) (map[string]*model.FingerprintRecord, map
}

func PrintRecord(record *model.FingerprintRecord) {
fmt.Printf(" Application: %s\n", deref(record.Application))
fmt.Printf(" Library: %s\n", deref(record.Library))
fmt.Printf(" Device: %s\n", deref(record.Device))
fmt.Printf(" OS: %s\n", deref(record.OS))
fmt.Printf(" User Agent String: %s\n", deref(record.UserAgentString))
fmt.Printf(" Certificate Authority: %s\n", deref(record.CertificateAuthority))
fmt.Printf(" Observation Count: %d\n", record.ObservationCount)
fmt.Printf(" Verified: %t\n", record.Verified)
fmt.Printf(" Notes: %s\n", deref(record.Notes))
fmt.Printf(" JA4 Fingerprint: %s\n", deref(record.Ja4Fingerprint))
fmt.Printf(" JA4 Fingerprint String: %s\n", deref(record.Ja4FingerprintString))
fmt.Printf(" JA4s Fingerprint: %s\n", deref(record.Ja4sFingerprint))
fmt.Printf(" JA4h Fingerprint: %s\n", deref(record.Ja4hFingerprint))
fmt.Printf(" JA4x Fingerprint: %s\n", deref(record.Ja4xFingerprint))
fmt.Printf(" JA4t Fingerprint: %s\n", deref(record.Ja4tFingerprint))
fmt.Printf(" JA4ts Fingerprint: %s\n", deref(record.Ja4tsFingerprint))
fmt.Printf(" JA4tscan Fingerprint: %s\n", deref(record.Ja4tscanFingerprint))
fmt.Println()
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger.Info("matched known fingerprint",
slog.String("application", deref(record.Application)),
slog.String("library", deref(record.Library)),
slog.String("device", deref(record.Device)),
slog.String("os", deref(record.OS)),
slog.String("user_agent_string", deref(record.UserAgentString)),
slog.String("certificate_authority", deref(record.CertificateAuthority)),
slog.Int("observation_count", record.ObservationCount),
slog.Bool("verified", record.Verified),
slog.String("notes", deref(record.Notes)),
slog.String("JA4_fingerprint", deref(record.Ja4Fingerprint)),
slog.String("JA4_fingerprint_string", deref(record.Ja4FingerprintString)),
slog.String("JA4s_fingerprint", deref(record.Ja4sFingerprint)),
slog.String("JA4h_fingerprint", deref(record.Ja4hFingerprint)),
slog.String("JA4x_fingerprint", deref(record.Ja4xFingerprint)),
slog.String("JA4t_fingerprint", deref(record.Ja4tFingerprint)),
slog.String("JA4ts_fingerprint", deref(record.Ja4tsFingerprint)),
slog.String("JA4tscan_fingerprint", deref(record.Ja4tscanFingerprint)),
)
}

func deref(str *string) string {
Expand Down
15 changes: 8 additions & 7 deletions internal/tcp/stream_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package tcp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"log/slog"
"os"

"github.com/google/gopacket"
"github.com/google/gopacket/tcpassembly/tcpreader"
Expand All @@ -17,6 +17,7 @@ import (
const maxBufferSize = 16 * 1024 // 16 KB

func processStream(r *tcpreader.ReaderStream, net gopacket.Flow, ja4Map, ja4sMap map[string]*model.FingerprintRecord) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
// fmt.Printf("Processing stream from %v to %v\n", net.Src(), net.Dst())
// defer fmt.Printf("Finished processing stream from %v to %v\n", net.Src(), net.Dst())
var buffer bytes.Buffer
Expand All @@ -29,7 +30,7 @@ func processStream(r *tcpreader.ReaderStream, net gopacket.Flow, ja4Map, ja4sMap
break
}

log.Println("Error reading from stream:", err)
logger.Error("error reading from stream", "err", err.Error())
return
}

Expand Down Expand Up @@ -70,9 +71,9 @@ func processStream(r *tcpreader.ReaderStream, net gopacket.Flow, ja4Map, ja4sMap
protocol := 't'
ja4Fingerprint, err := ja4.ParseClientHelloForJA4(data[:totalLength], byte(protocol))
if err != nil {
fmt.Println("Error parsing TLS Client Hello:", err)
logger.Error("error parsing TLS Client Hello", "err", err.Error())
} else {
fmt.Printf("JA4 Fingerprint: %s network: %v\n", ja4Fingerprint, net)
logger.Info("found JA4 fingerprint", "ja4", ja4Fingerprint, "src", net.Src(), "dest", net.Dst())
if val := ja4Map[ja4Fingerprint]; val != nil {
parser.PrintRecord(val)
}
Expand All @@ -88,10 +89,10 @@ func processStream(r *tcpreader.ReaderStream, net gopacket.Flow, ja4Map, ja4sMap
protocol := 't' // 't' for TCP
ja4sFingerprint, err := ja4.ParseServerHelloForJA4S(data[:totalLength], byte(protocol))
if err != nil {
fmt.Println("Error parsing TLS Server Hello:", err)
logger.Error("error parsing TLS Server Hello:", "err", err.Error())
} else {
//fmt.Printf("JA4S Fingerprint: %s\n", ja4sFingerprint)
fmt.Printf("JA4S Fingerprint: %s network: %v\n", ja4sFingerprint, net)
logger.Info("found JA4S fingerprint", "ja4s", ja4sFingerprint, "src", net.Src(), "dst", net.Dst())
if val := ja4sMap[ja4sFingerprint]; val != nil {
parser.PrintRecord(val)
}
Expand Down