Skip to content

Commit

Permalink
Merge pull request #645 from BishopFox/session_clean_fix
Browse files Browse the repository at this point in the history
Make sure timeout gets set when cleaning up dead sessions.
  • Loading branch information
kmathisbf authored Mar 23, 2022
2 parents e42e6be + 1073ded commit 3cc9820
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions client/command/beacons/beacons.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func BeaconsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
con.PrintErrorf("%s\n", err)
return
}
err = kill.KillBeacon(beacon, false, con)
err = kill.KillBeacon(beacon, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
return
Expand All @@ -62,7 +62,7 @@ func BeaconsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
return
}
for _, beacon := range beacons.Beacons {
err = kill.KillBeacon(beacon, true, con)
err = kill.KillBeacon(beacon, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
return
Expand Down
4 changes: 3 additions & 1 deletion client/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ func BindCommands(con *console.SliverConsoleClient) {
f.String("k", "kill", "", "kill the designated session")
f.Bool("K", "kill-all", false, "kill all the sessions")
f.Bool("C", "clean", false, "clean out any sessions marked as [DEAD]")
f.Bool("f", "force", false, "force session action without waiting for results")

f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
},
Expand All @@ -574,7 +575,7 @@ func BindCommands(con *console.SliverConsoleClient) {
Help: "Kill all stale/dead sessions",
LongHelp: help.GetHelpFor([]string{consts.SessionsStr, consts.PruneStr}),
Flags: func(f *grumble.Flags) {

f.Bool("f", "force", false, "Force the killing of stale/dead sessions")
f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
},
Run: func(ctx *grumble.Context) error {
Expand Down Expand Up @@ -2163,6 +2164,7 @@ func BindCommands(con *console.SliverConsoleClient) {
f.String("k", "kill", "", "kill a beacon")
f.Bool("K", "kill-all", false, "kill all beacons")
f.Int("t", "timeout", defaultTimeout, "command timeout in seconds")
f.Bool("f", "force", false, "force killing of the beacon")
},
HelpGroup: consts.GenericHelpGroup,
Run: func(ctx *grumble.Context) error {
Expand Down
16 changes: 10 additions & 6 deletions client/command/kill/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/protobuf/sliverpb"
"github.com/bishopfox/sliver/server/core"
"github.com/desertbit/grumble"
)

Expand All @@ -41,7 +42,7 @@ func KillCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
if !confirm {
return
}
err := KillSession(session, ctx.Flags.Bool("force"), con)
err := KillSession(session, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
return
Expand All @@ -54,7 +55,7 @@ func KillCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
if !confirm {
return
}
err := KillBeacon(beacon, ctx.Flags.Bool("force"), con)
err := KillBeacon(beacon, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
return
Expand All @@ -67,28 +68,31 @@ func KillCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
return
}

func KillSession(session *clientpb.Session, force bool, con *console.SliverConsoleClient) error {
func KillSession(session *clientpb.Session, ctx *grumble.Context, con *console.SliverConsoleClient) error {
if session == nil {
return errors.New("session does not exist")
}
_, err := con.Rpc.Kill(context.Background(), &sliverpb.KillReq{
Request: &commonpb.Request{
SessionID: session.ID,
Timeout: int64(ctx.Flags.Int("timeout")),
},
Force: force,
Force: ctx.Flags.Bool("force"),
})
core.Sessions.Remove(session.ID)
return err
}

func KillBeacon(beacon *clientpb.Beacon, force bool, con *console.SliverConsoleClient) error {
func KillBeacon(beacon *clientpb.Beacon, ctx *grumble.Context, con *console.SliverConsoleClient) error {
if beacon == nil {
return errors.New("session does not exist")
}
_, err := con.Rpc.Kill(context.Background(), &sliverpb.KillReq{
Request: &commonpb.Request{
BeaconID: beacon.ID,
Timeout: int64(ctx.Flags.Int("timeout")),
},
Force: force,
Force: ctx.Flags.Bool("force"),
})
return err

Expand Down
2 changes: 1 addition & 1 deletion client/command/sessions/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func SessionsPruneCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
for _, session := range sessions.GetSessions() {
if session.IsDead {
con.Printf("Pruning session %s ... ", session.ID)
err = kill.KillSession(session, true, con)
err = kill.KillSession(session, ctx, con)
if err != nil {
con.Printf("failed!\n")
con.PrintErrorf("%s\n", err)
Expand Down
6 changes: 3 additions & 3 deletions client/command/sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func SessionsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
if killAll {
con.ActiveTarget.Background()
for _, session := range sessions.Sessions {
err := kill.KillSession(session, true, con)
err := kill.KillSession(session, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
}
Expand All @@ -65,7 +65,7 @@ func SessionsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
con.ActiveTarget.Background()
for _, session := range sessions.Sessions {
if session.IsDead {
err := kill.KillSession(session, true, con)
err := kill.KillSession(session, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
}
Expand All @@ -81,7 +81,7 @@ func SessionsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
if activeSession != nil && session.ID == activeSession.ID {
con.ActiveTarget.Background()
}
err := kill.KillSession(session, true, con)
err := kill.KillSession(session, ctx, con)
if err != nil {
con.PrintErrorf("%s\n", err)
}
Expand Down
9 changes: 8 additions & 1 deletion server/core/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,17 @@ func (s *Session) Request(msgType uint32, timeout time.Duration, data []byte) ([
// close(resp)
delete(s.Connection.Resp, reqID)
}()
s.Connection.Send <- &sliverpb.Envelope{
if timeout == 0 {
timeout = 60 * time.Second
}
select {
case s.Connection.Send <- &sliverpb.Envelope{
ID: reqID,
Type: msgType,
Data: data,
}:
case <-time.After(timeout):
return nil, ErrImplantTimeout
}

var respEnvelope *sliverpb.Envelope
Expand Down

0 comments on commit 3cc9820

Please sign in to comment.