Skip to content

Commit 7dc66f6

Browse files
committed
Fix HTTPServeShell accidentally sharing channels & fix session timeout
with sessions open.
1 parent 4b6c61b commit 7dc66f6

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

c2/httpserveshell/httpserveshell.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,42 @@ func (serveShell *Server) CreateFlags() {
7777
}
7878

7979
// load the provided file into memory. Generate the random filename.
80-
func (serveShell *Server) Init(channel *channel.Channel) bool {
81-
if channel.Shutdown == nil {
80+
func (serveShell *Server) Init(ch *channel.Channel) bool {
81+
if ch.Shutdown == nil {
8282
// Initialize the shutdown atomic. This lets us not have to define it if the C2 is manually
8383
// configured.
8484
var shutdown atomic.Bool
8585
shutdown.Store(false)
86-
channel.Shutdown = &shutdown
86+
ch.Shutdown = &shutdown
8787
}
88-
serveShell.channel = channel
88+
serveShell.channel = ch
8989
if len(serveShell.HTTPAddr) == 0 {
9090
output.PrintFrameworkError("User must specify -httpServeFile.BindAddr")
9191

9292
return false
9393
}
94-
channel.HTTPAddr = serveShell.HTTPAddr
95-
channel.HTTPPort = serveShell.HTTPPort
94+
ch.HTTPAddr = serveShell.HTTPAddr
95+
ch.HTTPPort = serveShell.HTTPPort
9696

97-
if !httpservefile.GetInstance().Init(channel) {
97+
if !httpservefile.GetInstance().Init(ch) {
9898
return false
9999
}
100100

101+
// Initialize the shell server channels with variables from upstream
102+
var shutdown atomic.Bool
103+
shutdown.Store(false)
104+
shellChannel := &channel.Channel{
105+
IPAddr: ch.IPAddr,
106+
Port: ch.Port,
107+
IsClient: false,
108+
Shutdown: &shutdown,
109+
}
110+
shellChannel.Shutdown = &shutdown
101111
if serveShell.SSLShell {
102-
return sslshell.GetInstance().Init(channel)
112+
return sslshell.GetInstance().Init(shellChannel)
103113
}
104114

105-
return simpleshell.GetServerInstance().Init(channel)
115+
return simpleshell.GetServerInstance().Init(shellChannel)
106116
}
107117

108118
// Shutdown triggers the shutdown for all running C2s.
@@ -154,7 +164,6 @@ func (serveShell *Server) Run(timeout int) {
154164
go func() {
155165
for {
156166
if sslshell.GetInstance().Channel().Shutdown.Load() {
157-
sslshell.GetInstance().Shutdown()
158167
wg.Done()
159168

160169
break
@@ -167,7 +176,6 @@ func (serveShell *Server) Run(timeout int) {
167176
go func() {
168177
for {
169178
if simpleshell.GetServerInstance().Channel().Shutdown.Load() {
170-
simpleshell.GetServerInstance().Shutdown()
171179
wg.Done()
172180

173181
break

c2/simpleshell/simpleshellserver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (shellServer *Server) Shutdown() bool {
4848
output.PrintFrameworkStatus("C2 received shutdown, killing server and client sockets for shell server")
4949
if len(shellServer.Channel().Sessions) > 0 {
5050
for k, session := range shellServer.Channel().Sessions {
51-
output.PrintfFrameworkStatus("Connection closed: %s", session.RemoteAddr)
51+
output.PrintfFrameworkStatus("Connection closed for shell server: %s", session.RemoteAddr)
5252
shellServer.Channel().RemoveSession(k)
5353
}
5454
}
@@ -95,8 +95,10 @@ func (shellServer *Server) Run(timeout int) {
9595
go func() {
9696
time.Sleep(time.Duration(timeout) * time.Second)
9797
if !shellServer.Channel().HasSessions() {
98-
output.PrintFrameworkError("Timeout met. Shutting down shell listener.")
99-
shellServer.Channel().Shutdown.Store(true)
98+
if shellServer.Channel().Shutdown.Load() {
99+
output.PrintFrameworkError("Timeout met. Shutting down shell listener.")
100+
shellServer.Channel().Shutdown.Store(true)
101+
}
100102
}
101103
}()
102104
// Track if the shutdown is signaled for any reason.

0 commit comments

Comments
 (0)