Skip to content
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

feat: support service banner for TCP #168

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions protocols/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func StorePayload(data []byte) (string, error) {
}
return sha256Hash, nil
}

func HashData(data []byte) string {
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}
1 change: 1 addition & 0 deletions protocols/tcp/responses/110_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+OK
Binary file added protocols/tcp/responses/135_tcp
Binary file not shown.
Binary file added protocols/tcp/responses/139_tcp
Binary file not shown.
Binary file added protocols/tcp/responses/1433_tcp
Binary file not shown.
4 changes: 4 additions & 0 deletions protocols/tcp/responses/21000_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>
1 change: 1 addition & 0 deletions protocols/tcp/responses/21_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
220 Welcome to localhost
1 change: 1 addition & 0 deletions protocols/tcp/responses/25_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
250 localhost ESMTP Postfix
Binary file added protocols/tcp/responses/3306_tcp
Binary file not shown.
4 changes: 4 additions & 0 deletions protocols/tcp/responses/4444_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>
Binary file added protocols/tcp/responses/445_tcp
Binary file not shown.
Binary file added protocols/tcp/responses/4899_tcp
Binary file not shown.
26 changes: 26 additions & 0 deletions protocols/tcp/responses/5060_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
SIP/2.0 200 OK
Via: SIP/2.0/TCP 127.0.0.1:5060;branch=1234567890
From: sip:[email protected];tag=bad-012345
To: <sip:[email protected];user=phone>;tag=bad-012345
Call-ID: 1348979872-797979222304855
Cseq: 15 INVITE
Contact: sip:[email protected]
Content-Length: 401
Content-Type: application/sdp

v=0
Anonymous 1234567890 9876543210 IN IP4 127.0.0.1
s=SIGMA is the best
s=gotcha
c=IN IP4 127.0.0.1
t=0 0
m=audio 36952 RTP/AVP 107 119 100 106 6 0 97 105 98 8 18 3 5 101
a=rtpmap:107 BV32/16000
a=rtpmap:119 BV32-FEC/16000
a=rtpmap:100 SPEEX/16000
a=rtpmap:106 SPEEX-FEC/16000
a=rtpmap:97 SPEEX/8000
a=rtpmap:105 SPEEX-FEC/8000
a=rtpmap:98 iLBC/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-11
1 change: 1 addition & 0 deletions protocols/tcp/responses/5900_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RFB 003.008
Binary file added protocols/tcp/responses/8009_tcp
Binary file not shown.
15 changes: 15 additions & 0 deletions protocols/tcp/responses/80_tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Connection: close
Date: Sun, 27 Nov 2005 13:07:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Accept-Ranges: bytes
Content-Length: 30
Cache-Control: private
Content-Type: text/html; charset=utf-8

<HTML>
<BODY>
</BODY>
</HTML>
57 changes: 55 additions & 2 deletions protocols/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tcp
import (
"context"
"crypto/rand"
"embed"
"encoding/hex"
"fmt"
"log/slog"
Expand All @@ -28,6 +29,58 @@ type tcpServer struct {
events []parsedTCP
}

//go:embed responses/*
var embeddedFiles embed.FS

var responseFileMap = map[uint16]string{
glaslos marked this conversation as resolved.
Show resolved Hide resolved
21: "responses/21_tcp",
25: "responses/25_tcp",
80: "responses/80_tcp",
110: "responses/110_tcp",
135: "responses/135_tcp",
139: "responses/139_tcp",
445: "responses/445_tcp",
1433: "responses/1433_tcp",
3306: "responses/3306_tcp",
4444: "responses/4444_tcp",
4899: "responses/4899_tcp",
5060: "responses/5060_tcp",
5900: "responses/5900_tcp",
8009: "responses/8009_tcp",
21000: "responses/21000_tcp",
}

func (s *tcpServer) getResponse(port uint16) ([]byte, error) {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
filePath, exists := responseFileMap[port]
if !exists {
return nil, fmt.Errorf("file not found for port: %v", port)
}

return embeddedFiles.ReadFile(filePath)
}

func (s *tcpServer) sendPortSpecificResponse(conn net.Conn, port uint16, logger interfaces.Logger) error {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
response, err := s.getResponse(port)
if err != nil {
// If no specific response file exists, fall back to random data
logger.Debug("No specific response file found, sending random data",
glaslos marked this conversation as resolved.
Show resolved Hide resolved
slog.String("handler", "tcp"),
slog.Uint64("port", uint64(port)))
return s.sendRandom(conn)
}

s.events = append(s.events, parsedTCP{
Direction: "write",
PayloadHash: helpers.HashData(response),
Payload: response,
})

if _, err := conn.Write(response); err != nil {
return fmt.Errorf("failed to write response: %w", err)
}
return nil
}

func (s *tcpServer) sendRandom(conn net.Conn) error {
randomInt, err := rand.Int(rand.Reader, big.NewInt(500))
if err != nil {
Expand Down Expand Up @@ -114,8 +167,8 @@ func HandleTCP(ctx context.Context, conn net.Conn, md connection.Metadata, logge
}
}

// sending some random data
if err := server.sendRandom(conn); err != nil {
// Send port-specific or random response
if err := server.sendPortSpecificResponse(conn, md.TargetPort, logger); err != nil {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
logger.Error("write error", slog.String("handler", "tcp"), producer.ErrAttr(err))
}

Expand Down