def testFollowLinks(self): self.Touch("foo", "0") self.Touch("foo", "bar", "0") self.Touch("foo", "baz", "0") self.Touch("foo", "baz", "1") self.Touch("quux", "0") self.Touch("norf", "0") os.symlink(self.Path("foo", "bar"), self.Path("quux", "bar")) os.symlink(self.Path("foo", "baz"), self.Path("quux", "baz")) os.symlink(self.Path("quux"), self.Path("norf", "quux")) opts = globbing.PathOpts(follow_links=True) component = globbing.RecursiveComponent(opts=opts) # It should resolve two links and recur to linked directories. results = list(component.Generate(self.Path("quux"))) self.assertCountEqual(results, [ self.Path("quux", "0"), self.Path("quux", "bar"), self.Path("quux", "bar", "0"), self.Path("quux", "baz"), self.Path("quux", "baz", "0"), self.Path("quux", "baz", "1"), ]) # It should resolve symlinks recursively. results = list(component.Generate(self.Path("norf"))) self.assertCountEqual(results, [ self.Path("norf", "0"), self.Path("norf", "quux"), self.Path("norf", "quux", "0"), self.Path("norf", "quux", "bar"), self.Path("norf", "quux", "bar", "0"), self.Path("norf", "quux", "baz"), self.Path("norf", "quux", "baz", "0"), self.Path("norf", "quux", "baz", "1"), ]) opts = globbing.PathOpts(follow_links=False) component = globbing.RecursiveComponent(opts=opts) # It should list symlinks but should not recur to linked directories. results = list(component.Generate(self.Path())) self.assertCountEqual(results, [ self.Path("foo"), self.Path("foo", "0"), self.Path("foo", "bar"), self.Path("foo", "bar", "0"), self.Path("foo", "baz"), self.Path("foo", "baz", "0"), self.Path("foo", "baz", "1"), self.Path("quux"), self.Path("quux", "0"), self.Path("quux", "bar"), self.Path("quux", "baz"), self.Path("norf"), self.Path("norf", "0"), self.Path("norf", "quux"), ])
def GetExpandedPaths( args: rdf_file_finder.FileFinderArgs, heartbeat_cb: Callable[[], None] = _NoOp) -> Iterator[Text]: """Expands given path patterns. Args: args: A `FileFinderArgs` instance that dictates the behaviour of the path expansion. heartbeat_cb: A function to be called regularly to send heartbeats. Yields: Absolute paths (as string objects) derived from input patterns. Raises: ValueError: For unsupported path types. """ if args.pathtype == rdf_paths.PathSpec.PathType.OS: pathtype = rdf_paths.PathSpec.PathType.OS else: raise ValueError("Unsupported path type: ", args.pathtype) opts = globbing.PathOpts(follow_links=args.follow_links, xdev=args.xdev, pathtype=pathtype) for path in args.paths: for expanded_path in globbing.ExpandPath(str(path), opts, heartbeat_cb): yield expanded_path
def testGlobbingKeyDoesNotYieldDuplicates(self): opts = globbing.PathOpts(pathtype="REGISTRY") results = globbing.ExpandGlobs( r"HKEY_LOCAL_MACHINE\SOFTWARE\GRR_TEST\*\aaa", opts) self.assertCountEqual(results, [ r"HKEY_LOCAL_MACHINE\SOFTWARE\GRR_TEST\1\aaa", ])
def GetExpandedPaths(args): """Expands given path patterns. Args: args: A `FileFinderArgs` instance that dictates the behaviour of the path expansion. Yields: Absolute paths (as string objects) derived from input patterns. Raises: ValueError: For unsupported path types. """ if args.pathtype == rdf_paths.PathSpec.PathType.REGISTRY: pathtype = rdf_paths.PathSpec.PathType.REGISTRY elif args.pathtype == rdf_paths.PathSpec.PathType.OS: pathtype = rdf_paths.PathSpec.PathType.OS else: raise ValueError("Unsupported path type: ", args.pathtype) opts = globbing.PathOpts(follow_links=args.follow_links, recursion_blacklist=_GetMountpointBlacklist( args.xdev), pathtype=pathtype) for path in args.paths: for expanded_path in globbing.ExpandPath(unicode(path), opts): yield expanded_path
def testIgnore(self): self.Touch("foo", "0") self.Touch("foo", "1") self.Touch("foo", "bar", "0") self.Touch("bar", "0") self.Touch("bar", "quux", "0") self.Touch("bar", "quux", "1") self.Touch("baz", "0") self.Touch("baz", "1") self.Touch("baz", "quux", "0") opts = globbing.PathOpts(recursion_blacklist=[ self.Path("foo"), self.Path("bar", "quux"), ]) component = globbing.RecursiveComponent(opts=opts) results = list(component.Generate(self.Path())) # Recursion should not visit into the blacklisted folders. self.assertNotIn(self.Path("foo", "0"), results) self.assertNotIn(self.Path("foo", "1"), results) self.assertNotIn(self.Path("bar", "quux", "0"), results) self.assertNotIn(self.Path("bar", "quux", "1"), results) # Blacklisted folders themselves should appear in the results. self.assertIn(self.Path("foo"), results) self.assertIn(self.Path("bar", "quux"), results) # Recursion should visit not blacklisted folders. self.assertIn(self.Path("baz"), results) self.assertIn(self.Path("baz", "0"), results) self.assertIn(self.Path("baz", "1"), results) self.assertIn(self.Path("baz", "quux"), results) self.assertIn(self.Path("baz", "quux", "0"), results)
def _GetExpandedPaths(args): """Yields all possible expansions from given path patterns.""" opts = globbing.PathOpts(follow_links=args.follow_links, pathtype=args.pathtype) for path in args.paths: for expanded_path in globbing.ExpandPath(str(path), opts): yield expanded_path
def _GetExpandedPaths( args: rdf_file_finder.FileFinderArgs, heartbeat_cb: Callable[[], None] = _NoOp, ) -> Iterator[Text]: """Yields all possible expansions from given path patterns.""" opts = globbing.PathOpts( follow_links=args.follow_links, pathtype=args.pathtype) for path in args.paths: for expanded_path in globbing.ExpandPath(str(path), opts, heartbeat_cb): yield expanded_path
def _GetExpandedPaths(self, args): """Expands given path patterns. Args: args: A `FileFinderArgs` instance that dictates the behaviour of the path expansion. Yields: Absolute paths (as string objects) derived from input patterns. """ opts = globbing.PathOpts( follow_links=args.follow_links, recursion_blacklist=_GetMountpointBlacklist(args.xdev)) for path in args.paths: for expanded_path in globbing.ExpandPath(utils.SmartStr(path), opts): yield expanded_path
def testFileArtifactParser(self): """Test parsing a fake file artifact with a file parser.""" processor = config_file.CronAtAllowDenyParser() source = rdf_artifact.ArtifactSource( type=rdf_artifact.ArtifactSource.SourceType.FILE, attributes={ "paths": ["VFSFixture/etc/passwd", "numbers.txt"], }) paths = [] for path in source.attributes["paths"]: paths.append(os.path.join(self.base_path, path)) stat_cache = utils.StatCache() expanded_paths = [] opts = globbing.PathOpts(follow_links=True) for path in paths: for expanded_path in globbing.ExpandPath(path, opts): expanded_paths.append(expanded_path) path_type = rdf_paths.PathSpec.PathType.OS results = [] for path in expanded_paths: stat = stat_cache.Get(path, follow_symlink=True) pathspec = rdf_paths.PathSpec( pathtype=path_type, path=client_utils.LocalPathToCanonicalPath(stat.GetPath()), path_options=rdf_paths.PathSpec.Options.CASE_LITERAL) response = rdf_client_fs.FindSpec(pathspec=pathspec) for res in artifact_collector.ParseSingleResponse( processor, response, {}, path_type): results.append(res) self.assertEqual(len(results), 3) self.assertTrue( results[0]["filename"].endswith("test_data/VFSFixture/etc/passwd")) self.assertIsInstance(results[0], rdf_protodict.AttributedDict) self.assertEqual(len(results[0]["users"]), 3) self.assertIsInstance(results[1], rdf_anomaly.Anomaly) self.assertEqual(len(results[2]["users"]), 1000)
def testIgnore(self): filepaths = [ ("foo", "0"), ("foo", "1"), ("foo", "bar", "0"), ("bar", "0"), ("bar", "quux", "0"), ("bar", "quux", "1"), ("baz", "0"), ("baz", "1"), ("baz", "quux", "0"), ] with DirHierarchy(filepaths) as hierarchy: opts = globbing.PathOpts(recursion_blacklist=[ hierarchy(("foo", )), hierarchy(("bar", "quux")), ]) component = globbing.RecursiveComponent(opts=opts) results = list(component.Generate(hierarchy(()))) # Recursion should not visit into the blacklisted folders. self.assertNotIn(hierarchy(("foo", "0")), results) self.assertNotIn(hierarchy(("foo", "1")), results) self.assertNotIn(hierarchy(("bar", "quux", "0")), results) self.assertNotIn(hierarchy(("bar", "quux", "1")), results) # Blacklisted folders themselves should appear in the results. self.assertIn(hierarchy(("foo", )), results) self.assertIn(hierarchy(("bar", "quux")), results) # Recursion should visit not blacklisted folders. self.assertIn(hierarchy(("baz", )), results) self.assertIn(hierarchy(("baz", "0")), results) self.assertIn(hierarchy(("baz", "1")), results) self.assertIn(hierarchy(("baz", "quux")), results) self.assertIn(hierarchy(("baz", "quux", "0")), results)
def testFollowLinks(self): filepaths = [ ("foo", "0"), ("foo", "bar", "0"), ("foo", "baz", "0"), ("foo", "baz", "1"), ("quux", "0"), ("norf", "0"), ] with DirHierarchy(filepaths) as hierarchy: os.symlink(hierarchy(("foo", "bar")), hierarchy(("quux", "bar"))) os.symlink(hierarchy(("foo", "baz")), hierarchy(("quux", "baz"))) os.symlink(hierarchy(("quux", )), hierarchy(("norf", "quux"))) opts = globbing.PathOpts(follow_links=True) component = globbing.RecursiveComponent(opts=opts) # It should resolve two links and recur to linked directories. results = list(component.Generate(hierarchy(("quux", )))) self.assertCountEqual(results, [ hierarchy(("quux", "0")), hierarchy(("quux", "bar")), hierarchy(("quux", "bar", "0")), hierarchy(("quux", "baz")), hierarchy(("quux", "baz", "0")), hierarchy(("quux", "baz", "1")), ]) # It should resolve symlinks recursively. results = list(component.Generate(hierarchy(("norf", )))) self.assertCountEqual(results, [ hierarchy(("norf", "0")), hierarchy(("norf", "quux")), hierarchy(("norf", "quux", "0")), hierarchy(("norf", "quux", "bar")), hierarchy(("norf", "quux", "bar", "0")), hierarchy(("norf", "quux", "baz")), hierarchy(("norf", "quux", "baz", "0")), hierarchy(("norf", "quux", "baz", "1")), ]) opts = globbing.PathOpts(follow_links=False) component = globbing.RecursiveComponent(opts=opts) # It should list symlinks but should not recur to linked directories. results = list(component.Generate(hierarchy(()))) self.assertCountEqual(results, [ hierarchy(("foo", )), hierarchy(("foo", "0")), hierarchy(("foo", "bar")), hierarchy(("foo", "bar", "0")), hierarchy(("foo", "baz")), hierarchy(("foo", "baz", "0")), hierarchy(("foo", "baz", "1")), hierarchy(("quux", )), hierarchy(("quux", "0")), hierarchy(("quux", "bar")), hierarchy(("quux", "baz")), hierarchy(("norf", )), hierarchy(("norf", "0")), hierarchy(("norf", "quux")), ])
def testGlobbingExpandPathWorksWithTSK(self): opts = globbing.PathOpts(pathtype=rdf_paths.PathSpec.PathType.TSK) paths = globbing.ExpandPath("C:/Windows/System32/notepad.exe", opts=opts) self.assertEqual(list(paths), [u"C:\\Windows\\System32\\notepad.exe"])
def testGlobComponentGenerateWorksWithTSK(self): opts = globbing.PathOpts(pathtype=rdf_paths.PathSpec.PathType.TSK) paths = globbing.GlobComponent(u"Windows", opts=opts).Generate("C:\\") self.assertEqual(list(paths), [u"C:\\Windows"])