Skip to content

Commit 19ed5d3

Browse files
committed
Integrate SMBv1 setup on first connection into create_conn_obj
1 parent 5fe634d commit 19ed5d3

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

nxc/protocols/smb.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __init__(self, args, db, host):
121121
self.output_filename = None
122122
self.smbv1 = None # Check if SMBv1 is supported
123123
self.smbv3 = None # Check if SMBv3 is supported
124-
self.is_timeouted = False
124+
self.is_timed_out = False
125125
self.signing = False
126126
self.smb_share_name = smb_share_name
127127
self.pvkbytes = None
@@ -171,10 +171,6 @@ def enum_host_info(self):
171171
self.local_ip = self.conn.getSMBServer().get_socket().getsockname()[0]
172172
self.is_host_dc()
173173

174-
# Create SMBv1 connection to get host info, connection will be reinitiated on login
175-
if not self.args.no_smbv1:
176-
self.smbv1 = self.create_smbv1_conn()
177-
178174
try:
179175
self.conn.login("", "")
180176
self.null_auth = True
@@ -562,7 +558,7 @@ def create_smbv1_conn(self, check=False):
562558
if "Connection reset by peer" in str(e):
563559
self.logger.info(f"SMBv1 might be disabled on {self.host}")
564560
elif "timed out" in str(e):
565-
self.is_timeouted = True
561+
self.is_timed_out = True
566562
self.logger.debug(f"Timeout creating SMBv1 connection to {self.host}")
567563
else:
568564
self.logger.info(f"Error creating SMBv1 connection to {self.host}: {e}")
@@ -588,28 +584,34 @@ def create_smbv3_conn(self):
588584
self.smbv3 = True
589585
except (Exception, NetBIOSTimeout, OSError) as e:
590586
if "timed out" in str(e):
591-
self.is_timeouted = True
587+
self.is_timed_out = True
592588
self.logger.debug(f"Timeout creating SMBv3 connection to {self.host}")
593589
else:
594590
self.logger.info(f"Error creating SMBv3 connection to {self.host}: {e}")
595591
return False
596592
return True
597593

598-
def create_conn_obj(self):
594+
def create_conn_obj(self, no_smbv1=False):
599595
"""
600596
Tries to create a connection object to the target host.
601-
On first try, it will try to create a SMBv3 connection.
602-
On further tries, it will remember which SMB version is supported and create a connection object accordingly.
597+
On first try, it will try to create a SMBv1 connection to be able to get the plaintext server OS version if available.
598+
On further tries, it will remember which SMB version is supported and create a connection object accordingly, preferably SMBv3.
599+
600+
:param no_smbv1: If True, it will not try to create a SMBv1 connection
603601
"""
604602
# Initial negotiation
605-
if self.smbv3 is None:
606-
self.smbv3 = self.create_smbv3_conn()
607-
if self.smbv3:
603+
if self.smbv1 is None and not no_smbv1 and not self.args.no_smbv1:
604+
if not self.create_smbv1_conn() and not self.is_timed_out:
605+
# Fallback if SMBv1 fails
606+
return self.create_smbv3_conn()
607+
else:
608608
return True
609-
elif not self.is_timeouted:
609+
elif self.smbv3 is not False:
610+
if not self.create_smbv3_conn():
611+
# Fallback if SMBv3 fails
610612
return self.create_smbv1_conn()
611-
elif self.smbv3:
612-
return self.create_smbv3_conn()
613+
else:
614+
return True
613615
else:
614616
return self.create_smbv1_conn()
615617

0 commit comments

Comments
 (0)