Skip to content

Commit 1070bca

Browse files
furusiyyanamay26
andauthored
Code refactor to improve naming, logging and connection handling (GSoC2025/issue 161) (#189)
* Add initial passthrough implementation * Add destination routing logic * Add traffic capture config for passthrough and host:port target for dest * Add tests and improve function structure * Add io.copy and change to tcp_proxy * refactor: improve naming, logging, and connection handling * (fixes) proxytcp code and tests cleaning * (docs) minor fix, removed untracked file --------- Co-authored-by: namay26 <namayrohatgi@gmail.com>
1 parent 1e53480 commit 1070bca

12 files changed

Lines changed: 1138 additions & 15 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ poc/
3737

3838
# Dev
3939
.vscode
40+
openspec/
41+
.cache
42+
.codex
43+
openspec/
44+
docs/roadmap.md
45+
docs/engineering-guidelines.md
46+

config/config.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ producers:
2222
auth: auth
2323
channel: test
2424

25-
conn_timeout: 45
26-
max_tcp_payload: 4096
25+
conn_timeout: 45 # idle I/O timeout in seconds for established connections.
26+
max_tcp_payload: 4096 # bytes
27+
dial_timeout: 5 # timeout in seconds for proxy target connection.
28+
29+
capture_traffic:
30+
enabled: false

config/rules.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ rules:
3535
- match: tcp dst port 27017
3636
type: conn_handler
3737
target: mongodb
38+
- match: tcp dst port 9889
39+
type: proxy_tcp
40+
target: 127.0.0.1:9889
41+
- match: tcp dst port 3306
42+
type: proxy_tcp
43+
target: 127.0.0.1:3306
3844
- match: tcp
3945
type: conn_handler
4046
target: tcp

docs/configuration.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ This file holds the core settings for Glutton. Key configuration options include
1313
- **udp:** The UDP port for intercepted packets (default: `5001`).
1414
- **ssh:** Typically excluded from redirection to avoid interfering with SSH (default: `22`).
1515
- **interface:** The network interface Glutton listens on (default: `eth0`).
16-
- **max_tcp_payload:** Maximum TCP payload size in bytes (default: `4096`).
17-
- **conn_timeout:** The connection timeout duration in seconds (default: `45`).
16+
- **conn_timeout:** Idle I/O timeout, in seconds, for established connections (default: `45`).
17+
- **max_tcp_payload:** Maximum TCP payload size in bytes (default: `4096`). Proxy TCP uses this as the per-direction captured payload cap.
18+
- **dial_timeout:** Timeout, in seconds, for opening outbound proxy TCP target connections (default: `5`).
19+
- **capture_traffic.enabled:** Enables raw payload capture in logs and produced decoded events. When disabled, proxy TCP still forwards traffic and logs metadata, but raw payload bytes are omitted from decoded events.
1820
- **confpath:** The directory path where the configuration file resides.
1921
- **producers:**
2022
- **enabled**: Boolean flag to enable or disable logging/producer functionality.
@@ -55,6 +57,10 @@ producers:
5557

5658
conn_timeout: 45
5759
max_tcp_payload: 4096
60+
dial_timeout: 5
61+
62+
capture_traffic:
63+
enabled: false
5864
```
5965
6066
### config/rules.yaml
@@ -63,8 +69,8 @@ This file defines the rules that Glutton uses to determine which protocol handle
6369
6470
Key elements include:
6571
66-
- **type**: `conn_handler` to pass off to the appropriate protocol handler or `drop` to ignore packets.
67-
- **target**: Indicates the protocol handler (e.g., "http", "ftp") to be used.
72+
- **type**: `conn_handler` to pass off to the appropriate protocol handler, `proxy_tcp` to forward the TCP connection to an upstream target, or `drop` to ignore packets.
73+
- **target**: For `conn_handler`, indicates the protocol handler (e.g., `http`, `ftp`) to use. For `proxy_tcp`, this must be the upstream target in `host:port` form.
6874
- **match**: Define criteria such as source IP ranges or destination ports to match incoming traffic, according to [BPF syntax](https://biot.com/capstats/bpf.html).
6975

7076
Example rule:
@@ -80,8 +86,14 @@ rules:
8086
- match: tcp dst port 6969
8187
type: drop # drops any matching packets
8288
target: bittorrent
89+
- name: Proxy TCP example
90+
match: tcp dst port 9889
91+
type: proxy_tcp
92+
target: 127.0.0.1:9889
8393
```
8494

95+
`proxy_tcp` dials the configured `target` and forwards bytes in both directions between the incoming connection and the upstream service. Produced decoded events use the `proxy_tcp` protocol name and can include one captured payload entry per direction. Captured payloads are capped by `max_tcp_payload`; when a direction transfers more bytes than the cap, the decoded event is marked as truncated.
96+
8597
## Configuration Loading Process
8698
Glutton uses the [Viper](https://github.com/spf13/viper) library to load configuration settings. The process works as follows:
8799

glutton.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,18 @@ func (g *Glutton) tcpListen() {
222222
g.Logger.Error("Failed to set connection timeout", producer.ErrAttr(err))
223223
}
224224

225-
if hfunc, ok := g.tcpProtocolHandlers[rule.Target]; ok {
225+
var handlerName string
226+
switch rule.Type {
227+
case "proxy_tcp":
228+
handlerName = rule.Type
229+
default:
230+
handlerName = rule.Target
231+
}
232+
233+
if hfunc, ok := g.tcpProtocolHandlers[handlerName]; ok {
226234
go func() {
227235
if err := hfunc(g.ctx, conn, md); err != nil {
228-
g.Logger.Error("Failed to handle TCP connection", producer.ErrAttr(err), slog.String("handler", rule.Target))
236+
g.Logger.Error("Failed to handle ", producer.ErrAttr(err), slog.String("handler", handlerName))
229237
}
230238
}()
231239
}

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ nav:
88
- FAQs: faq.md
99
theme:
1010
name: readthedocs
11-

protocols/protocols.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func MapTCPProtocolHandlers(log interfaces.Logger, h interfaces.Honeypot) map[st
7272
protocolHandlers["mongodb"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
7373
return tcp.HandleMongoDB(ctx, conn, md, log, h)
7474
}
75+
protocolHandlers["proxy_tcp"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
76+
return tcp.HandleProxyTCP(ctx, conn, md, log, h)
77+
}
7578
protocolHandlers["tcp"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
7679
snip, bufConn, err := Peek(conn, 4)
7780
if err != nil {

0 commit comments

Comments
 (0)