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

Implement the pass through #172

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ rules:
- match: tcp dst port 11211
type: conn_handler
target: memcache
- match: tcp dst port 8080
type: pass_through
target: pass_through
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use a host:port as a target here

- match: tcp
type: conn_handler
target: tcp
Expand Down
1 change: 1 addition & 0 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (g *Glutton) tcpListen() {
}

md, err := g.connTable.RegisterConn(conn, rule)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

if err != nil {
g.Logger.Error("Failed to register connection", producer.ErrAttr(err))
continue
Expand Down
5 changes: 5 additions & 0 deletions protocols/protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package protocols
import (
"bytes"
"context"
"github.com/mushorg/glutton/rules"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use go fmt

"net"
"strings"

Expand Down Expand Up @@ -66,6 +67,10 @@ func MapTCPProtocolHandlers(log interfaces.Logger, h interfaces.Honeypot) map[st
return tcp.HandleADB(ctx, conn, md, log, h)
}
protocolHandlers["tcp"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
if md.Rule != nil && md.Rule.RuleType == rules.PassThrough {
return tcp.HandlePassThrough(ctx, conn, md, log, h)
}

snip, bufConn, err := Peek(conn, 4)
if err != nil {
if err := conn.Close(); err != nil {
Expand Down
66 changes: 66 additions & 0 deletions protocols/tcp/pass_through.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tcp

import (
"context"
"fmt"
"github.com/mushorg/glutton/connection"
"github.com/mushorg/glutton/producer"
"github.com/mushorg/glutton/protocols/interfaces"
"io"
"log/slog"
"net"
)

type parsedPassThrough struct {
Direction string `json:"direction,omitempty"`
Payload []byte `json:"payload,omitempty"`
PayloadHash string `json:"payload_hash,omitempty"`
}

type passThroughServer struct {
events []parsedPassThrough
target string
}

func HandlePassThrough(ctx context.Context, conn net.Conn, md connection.Metadata, logger interfaces.Logger, h interfaces.Honeypot) error {
srcAddr := conn.RemoteAddr().String()
logger.Info("PassThrough details",
slog.String("srcAddr", srcAddr),
slog.String("localAddr", conn.LocalAddr().String()))

destAddr := conn.LocalAddr().String()
targetConn, err := net.Dial("tcp", destAddr)
if err != nil {
return fmt.Errorf("connection failed: %w", err)
}
defer targetConn.Close()

errChan := make(chan error, 2)

// source to target
go func() {
_, err := io.Copy(targetConn, conn)
errChan <- err
}()
Comment on lines +43 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this go routine closed?


// target to source
go func() {
_, err := io.Copy(conn, targetConn)
errChan <- err
}()

// wait for either direction to succeed
select {
case err := <-errChan:
if err != nil && err != io.EOF {
logger.Error("Transfer error", producer.ErrAttr(err))
return err
}
case <-ctx.Done():
logger.Info("Context cancelled")
return ctx.Err()
}

logger.Info("Pass through completed successfully")
return nil
}
9 changes: 6 additions & 3 deletions rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type RuleType int
const (
UserConnHandler RuleType = iota
Drop
PassThrough
)

type Config struct {
Expand All @@ -32,7 +33,7 @@ type Rule struct {
Name string `yaml:"name,omitempty"`

isInit bool
ruleType RuleType
RuleType RuleType
index int
matcher *pcap.BPF
}
Expand All @@ -59,9 +60,11 @@ func (rule *Rule) init(idx int) error {

switch rule.Type {
case "conn_handler":
rule.ruleType = UserConnHandler
rule.RuleType = UserConnHandler
case "drop":
rule.ruleType = Drop
rule.RuleType = Drop
case "pass_through":
rule.RuleType = PassThrough
default:
return fmt.Errorf("unknown rule type: %s", rule.Type)
}
Expand Down