Skip to content

Commit 08231f2

Browse files
Add HadSessions to check if there have ever been sessions (#487)
--------- Co-authored-by: terrorbyte <terrorbyte@users.noreply.github.com>
1 parent e37e979 commit 08231f2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

c2/channel/channel.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ type Session struct {
3535
LastSeen time.Time
3636
}
3737

38+
// HadSessions checks if a channel has any tracked sessions. This can be used to lookup if a C2
39+
// successfully received callbacks ever, regardless of whether or not it is currently active.
40+
//
41+
// c, ok := c2.GetInstance(conf.C2Type)
42+
// c.Channel().HadSessions()
43+
func (c *Channel) HadSessions() bool {
44+
// Currently sessions are only added to the session structure and then their states are modified.
45+
// This will only work as long as the sessions are never actually removed from the map, which for
46+
// now isn't an issue but if we ever switch to a history vs active tracking systtem then this will
47+
// not be sufficient.
48+
return len(c.Sessions) > 0
49+
}
50+
3851
// HasSessions checks if a channel has any tracked sessions. This can be used to lookup if a C2
3952
// successfully received callbacks:
4053
//

c2/factory.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,35 @@ func CreateFlags(implementation Impl) {
152152
}
153153
}
154154

155+
// HadSessions returns if the underlying channel has any sessions, regardless of their Active value.
156+
func HadSessions(implementation Impl) bool {
157+
switch implementation.Category {
158+
case SimpleShellServerCategory:
159+
return simpleshell.GetServerInstance().Channel().HadSessions()
160+
case SimpleShellClientCategory:
161+
return simpleshell.GetClientInstance().Channel().HadSessions()
162+
case SSLShellServerCategory:
163+
return sslshell.GetInstance().Channel().HadSessions()
164+
case HTTPServeFileCategory:
165+
return httpservefile.GetInstance().Channel().HadSessions()
166+
case HTTPServeShellCategory:
167+
return httpserveshell.GetInstance().Channel().HadSessions()
168+
case ExternalCategory:
169+
if implementation.Name != "" {
170+
return external.GetInstance(implementation.Name).Channel().HadSessions()
171+
}
172+
case HTTPShellServerCategory:
173+
return httpshellserver.GetInstance().Channel().HadSessions()
174+
case ShellTunnelCategory:
175+
return shelltunnel.GetInstance().Channel().HadSessions()
176+
case InvalidCategory:
177+
default:
178+
}
179+
output.PrintFrameworkError("Invalid C2 Server")
180+
181+
return false
182+
}
183+
155184
// HasSessions returns if the underlying channel has active sessions. This is useful for code that
156185
// needs to validate if callbacks have occurred and is a helper wrapper around the channel package
157186
// function of the same name.

0 commit comments

Comments
 (0)