@@ -1147,47 +1147,43 @@ def format_row(procInfo):
11471147
11481148 def reg_sessions (self ):
11491149
1150- def output (sessions , usernames ):
1151- # Calculate max lengths for formatting
1152- maxSidLen = max (len (key ) + 1 for key in sessions )
1153- maxSidLen = max (maxSidLen , len ("SID" ) + 1 )
1154- maxUsernameLen = max (len (vals ["Username" ] + vals ["Domain" ]) + 1 for vals in sessions .values ()) + 1
1155- maxUsernameLen = max (maxUsernameLen , len ("USERNAME" ) + 1 )
1156-
1157- # Create the template for formatting
1158- template = (f"{{USERNAME: <{ maxUsernameLen } }} "
1159- f"{{SID: <{ maxSidLen } }}" )
1160-
1161- # Create headers
1162- header = template .format (
1163- USERNAME = "USERNAME" ,
1164- SID = "SID" ,
1165- )
1166-
1167- header2 = template .replace (" <" , "=<" ).format (
1168- USERNAME = "" ,
1169- SID = "" ,
1170- )
1171-
1172- # Store result
1173- result = [header , header2 ]
1174-
1175- for sid , vals in sessions .items ():
1176- username = vals ["Username" ]
1177- domain = vals ["Domain" ]
1178- user_full = f"{ domain } \\ { username } " if username else ""
1179-
1180- # If usernames are provided, filter them
1181- if usernames and username .lower () not in usernames :
1182- continue
1183-
1184- row = template .format (
1185- USERNAME = user_full ,
1186- SID = sid
1150+ def output (sessions ):
1151+ if sessions :
1152+ # Calculate max lengths for formatting
1153+ maxSidLen = max (len (key ) + 1 for key in sessions )
1154+ maxSidLen = max (maxSidLen , len ("SID" ) + 1 )
1155+ maxUsernameLen = max (len (vals ["Username" ] + vals ["Domain" ]) + 1 for vals in sessions .values ()) + 1
1156+ maxUsernameLen = max (maxUsernameLen , len ("USERNAME" ) + 1 )
1157+
1158+ # Create the template for formatting
1159+ template = (f"{{USERNAME: <{ maxUsernameLen } }} "
1160+ f"{{SID: <{ maxSidLen } }}" )
1161+
1162+ # Create headers
1163+ header = template .format (
1164+ USERNAME = "USERNAME" ,
1165+ SID = "SID" ,
1166+ )
1167+
1168+ header2 = template .replace (" <" , "=<" ).format (
1169+ USERNAME = "" ,
1170+ SID = "" ,
11871171 )
1188- result .append (row )
11891172
1190- if len (result ) > 2 :
1173+ # Store result
1174+ result = [header , header2 ]
1175+
1176+ for sid , vals in sessions .items ():
1177+ username = vals ["Username" ]
1178+ domain = vals ["Domain" ]
1179+ user_full = f"{ domain } \\ { username } " if username else ""
1180+
1181+ row = template .format (
1182+ USERNAME = user_full ,
1183+ SID = sid
1184+ )
1185+ result .append (row )
1186+
11911187 self .logger .success ("Remote Registry enumerated sessions" )
11921188 for row in result :
11931189 self .logger .highlight (row )
@@ -1260,7 +1256,7 @@ def output(sessions, usernames):
12601256 dce .bind (lsat .MSRPC_UUID_LSAT )
12611257 except Exception as e :
12621258 self .logger .debug (f"Failed to connect to LSARPC for SID resolution : { e } " )
1263- output (sessions , None )
1259+ output (sessions )
12641260 return
12651261
12661262 # Resolve SIDs with names
@@ -1281,8 +1277,7 @@ def output(sessions, usernames):
12811277 sessions [sid ]["Username" ] = item ["Name" ]
12821278 sessions [sid ]["Domain" ] = resp ["ReferencedDomains" ]["Domains" ][item ["DomainIndex" ]]["Name" ]
12831279
1284- # Check if we need to filter for usernames
1285- usernames = None
1280+ # Filter for usernames
12861281 if self .args .reg_sessions :
12871282 arg = self .args .reg_sessions
12881283 if os .path .isfile (arg ):
@@ -1291,8 +1286,15 @@ def output(sessions, usernames):
12911286 else :
12921287 usernames = [arg .lower ()]
12931288
1294- output (sessions , usernames )
1295- return
1289+ filtered_sessions = {}
1290+ for sid , info in sessions .items ():
1291+ if info ["Username" ].lower () not in usernames :
1292+ continue
1293+ else :
1294+ filtered_sessions [sid ] = info
1295+ output (filtered_sessions )
1296+ else :
1297+ output (sessions )
12961298
12971299 def shares (self ):
12981300 temp_dir = ntpath .normpath ("\\ " + gen_random_string ())
0 commit comments