예제 #1
0
    def testGrep(self):
        class MockCallFlow(object):
            def CallFlow(self, *args, **kwargs):
                self.args = args
                self.kwargs = kwargs

        mock_call_flow = MockCallFlow()
        with utils.Stubber(aff4_flows.ArtifactCollectorFlow, "CallFlow",
                           mock_call_flow.CallFlow):

            collect_flow = aff4_flows.ArtifactCollectorFlow(None,
                                                            token=self.token)
            collect_flow.args = mock.Mock()
            collect_flow.args.ignore_interpolation_errors = False
            kb = rdf_client.KnowledgeBase()
            kb.MergeOrAddUser(rdf_client.User(username="******"))
            kb.MergeOrAddUser(rdf_client.User(username="******"))
            collect_flow.state["knowledge_base"] = kb
            collect_flow.current_artifact_name = "blah"

            collector = rdf_artifacts.ArtifactSource(
                type=rdf_artifacts.ArtifactSource.SourceType.GREP,
                attributes={
                    "paths": ["/etc/passwd"],
                    "content_regex_list": [r"^a%%users.username%%b$"]
                })
            collect_flow.Grep(collector, rdf_paths.PathSpec.PathType.TSK)

        conditions = mock_call_flow.kwargs["conditions"]
        self.assertLen(conditions, 1)
        regexes = conditions[0].contents_regex_match.regex.SerializeToString()
        self.assertCountEqual(regexes.split("|"),
                              ["(^atest1b$)", "(^atest2b$)"])
        self.assertEqual(mock_call_flow.kwargs["paths"], ["/etc/passwd"])
예제 #2
0
 def testGrepRegexCombination(self):
     collect_flow = aff4_flows.ArtifactCollectorFlow(None, token=self.token)
     self.assertEqual(collect_flow._CombineRegex([r"simple"]), "simple")
     self.assertEqual(collect_flow._CombineRegex(["a", "b"]), "(a)|(b)")
     self.assertEqual(collect_flow._CombineRegex(["a", "b", "c"]),
                      "(a)|(b)|(c)")
     self.assertEqual(collect_flow._CombineRegex(["a|b", "[^_]b", "c|d"]),
                      "(a|b)|([^_]b)|(c|d)")
예제 #3
0
    def testInterpolateArgs(self):
        collect_flow = aff4_flows.ArtifactCollectorFlow(None, token=self.token)

        kb = rdf_client.KnowledgeBase()
        kb.MergeOrAddUser(rdf_client.User(username="******"))
        kb.MergeOrAddUser(rdf_client.User(username="******"))
        collect_flow.state["knowledge_base"] = kb

        collect_flow.current_artifact_name = "blah"
        collect_flow.args = rdf_artifacts.ArtifactCollectorFlowArgs()

        test_rdf = rdf_client.KnowledgeBase()
        action_args = {
            "usernames": ["%%users.username%%", "%%users.username%%"],
            "nointerp": "asdfsdf",
            "notastring": test_rdf
        }

        kwargs = collect_flow.InterpolateDict(action_args)
        self.assertCountEqual(kwargs["usernames"],
                              ["test1", "test2", "test1", "test2"])
        self.assertEqual(kwargs["nointerp"], "asdfsdf")
        self.assertEqual(kwargs["notastring"], test_rdf)

        # We should be using an array since users.username will expand to multiple
        # values.
        self.assertRaises(ValueError, collect_flow.InterpolateDict,
                          {"bad": "%%users.username%%"})

        list_args = collect_flow.InterpolateList(
            ["%%users.username%%", r"%%users.username%%\aa"])
        self.assertCountEqual(list_args,
                              ["test1", "test2", r"test1\aa", r"test2\aa"])

        list_args = collect_flow.InterpolateList(["one"])
        self.assertEqual(list_args, ["one"])

        # Ignore the failure in users.desktop, report the others.
        collect_flow.args.ignore_interpolation_errors = True
        list_args = collect_flow.InterpolateList(
            ["%%users.desktop%%", r"%%users.username%%\aa"])
        self.assertCountEqual(list_args, [r"test1\aa", r"test2\aa"])

        # Both fail.
        list_args = collect_flow.InterpolateList(
            [r"%%users.desktop%%\aa", r"%%users.sid%%\aa"])
        self.assertCountEqual(list_args, [])