Skip to content

Commit

Permalink
Improved output of SessionsManager commands
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jun 24, 2024
1 parent eefa2cc commit 95ae73c
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions smbclientng/core/SessionsManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
# Author : Podalirius (@podalirius_)
# Date created : 20 may 2024


import datetime
from smbclientng.core.Credentials import Credentials
from smbclientng.core.ModuleArgumentParser import ModuleArgumentParser
from smbclientng.core.SMBSession import SMBSession
import time


class SessionsManager(object):
"""
A class to manage SMB sessions.
This class is responsible for creating, managing, and switching between multiple SMB sessions. It allows for the creation of new sessions with specified credentials and hosts, and provides methods to switch between existing sessions. It also keeps track of the current session and its ID.
Attributes:
next_session_id (int): The next available session ID.
current_session (SMBSession): The currently active SMB session.
current_session_id (int): The ID of the currently active session.
sessions (dict): A dictionary of all active sessions, keyed by their session ID.
"""

next_session_id = 1
current_session = None
Expand Down Expand Up @@ -44,7 +56,11 @@ def create_new_session(self, credentials, host, port=445):
)
smbSession.init_smb_session()

self.sessions[self.next_session_id] = {"id":self.next_session_id, "smbSession":smbSession}
self.sessions[self.next_session_id] = {
"id": self.next_session_id,
"smbSession": smbSession,
"created_at": int(time.time()),
}
self.switch_session(self.next_session_id)
self.next_session_id += 1

Expand Down Expand Up @@ -179,35 +195,43 @@ def process_command_line(self, arguments):

#
elif options.action == "delete":
if options.session_id is not None:
if len(options.session_id) != 0:
for session_id in options.session_id:
if session_id in self.sessions.keys():
print("[+] Closing and deleting session #%d" % session_id)
self.delete_session(session_id=session_id)
else:
print("[!] No session with id #%d" % session_id)
elif options.all == True:
all_session_ids = list(self.sessions.keys())
for session_id in all_session_ids:
print("[+] Closing and deleting session #%d" % session_id)
self.delete_session(session_id=session_id)

#
elif options.action == "execute":
if options.command is not None:
if options.session_id is not None:
if options.session_id in self.sessions.keys():
print("[+] Executing '%s to session #%d" % (options.command, options.session_id))
else:
print("[!] No session with id #%d" % options.session_id)
if len(options.session_id) != 0:
for session_id in session_id:
if session_id in self.sessions.keys():
print("[+] Executing '%s to session #%d" % (options.command, session_id))
else:
print("[!] No session with id #%d" % options.session_id)
elif options.all == True:
for session_id in self.sessions.keys():
all_session_ids = list(self.sessions.keys())
for session_id in all_session_ids:
pass

#
elif options.action == "list":
for sessionId in sorted(self.sessions.keys()):
session = self.sessions[sessionId]["smbSession"]
created_at_str = str(datetime.datetime.fromtimestamp(self.sessions[sessionId]["created_at"]))
if sessionId == self.current_session_id:
if self.config.no_colors:
print(f"=> [Session #{sessionId:<2} - '{session.credentials.domain}\\{session.credentials.username}' @ {session.host}:{session.port}] [current session]")
print(f"=> [#{sessionId:<2} - '{session.credentials.domain}\\{session.credentials.username}' @ {session.host}:{session.port}] created at [{created_at_str}] [current session]")
else:
print(f"\x1b[1m=> Session #{sessionId:<2} - '\x1b[1;96m{session.credentials.domain}\x1b[0m\\\x1b[1;96m{session.credentials.username}\x1b[0m\x1b[1m' @ {session.host}:{session.port}\x1b[0m [\x1b[93mcurrent session\x1b[0m]")
print(f"\x1b[48;2;50;50;50m=> #{sessionId:<2} - '\x1b[1;96m{session.credentials.domain}\x1b[0m\x1b[48;2;50;50;50m\\\x1b[1;96m{session.credentials.username}\x1b[0m\x1b[48;2;50;50;50m\x1b[1m' @ {session.host}:{session.port} created at [{created_at_str}]\x1b[0m\x1b[48;2;50;50;50m [\x1b[93mcurrent session\x1b[0m\x1b[48;2;50;50;50m]\x1b[0m")
else:
print(f"── Session #{sessionId:<2} - '{session.credentials.domain}\\{session.credentials.username}' @ {session.host}:{session.port}")
print(f"── #{sessionId:<2} - '\x1b[1;96m{session.credentials.domain}\x1b[0m\\\x1b[1;96m{session.credentials.username}\x1b[0m\x1b[1m' @ {session.host}:{session.port} created at [{created_at_str}]\x1b[0m")

0 comments on commit 95ae73c

Please sign in to comment.