Skip to content

Commit d33f164

Browse files
committed
[SMB] better control of smbv1
Signed-off-by: XiaoliChan <30458572+XiaoliChan@users.noreply.github.com>
1 parent 1b7dbe3 commit d33f164

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

nxc/protocols/smb.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ def create_smbv1_conn(self):
549549
preferredDialect=SMB_DIALECT,
550550
timeout=self.args.smb_timeout,
551551
)
552-
self.smbv1 = True
553552
except OSError as e:
554553
if "Connection reset by peer" in str(e):
555554
self.logger.info(f"SMBv1 might be disabled on {self.host}")
@@ -577,20 +576,20 @@ def create_smbv3_conn(self):
577576
self.port,
578577
timeout=self.args.smb_timeout,
579578
)
580-
self.smbv1 = False
581579
except (Exception, NetBIOSTimeout, OSError) as e:
582580
self.logger.info(f"Error creating SMBv3 connection to {self.host}: {e}")
583581
return False
584582
return True
585583

586-
def create_conn_obj(self):
584+
def create_conn_obj(self, no_smbv1=False):
587585
"""
588586
Tries to create a connection object to the target host.
589587
On first try, it will try to create a SMBv1 connection.
590588
On further tries, it will remember which SMB version is supported and create a connection object accordingly.
589+
590+
:param no_smbv1: If True, it will not try to create a SMBv1 connection
591591
"""
592-
if self.args.force_smbv2:
593-
return self.create_smbv3_conn()
592+
no_smbv1 = self.args.no_smbv1 if self.args.no_smbv1 else no_smbv1
594593

595594
# Initial negotiation
596595
if self.smbv1 is None:
@@ -599,7 +598,7 @@ def create_conn_obj(self):
599598
return True
600599
elif not self.is_timeouted:
601600
return self.create_smbv3_conn()
602-
elif self.smbv1:
601+
elif not no_smbv1 and self.smbv1:
603602
return self.create_smbv1_conn()
604603
else:
605604
return self.create_smbv3_conn()
@@ -841,6 +840,7 @@ def shares(self):
841840
temp_dir = ntpath.normpath("\\" + gen_random_string())
842841
temp_file = ntpath.normpath("\\" + gen_random_string() + ".txt")
843842
permissions = []
843+
write_check = True if not self.args.no_write_check else False
844844

845845
try:
846846
self.logger.debug(f"domain: {self.domain}")
@@ -880,17 +880,21 @@ def shares(self):
880880
write = False
881881
write_dir = False
882882
write_file = False
883-
pwd = ntpath.join("\\", "*")
884-
pwd = ntpath.normpath(pwd)
885883
try:
886-
self.conn.listPath(share_name, pwd)
884+
self.conn.listPath(share_name, "*")
887885
read = True
888886
share_info["access"].append("READ")
889887
except SessionError as e:
890888
error = get_error_string(e)
891889
self.logger.debug(f"Error checking READ access on share {share_name}: {error}")
890+
except (NetBIOSError, UnicodeEncodeError) as e:
891+
write_check = False
892+
share_info["access"].append("UNKNOWN (try '--no-smbv1')")
893+
error = get_error_string(e)
894+
self.logger.debug(f"Error checking READ access on share {share_name}: {error}. This exception always caused by special character in share name with SMBv1")
895+
self.logger.info(f"Skipping WRITE permission check on share {share_name}")
892896

893-
if not self.args.no_write_check:
897+
if write_check:
894898
try:
895899
self.conn.createDirectory(share_name, temp_dir)
896900
write_dir = True

nxc/protocols/smb/proto_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def proto_args(parser, parents):
1616
smb_parser.add_argument("--port", type=int, default=445, help="SMB port")
1717
smb_parser.add_argument("--share", metavar="SHARE", default="C$", help="specify a share")
1818
smb_parser.add_argument("--smb-server-port", default="445", help="specify a server port for SMB", type=int)
19-
smb_parser.add_argument("--force-smbv2", action="store_true", help="Force to use SMBv2 in connection")
19+
smb_parser.add_argument("--no-smbv1", action="store_true", help="Force to disable SMBv1 in connection")
2020
smb_parser.add_argument("--gen-relay-list", metavar="OUTPUT_FILE", help="outputs all hosts that don't require SMB signing to the specified file")
2121
smb_parser.add_argument("--smb-timeout", help="SMB connection timeout", type=int, default=2)
2222
smb_parser.add_argument("--laps", dest="laps", metavar="LAPS", type=str, help="LAPS authentification", nargs="?", const="administrator")

0 commit comments

Comments
 (0)