diff --git a/c2/channel/channel.go b/c2/channel/channel.go index a91d333..37b616c 100644 --- a/c2/channel/channel.go +++ b/c2/channel/channel.go @@ -35,6 +35,19 @@ type Session struct { LastSeen time.Time } +// HadSessions checks if a channel has any tracked sessions. This can be used to lookup if a C2 +// successfully received callbacks ever, regardless of whether or not it is currently active. +// +// c, ok := c2.GetInstance(conf.C2Type) +// c.Channel().HadSessions() +func (c *Channel) HadSessions() bool { + // Currently sessions are only added to the session structure and then their states are modified. + // This will only work as long as the sessions are never actually removed from the map, which for + // now isn't an issue but if we ever switch to a history vs active tracking systtem then this will + // not be sufficient. + return len(c.Sessions) > 0 +} + // HasSessions checks if a channel has any tracked sessions. This can be used to lookup if a C2 // successfully received callbacks: // diff --git a/c2/factory.go b/c2/factory.go index a89c0c6..51047bd 100644 --- a/c2/factory.go +++ b/c2/factory.go @@ -152,6 +152,35 @@ func CreateFlags(implementation Impl) { } } +// HadSessions returns if the underlying channel has any sessions, regardless of their Active value. +func HadSessions(implementation Impl) bool { + switch implementation.Category { + case SimpleShellServerCategory: + return simpleshell.GetServerInstance().Channel().HadSessions() + case SimpleShellClientCategory: + return simpleshell.GetClientInstance().Channel().HadSessions() + case SSLShellServerCategory: + return sslshell.GetInstance().Channel().HadSessions() + case HTTPServeFileCategory: + return httpservefile.GetInstance().Channel().HadSessions() + case HTTPServeShellCategory: + return httpserveshell.GetInstance().Channel().HadSessions() + case ExternalCategory: + if implementation.Name != "" { + return external.GetInstance(implementation.Name).Channel().HadSessions() + } + case HTTPShellServerCategory: + return httpshellserver.GetInstance().Channel().HadSessions() + case ShellTunnelCategory: + return shelltunnel.GetInstance().Channel().HadSessions() + case InvalidCategory: + default: + } + output.PrintFrameworkError("Invalid C2 Server") + + return false +} + // HasSessions returns if the underlying channel has active sessions. This is useful for code that // needs to validate if callbacks have occurred and is a helper wrapper around the channel package // function of the same name.