async def connect_share(self, share): """ Connect to the share and fills connection related info in the SMBShare object """ tree_entry = await self.connection.tree_connect(share.fullpath) share.tree_id = tree_entry.tree_id share.maximal_access = tree_entry.maximal_access init_dir = SMBDirectory() init_dir.parent_share = share init_dir.fullpath = '' share.subdirs[''] = init_dir return
async def connect(self, connection): """ Connect to the share and fills connection related info in the SMBShare object FULLPATH MUST BE SPECIFIED ALREADY FOR THIS OBJECT! """ tree_entry = await connection.tree_connect(self.fullpath) self.tree_id = tree_entry.tree_id self.maximal_access = tree_entry.maximal_access self.unc_path = '\\\\%s\\' % connection.target.get_hostname_or_ip() init_dir = SMBDirectory() init_dir.tree_id = self.tree_id init_dir.fullpath = '' init_dir.unc_path = self.unc_path #init_dir.name = '' self.subdirs[''] = init_dir
async def connect(self, connection): """ Connect to the share and fills connection related info in the SMBShare object FULLPATH MUST BE SPECIFIED ALREADY FOR THIS OBJECT! """ try: tree_entry, err = await connection.tree_connect(self.fullpath) if err is not None: raise err self.tree_id = tree_entry.tree_id self.maximal_access = tree_entry.maximal_access self.unc_path = self.fullpath init_dir = SMBDirectory() init_dir.tree_id = self.tree_id init_dir.fullpath = '' init_dir.unc_path = self.unc_path self.subdirs[''] = init_dir return True, None except Exception as e: return None, e
async def list_directory(self, directory): """ Lists all files and folders in the directory directory: SMBDirectory fills the SMBDirectory's data """ if not directory.parent_share.tree_id: await self.connect_share(directory.parent_share) desired_access = FileAccessMask.FILE_READ_DATA share_mode = ShareAccess.FILE_SHARE_READ create_options = CreateOptions.FILE_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT file_attrs = 0 create_disposition = CreateDisposition.FILE_OPEN try: file_id = await self.connection.create( directory.parent_share.tree_id, directory.fullpath, desired_access, share_mode, create_options, create_disposition, file_attrs) except Exception as e: print(e) return while True: fileinfos = await self.connection.query_directory( directory.parent_share.tree_id, file_id) if not fileinfos: break for info in fileinfos: if info.FileAttributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY: dirname = info.FileName if info.FileName in ['.', '..']: continue subdir = SMBDirectory() subdir.parent_share = directory.parent_share if directory.fullpath != '': subdir.fullpath = '%s\\%s' % (directory.fullpath, info.FileName) else: subdir.fullpath = info.FileName subdir.name = info.FileName subdir.creation_time = info.CreationTime subdir.last_access_time = info.LastAccessTime subdir.last_write_time = info.LastWriteTime subdir.change_time = info.ChangeTime subdir.allocation_size = info.AllocationSize subdir.attributes = info.FileAttributes directory.subdirs[subdir.name] = subdir else: file = SMBFile() file.parent_share = directory.parent_share file.parent_dir = None if directory.fullpath != '': file.fullpath = '%s\\%s' % (directory.fullpath, info.FileName) else: file.fullpath = info.FileName file.name = info.FileName file.size = info.EndOfFile file.creation_time = info.CreationTime file.last_access_time = info.LastAccessTime file.last_write_time = info.LastWriteTime file.change_time = info.ChangeTime file.allocation_size = info.AllocationSize file.attributes = info.FileAttributes directory.files[file.name] = file