-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Open
namay26
wants to merge
5
commits into
mushorg:gsoc2025/issue-161
Choose a base branch
from
namay26:Passthrough_implementation
base: gsoc2025/issue-161
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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,200 @@ | ||
| package tcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "log/slog" | ||
| "net" | ||
| "time" | ||
|
|
||
| "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 | ||
| } | ||
|
|
||
| type loggingWriter struct { | ||
| dst net.Conn | ||
| server *passThroughServer | ||
| logger interfaces.Logger | ||
| capture bool | ||
| dir string | ||
| } | ||
|
|
||
| func (lw *loggingWriter) Write(p []byte) (int, error) { | ||
| lw.server.logPayload(lw.dir, p, lw.logger) | ||
| lw.server.recordEvent(lw.dir, p, lw.capture) | ||
| return lw.dst.Write(p) | ||
| } | ||
|
|
||
| // 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(src, dst net.Conn, server *passThroughServer, logger interfaces.Logger, capture bool, errChan chan error) { | ||
| direction := getDirection(src, dst) | ||
| writer := &loggingWriter{dst: dst, server: server, logger: logger, capture: capture, dir: direction} | ||
|
|
||
| // source to target | ||
| go func() { | ||
| _, err := io.Copy(writer, src) | ||
| errChan <- err | ||
| }() | ||
|
|
||
| revDirection := getDirection(dst, src) | ||
| revWriter := &loggingWriter{dst: src, server: server, logger: logger, capture: capture, dir: revDirection} | ||
|
|
||
| // target to source | ||
| go func() { | ||
| _, err := io.Copy(revWriter, dst) | ||
| errChan <- err | ||
| }() | ||
| } | ||
|
|
||
| // 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) | ||
| } | ||
|
|
||
| // 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 | ||
| handler := "tcp_proxy" | ||
|
|
||
| srcAddr := conn.RemoteAddr().String() | ||
| destAddr := md.Rule.Target | ||
|
|
||
| host, _, err := net.SplitHostPort(destAddr) | ||
| if err != nil { | ||
| logger.Error("invalid address format", producer.ErrAttr(err)) | ||
| return nil | ||
| } | ||
|
|
||
| if ip := net.ParseIP(host); ip == nil { | ||
| if _, err := net.LookupHost(host); err != nil { | ||
| return fmt.Errorf("invalid host: %w", err) | ||
| } | ||
| } | ||
|
|
||
| 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", handler), producer.ErrAttr(err)) | ||
| } | ||
| }() | ||
|
|
||
| if destAddr == "" { | ||
| logger.Error("no target defined", slog.String("handler", handler)) | ||
| return nil | ||
| } | ||
|
|
||
| timeout := 5 * time.Second | ||
|
|
||
| targetConn, err := net.DialTimeout("tcp", destAddr, timeout) | ||
| if err != nil { | ||
| logger.Error("failed to connect to the target", slog.String("handler", handler), slog.String("target", string(destAddr)), producer.ErrAttr(err)) | ||
| return nil | ||
| } | ||
| defer targetConn.Close() | ||
|
|
||
| logger.Info("starting passthrough", slog.String("source", srcAddr), slog.String("target", string(destAddr)), slog.String("handler", handler)) | ||
|
|
||
| errChan := make(chan error, 2) | ||
|
|
||
| go pipeBidirectional(conn, targetConn, server, logger, capture, errChan) | ||
|
|
||
| // wait for either side to close | ||
| if err := <-errChan; err != nil { | ||
| log.Printf("connection closed: %v", 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.