Skip to content

Commit c3f10ef

Browse files
author
y0no
committed
Add --enum-shares options to SMB protocol
1 parent 6d4fdfd commit c3f10ef

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

nxc/protocols/smb.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
from pywerview.cli.helpers import get_localdisks, get_netsession, get_netgroupmember, get_netgroup, get_netcomputer, get_netloggedon, get_netlocalgroup
6262

63-
from time import time
63+
from time import time, ctime
6464
from datetime import datetime
6565
from functools import wraps
6666
from traceback import format_exc
@@ -903,6 +903,65 @@ def shares(self):
903903
self.logger.highlight(f"{name:<15} {','.join(perms):<15} {remark}")
904904
return permissions
905905

906+
907+
def enum_shares(self):
908+
try:
909+
shares = self.conn.listShares()
910+
self.logger.info(f"Shares returned: {shares}")
911+
except SessionError as e:
912+
error = get_error_string(e)
913+
self.logger.fail(
914+
f"Error enumerating shares: {error}",
915+
color="magenta" if error in smb_error_status else "red",
916+
)
917+
return
918+
except Exception as e:
919+
error = get_error_string(e)
920+
self.logger.fail(
921+
f"Error enumerating shares: {error}",
922+
color="magenta" if error in smb_error_status else "red",
923+
)
924+
return
925+
926+
self.logger.display("Enumerating SMB Shares Directories")
927+
for share in shares:
928+
share_name = share["shi1_netname"][:-1]
929+
depth = 1
930+
contents = self.conn.listPath(share_name, "*")
931+
932+
self.logger.success(share_name)
933+
934+
if contents and depth == 1:
935+
self.logger.highlight(f"{'Perms':<9}{'File Size':<15}{'Date':<30}{'File Path':<45}")
936+
self.logger.highlight(f"{'-----':<9}{'---------':<15}{'----':<30}{'---------':<45}")
937+
self.list_share(share_name, "")
938+
939+
940+
def list_share(self, share_name, path_dir, depth=1):
941+
search_path = ntpath.join(path_dir, "*")
942+
943+
try:
944+
contents = self.conn.listPath(share_name, search_path)
945+
except SessionError as e:
946+
error = get_error_string(e)
947+
self.logger.fail(
948+
f"Error enumerating '{search_path}': {error}",
949+
color="magenta" if error in smb_error_status else "red",
950+
)
951+
return
952+
953+
for content in contents:
954+
path_name = content.get_longname()
955+
full_path = ntpath.join(path_dir, path_name)
956+
957+
if path_name in [".", ".."]:
958+
continue
959+
960+
if path_name != path_dir:
961+
self.logger.highlight(f"{'d' if content.is_directory() else 'f'}{'rw-' if content.is_readonly() > 0 else 'r--':<8}{content.get_filesize():<15}{ctime(float(content.get_mtime_epoch())):<30}{full_path:<45}")
962+
if content.is_directory() and depth < self.args.enum_shares and path_name not in [ ".", ".."]:
963+
self.list_share(share_name, full_path, depth+1)
964+
906965
@requires_admin
907966
def interfaces(self):
908967
"""

nxc/protocols/smb/proto_args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def proto_args(parser, parents):
3434

3535
mapping_enum_group = smb_parser.add_argument_group("Mapping/Enumeration", "Options for Mapping/Enumerating")
3636
mapping_enum_group.add_argument("--shares", action="store_true", help="enumerate shares and access")
37+
mapping_enum_group.add_argument("--enum-shares", nargs="?", type=int, const=3, help="Authenticate and enumerate exposed shares recursively (default depth: %(const)s)")
3738
mapping_enum_group.add_argument("--interfaces", action="store_true", help="enumerate network interfaces")
3839
mapping_enum_group.add_argument("--no-write-check", action="store_true", help="Skip write check on shares (avoid leaving traces when missing delete permissions)")
3940
mapping_enum_group.add_argument("--filter-shares", nargs="+", help="Filter share by access, option 'read' 'write' or 'read,write'")

0 commit comments

Comments
 (0)