-
-
Notifications
You must be signed in to change notification settings - Fork 92
Passthrough implementation #185
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
Merged
furusiyya
merged 5 commits into
mushorg:gsoc2025/issue-161
from
namay26:Passthrough_implementation
Apr 26, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
58b881c
Add initial passthrough implementation
namay26 84d3ba6
Add destination routing logic
namay26 50bec0e
Add traffic capture config for passthrough and host:port target for dest
namay26 de76bf4
Add tests and improve function structure
namay26 797bb89
Add io.copy and change to tcp_proxy
namay26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,6 @@ producers: | |
|
|
||
| conn_timeout: 45 | ||
| max_tcp_payload: 4096 | ||
|
|
||
| capture_traffic: | ||
| enabled: false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package tcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "net" | ||
|
|
||
| "github.com/mushorg/glutton/connection" | ||
| "github.com/mushorg/glutton/producer" | ||
| "github.com/mushorg/glutton/protocols/interfaces" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| type parsedPassThrough struct { | ||
| Direction string `json:"direction,omitempty"` | ||
| Payload []byte `json:"payload,omitempty"` | ||
| PayloadHash string `json:"payload_hash,omitempty"` // Used for easier identification, can remove | ||
| } | ||
|
|
||
| type passThroughServer struct { | ||
| events []parsedPassThrough | ||
| conn net.Conn | ||
| target string | ||
| source string | ||
| } | ||
|
|
||
| // checks whether the payload can be converted to text, to prevent expensive hex coding. | ||
| func (srv *passThroughServer) isLikelyText(data []byte) bool { | ||
| if len(data) == 0 { | ||
| return false | ||
| } | ||
|
|
||
| printable := 0 | ||
| for _, b := range data { | ||
| if b >= 32 && b <= 126 || b == '\n' || b == '\r' || b == '\t' { | ||
| printable++ | ||
| } | ||
| } | ||
|
|
||
| return (printable*100)/len(data) > 80 // threshold value --> 80% | ||
| } | ||
|
|
||
| // logs the payload hex or payload text. | ||
| func (srv *passThroughServer) logPayload(direction string, data []byte, logger interfaces.Logger) { | ||
| if len(data) == 0 { | ||
| return | ||
| } | ||
|
|
||
| fields := []any{ | ||
| slog.String("direction", direction), | ||
| slog.Int("length", len(data)), | ||
| slog.String("sha256", fmt.Sprintf("%x", sha256.Sum256(data))), | ||
| } | ||
|
|
||
| if srv.isLikelyText(data) { | ||
| fields = append(fields, slog.String("payload", string(data))) | ||
| } else { | ||
| fields = append(fields, slog.String("hex", hex.EncodeToString(data))) | ||
| } | ||
|
|
||
| logger.Info("payload_transferred", fields...) | ||
| } | ||
|
|
||
| // records the events in the server | ||
| func (srv *passThroughServer) recordEvent(dir string, buf []byte, capture bool) { | ||
| if !capture { | ||
| return | ||
| } | ||
| hash := sha256.Sum256(buf) | ||
|
|
||
| payload := append([]byte(nil), buf...) // defensive copy | ||
|
|
||
| srv.events = append(srv.events, parsedPassThrough{ | ||
| Direction: dir, | ||
| Payload: payload, | ||
| PayloadHash: fmt.Sprintf("%x", hash[:]), | ||
| }) | ||
| } | ||
|
|
||
| // pipeBidirectional handles data transfer between the two connections | ||
| func pipeBidirectional(ctx context.Context, src, dst net.Conn, server *passThroughServer, logger interfaces.Logger, capture bool, errChan chan error) { | ||
| buf := make([]byte, 4096) | ||
| direction := getDirection(src, dst) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| errChan <- ctx.Err() | ||
| return | ||
| default: | ||
| n, err := src.Read(buf) | ||
| if err != nil { | ||
| errChan <- err | ||
| return | ||
| } | ||
|
|
||
| if n > 0 { | ||
| server.logPayload(direction, buf[:n], logger) | ||
| server.recordEvent(direction, buf[:n], capture) | ||
|
|
||
| if _, err := dst.Write(buf[:n]); err != nil { | ||
| errChan <- err | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // getDirection returns the direction as a string | ||
| func getDirection(src, dst net.Conn) string { | ||
| srcAddr := src.RemoteAddr().String() | ||
| dstAddr := dst.RemoteAddr().String() | ||
| return fmt.Sprintf("%s -> %s", srcAddr, dstAddr) | ||
| } | ||
|
|
||
|
glaslos marked this conversation as resolved.
|
||
| // Dial to the source ip, acting as a proxy between the client and real source by piping the data back and forth w/o interfering w it. | ||
| func HandlePassThrough(ctx context.Context, conn net.Conn, md connection.Metadata, logger interfaces.Logger, h interfaces.Honeypot) error { | ||
| var err error | ||
|
|
||
| srcAddr := conn.RemoteAddr().String() | ||
| destAddr := md.Rule.Target | ||
|
namay26 marked this conversation as resolved.
|
||
|
|
||
| server := &passThroughServer{ | ||
| events: []parsedPassThrough{}, | ||
| conn: conn, | ||
| target: destAddr, | ||
| source: srcAddr, | ||
| } | ||
|
|
||
| var capture bool | ||
| if viper.GetBool("capture_traffic.enabled") { | ||
| capture = true | ||
| } | ||
|
|
||
| defer func() { | ||
| var events []parsedPassThrough | ||
| if capture { | ||
| events = server.events | ||
| } | ||
| if err := h.ProduceTCP("passthrough", conn, md, nil, events); err != nil { | ||
| logger.Error("failed to produce passthrough message", producer.ErrAttr(err)) | ||
| } | ||
| if err := conn.Close(); err != nil { | ||
| logger.Error("failed to close incoming connection", slog.String("handler", "passthrough"), producer.ErrAttr(err)) | ||
| } | ||
| }() | ||
|
|
||
| if destAddr == "" { | ||
| logger.Error("no target defined", slog.String("handler", "passthrough")) | ||
| return nil | ||
| } | ||
|
|
||
| targetConn, err := net.Dial("tcp", destAddr) | ||
|
namay26 marked this conversation as resolved.
Outdated
|
||
| if err != nil { | ||
| logger.Error("failed to connect to the target", slog.String("handler", "passthrough"), slog.String("target", string(destAddr)), producer.ErrAttr(err)) | ||
|
namay26 marked this conversation as resolved.
Outdated
|
||
| return nil | ||
| } | ||
| defer targetConn.Close() | ||
|
|
||
| logger.Info("starting passthrough", slog.String("source", srcAddr), slog.String("target", string(destAddr)), slog.String("handler", "passthrough")) | ||
|
|
||
| errChan := make(chan error, 2) | ||
|
|
||
| go pipeBidirectional(ctx, conn, targetConn, server, logger, capture, errChan) // source to target | ||
|
namay26 marked this conversation as resolved.
Outdated
|
||
| go pipeBidirectional(ctx, targetConn, conn, server, logger, capture, errChan) // target to source | ||
|
|
||
| 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("Passthrough completed successfully") | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.