-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
8b8c437
5f3f0fc
db77c15
751425c
09c86b9
de9bd79
5b2075f
f5df923
26ea5ef
d4cba0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,7 @@ func (g *Glutton) tcpListen() { | |
} | ||
|
||
md, err := g.connTable.RegisterConn(conn, rule) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package protocols | |
import ( | ||
"bytes" | ||
"context" | ||
"github.com/mushorg/glutton/rules" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
"net" | ||
"strings" | ||
|
||
|
@@ -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 { | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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