@@ -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()
4143func (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.
74124func (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
0 commit comments