Skip to content

Commit e737dd6

Browse files
committed
Remove unused function, fix uid detection, add comments and simplify code
1 parent 5553477 commit e737dd6

1 file changed

Lines changed: 49 additions & 55 deletions

File tree

nxc/protocols/nfs.py

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def enum_shares(self):
218218
output_export = str(self.mount.export())
219219
reg = re.compile(r"ex_dir=b'([^']*)'")
220220
shares = list(reg.findall(output_export))
221-
networks = self.export_info(self.mount.export())
221+
networks = self.export_info(self.mount.export())
222222

223223
self.logger.display("Enumerating NFS Shares Directories")
224224
for share, network in zip(shares, networks):
@@ -247,114 +247,108 @@ def enum_shares(self):
247247
finally:
248248
self.nfs3.disconnect()
249249

250-
def list_exported_shares(self, max_uid, shares):
251-
self.logger.display(f"Enumerating NFS Shares up to UID {max_uid}")
252-
white_list = []
253-
for uid in range(max_uid + 1):
254-
self.auth["uid"] = uid
255-
for share in shares:
256-
try:
257-
if share in white_list:
258-
self.logger.debug(f"Skipping {share} as it is already listed.")
259-
continue
260-
else:
261-
mount_info = self.mount.mnt(share, self.auth)
262-
contents = self.list_dir(mount_info["mountinfo"]["fhandle"], share, 1) # Try to list the share with depth 1
263-
white_list.append(share)
264-
self.logger.success(share)
265-
for content in contents:
266-
self.logger.highlight(f"UID: {self.auth['uid']} {content['path']}")
267-
except Exception as e:
268-
if "Insufficient Permissions" in str(e):
269-
continue
270-
self.logger.exception(f"{share} - {e}")
250+
def get_file(self):
251+
"""Downloads a file from the NFS share"""
252+
remote_file_path = self.args.get_file[0]
253+
local_file_path = self.args.get_file[1]
254+
255+
# Do a bit of smart handling for the local file path
256+
if local_file_path.endswith("/"):
257+
local_file_path += remote_file_path.split("/")[-1]
271258

272-
def get_file_single(self, remote_file, local_file):
273-
local_file_path = local_file
274-
remote_file_path = remote_file
275259
self.logger.display(f"Downloading {remote_file_path} to {local_file_path}")
276260
try:
277261
# Connect to NFS
278262
nfs_port = self.portmap.getport(NFS_PROGRAM, NFS_V3)
279263
self.nfs3 = NFSv3(self.host, nfs_port, self.args.nfs_timeout, self.auth)
280264
self.nfs3.connect()
281-
265+
282266
# Mount the NFS share
283267
mnt_info = self.mount.mnt(remote_file_path, self.auth)
268+
# Update the UID for the file
269+
attrs = self.nfs3.getattr(mnt_info["mountinfo"]["fhandle"], auth=self.auth)
270+
self.auth["uid"] = attrs["attributes"]["uid"]
284271
file_handle = mnt_info["mountinfo"]["fhandle"]
272+
# Read the file data
285273
file_data = self.nfs3.read(file_handle, auth=self.auth)
286274

287275
if "resfail" in file_data:
288276
raise Exception("Insufficient Permissions")
289277
else:
290-
entries = file_data["resok"]["data"]
291-
278+
data = file_data["resok"]["data"]
279+
292280
# Write the data to the local file
293281
with open(local_file_path, "wb+") as local_file:
294-
local_file.write(entries)
282+
local_file.write(data)
295283

296284
self.logger.highlight(f"File successfully downloaded to {local_file_path} from {remote_file_path}")
297285

298286
# Unmount the share
299287
self.mount.umnt(self.auth)
300-
301288
except Exception as e:
302289
self.logger.fail(f'Error writing file "{remote_file_path}" from share "{local_file_path}": {e}')
303-
if os.path.getsize(local_file_path) == 0:
290+
if os.path.exists(local_file_path) and os.path.getsize(local_file_path) == 0:
304291
os.remove(local_file_path)
305-
306-
def get_file(self):
307-
self.get_file_single(self.args.get_file[0], self.args.get_file[1])
308-
309-
def put_file_single(self, local_file, remote_file):
310-
local_file_path = local_file
311-
remote_file_path = remote_file
292+
293+
def put_file(self):
294+
"""Uploads a file to the NFS share"""
295+
local_file_path = self.args.put_file[0]
296+
remote_file_path = self.args.put_file[1]
297+
file_name = ""
298+
299+
# Do a bit of smart handling for the file paths
300+
if "/" in local_file_path:
301+
file_name = local_file_path.split("/")[-1]
312302
if not remote_file_path.endswith("/"):
313303
remote_file_path += "/"
314-
self.logger.display(f"Uploading {local_file_path} to {remote_file_path}")
304+
305+
self.logger.display(f"Uploading from {local_file_path} to {remote_file_path}")
315306
try:
316307
# Connect to NFS
317308
nfs_port = self.portmap.getport(NFS_PROGRAM, NFS_V3)
318309
self.nfs3 = NFSv3(self.host, nfs_port, self.args.nfs_timeout, self.auth)
319310
self.nfs3.connect()
320-
311+
321312
try:
322-
# Mount the NFS share for create file
313+
# Mount the NFS share to create the file
323314
mnt_info = self.mount.mnt(remote_file_path, self.auth)
324315
dir_handle = mnt_info["mountinfo"]["fhandle"]
316+
# Update the UID from the directory
325317
attrs = self.nfs3.getattr(dir_handle, auth=self.auth)
326318
self.auth["uid"] = attrs["attributes"]["uid"]
327-
self.logger.display(f"Trying to create {remote_file_path}{local_file_path}")
328-
self.nfs3.create(dir_handle, local_file_path, 1, auth=self.auth)
329-
self.logger.success(f"{local_file_path} successfully created.")
319+
320+
# Create file
321+
self.logger.display(f"Trying to create {remote_file_path}{file_name}")
322+
self.nfs3.create(dir_handle, file_name, 1, auth=self.auth)
323+
self.logger.success(f"{file_name} successfully created.")
330324
except Exception as e:
331-
self.logger.fail(f"{local_file_path} was not created.")
325+
self.logger.fail(f"{file_name} was not created.")
332326
self.logger.debug(f"Error while creating remote file: {e}")
333-
327+
334328
try:
335-
# Mount the NFS share for mount created file
336-
mnt_info = self.mount.mnt(remote_file_path + local_file, self.auth)
329+
# Mount the NFS share to write the file
330+
mnt_info = self.mount.mnt(remote_file_path, self.auth)
337331
file_handle = mnt_info["mountinfo"]["fhandle"]
338332
attrs = self.nfs3.getattr(file_handle, auth=self.auth)
339333
self.auth["uid"] = attrs["attributes"]["uid"]
340334
with open(local_file_path, "rb") as file:
341335
file_data = file.read().decode()
342-
343-
self.logger.display(f"Trying to write data from {local_file_path}")
344-
self.nfs3.write(file_handle, 0, len(file_data), file_data, 1, auth=self.auth)
345336

346-
self.logger.highlight(f"File {local_file_path} successfully uploaded on {remote_file_path}")
337+
# Write the data to the remote file
338+
self.logger.display(f"Trying to write data from {local_file_path} to {remote_file_path}")
339+
self.nfs3.write(file_handle, 0, len(file_data), file_data, 1, auth=self.auth)
340+
self.logger.success(f"Data from {local_file_path} successfully written to {remote_file_path}")
347341
except Exception as e:
348342
self.logger.fail(f"{local_file_path} was not writed.")
349343
self.logger.debug(f"Error while creating remote file: {e}")
350-
344+
351345
# Unmount the share
352346
self.mount.umnt(self.auth)
353347
except Exception as e:
354348
self.logger.fail(f"Error writing file to share {remote_file_path}: {e}")
349+
else:
350+
self.logger.highlight(f"File {local_file_path} successfully uploaded to {remote_file_path}")
355351

356-
def put_file(self):
357-
self.put_file_single(self.args.put_file[0], self.args.put_file[1])
358352

359353
def convert_size(size_bytes):
360354
if size_bytes == 0:

0 commit comments

Comments
 (0)