sensor-d4-tls-fingerprinting/d4-tlsf.go

491 lines
13 KiB
Go
Raw Permalink Normal View History

2019-01-23 14:41:30 +01:00
package main
import (
2019-01-29 11:38:27 +01:00
"bytes"
2019-01-30 15:04:12 +01:00
// TODO consider
//"github.com/google/certificate-transparency-go/x509"
2019-02-04 16:26:34 +01:00
2019-01-29 11:38:27 +01:00
"encoding/json"
2019-01-23 14:41:30 +01:00
"flag"
"fmt"
2019-01-29 11:38:27 +01:00
"io"
2019-01-23 14:41:30 +01:00
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/examples/util"
"github.com/google/gopacket/ip4defrag"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/reassembly"
2019-02-01 11:28:20 +01:00
2019-02-04 16:26:34 +01:00
"github.com/D4-project/sensor-d4-tls-fingerprinting/d4tls"
2019-02-01 11:28:20 +01:00
"github.com/D4-project/sensor-d4-tls-fingerprinting/etls"
2019-01-23 14:41:30 +01:00
)
var nodefrag = flag.Bool("nodefrag", false, "If true, do not do IPv4 defrag")
var checksum = flag.Bool("checksum", false, "Check TCP checksum")
var nooptcheck = flag.Bool("nooptcheck", false, "Do not check TCP options (useful to ignore MSS on captures with TSO)")
var ignorefsmerr = flag.Bool("ignorefsmerr", false, "Ignore TCP FSM errors")
var verbose = flag.Bool("verbose", false, "Be verbose")
var debug = flag.Bool("debug", false, "Display debug information")
var quiet = flag.Bool("quiet", false, "Be quiet regarding errors")
var partial = flag.Bool("partial", true, "Output partial TLS Sessions, e.g. where we don't see all three of client hello, server hello and certificate")
2019-01-23 14:41:30 +01:00
// capture
var iface = flag.String("i", "eth0", "Interface to read packets from")
var fname = flag.String("r", "", "Filename to read from, overrides -i")
// writing
2019-01-29 11:38:27 +01:00
var outCerts = flag.String("w", "", "Folder to write certificates into")
var outJSON = flag.String("j", "", "Folder to write certificates into, stdin if not set")
2019-02-04 16:26:34 +01:00
var jobQ chan d4tls.TLSSession
2019-03-11 10:18:25 +01:00
// memory
var bufferedPagesPerConnection = flag.Int("mbpc", 16, "Max Buffered Pages per Connection.")
var bufferedPagesTotal = flag.Int("mbpt", 1024, "Max Total Buffered Pages.")
// flushing
var flushEvery = flag.Int("flush", 5000, "Flush every N packets")
var dtf, _ = time.ParseDuration("5m")
var dtc, _ = time.ParseDuration("48h")
var flushTf = flag.Duration("flushtf", dtf, "Flush older than t")
var flushTc = flag.Duration("flushtc", dtc, "Close older that t")
2019-01-23 14:41:30 +01:00
2019-02-20 10:31:55 +01:00
var assemblerOptions = reassembly.AssemblerOptions{
2019-03-11 10:18:25 +01:00
MaxBufferedPagesPerConnection: *bufferedPagesPerConnection,
MaxBufferedPagesTotal: *bufferedPagesTotal, // unlimited
2019-02-20 10:31:55 +01:00
}
2019-01-23 14:41:30 +01:00
var outputLevel int
var errorsMap map[string]uint
var errorsMapMutex sync.Mutex
var errors uint
// Too bad for perf that a... is evaluated
func Error(t string, s string, a ...interface{}) {
errorsMapMutex.Lock()
errors++
nb, _ := errorsMap[t]
errorsMap[t] = nb + 1
errorsMapMutex.Unlock()
if outputLevel >= 0 {
//fmt.Printf(s, a...)
2019-01-23 14:41:30 +01:00
}
}
func Info(s string, a ...interface{}) {
if outputLevel >= 1 {
fmt.Printf(s, a...)
}
}
func Debug(s string, a ...interface{}) {
if outputLevel >= 2 {
fmt.Printf(s, a...)
}
}
/*
* The TCP factory: returns a new Stream
*/
type tcpStreamFactory struct {
wg sync.WaitGroup
}
func (factory *tcpStreamFactory) New(net, transport gopacket.Flow, tcp *layers.TCP, ac reassembly.AssemblerContext) reassembly.Stream {
fsmOptions := reassembly.TCPSimpleFSMOptions{
2019-02-15 10:28:25 +01:00
SupportMissingEstablishment: true,
2019-01-23 14:41:30 +01:00
}
stream := &tcpStream{
net: net,
transport: transport,
isTLS: true,
tcpstate: reassembly.NewTCPSimpleFSM(fsmOptions),
ident: fmt.Sprintf("%s:%s", net, transport),
optchecker: reassembly.NewTCPOptionCheck(),
2019-02-04 16:26:34 +01:00
tlsSession: d4tls.TLSSession{},
2019-01-23 14:41:30 +01:00
}
// Set network information in the TLSSession
cip, sip, cp, sp := getIPPorts(stream)
stream.tlsSession.SetNetwork(cip, sip, cp, sp)
2019-01-23 14:41:30 +01:00
return stream
}
func (factory *tcpStreamFactory) WaitGoRoutines() {
factory.wg.Wait()
}
/*
* The assembler context
*/
type Context struct {
CaptureInfo gopacket.CaptureInfo
}
func (c *Context) GetCaptureInfo() gopacket.CaptureInfo {
return c.CaptureInfo
}
/*
* TCP stream
*/
/* It's a connection (bidirectional) */
type tcpStream struct {
tcpstate *reassembly.TCPSimpleFSM
fsmerr bool
optchecker reassembly.TCPOptionCheck
net, transport gopacket.Flow
isTLS bool
reversed bool
urls []string
ident string
2019-02-04 16:26:34 +01:00
tlsSession d4tls.TLSSession
queued bool
2019-02-15 10:28:25 +01:00
ignorefsmerr bool
nooptcheck bool
checksum bool
2019-01-23 14:41:30 +01:00
sync.Mutex
}
func (t *tcpStream) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir reassembly.TCPFlowDirection, nextSeq reassembly.Sequence, start *bool, ac reassembly.AssemblerContext) bool {
// FSM
if !t.tcpstate.CheckState(tcp, dir) {
if !t.fsmerr {
t.fsmerr = true
}
2019-02-15 10:28:25 +01:00
if !t.ignorefsmerr {
2019-01-23 14:41:30 +01:00
return false
}
}
// Options
err := t.optchecker.Accept(tcp, ci, dir, nextSeq, start)
if err != nil {
2019-02-15 10:28:25 +01:00
if !t.nooptcheck {
2019-01-23 14:41:30 +01:00
return false
}
}
// Checksum
accept := true
2019-02-15 10:28:25 +01:00
if t.checksum {
2019-01-23 14:41:30 +01:00
c, err := tcp.ComputeChecksum()
if err != nil {
accept = false
} else if c != 0x0 {
accept = false
}
}
return accept
}
func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.AssemblerContext) {
_, _, _, skip := sg.Info()
length, _ := sg.Lengths()
2019-02-15 10:28:25 +01:00
if skip == -1 {
2019-01-23 14:41:30 +01:00
// this is allowed
} else if skip != 0 {
// Missing bytes in stream: do not even try to parse it
return
}
data := sg.Fetch(length)
if t.isTLS {
2019-01-23 14:41:30 +01:00
if length > 0 {
2019-02-05 16:26:44 +01:00
// We attempt to decode TLS
2019-02-01 11:28:20 +01:00
tls := &etls.ETLS{}
2019-01-23 14:41:30 +01:00
var decoded []gopacket.LayerType
2019-02-01 11:28:20 +01:00
p := gopacket.NewDecodingLayerParser(etls.LayerTypeETLS, tls)
p.DecodingLayerParserOptions.IgnoreUnsupported = true
2019-01-23 14:41:30 +01:00
err := p.DecodeLayers(data, &decoded)
if err != nil {
2019-02-05 16:26:44 +01:00
// If it's fragmented we keep for next round
2019-01-23 14:41:30 +01:00
sg.KeepFrom(0)
} else {
//Debug("TLS: %s\n", gopacket.LayerDump(tls))
// Debug("TLS: %s\n", gopacket.LayerGoString(tls))
if tls.Handshake != nil {
// If the timestamp has not been set, we set it for the first time.
if t.tlsSession.Record.Timestamp.IsZero() {
info := sg.CaptureInfo(0)
t.tlsSession.SetTimestamp(info.Timestamp)
}
for _, tlsrecord := range tls.Handshake {
2019-02-01 11:28:20 +01:00
switch tlsrecord.ETLSHandshakeMsgType {
// Client Hello
case 1:
t.tlsSession.PopulateClientHello(tlsrecord.ETLSHandshakeClientHello)
2019-02-04 16:26:34 +01:00
t.tlsSession.D4Fingerprinting("ja3")
// Server Hello
case 2:
2019-02-04 16:26:34 +01:00
t.tlsSession.PopulateServerHello(tlsrecord.ETLSHandshakeServerHello)
t.tlsSession.D4Fingerprinting("ja3s")
// Server Certificate
case 11:
2019-02-04 16:26:34 +01:00
t.tlsSession.PopulateCertificate(tlsrecord.ETLSHandshakeCertificate)
t.tlsSession.D4Fingerprinting("tlsh")
2019-01-23 14:41:30 +01:00
}
}
if t.tlsSession.HandshakeComplete() && !t.queued {
t.queueSession()
}
2019-01-23 14:41:30 +01:00
}
}
}
}
}
func getIPPorts(t *tcpStream) (string, string, string, string) {
tmp := strings.Split(fmt.Sprintf("%v", t.net), "->")
ipc := tmp[0]
ips := tmp[1]
tmp = strings.Split(fmt.Sprintf("%v", t.transport), "->")
cp := tmp[0]
ps := tmp[1]
return ipc, ips, cp, ps
}
2019-01-23 14:41:30 +01:00
func (t *tcpStream) ReassemblyComplete(ac reassembly.AssemblerContext) bool {
// If the handshakre has not yet been outputted, but there are some information such as
// either the client hello or the server hello, we also output a partial.
if *partial && !t.queued && t.tlsSession.HandshakeAny() {
t.queueSession()
}
2019-02-20 10:31:55 +01:00
// remove connection from the pool
return true
2019-01-23 14:41:30 +01:00
}
func main() {
2019-02-20 10:31:55 +01:00
2019-01-23 14:41:30 +01:00
defer util.Run()()
var handle *pcap.Handle
var err error
if *debug {
outputLevel = 2
} else if *verbose {
outputLevel = 1
} else if *quiet {
outputLevel = -1
}
errorsMap = make(map[string]uint)
if *fname != "" {
if handle, err = pcap.OpenOffline(*fname); err != nil {
log.Fatal("PCAP OpenOffline error:", err)
}
} else {
// Open live on interface
if handle, err = pcap.OpenLive(*iface, 65536, true, 0); err != nil {
log.Fatal("PCAP OpenOffline error:", err)
}
defer handle.Close()
}
if len(flag.Args()) > 0 {
bpffilter := strings.Join(flag.Args(), " ")
Info("Using BPF filter %q\n", bpffilter)
if err = handle.SetBPFFilter(bpffilter); err != nil {
log.Fatal("BPF filter error:", err)
}
}
2019-02-15 12:51:30 +01:00
source := gopacket.NewPacketSource(handle, handle.LinkType())
2019-01-23 14:41:30 +01:00
source.NoCopy = true
Info("Starting to read packets\n")
count := 0
bytes := int64(0)
defragger := ip4defrag.NewIPv4Defragmenter()
streamFactory := &tcpStreamFactory{}
streamPool := reassembly.NewStreamPool(streamFactory)
assembler := reassembly.NewAssembler(streamPool)
2019-02-20 10:31:55 +01:00
assembler.AssemblerOptions = assemblerOptions
2019-01-23 14:41:30 +01:00
// Signal chan for system signals
2019-01-23 14:41:30 +01:00
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
// Job chan to hold Completed sessions to write
jobQ = make(chan d4tls.TLSSession, 4096)
cancelC := make(chan string)
// We start a worker to send the processed TLS connection the outside world
2019-01-29 16:06:23 +01:00
var w sync.WaitGroup
w.Add(1)
2019-02-15 15:22:23 +01:00
go processCompletedSession(cancelC, jobQ, &w)
2019-02-15 12:51:30 +01:00
var eth layers.Ethernet
var ip4 layers.IPv4
var ip6 layers.IPv6
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &eth, &ip4, &ip6)
decoded := []gopacket.LayerType{}
2019-01-23 14:41:30 +01:00
for packet := range source.Packets() {
count++
Debug("PACKET #%d\n", count)
2019-02-15 12:51:30 +01:00
2019-01-23 14:41:30 +01:00
data := packet.Data()
2019-02-15 12:51:30 +01:00
if err := parser.DecodeLayers(data, &decoded); err != nil {
// Well it sures complaing about not knowing how to decode TCP
2019-01-23 14:41:30 +01:00
}
2019-02-15 12:51:30 +01:00
for _, layerType := range decoded {
switch layerType {
case layers.LayerTypeIPv6:
2019-02-15 14:55:32 +01:00
//fmt.Println(" IP6 ", ip6.SrcIP, ip6.DstIP)
2019-02-15 12:51:30 +01:00
case layers.LayerTypeIPv4:
2019-02-15 14:55:32 +01:00
//fmt.Println(" IP4 ", ip4.SrcIP, ip4.DstIP)
2019-02-15 12:51:30 +01:00
// defrag the IPv4 packet if required
if !*nodefrag {
l := ip4.Length
2019-02-15 14:55:32 +01:00
newip4, err := defragger.DefragIPv4(&ip4)
2019-02-15 12:51:30 +01:00
if err != nil {
log.Fatalln("Error while de-fragmenting", err)
} else if newip4 == nil {
Debug("Fragment...\n")
continue // ip packet fragment, we don't have whole packet yet.
}
if newip4.Length != l {
Debug("Decoding re-assembled packet: %s\n", newip4.NextLayerType())
pb, ok := packet.(gopacket.PacketBuilder)
if !ok {
panic("Not a PacketBuilder")
}
nextDecoder := newip4.NextLayerType()
nextDecoder.Decode(newip4.Payload, pb)
}
}
tcp := packet.Layer(layers.LayerTypeTCP)
if tcp != nil {
tcp := tcp.(*layers.TCP)
if *checksum {
err := tcp.SetNetworkLayerForChecksum(packet.NetworkLayer())
if err != nil {
log.Fatalf("Failed to set network layer for checksum: %s\n", err)
}
}
c := Context{
CaptureInfo: packet.Metadata().CaptureInfo,
}
assembler.AssembleWithContext(packet.NetworkLayer().NetworkFlow(), tcp, &c)
2019-01-23 14:41:30 +01:00
}
if count%*flushEvery == 0 {
ref := packet.Metadata().CaptureInfo.Timestamp
flushed, closed := assembler.FlushWithOptions(reassembly.FlushOptions{T: ref.Add(-*flushTf), TC: ref.Add(-*flushTc)})
Debug("Forced flush: %d flushed, %d closed (%s)", flushed, closed, ref)
}
2019-01-23 14:41:30 +01:00
}
}
2019-02-15 12:51:30 +01:00
bytes += int64(len(data))
2019-01-23 14:41:30 +01:00
var done bool
select {
case <-signalChan:
fmt.Fprintf(os.Stderr, "\nCaught SIGINT: aborting\n")
cancelC <- "stop"
2019-01-23 14:41:30 +01:00
done = true
2019-02-15 15:22:23 +01:00
break
2019-01-23 14:41:30 +01:00
default:
// NOP: continue
}
if done {
break
}
}
2019-02-15 14:55:32 +01:00
Info(fmt.Sprintf("%d Bytes read.\n", bytes))
2019-01-23 14:41:30 +01:00
assembler.FlushAll()
streamFactory.WaitGoRoutines()
2019-01-29 16:06:23 +01:00
// All systems gone
// We close the processing queue
close(jobQ)
w.Wait()
}
// queueSession tries to enqueue the tlsSession for output
// returns true if it succeeded or false if it failed to publish the
// tlsSession for output
func (t *tcpStream) queueSession() bool {
t.queued = true
2019-02-15 10:28:25 +01:00
select {
case jobQ <- t.tlsSession:
2019-02-15 10:28:25 +01:00
return true
default:
return false
}
}
2019-02-15 15:22:23 +01:00
func processCompletedSession(cancelC <-chan string, jobQ <-chan d4tls.TLSSession, w *sync.WaitGroup) {
for {
2019-02-15 15:22:23 +01:00
select {
case tlss, more := <-jobQ:
if more {
output(tlss)
} else {
w.Done()
return
}
case <-cancelC:
2019-01-29 16:06:23 +01:00
w.Done()
}
}
}
2019-01-23 14:41:30 +01:00
2019-02-04 16:26:34 +01:00
func output(t d4tls.TLSSession) {
jsonRecord, _ := json.Marshal(t.Record)
2019-01-29 11:38:27 +01:00
// If an output folder was specified for certificates
if *outCerts != "" {
if _, err := os.Stat(fmt.Sprintf("./%s", *outCerts)); !os.IsNotExist(err) {
2019-02-04 16:26:34 +01:00
for _, certMe := range t.Record.Certificates {
2019-02-04 13:55:58 +01:00
err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.crt", *outCerts, certMe.CertHash), certMe.Certificate.Raw, 0644)
2019-01-29 16:06:23 +01:00
if err != nil {
panic("Could not write to file.")
}
2019-01-29 11:38:27 +01:00
}
} else {
panic(fmt.Sprintf("./%s does not exist", *outCerts))
}
}
2019-01-29 11:38:27 +01:00
// If an output folder was specified for json files
if *outJSON != "" {
if _, err := os.Stat(fmt.Sprintf("./%s", *outJSON)); !os.IsNotExist(err) {
err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.json", *outJSON, t.Record.Timestamp.Format(time.RFC3339Nano)), jsonRecord, 0644)
2019-01-29 16:06:23 +01:00
if err != nil {
panic("Could not write to file.")
}
2019-01-29 11:38:27 +01:00
} else {
panic(fmt.Sprintf("./%s does not exist", *outJSON))
}
// If not folder specified, we output to stdout
2019-01-29 11:38:27 +01:00
} else {
var buf bytes.Buffer
if err := json.Compact(&buf, jsonRecord); err != nil {
fmt.Println("Failed to compact json output")
}
if _, err := buf.Write([]byte{0x0A}); err != nil {
fmt.Println("Could not append delimiter")
}
if _, err := io.Copy(os.Stdout, &buf); err != nil {
2019-01-29 16:06:23 +01:00
panic("Could not write to stdout.")
}
2019-01-29 11:38:27 +01:00
}
Debug(t.String())
2019-01-23 14:41:30 +01:00
}