From 0ad5254cc8859abf782680d643cb89edacd12c0f Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Mon, 2 Sep 2024 10:59:52 -0500 Subject: [PATCH] sni detection --- Makefile | 2 +- go.mod | 21 ++++---- go.sum | 57 +++++--------------- pkg/wireguard/packetlogger.go | 86 +++++++++++++++++++++++++++--- pkg/wireguard/packetlogger_test.go | 44 ++++++++++++++- 5 files changed, 145 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index b47e1ca..93e7ee8 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ install-digitalocean: cd provisioning && packer build packer-digitalocean-amd64.pkr.hcl install-s3: - cd provisioning && AWS_PROFILE=in4it-vpn-server scripts/install_s3.sh --release + cd provisioning && AWS_PROFILE=in4it-vpn-server scripts/install_s3.sh install-docs: provisioning/scripts/deploy_documentation.sh diff --git a/go.mod b/go.mod index d0b2b45..a4cf984 100644 --- a/go.mod +++ b/go.mod @@ -1,34 +1,31 @@ module github.com/in4it/wireguard-server -go 1.22.2 +go 1.23.0 require ( github.com/go-jose/go-jose/v4 v4.0.2 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 + github.com/gopacket/gopacket v1.3.0 github.com/mdlayher/genetlink v1.3.2 github.com/mdlayher/netlink v1.7.2 + github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5 github.com/russellhaering/gosaml2 v0.9.1 github.com/russellhaering/goxmldsig v1.4.0 - golang.org/x/crypto v0.25.0 - golang.org/x/sys v0.22.0 - golang.org/x/term v0.22.0 + golang.org/x/crypto v0.26.0 + golang.org/x/sys v0.24.0 + golang.org/x/term v0.23.0 ) require ( github.com/beevik/etree v1.4.0 // indirect github.com/google/go-cmp v0.6.0 // indirect - github.com/google/gopacket v1.1.19 // indirect - github.com/gopacket/gopacket v1.2.0 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/native v1.1.0 // indirect github.com/mattermost/xml-roundtrip-validator v0.1.0 // indirect github.com/mdlayher/socket v0.5.1 // indirect - github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5 // indirect github.com/sirupsen/logrus v1.9.3 // indirect - github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect - github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect - golang.org/x/net v0.27.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/text v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index bfc25fe..e3965de 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs= github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA= @@ -12,14 +11,11 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= -github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gopacket/gopacket v1.2.0 h1:eXbzFad7f73P1n2EJHQlsKuvIMJjVXK5tXoSca78I3A= -github.com/gopacket/gopacket v1.2.0/go.mod h1:BrAKEy5EOGQ76LSqh7DMAr7z0NNPdczWm2GxCG7+I8M= +github.com/gopacket/gopacket v1.3.0 h1:MouZCc+ej0vnqzB0WeiaO/6+tGvb+KU7UczxoQ+X0Yc= +github.com/gopacket/gopacket v1.3.0/go.mod h1:WnFrU1Xkf5lWKV38uKNR9+yYtppn+ZYzOyNqMeH4oNE= github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= -github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg= github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= @@ -48,7 +44,6 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/russellhaering/gosaml2 v0.9.1 h1:H/whrl8NuSoxyW46Ww5lKPskm+5K+qYLw9afqJ/Zef0= github.com/russellhaering/gosaml2 v0.9.1/go.mod h1:ja+qgbayxm+0mxBRLMSUuX3COqy+sb0RRhIGun/W2kc= -github.com/russellhaering/goxmldsig v1.3.0 h1:DllIWUgMy0cRUMfGiASiYEa35nsieyD3cigIwLonTPM= github.com/russellhaering/goxmldsig v1.3.0/go.mod h1:gM4MDENBQf7M+V824SGfyIUVFWydB7n0KkEubVJl+Tw= github.com/russellhaering/goxmldsig v1.4.0 h1:8UcDh/xGyQiyrW+Fq5t8f+l2DLB1+zlhYzkPUJ7Qhys= github.com/russellhaering/goxmldsig v1.4.0/go.mod h1:gM4MDENBQf7M+V824SGfyIUVFWydB7n0KkEubVJl+Tw= @@ -59,43 +54,19 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= -github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= -github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= -github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= -golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= +golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/pkg/wireguard/packetlogger.go b/pkg/wireguard/packetlogger.go index 336094f..2aa03ec 100644 --- a/pkg/wireguard/packetlogger.go +++ b/pkg/wireguard/packetlogger.go @@ -3,6 +3,7 @@ package wireguard import ( "bufio" "bytes" + "encoding/binary" "fmt" "net" "net/http" @@ -12,8 +13,8 @@ import ( "strings" "time" - "github.com/google/gopacket" - "github.com/google/gopacket/layers" + "github.com/gopacket/gopacket" + "github.com/gopacket/gopacket/layers" "github.com/in4it/wireguard-server/pkg/logging" "github.com/in4it/wireguard-server/pkg/storage" "github.com/packetcap/go-pcap" @@ -114,11 +115,22 @@ func parsePacket(storage storage.Iface, data []byte, clientCache *ClientCache) e } } case 443: - //fmt.Printf("dstport: %s: %s\n", tcpPacket.DstPort.String(), packet.ApplicationLayer().Payload()) - // TODO: get SNI from handshake - /*if tls, ok := packet.Layer(layers.LayerTypeTLS).(*layers.TLS); ok { - fmt.Printf("handshake: %+v\n", tls.Handshake) - }*/ + if tls, ok := packet.Layer(layers.LayerTypeTLS).(*layers.TLS); ok { + for _, handshake := range tls.Handshake { + if sni := parseTLSExtensionSNI([]byte(handshake.ClientHello.Extensions)); sni != nil { + storage.AppendFile(filename, []byte(strings.Join([]string{ + time.Now().Format(TIMESTAMP_FORMAT), + "https", + srcIP.String(), + dstIP.String(), + strconv.FormatUint(uint64(tcpPacket.SrcPort), 10), + strconv.FormatUint(uint64(tcpPacket.DstPort), 10), + string(sni)}, + ",")+"\n", + )) + } + } + } } } if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil { @@ -156,3 +168,63 @@ func parsePacket(storage storage.Iface, data []byte, clientCache *ClientCache) e return nil } + +// TLS Extensions http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml +type TLSExtension uint16 + +const ( + ExtServerName TLSExtension = 0 + ExtMaxFragLen TLSExtension = 1 + ExtClientCertURL TLSExtension = 2 + ExtTrustedCAKeys TLSExtension = 3 + ExtTruncatedHMAC TLSExtension = 4 + ExtStatusRequest TLSExtension = 5 + ExtUserMapping TLSExtension = 6 + ExtClientAuthz TLSExtension = 7 + ExtServerAuthz TLSExtension = 8 + ExtCertType TLSExtension = 9 + ExtSupportedGroups TLSExtension = 10 + ExtECPointFormats TLSExtension = 11 + ExtSRP TLSExtension = 12 + ExtSignatureAlgs TLSExtension = 13 + ExtUseSRTP TLSExtension = 14 + ExtHeartbeat TLSExtension = 15 + ExtALPN TLSExtension = 16 + ExtStatusRequestV2 TLSExtension = 17 + ExtSignedCertTS TLSExtension = 18 + ExtClientCertType TLSExtension = 19 + ExtServerCertType TLSExtension = 20 + ExtPadding TLSExtension = 21 + ExtEncryptThenMAC TLSExtension = 22 + ExtExtendedMasterSecret TLSExtension = 23 + ExtSessionTicket TLSExtension = 35 + ExtNPN TLSExtension = 13172 + ExtRenegotiationInfo TLSExtension = 65281 +) + +func parseTLSExtensionSNI(data []byte) []byte { + for len(data) > 0 { + if len(data) < 4 { + break + } + extensionType := binary.BigEndian.Uint16(data[:2]) + length := binary.BigEndian.Uint16(data[2:4]) + if len(data) < 4+int(length) { + break + } + if TLSExtension(extensionType) == ExtServerName && len(data) > 6 { + serverNameExtensionLength := binary.BigEndian.Uint16(data[4:6]) + entryType := data[6] + + if serverNameExtensionLength > 0 && entryType == 0 && len(data) > 8 { // 0 = DNS hostname + hostnameLength := binary.BigEndian.Uint16(data[7:9]) + fmt.Printf("extensionType: %d length: %d; slice: %d\n", extensionType, serverNameExtensionLength, 9+hostnameLength) + if len(data) > int(8+hostnameLength) { + return data[9 : 9+hostnameLength] + } + } + } + data = data[4+length:] + } + return nil +} diff --git a/pkg/wireguard/packetlogger_test.go b/pkg/wireguard/packetlogger_test.go index 838dd03..fd427d5 100644 --- a/pkg/wireguard/packetlogger_test.go +++ b/pkg/wireguard/packetlogger_test.go @@ -66,8 +66,48 @@ func TestParsePacket(t *testing.T) { if err != nil { t.Fatalf("read file error: %s", err) } - if !strings.Contains(string(out), `udp,10.189.184.2,10.0.0.2,58893,53,apple.com`) { - t.Fatalf("unexpected output") + if !strings.Contains(string(out), `,udp,10.189.184.2,10.0.0.2,58893,53,apple.com`) { + t.Fatalf("unexpected output. Expected udp record") } + if !strings.Contains(string(out), `,https,10.189.184.2,10.0.1.12,62026,443,vpn-server.in4it.io`) { + t.Fatalf("unexpected output. Expected https record") + } +} + +func TestParseTLSExtensionSNI(t *testing.T) { + input := []string{ + "00000018001600001376706e2d7365727665722e696e3469742e696f", + "00000018001600001376706e2d7365727665722e696e3469742e696f000b00020100000a000a0008001d001700180019000d00180016080606010603080505010503080404010403020102030010000e000c02683208687474702f312e31", + } + for _, s := range input { + data, err := hex.DecodeString(s) + if err != nil { + t.Fatalf("hex decode error: %s", err) + } + if sni := parseTLSExtensionSNI(data); sni != nil { + if string(sni) != "vpn-server.in4it.io" { + t.Fatalf("got SNI, but expected different hostname. Got: %s", sni) + } + } else { + t.Fatalf("no SNI found") + } + } +} +func TestParseTLSExtensionSNINoMatch(t *testing.T) { + input := []string{ + "0010000e000c02", + "000d00180016080606010603080505010503080404010403020102030010000e000c02683208687474702f312e31", + "00", + } + for _, s := range input { + + data, err := hex.DecodeString(s) + if err != nil { + t.Fatalf("hex decode error: %s", err) + } + if sni := parseTLSExtensionSNI(data); sni != nil { + t.Fatalf("got match, expected no match. Got: %s", sni) + } + } }