|
| 1 | +package protocols |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mushorg/glutton/connection" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +type nopLogger struct{} |
| 16 | + |
| 17 | +func (nopLogger) Debug(string, ...any) {} |
| 18 | +func (nopLogger) Info(string, ...any) {} |
| 19 | +func (nopLogger) Warn(string, ...any) {} |
| 20 | +func (nopLogger) Error(string, ...any) {} |
| 21 | + |
| 22 | +// protocolHoneypot records the handler name of each produced TCP event. |
| 23 | +type protocolHoneypot struct { |
| 24 | + produced chan string |
| 25 | +} |
| 26 | + |
| 27 | +func (h *protocolHoneypot) ProduceTCP(protocol string, _ net.Conn, _ connection.Metadata, _ []byte, _ interface{}) error { |
| 28 | + h.produced <- protocol |
| 29 | + return nil |
| 30 | +} |
| 31 | +func (h *protocolHoneypot) ProduceUDP(string, *net.UDPAddr, *net.UDPAddr, connection.Metadata, []byte, interface{}) error { |
| 32 | + return nil |
| 33 | +} |
| 34 | +func (h *protocolHoneypot) ReplyUDP(*net.UDPAddr, *net.UDPAddr, []byte) error { return nil } |
| 35 | +func (h *protocolHoneypot) ConnectionByFlow([2]uint64) connection.Metadata { |
| 36 | + return connection.Metadata{} |
| 37 | +} |
| 38 | +func (h *protocolHoneypot) UpdateConnectionTimeout(_ context.Context, conn net.Conn) error { |
| 39 | + return conn.SetDeadline(time.Now().Add(2 * time.Second)) |
| 40 | +} |
| 41 | +func (h *protocolHoneypot) MetadataByConnection(net.Conn) (connection.Metadata, error) { |
| 42 | + return connection.Metadata{}, nil |
| 43 | +} |
| 44 | + |
| 45 | +// dispatchMCTP runs the "mctp" target on a loopback connection, lets send |
| 46 | +// drive the client side, and returns the produced handler names. |
| 47 | +func dispatchMCTP(t *testing.T, send func(net.Conn)) []string { |
| 48 | + t.Helper() |
| 49 | + t.Chdir(t.TempDir()) // HandleTCP stores payloads in ./payloads |
| 50 | + prev := viper.GetInt("max_tcp_payload") |
| 51 | + viper.Set("max_tcp_payload", 4096) |
| 52 | + t.Cleanup(func() { viper.Set("max_tcp_payload", prev) }) |
| 53 | + |
| 54 | + l, err := net.Listen("tcp", "127.0.0.1:0") |
| 55 | + require.NoError(t, err) |
| 56 | + defer l.Close() |
| 57 | + |
| 58 | + hp := &protocolHoneypot{produced: make(chan string, 4)} |
| 59 | + handler := MapTCPProtocolHandlers(nopLogger{}, hp)["mctp"] |
| 60 | + require.NotNil(t, handler) |
| 61 | + |
| 62 | + done := make(chan error, 1) |
| 63 | + go func() { |
| 64 | + conn, err := l.Accept() |
| 65 | + if err != nil { |
| 66 | + done <- err |
| 67 | + return |
| 68 | + } |
| 69 | + done <- handler(context.Background(), conn, connection.Metadata{TargetPort: 9000}) |
| 70 | + }() |
| 71 | + |
| 72 | + client, err := net.Dial("tcp", l.Addr().String()) |
| 73 | + require.NoError(t, err) |
| 74 | + require.NoError(t, client.SetDeadline(time.Now().Add(3*time.Second))) |
| 75 | + send(client) |
| 76 | + require.NoError(t, client.Close()) |
| 77 | + |
| 78 | + select { |
| 79 | + case err := <-done: |
| 80 | + require.NoError(t, err) |
| 81 | + case <-time.After(3 * time.Second): |
| 82 | + t.Fatal("handler did not finish") |
| 83 | + } |
| 84 | + close(hp.produced) |
| 85 | + var names []string |
| 86 | + for name := range hp.produced { |
| 87 | + names = append(names, name) |
| 88 | + } |
| 89 | + return names |
| 90 | +} |
| 91 | + |
| 92 | +func TestMCTPTargetRoutesMCTP(t *testing.T) { |
| 93 | + names := dispatchMCTP(t, func(c net.Conn) { |
| 94 | + _, err := c.Write([]byte("REMOTE HI_SRDK_DEV_GetHddInfo MCTP/1.0\r\nCSeq:173\r\nContent-Length:15\r\n\r\nSegment-Num:0\r\n")) |
| 95 | + require.NoError(t, err) |
| 96 | + buf := make([]byte, 256) |
| 97 | + n, err := c.Read(buf) |
| 98 | + require.NoError(t, err) |
| 99 | + require.Contains(t, string(buf[:n]), "MCTP/1.0 200 OK\r\n") |
| 100 | + }) |
| 101 | + require.Equal(t, []string{"mctp"}, names) |
| 102 | +} |
| 103 | + |
| 104 | +func TestMCTPTargetFallsBackToTCP(t *testing.T) { |
| 105 | + // FastCGI BEGIN_REQUEST header, as sent by PHP-FPM exploit scanners on :9000 |
| 106 | + names := dispatchMCTP(t, func(c net.Conn) { |
| 107 | + _, err := c.Write([]byte{0x01, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) |
| 108 | + require.NoError(t, err) |
| 109 | + _, _ = io.ReadAll(c) // random-bytes reply until the handler closes |
| 110 | + }) |
| 111 | + require.Equal(t, []string{"tcp"}, names) |
| 112 | +} |
| 113 | + |
| 114 | +func TestMCTPTargetShortPayloadFallsBackToTCP(t *testing.T) { |
| 115 | + names := dispatchMCTP(t, func(c net.Conn) { |
| 116 | + _, err := c.Write([]byte("REM")) |
| 117 | + require.NoError(t, err) |
| 118 | + _, _ = io.ReadAll(c) |
| 119 | + }) |
| 120 | + require.Equal(t, []string{"tcp"}, names) |
| 121 | +} |
| 122 | + |
| 123 | +func TestMCTPTargetDelayedRequest(t *testing.T) { |
| 124 | + names := dispatchMCTP(t, func(c net.Conn) { |
| 125 | + time.Sleep(300 * time.Millisecond) // longer than the request-line peek window |
| 126 | + _, err := c.Write([]byte("REMOTE HI_SRDK_SYS_GetSystemAttr MCTP/1.0\r\nCSeq:1\r\nContent-Length:0\r\n\r\n")) |
| 127 | + require.NoError(t, err) |
| 128 | + buf := make([]byte, 256) |
| 129 | + _, err = c.Read(buf) |
| 130 | + require.NoError(t, err) |
| 131 | + }) |
| 132 | + require.Equal(t, []string{"mctp"}, names) |
| 133 | +} |
| 134 | + |
| 135 | +func TestMCTPTargetSilentClientNoEvent(t *testing.T) { |
| 136 | + names := dispatchMCTP(t, func(net.Conn) {}) |
| 137 | + require.Empty(t, names) |
| 138 | +} |
0 commit comments