Skip to content

Commit

Permalink
Improve closed connection handling
Browse files Browse the repository at this point in the history
It's okay not to close channels if it is used for writing. Close tunnels
instead.
  • Loading branch information
mrThe committed Jun 4, 2022
1 parent 136b506 commit 838db70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 6 additions & 6 deletions implant/sliver/sliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,19 +519,19 @@ func openSessionHandler(data []byte) {
// {{end}} -IsBeacon

func sessionMainLoop(connection *transports.Connection) error {
if connection == nil {
// {{if .Config.Debug}}
log.Printf("[session] nil connection!")
// {{end}}
return nil
}
err := connection.Start()
if err != nil {
// {{if .Config.Debug}}
log.Printf("[session] failed to establish connection: %s", err)
// {{end}}
return err
}
if connection == nil {
// {{if .Config.Debug}}
log.Printf("[session] nil connection!")
// {{end}}
return nil
}
pivots.RestartAllListeners(connection.Send)
defer pivots.StopAllListeners()
defer connection.Stop()
Expand Down
12 changes: 12 additions & 0 deletions implant/sliver/transports/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c *Connection) Cleanup() {
c.once.Do(func() {
c.cleanup()
c.IsOpen = false
c.removeAndCloseAllTunnels()
})
}

Expand All @@ -71,6 +72,17 @@ func (c *Connection) RemoveTunnel(ID uint64) {
delete(*c.tunnels, ID)
}

func (c *Connection) removeAndCloseAllTunnels() {
c.mutex.Lock()
defer c.mutex.Unlock()

for id, tunnel := range *c.tunnels {
tunnel.Close()

delete(*c.tunnels, id)
}
}

func (c *Connection) RequestResend(data []byte) {
c.Send <- &pb.Envelope{
Type: pb.MsgTunnelData,
Expand Down
5 changes: 0 additions & 5 deletions implant/sliver/transports/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func mtlsConnect(uri *url.URL) (*Connection, error) {
// {{if .Config.Debug}}
log.Printf("[mtls] lost connection, cleanup...")
// {{end}}
close(send)
conn.Close()
close(recv)
},
Expand Down Expand Up @@ -313,7 +312,6 @@ func wgConnect(uri *url.URL) (*Connection, error) {
// {{if .Config.Debug}}
log.Printf("[wg] lost connection, cleanup...")
// {{end}}
close(send)
conn.Close()
dev.Down()
close(recv)
Expand Down Expand Up @@ -419,7 +417,6 @@ func httpConnect(uri *url.URL) (*Connection, error) {
// {{if .Config.Debug}}
log.Printf("[http] lost connection, cleanup...")
// {{end}}
close(send)
ctrl <- struct{}{}
close(recv)
},
Expand Down Expand Up @@ -534,7 +531,6 @@ func dnsConnect(uri *url.URL) (*Connection, error) {
// {{if .Config.Debug}}
log.Printf("[dns] lost connection, cleanup...")
// {{end}}
close(send)
ctrl <- struct{}{} // Stop polling
close(recv)
},
Expand Down Expand Up @@ -640,7 +636,6 @@ func tcpPivotConnect(uri *url.URL) (*Connection, error) {
log.Printf("[tcp pivot] lost connection, cleanup...")
// {{end}}
pingCtrl <- struct{}{}
close(send)
ctrl <- struct{}{}
close(recv)
},
Expand Down
6 changes: 6 additions & 0 deletions implant/sliver/transports/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ func (c *Tunnel) IncWriteSequence() {

c.writeSequence += 1
}

// Close - close tunnel reader and writer
func (c *Tunnel) Close() {
c.Reader.Close()
c.Writer.Close()
}

0 comments on commit 838db70

Please sign in to comment.