Skip to content

Commit 29013be

Browse files
Added a LastSeen value to session and added automatic updating of it. Also added an Active param to the Session struct and changed the logic to RemoveSession(s) functions to set this value to false instead of deleting it (#374)
1 parent 9916595 commit 29013be

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

c2/channel/channel.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Session struct {
3131
RemoteAddr string
3232
ConnectionTime time.Time
3333
conn *net.Conn
34+
Active bool
35+
LastSeen time.Time
3436
}
3537

3638
// HasSessions checks if a channel has any tracked sessions. This can be used to lookup if a C2
@@ -39,7 +41,13 @@ type Session struct {
3941
// c, ok := c2.GetInstance(conf.C2Type)
4042
// c.Channel().HasSessions()
4143
func (c *Channel) HasSessions() bool {
42-
return len(c.Sessions) > 0
44+
for _, sess := range c.Sessions {
45+
if sess.Active {
46+
return true
47+
}
48+
}
49+
50+
return false
4351
}
4452

4553
// AddSession adds a remote connection for session tracking. If a network connection is being
@@ -65,19 +73,61 @@ func (c *Channel) AddSession(conn *net.Conn, addr string) bool {
6573
ConnectionTime: time.Now(),
6674
conn: conn,
6775
RemoteAddr: addr,
76+
LastSeen: time.Now(),
77+
Active: true,
78+
}
79+
80+
return true
81+
}
82+
83+
// Updates the LastSeen value for provided connection to the provided time
84+
func (c *Channel) UpdateLastSeenByConn(conn net.Conn, timeStamp time.Time) bool {
85+
id, ok := c.GetSessionIDByConn(conn)
86+
if !ok {
87+
return false
88+
}
89+
90+
session, ok := c.Sessions[id]
91+
if !ok {
92+
output.PrintFrameworkError("Session ID does not exist")
93+
94+
return false
6895
}
6996

97+
session.LastSeen = timeStamp
98+
c.Sessions[id] = session
99+
70100
return true
71101
}
72102

103+
// Returns the session ID that contains a given connection
104+
func (c *Channel) GetSessionIDByConn(conn net.Conn) (string, bool) {
105+
if len(c.Sessions) == 0 {
106+
output.PrintFrameworkDebug("No sessions exist")
107+
108+
return "", false
109+
}
110+
111+
for id, session := range c.Sessions {
112+
if *session.conn == conn {
113+
return id, true
114+
}
115+
}
116+
117+
output.PrintFrameworkError("Conn does not exist in sessions")
118+
119+
return "", false
120+
}
121+
122+
73123
// RemoveSession removes a specific session ID and if a connection exists, closes it.
74124
func (c *Channel) RemoveSession(id string) bool {
75125
if len(c.Sessions) == 0 {
76126
output.PrintFrameworkDebug("No sessions exist")
77127

78128
return false
79129
}
80-
_, ok := c.Sessions[id]
130+
session, ok := c.Sessions[id]
81131
if !ok {
82132
output.PrintFrameworkError("Session ID does not exist")
83133

@@ -86,7 +136,8 @@ func (c *Channel) RemoveSession(id string) bool {
86136
if c.Sessions[id].conn != nil {
87137
(*c.Sessions[id].conn).Close()
88138
}
89-
delete(c.Sessions, id)
139+
session.Active = false
140+
c.Sessions[id] = session
90141

91142
return true
92143
}
@@ -98,11 +149,8 @@ func (c *Channel) RemoveSessions() bool {
98149

99150
return false
100151
}
101-
for k := range c.Sessions {
102-
if c.Sessions[k].conn != nil {
103-
(*c.Sessions[k].conn).Close()
104-
}
105-
delete(c.Sessions, k)
152+
for id := range c.Sessions {
153+
c.RemoveSession(id)
106154
}
107155

108156
return true

c2/cli/basic.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func backgroundResponse(ch *channel.Channel, wg *sync.WaitGroup, conn net.Conn,
4848
// could have move data to write, but the user has already called exit
4949
// below. I that that's tolerable for now.
5050
responseCh <- string(responseBuffer[:bytesRead])
51+
// Update "Last Seen"
52+
ok := ch.UpdateLastSeenByConn(conn, time.Now())
53+
if !ok {
54+
output.PrintFrameworkError("Failed to update LastSeen value for connection")
55+
56+
return
57+
}
5158
}
5259
time.Sleep(10 * time.Millisecond)
5360
}

0 commit comments

Comments
 (0)