/
capture_keylog_test.go
81 lines (62 loc) · 2.31 KB
/
capture_keylog_test.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package gont_test
import (
"bytes"
"encoding/hex"
"net"
"os"
"os/exec"
"strings"
"testing"
"time"
g "cunicu.li/gont/v2/pkg"
o "cunicu.li/gont/v2/pkg/options"
co "cunicu.li/gont/v2/pkg/options/capture"
"github.com/stretchr/testify/require"
)
// TestCaptureKeyLog tests the decryption of captured traffic
func TestCaptureKeyLog(t *testing.T) {
tmpPCAP, err := os.CreateTemp(t.TempDir(), "gont-capture-*.pcapng")
require.NoError(t, err, "Failed to open temporary file")
c1 := g.NewCapture(
co.ToFile(tmpPCAP),
co.LogKeys(true),
co.Comment("This PCAPng file contains TLS decryption secrets"),
)
n, err := g.NewNetwork(*nname,
o.Customize[g.NetworkOption](globalNetworkOptions, c1, // Also multiple capturers are supported
g.NewCapture(
co.ToFilename("all.pcapng")), // We can create a file
)...)
require.NoError(t, err, "Failed to create network")
server, err := AddWebServer(n, "server")
require.NoError(t, err, "Failed to create host")
client, err := n.AddHost("client")
require.NoError(t, err, "Failed to create host")
err = n.AddLink(
g.NewInterface("veth0", client,
o.AddressIP("fc::1:2/112")),
g.NewInterface("veth0", server,
o.AddressIP("fc::1:1/112")))
require.NoError(t, err, "Failed to add link")
_, err = client.Run("curl", "--http2", "--silent", "--insecure", "--connect-timeout", 5, "https://server")
require.NoError(t, err, "cURL Request failed: %s")
// Wait until all traffic propagates through PCAP
time.Sleep(time.Second)
// We must close here so all decryption secrets are written to the PCAP files
err = n.Close()
require.NoError(t, err, "Failed to close network")
t.Logf("PCAPng file: %s", tmpPCAP.Name())
c := exec.Command("tshark", "-r", tmpPCAP.Name(), "-T", "fields", "-e", "http2.data.data", "len(http2.data.data) > 0") //nolint:gosec
out := &bytes.Buffer{}
c.Stdout = out
err = c.Run()
require.NoError(t, err, "Failed to run tshark")
hostPortBytes, err := hex.DecodeString(strings.TrimSpace(out.String()))
require.NoError(t, err, "Failed to decode HTTP response body")
hostPort := string(hostPortBytes)
ip, _, err := net.SplitHostPort(hostPort)
require.NoError(t, err, "Failed to split host:port")
require.Equal(t, ip, "fc::1:2", "Got wrong IP")
}