Skip to content

Commit 675480c

Browse files
author
wil
committed
Add file write check on smb
1 parent 83514bf commit 675480c

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

nxc/protocols/smb.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from impacket.dcerpc.v5.dtypes import NULL
2828
from impacket.dcerpc.v5.dcomrt import DCOMConnection
2929
from impacket.dcerpc.v5.dcom.wmi import CLSID_WbemLevel1Login, IID_IWbemLevel1Login, IWbemLevel1Login
30+
from impacket.smb3structs import FILE_SHARE_WRITE, FILE_SHARE_DELETE
3031

3132
from nxc.config import process_secret, host_info_colors
3233
from nxc.connection import connection, sem, requires_admin, dcom_FirewallChecker
@@ -774,6 +775,7 @@ def ps_execute(self, payload=None, get_output=False, methods=None, force_ps32=Fa
774775

775776
def shares(self):
776777
temp_dir = ntpath.normpath("\\" + gen_random_string())
778+
temp_file = ntpath.normpath("\\" + gen_random_string()+ ".txt")
777779
permissions = []
778780

779781
try:
@@ -825,16 +827,35 @@ def shares(self):
825827
self.conn.createDirectory(share_name, temp_dir)
826828
write = True
827829
share_info["access"].append("WRITE")
830+
try:
831+
self.conn.deleteDirectory(share_name, temp_dir)
832+
except SessionError as e:
833+
error = get_error_string(e)
834+
if error == 'STATUS_OBJECT_NAME_NOT_FOUND':
835+
pass
836+
else:
837+
self.logger.debug(f"Error DELETING created temp dir {temp_dir} on share {share_name}: {error}")
828838
except SessionError as e:
829839
error = get_error_string(e)
830840
self.logger.debug(f"Error checking WRITE access on share {share_name}: {error}")
831841

832-
if write:
842+
try:
843+
tid = self.conn.connectTree(share_name)
844+
fid = self.conn.createFile(tid, temp_file, desiredAccess=FILE_SHARE_WRITE, shareMode=FILE_SHARE_DELETE)
845+
self.conn.closeFile(tid, fid)
846+
write = True
847+
share_info["access"].append("WRITE")
833848
try:
834-
self.conn.deleteDirectory(share_name, temp_dir)
849+
self.conn.deleteFile(share_name, temp_file)
835850
except SessionError as e:
836851
error = get_error_string(e)
837-
self.logger.debug(f"Error DELETING created temp dir {temp_dir} on share {share_name}: {error}")
852+
if error == 'STATUS_OBJECT_NAME_NOT_FOUND':
853+
pass
854+
else:
855+
self.logger.debug(f"Error DELETING created temp file {temp_file} on share {share_name}")
856+
except SessionError as e:
857+
error = get_error_string(e)
858+
self.logger.debug(f"Error checking WRITE access with file on share {share_name}: {error}")
838859

839860
permissions.append(share_info)
840861

0 commit comments

Comments
 (0)