@@ -31,6 +31,8 @@ type Session struct {
31
31
RemoteAddr string
32
32
ConnectionTime time.Time
33
33
conn * net.Conn
34
+ Active bool
35
+ LastSeen time.Time
34
36
}
35
37
36
38
// 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 {
39
41
// c, ok := c2.GetInstance(conf.C2Type)
40
42
// c.Channel().HasSessions()
41
43
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
43
51
}
44
52
45
53
// 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 {
65
73
ConnectionTime : time .Now (),
66
74
conn : conn ,
67
75
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
68
95
}
69
96
97
+ session .LastSeen = timeStamp
98
+ c .Sessions [id ] = session
99
+
70
100
return true
71
101
}
72
102
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
+
73
123
// RemoveSession removes a specific session ID and if a connection exists, closes it.
74
124
func (c * Channel ) RemoveSession (id string ) bool {
75
125
if len (c .Sessions ) == 0 {
76
126
output .PrintFrameworkDebug ("No sessions exist" )
77
127
78
128
return false
79
129
}
80
- _ , ok := c .Sessions [id ]
130
+ session , ok := c .Sessions [id ]
81
131
if ! ok {
82
132
output .PrintFrameworkError ("Session ID does not exist" )
83
133
@@ -86,7 +136,8 @@ func (c *Channel) RemoveSession(id string) bool {
86
136
if c .Sessions [id ].conn != nil {
87
137
(* c .Sessions [id ].conn ).Close ()
88
138
}
89
- delete (c .Sessions , id )
139
+ session .Active = false
140
+ c .Sessions [id ] = session
90
141
91
142
return true
92
143
}
@@ -98,11 +149,8 @@ func (c *Channel) RemoveSessions() bool {
98
149
99
150
return false
100
151
}
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 )
106
154
}
107
155
108
156
return true
0 commit comments