Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions nxc/nxcdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ def do_export(self, line):
return

shares = self.db.get_shares()
csv_header = ("id", "host", "userid", "name", "remark", "read", "write")
csv_header = ("id", "host", "userid", "name", "read", "write", "type", "remark")
Comment thread
azoxlpf marked this conversation as resolved.
Resolved
filename = line[2]

if line[1].lower() == "simple":
write_csv(filename, csv_header, shares)
write_csv(filename, csv_header, [(share.id, share.hostid, share.userid, share.name, share.read, share.write, share.type, share.remark) for share in shares])
print("[+] shares exported")
# Detailed view gets hostname, usernames, and true false statement
elif line[1].lower() == "detailed":
Expand All @@ -220,9 +220,10 @@ def do_export(self, line):
share_host, # hosts
f"{user[1]}\\{user[2]}", # userID
share[3], # name
share[4], # remark
bool(share[5]), # read
bool(share[6]), # write
share[7], # type
share[4], # remark
)
formatted_shares.append(entry)
write_csv(filename, csv_header, formatted_shares)
Expand Down
49 changes: 27 additions & 22 deletions nxc/protocols/smb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
)
from impacket.nmb import NetBIOSError, NetBIOSTimeout
from impacket.dcerpc.v5 import lsat, lsad, scmr, rrp, srvs, wkst
from impacket.dcerpc.v5.srvs import STYPE_DISKTREE, STYPE_MASK
from impacket.dcerpc.v5.srvs import (
STYPE_DISKTREE, STYPE_PRINTQ, STYPE_DEVICE, STYPE_IPC,
STYPE_CLUSTER_FS, STYPE_CLUSTER_SOFS, STYPE_CLUSTER_DFS,
STYPE_SPECIAL, STYPE_TEMPORARY, STYPE_MASK,
)
from impacket.dcerpc.v5.rpcrt import DCERPCException
from impacket.dcerpc.v5.transport import DCERPCTransportFactory
from impacket.dcerpc.v5.rpcrt import RPC_C_AUTHN_GSS_NEGOTIATE
Expand Down Expand Up @@ -1576,6 +1580,15 @@ def shares(self):
(WRITE_DAC, "WRITE (ACL)"),
(WRITE_OWNER, "WRITE (ACL)"),
]
SMB_SHARE_TYPES = {
STYPE_DISKTREE: "DISKTREE",
STYPE_PRINTQ: "PRINTQ",
STYPE_DEVICE: "DEVICE",
STYPE_IPC: "IPC",
STYPE_CLUSTER_FS: "CLUSTER_FS",
STYPE_CLUSTER_SOFS: "CLUSTER_SOFS",
STYPE_CLUSTER_DFS: "CLUSTER_DFS",
}

try:
self.logger.debug(f"domain: {self.domain}")
Expand Down Expand Up @@ -1616,20 +1629,19 @@ def shares(self):
self.logger.debug(f"Skipping excluded share: {share_name}")
continue

# Mask off STYPE_SPECIAL/STYPE_TEMPORARY flags; only the base type
# (DISKTREE/PRINTQ/DEVICE/IPC) determines whether write checks apply.
# Keep the base type for write checks and display cluster types and the temporary flag.
share_remark = share["shi1_remark"].rstrip("\x00")
share_info = {
"name": share_name,
"remark": share_remark,
"type": share["shi1_type"] & STYPE_MASK,
"type_name": SMB_SHARE_TYPES.get(share["shi1_type"] & ~(STYPE_SPECIAL | STYPE_TEMPORARY), "UNKNOWN"),
"access": [],
}
read = False
write = False
if share["shi1_type"] & STYPE_TEMPORARY:
share_info["type_name"] += ",TEMPORARY"
try:
self.conn.listPath(share_name, "*")
read = True
share_info["access"].append("READ")
except SessionError as e:
error = get_error_string(e)
Expand Down Expand Up @@ -1689,7 +1701,6 @@ def shares(self):

# If we either can create a file or a directory we add the write privs to the output. Agreed on in https://github.com/Pennyw0rth/NetExec/pull/404
if write_dir or write_file:
write = True
share_info["access"].append("WRITE")
else:
# ACL-based write check (default) — opens share root with FILE_OPEN +
Expand Down Expand Up @@ -1731,35 +1742,29 @@ def shares(self):
if "WRITE" in write_labels:
write_labels = ["WRITE"]

for lbl in write_labels:
share_info["access"].append(lbl)
if not write:
write = True
share_info["access"].extend(write_labels)

permissions.append(share_info)

if share_name != "IPC$":
try:
# TODO: check if this already exists in DB before adding
self.db.add_share(self.hostname, user_id, share_name, share_remark, read, write)
except Exception as e:
error = get_error_string(e)
self.logger.debug(f"Error adding share: {error}")

if self.args.filter_shares:
self.logger.display("[REMOVED] Use the --shares read,write options instead.")

self.logger.display("Enumerated shares")
self.logger.highlight(f"{'Share':<15} {'Permissions':<22} {'Remark'}")
self.logger.highlight(f"{'-----':<15} {'-----------':<22} {'------'}")
self.logger.highlight(f"{'Share':<15} {'Permissions':<15} {'Type':<10} {'Remark'}")
Comment thread
azoxlpf marked this conversation as resolved.
Resolved
self.logger.highlight(f"{'-----':<15} {'-----------':<15} {'----':<10} {'------'}")

for share in permissions:
if share["name"] != "IPC$":
try:
self.db.add_share(self.hostname, user_id, share["name"], share["remark"], "READ" in share["access"], any(access.startswith("WRITE") for access in share["access"]), share["type_name"])
except Exception as e:
self.logger.debug(f"Error adding share: {get_error_string(e)}")
name = share["name"]
remark = share["remark"]
perms = ",".join(share["access"])
if self.args.shares and self.args.shares.lower() not in perms.lower():
continue
self.logger.highlight(f"{name:<15} {perms:<22} {remark}")
self.logger.highlight(f"{name:<15} {perms:<15} {share['type_name']:<10} {remark}")
return permissions

def dir(self):
Expand Down
25 changes: 13 additions & 12 deletions nxc/protocols/smb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Share(BaseTable):
remark = Column(String)
read = Column(Boolean)
write = Column(Boolean)
type = Column(String)

__table_args__ = (
PrimaryKeyConstraint("id"),
Expand Down Expand Up @@ -655,19 +656,19 @@ def is_share_valid(self, share_id):
nxc_logger.debug(f"is_share_valid(shareID={share_id}) => {len(results) > 0}")
return len(results) > 0

def add_share(self, host_id, user_id, name, remark, read, write):
share_data = {
"hostid": host_id,
"userid": user_id,
"name": name,
"remark": remark,
"read": read,
"write": write,
}
def add_share(self, host_id, user_id, name, remark, read, write, share_type):
self.db_execute(
Insert(self.SharesTable).on_conflict_do_nothing(), # .returning(self.SharesTable.c.id),
share_data,
) # .scalar_one()
Insert(self.SharesTable).on_conflict_do_update(index_elements=["hostid", "userid", "name"], set_={"remark": remark, "read": read, "write": write, "type": share_type}),
{
"hostid": host_id,
"userid": user_id,
"name": name,
"remark": remark,
"read": read,
"write": write,
"type": share_type,
},
)

def get_shares(self, filter_term=None):
if self.is_share_valid(filter_term):
Expand Down
7 changes: 4 additions & 3 deletions nxc/protocols/smb/db_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def display_hosts(self, hosts):
print_table(data, title="Hosts")

def display_shares(self, shares):
data = [["ShareID", "host", "Name", "Remark", "Read Access", "Write Access"]]
data = [["ShareID", "host", "Name", "Read Access", "Write Access", "Type", "Remark"]]

for share in shares:
share_id = share[0]
Expand All @@ -132,9 +132,10 @@ def display_shares(self, shares):
share_id,
host_id,
name,
remark,
f"{len(users_r_access)} User(s)",
f"{len(users_w_access)} Users",
share.type,
remark,
]
)
print_table(data)
Expand Down Expand Up @@ -163,7 +164,7 @@ def do_shares(self, line):
users_r_access = self.db.get_users_with_share_access(host_id=host_id, share_name=name, permissions="r")
users_w_access = self.db.get_users_with_share_access(host_id=host_id, share_name=name, permissions="w")

data = [["ShareID", "Name", "Remark"], [share_id, name, remark]]
data = [["ShareID", "Name", "Type", "Remark"], [share_id, name, share.type, remark]]
print_table(data, title="Share")
host = self.db.get_hosts(filter_term=host_id)[0]
data = [
Expand Down
Loading