@@ -688,7 +688,7 @@ def is_host_dc(self):
688688 from impacket .dcerpc .v5 import nrpc , epm
689689
690690 self .logger .debug ("Performing authentication attempts..." )
691-
691+
692692 # First check if port 135 is open
693693 if self ._is_port_open (135 ):
694694 self .logger .debug ("Port 135 is open, attempting MSRPC connection..." )
@@ -964,7 +964,7 @@ def enumerate_sessions_info(self, sessions):
964964 sessions [SessionId ]["DisconnectTime" ] = sessdata ["LSMSessionInfoExPtr" ]["LSM_SessionInfo_Level1" ]["DisconnectTime" ]
965965 sessions [SessionId ]["LogonTime" ] = sessdata ["LSMSessionInfoExPtr" ]["LSM_SessionInfo_Level1" ]["LogonTime" ]
966966 sessions [SessionId ]["LastInputTime" ] = sessdata ["LSMSessionInfoExPtr" ]["LSM_SessionInfo_Level1" ]["LastInputTime" ]
967-
967+
968968 try :
969969 with TSTS .RCMPublic (self .conn , self .host , self .kerberos ) as rcm :
970970 for SessionId in sessions :
@@ -992,6 +992,7 @@ def qwinsta(self):
992992
993993 self .enumerate_sessions_info (sessions )
994994
995+ # Calculate max lengths for formatting
995996 maxSessionNameLen = max (len (sessions [i ]["SessionName" ]) + 1 for i in sessions )
996997 maxSessionNameLen = max (maxSessionNameLen , len ("SESSIONNAME" ) + 1 )
997998 maxUsernameLen = max (len (sessions [i ]["Username" ] + sessions [i ]["Domain" ]) + 1 for i in sessions ) + 1
@@ -1001,6 +1002,7 @@ def qwinsta(self):
10011002 maxStateLen = max (len (sessions [i ]["state" ]) + 1 for i in sessions )
10021003 maxStateLen = max (maxStateLen , len ("STATE" ) + 1 )
10031004
1005+ # Create the template for formatting
10041006 template = (f"{{SESSIONNAME: <{ maxSessionNameLen } }} "
10051007 f"{{USERNAME: <{ maxUsernameLen } }} "
10061008 f"{{ID: <{ maxIdLen } }} "
@@ -1009,7 +1011,6 @@ def qwinsta(self):
10091011 "{DSTATE: <9} "
10101012 "{CONNTIME: <20} "
10111013 "{DISCTIME: <20} " )
1012-
10131014 header = template .format (
10141015 SESSIONNAME = "SESSIONNAME" ,
10151016 USERNAME = "USERNAME" ,
@@ -1020,7 +1021,6 @@ def qwinsta(self):
10201021 CONNTIME = "ConnectTime" ,
10211022 DISCTIME = "DisconnectTime" ,
10221023 )
1023-
10241024 header2 = template .replace (" <" , "=<" ).format (
10251025 SESSIONNAME = "" ,
10261026 USERNAME = "" ,
@@ -1043,18 +1043,15 @@ def qwinsta(self):
10431043 else :
10441044 usernames = [arg .lower ()]
10451045
1046- found_user = False
1047-
10481046 for i in sessions :
10491047 username = sessions [i ]["Username" ]
10501048 domain = sessions [i ]["Domain" ]
10511049 user_full = f"{ domain } \\ { username } " if username else ""
10521050
1051+ # If usernames are provided, filter them
10531052 if usernames and username .lower () not in usernames :
10541053 continue
10551054
1056- found_user = True
1057-
10581055 connectTime = sessions [i ]["ConnectTime" ]
10591056 connectTime = connectTime .strftime (r"%Y/%m/%d %H:%M:%S" ) if connectTime .year > 1601 else "None"
10601057
@@ -1072,8 +1069,8 @@ def qwinsta(self):
10721069 DISCTIME = disconnectTime ,
10731070 )
10741071 result .append (row )
1075-
1076- if found_user :
1072+
1073+ if len ( result ) > 2 :
10771074 self .logger .success ("Enumerated qwinsta sessions" )
10781075 for row in result :
10791076 self .logger .highlight (row )
@@ -2076,4 +2073,4 @@ def add_ntds_hash(ntds_hash, host_id):
20762073 NTDS .finish ()
20772074
20782075 def mark_guest (self ):
2079- return highlight (f"{ highlight ('(Guest)' )} " if self .is_guest else "" )
2076+ return highlight (f"{ highlight ('(Guest)' )} " if self .is_guest else "" )
0 commit comments