def smbwalk(smb, share, regex, path='\\', tid=None, *args, **kwargs): max_size = kwargs.get("max_size") or 100*1024 ip = smb.getRemoteHost() try: if tid is None: tid = smb.connectTree(share) except Exception as e: if verbose: logger.warn("Failed to connect to tree '{}': {}".format(share, e)) return path = ntpath.normpath(path) try: gen = smb.listPath(share, ntpath.join(path, '*')) except Exception as e: if verbose: logger.warn("Failed to list share '{}'".format(share, e)) return for f in gen: cur_path = ntpath.join(path, f.get_longname()) if f.is_directory() and f.get_longname() not in (".", ".."): try: smbwalk(smb, share, regex, cur_path + "\\", tid) except Exception as e: if verbose: logger.warn("Failed to list path '{}': {}".format(cur_path, e)) continue if f.get_longname() in (".", ".."): continue if regex is None or regex.search(cur_path): try: entry = [ip, share, cur_path,] fhandle = smb.openFile(tid, cur_path, desiredAccess=FILE_READ_DATA, shareMode=FILE_SHARE_READ) fdata = smb.readFile(tid, fhandle, offset=0, bytesToRead=max_size) fmagic = magic.from_buffer(fdata) smb.closeFile(tid, fhandle) entry.append(fmagic) except Exception as e: if verbose: logger.warn("Failed to retrieve file: {}".format(e)) entry.append("") if verbose: logger.info( "\\\\" + " -> ".join(entry) ) q.append( entry ) return tid
def smbwalk(smb, share, regex, path="\\", tid=None, *args, **kwargs): max_size = kwargs.get("max_size") or 100 * 1024 ip = smb.getRemoteHost() try: if tid is None: tid = smb.connectTree(share) except Exception as e: if verbose: logger.warn("Failed to connect to tree '{}': {}".format(share, e)) return path = ntpath.normpath(path) try: gen = smb.listPath(share, ntpath.join(path, "*")) except Exception as e: if verbose: logger.warn("Failed to list share '{}'".format(share, e)) return for f in gen: cur_path = ntpath.join(path, f.get_longname()) if f.is_directory() and f.get_longname() not in (".", ".."): try: smbwalk(smb, share, regex, cur_path + "\\", tid) except Exception as e: if verbose: logger.warn("Failed to list path '{}': {}".format(cur_path, e)) continue if f.get_longname() in (".", ".."): continue if regex is None or regex.search(cur_path): try: entry = [ip, share, cur_path] fhandle = smb.openFile(tid, cur_path, desiredAccess=FILE_READ_DATA, shareMode=FILE_SHARE_READ) fdata = smb.readFile(tid, fhandle, offset=0, bytesToRead=max_size) fmagic = magic.from_buffer(fdata) smb.closeFile(tid, fhandle) entry.append(fmagic) except Exception as e: if verbose: logger.warn("Failed to retrieve file: {}".format(e)) entry.append("") if verbose: logger.info("\\\\" + " -> ".join(entry)) q.append(entry) return tid
def test_readwriteFile(self): smb = SMBConnection('*SMBSERVER', self.machine, preferredDialect = self.dialects) smb.login(self.username, self.password, self.domain) tid = smb.connectTree(self.share) fid = smb.createFile(tid, self.file) smb.writeFile(tid, fid, "A"*65535) data = smb.readFile(tid,fid, 0, 65535) self.assertTrue(len(data) == 65535) self.assertTrue(data == "A"*65535) smb.closeFile(tid,fid) fid = smb.openFile(tid, self.file) smb.closeFile(tid, fid) smb.deleteFile(self.share, self.file) smb.disconnectTree(tid) smb.logoff()