Skip to content

Commit

Permalink
Merge pull request #1890 from BishopFox/fix/v1.5.x/GHSA-fh4v-v779-4g2w
Browse files Browse the repository at this point in the history
Track reverse portfwd state
  • Loading branch information
rkervella authored Feb 19, 2025
2 parents ba05d0a + 0b84466 commit 10e2453
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions implant/sliver/sliver.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifdef __WIN32
#include <windows.h>

void StartW();

DWORD WINAPI Start()
{
StartW();
Expand Down
16 changes: 16 additions & 0 deletions server/core/rtunnels/rtunnels.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
var (
Rtunnels map[uint64]*RTunnel = make(map[uint64]*RTunnel)
mutex sync.RWMutex
pending sync.Map
)

// RTunnel - Duplex byte read/write
Expand Down Expand Up @@ -95,6 +96,21 @@ func RemoveRTunnel(ID uint64) {
delete(Rtunnels, ID)
}

func AddPending(sessionID string, connStr string) {
pending.Store(sessionID, connStr)
}

func DeletePending(sessionID string) {
pending.Delete(sessionID)
}

func Check(sessionID string, connStr string) bool {
if val, ok := pending.Load(sessionID); ok {
return val == connStr
}
return false
}

// func removeAndCloseAllRTunnels() {
// mutex.Lock()
// defer mutex.Unlock()
Expand Down
9 changes: 7 additions & 2 deletions server/handlers/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,14 @@ func createReverseTunnelHandler(implantConn *core.ImplantConnection, data []byte
req := &sliverpb.TunnelData{}
proto.Unmarshal(data, req)

var defaultDialer = new(net.Dialer)

remoteAddress := fmt.Sprintf("%s:%d", req.Rportfwd.Host, req.Rportfwd.Port)
if !rtunnels.Check(session.ID, remoteAddress) {
sessionHandlerLog.Errorf("Session %s attempted to create reverse tunnel to %s without being initiated by a client", session.ID, remoteAddress)
return nil
}
defer rtunnels.DeletePending(session.ID)

var defaultDialer = new(net.Dialer)

ctx, cancelContext := context.WithCancel(context.Background())

Expand Down
2 changes: 2 additions & 0 deletions server/rpc/rpc-rportfwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/protobuf/sliverpb"
"github.com/bishopfox/sliver/server/core/rtunnels"
)

// GetRportFwdListeners - Get a list of all reverse port forwards listeners from an implant
Expand All @@ -38,6 +39,7 @@ func (rpc *Server) GetRportFwdListeners(ctx context.Context, req *sliverpb.Rport
// StartRportfwdListener - Instruct the implant to start a reverse port forward
func (rpc *Server) StartRportFwdListener(ctx context.Context, req *sliverpb.RportFwdStartListenerReq) (*sliverpb.RportFwdListener, error) {
resp := &sliverpb.RportFwdListener{Response: &commonpb.Response{}}
rtunnels.AddPending(req.Request.SessionID, req.ForwardAddress)
err := rpc.GenericHandler(req, resp)
if err != nil {
return nil, err
Expand Down

0 comments on commit 10e2453

Please sign in to comment.