def testRegexMatchConditionWithDifferentActions(self): expected_files = ["auth.log"] non_expected_files = ["dpkg.log", "dpkg_false.log"] regex_condition = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=( rdf_file_finder.FileFinderContentsRegexMatchCondition( mode="ALL_HITS", bytes_before=10, bytes_after=10, regex="session opened for user .*?john"))) for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults(action=action, conditions=[regex_condition], expected_files=expected_files, non_expected_files=non_expected_files) fd = flow.GRRFlow.ResultCollectionForFID(self.last_session_id) self.assertEqual(len(fd), 1) self.assertEqual(len(fd[0].matches), 1) self.assertEqual(fd[0].matches[0].offset, 350) self.assertEqual( fd[0].matches[0].data, "session): session opened for user dearjohn by (uid=0")
def Grep(self, source, pathtype): """Grep files in paths for any matches to content_regex_list. Args: source: artifact source pathtype: pathspec path type When multiple regexes are supplied, combine them into a single regex as an OR match so that we check all regexes at once. """ path_list = self.InterpolateList(source.attributes.get("paths", [])) content_regex_list = self.InterpolateList( source.attributes.get("content_regex_list", [])) regex_condition = rdf_file_finder.FileFinderContentsRegexMatchCondition( regex=self._CombineRegex(content_regex_list), bytes_before=0, bytes_after=0, mode="ALL_HITS") file_finder_condition = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=regex_condition) self.CallFlow(file_finder.FileFinder.__name__, paths=path_list, conditions=[file_finder_condition], action=rdf_file_finder.FileFinderAction(), pathtype=pathtype, request_data={ "artifact_name": self.current_artifact_name, "source": source.ToPrimitiveDict() }, next_state="ProcessCollected")
def testLiteralMatchConditionWithDifferentActions(self): expected_files = ["auth.log"] non_expected_files = ["dpkg.log", "dpkg_false.log"] match = rdf_file_finder.FileFinderContentsLiteralMatchCondition( mode=rdf_file_finder.FileFinderContentsLiteralMatchCondition.Mode. ALL_HITS, bytes_before=10, bytes_after=10, literal="session opened for user dearjohn") literal_condition = rdf_file_finder.FileFinderCondition( condition_type=rdf_file_finder.FileFinderCondition.Type. CONTENTS_LITERAL_MATCH, contents_literal_match=match) for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults(action=action, conditions=[literal_condition], expected_files=expected_files, non_expected_files=non_expected_files) # Check that the results' matches fields are correctly filled. fd = flow.GRRFlow.ResultCollectionForFID(self.last_session_id) self.assertEqual(len(fd), 1) self.assertEqual(len(fd[0].matches), 1) self.assertEqual(fd[0].matches[0].offset, 350) self.assertEqual( fd[0].matches[0].data, "session): session opened for user dearjohn by (uid=0")
def testLiteralMatchConditionWithHexEncodedValue(self): match = rdf_file_finder.FileFinderContentsLiteralMatchCondition( mode=rdf_file_finder.FileFinderContentsLiteralMatchCondition.Mode. FIRST_HIT, bytes_before=10, bytes_after=10, literal="\x4D\x5A\x90") literal_condition = rdf_file_finder.FileFinderCondition( condition_type=rdf_file_finder.FileFinderCondition.Type. CONTENTS_LITERAL_MATCH, contents_literal_match=match) paths = [os.path.join(os.path.dirname(self.fixture_path), "hello.exe")] for s in flow_test_lib.TestFlowHelper( file_finder.FileFinder.__name__, self.client_mock, client_id=self.client_id, paths=paths, pathtype=rdf_paths.PathSpec.PathType.OS, conditions=[literal_condition], token=self.token): session_id = s # Check that the results' matches fields are correctly filled. Expecting a # match from hello.exe fd = flow.GRRFlow.ResultCollectionForFID(session_id) self.assertEqual(len(fd[0].matches), 1) self.assertEqual(fd[0].matches[0].offset, 0) self.assertEqual(fd[0].matches[0].data, "MZ\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xff")
def testRegexMatchCondition(self): searching_path = os.path.join(self.base_path, "searching") paths = [searching_path + "/{dpkg.log,dpkg_false.log,auth.log}"] regex = r"pa[nm]_o?unix\(s{2}h" bytes_before = 10 bytes_after = 20 crmc = rdf_file_finder.FileFinderContentsRegexMatchCondition condition = rdf_file_finder.FileFinderCondition( condition_type="CONTENTS_REGEX_MATCH", contents_regex_match=crmc( regex=regex, bytes_before=bytes_before, bytes_after=bytes_after, )) raw_results = self._RunFileFinder(paths, self.stat_action, conditions=[condition]) relative_results = self._GetRelativeResults(raw_results, base_path=searching_path) self.assertEqual(len(relative_results), 1) self.assertIn("auth.log", relative_results) self.assertEqual(len(raw_results[0].matches), 1) buffer_ref = raw_results[0].matches[0] orig_data = open(os.path.join(searching_path, "auth.log")).read() self.assertEqual( orig_data[buffer_ref.offset:buffer_ref.offset + buffer_ref.length], buffer_ref.data)
def testRegexMatchConditionAllHits(self): searching_path = os.path.join(self.base_path, "searching") paths = [searching_path + "/{dpkg.log,dpkg_false.log,auth.log}"] bytes_before = 10 bytes_after = 20 crmc = rdf_file_finder.FileFinderContentsRegexMatchCondition regex = r"mydo....\.com" condition = rdf_file_finder.FileFinderCondition( condition_type="CONTENTS_REGEX_MATCH", contents_regex_match=crmc( regex=regex, mode="ALL_HITS", bytes_before=bytes_before, bytes_after=bytes_after, )) raw_results = self._RunFileFinder(paths, self.stat_action, conditions=[condition]) self.assertEqual(len(raw_results), 1) self.assertEqual(len(raw_results[0].matches), 6) for buffer_ref in raw_results[0].matches: needle = "mydomain.com" self.assertEqual( buffer_ref.data[bytes_before:bytes_before + len(needle)], needle)
def testLiteralMatchConditionAllHits(self): searching_path = os.path.join(self.base_path, "searching") paths = [searching_path + "/{dpkg.log,dpkg_false.log,auth.log}"] clmc = rdf_file_finder.FileFinderContentsLiteralMatchCondition bytes_before = 10 bytes_after = 20 literal = "mydomain.com" condition = rdf_file_finder.FileFinderCondition( condition_type="CONTENTS_LITERAL_MATCH", contents_literal_match=clmc(literal=literal, mode="ALL_HITS", bytes_before=bytes_before, bytes_after=bytes_after)) raw_results = self._RunFileFinder(paths, self.stat_action, conditions=[condition]) self.assertEqual(len(raw_results), 1) self.assertEqual(len(raw_results[0].matches), 6) for buffer_ref in raw_results[0].matches: self.assertEqual( buffer_ref.data[bytes_before:bytes_before + len(literal)], literal)
def testLiteralMatchConditionLargeFile(self): paths = [os.path.join(self.base_path, "new_places.sqlite")] literal = "RecentlyBookmarked" clmc = rdf_file_finder.FileFinderContentsLiteralMatchCondition bytes_before = 10 bytes_after = 20 condition = rdf_file_finder.FileFinderCondition( condition_type="CONTENTS_LITERAL_MATCH", contents_literal_match=clmc(literal=literal, mode="ALL_HITS", bytes_before=bytes_before, bytes_after=bytes_after)) raw_results = self._RunFileFinder(paths, self.stat_action, conditions=[condition]) self.assertEqual(len(raw_results), 1) self.assertEqual(len(raw_results[0].matches), 1) buffer_ref = raw_results[0].matches[0] with open(paths[0], "rb") as fd: fd.seek(buffer_ref.offset) self.assertEqual(buffer_ref.data, fd.read(buffer_ref.length)) self.assertEqual( buffer_ref.data[bytes_before:bytes_before + len(literal)], literal)
def testLiteralMatchCondition(self): searching_path = os.path.join(self.base_path, "searching") paths = [searching_path + "/{dpkg.log,dpkg_false.log,auth.log}"] literal = "pam_unix(ssh:session)" clmc = rdf_file_finder.FileFinderContentsLiteralMatchCondition bytes_before = 10 bytes_after = 20 condition = rdf_file_finder.FileFinderCondition( condition_type="CONTENTS_LITERAL_MATCH", contents_literal_match=clmc(literal=literal, bytes_before=bytes_before, bytes_after=bytes_after)) raw_results = self._RunFileFinder(paths, self.stat_action, conditions=[condition]) relative_results = self._GetRelativeResults(raw_results, base_path=searching_path) self.assertEqual(len(relative_results), 1) self.assertIn("auth.log", relative_results) self.assertEqual(len(raw_results[0].matches), 1) buffer_ref = raw_results[0].matches[0] orig_data = open(os.path.join(searching_path, "auth.log")).read() self.assertEqual(len(buffer_ref.data), bytes_before + len(literal) + bytes_after) self.assertEqual( orig_data[buffer_ref.offset:buffer_ref.offset + buffer_ref.length], buffer_ref.data)
def _CreateHuntFromFlow(self): self.client_id = self.SetupClient(0) flow_args = rdf_file_finder.FileFinderArgs( paths=["a/*", "b/*"], action=rdf_file_finder.FileFinderAction(action_type="STAT")) flow_runner_args = rdf_flows.FlowRunnerArgs( flow_name=file_finder.FileFinder.__name__) flow_urn = flow.GRRFlow.StartFlow(client_id=self.client_id, args=flow_args, runner_args=flow_runner_args, token=self.token) ref = rdf_hunts.FlowLikeObjectReference.FromFlowIdAndClientId( flow_urn.Basename(), self.client_id.Basename()) # Modify flow_args so that there are differences. flow_args.paths = ["b/*", "c/*"] flow_args.action.action_type = "DOWNLOAD" flow_args.conditions = [ rdf_file_finder.FileFinderCondition( condition_type="SIZE", size=rdf_file_finder.FileFinderSizeCondition(min_file_size=42)) ] return self.CreateHunt(flow_args=flow_args, flow_runner_args=flow_runner_args, original_object=ref), flow_urn
def testRegexMatchConditionWithDifferentActions(self): expected_files = ["auth.log"] non_expected_files = ["dpkg.log", "dpkg_false.log"] regex_condition = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=( rdf_file_finder.FileFinderContentsRegexMatchCondition( mode="ALL_HITS", bytes_before=10, bytes_after=10, regex="session opened for user .*?john"))) for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults(action=action, conditions=[regex_condition], expected_files=expected_files, non_expected_files=non_expected_files) fd = aff4.FACTORY.Open( self.last_session_id.Add(flow_runner.RESULTS_SUFFIX), aff4_type=sequential_collection.GeneralIndexedCollection, token=self.token) self.assertEqual(len(fd), 1) self.assertEqual(len(fd[0].matches), 1) self.assertEqual(fd[0].matches[0].offset, 350) self.assertEqual( fd[0].matches[0].data, "session): session opened for user dearjohn by (uid=0")
class TestFileFinderOSWindows(base.VFSPathContentIsPE): """Download a file with FileFinder. Exercise globbing, interpolation and filtering. """ platforms = ["Windows"] flow = "FileFinder" test_output_path = "/fs/os/C:/Windows/System32/notepad.exe" sizecondition = rdf_file_finder.FileFinderSizeCondition( max_file_size=1000000) filecondition = rdf_file_finder.FileFinderCondition( condition_type=rdf_file_finder.FileFinderCondition.Type.SIZE, size=sizecondition) download = rdf_file_finder.FileFinderDownloadActionOptions() action = rdf_file_finder.FileFinderAction( action_type=rdf_file_finder.FileFinderAction.Action.DOWNLOAD, download=download) args = { "paths": ["%%environ_systemroot%%\\System32\\notepad.*"], "conditions": filecondition, "action": action }
def StartRequests(self): """Generate and send the Find requests.""" client = aff4.FACTORY.Open(self.client_id, token=self.token) usernames = [ "%s\\%s" % (u.userdomain, u.username) for u in self.state.users ] usernames = [u.lstrip("\\") for u in usernames] # Strip \\ if no domain. condition = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=rdf_file_finder. FileFinderContentsRegexMatchCondition( regex=self.args.data_regex, mode=rdf_file_finder.FileFinderContentsRegexMatchCondition. Mode.FIRST_HIT)) for path in self.state.all_paths: full_paths = flow_utils.InterpolatePath(path, client, users=usernames) for full_path in full_paths: self.CallFlow( file_finder.FileFinder.__name__, paths=[os.path.join(full_path, "**5")], pathtype=self.args.pathtype, conditions=[condition], action=rdf_file_finder.FileFinderAction.Download(), next_state="HandleResults")
def testMatchOsxBitsUnset(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.osx_bits_unset = self.UF_NODUMP | self.UF_IMMUTABLE condition = client_file_finder.ExtFlagsCondition(params) self._Chflags(["hidden", "uappend"]) self.assertTrue(condition.Check(self.Stat()))
def testNoMatchLinuxBitsUnset(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.linux_bits_unset = self.FS_COMPR_FL condition = client_file_finder.ExtFlagsCondition(params) self._Chattr(["+c", "+d"]) self.assertFalse(condition.Check(self.Stat()))
def testMatchLinuxBitsUnset(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.linux_bits_unset = self.FS_IMMUTABLE_FL condition = conditions.ExtFlagsCondition(params) self._Chattr(["+c", "+d"]) self.assertTrue(condition.Check(self.Stat()))
def testNoMatchOsxBitsUnset(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.osx_bits_unset = self.UF_NODUMP | self.UF_HIDDEN condition = conditions.ExtFlagsCondition(params) self._Chflags(["hidden"]) self.assertFalse(condition.Check(self.Stat()))
def testNoMatchOsxBitsSet(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.osx_bits_set = self.UF_IMMUTABLE | self.UF_NODUMP condition = conditions.ExtFlagsCondition(params) self._Chflags(["nodump"]) self.assertFalse(condition.Check(self.Stat()))
def testDefault(self): params = rdf_file_finder.FileFinderCondition() condition = conditions.AccessTimeCondition(params) self.Touch("-a", "241007151200") # 2410-07-15 12:00 self.assertTrue(condition.Check(self.Stat())) self.Touch("-a", "201005160745") # 2010-05-16 7:45 self.assertTrue(condition.Check(self.Stat()))
def testDefault(self): params = rdf_file_finder.FileFinderCondition() condition = conditions.ModificationTimeCondition(params) self.Touch("-m", "198309121200") # 1983-09-12 12:00 self.assertTrue(condition.Check(self.Stat())) self.Touch("-m", "201710020815") # 2017-10-02 8:15 self.assertTrue(condition.Check(self.Stat()))
def testTwoRegexMatchConditionsWithDifferentActions1(self): expected_files = ["auth.log"] non_expected_files = ["dpkg.log", "dpkg_false.log"] regex_condition1 = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=( rdf_file_finder.FileFinderContentsRegexMatchCondition( mode="ALL_HITS", bytes_before=10, bytes_after=10, regex="session opened for user .*?john"))) regex_condition2 = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=( rdf_file_finder.FileFinderContentsRegexMatchCondition( mode="ALL_HITS", bytes_before=10, bytes_after=10, regex="format.*should"))) for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults( action=action, conditions=[regex_condition1, regex_condition2], expected_files=expected_files, non_expected_files=non_expected_files) # Check the output file is created fd = flow.GRRFlow.ResultCollectionForFID(self.last_session_id, token=self.token) self.assertEqual(len(fd), 1) self.assertEqual(len(fd[0].matches), 2) self.assertEqual(fd[0].matches[0].offset, 350) self.assertEqual( fd[0].matches[0].data, "session): session opened for user dearjohn by (uid=0") self.assertEqual(fd[0].matches[1].offset, 513) self.assertEqual(fd[0].matches[1].data, "rong line format.... should not be he")
def testMatchLinuxBitsMixed(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.linux_bits_set = self.FS_NODUMP_FL params.ext_flags.linux_bits_unset = self.FS_COMPR_FL params.ext_flags.osx_bits_unset = self.UF_IMMUTABLE condition = conditions.ExtFlagsCondition(params) self._Chattr(["+d"]) self.assertTrue(condition.Check(self.Stat()))
def testMatchOsxBitsMixed(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.osx_bits_set = self.UF_NODUMP params.ext_flags.osx_bits_unset = self.UF_HIDDEN params.ext_flags.linux_bits_unset = self.FS_NODUMP_FL condition = client_file_finder.ExtFlagsCondition(params) self._Chflags(["nodump", "uappend"]) self.assertTrue(condition.Check(self.Stat()))
def testNoHits(self): with open(self.temp_filepath, "wb") as fd: fd.write("foo bar quux") params = rdf_file_finder.FileFinderCondition() params.contents_regex_match.regex = "\\d+" params.contents_regex_match.mode = "FIRST_HIT" condition = conditions.RegexMatchCondition(params) results = list(condition.Search(self.temp_filepath)) self.assertFalse(results)
def testNoHits(self): with open(self.temp_filepath, "wb") as fd: fd.write("foo bar quux") params = rdf_file_finder.FileFinderCondition() params.contents_literal_match.literal = "baz" params.contents_literal_match.mode = "ALL_HITS" condition = conditions.LiteralMatchCondition(params) results = list(condition.Search(self.temp_filepath)) self.assertFalse(results)
def testDefault(self): params = rdf_file_finder.FileFinderCondition() condition = conditions.SizeCondition(params) with open(self.temp_filepath, "wb") as fd: fd.write("1234567") self.assertTrue(condition.Check(self.Stat())) with open(self.temp_filepath, "wb") as fd: fd.write("") self.assertTrue(condition.Check(self.Stat()))
def testSizeAndRegexConditionsWithDifferentActions(self): files_over_size_limit = ["auth.log"] filtered_files = ["dpkg.log", "dpkg_false.log"] expected_files = [] non_expected_files = files_over_size_limit + filtered_files sizes = [ os.stat(os.path.join(self.fixture_path, f)).st_size for f in files_over_size_limit ] size_condition = rdf_file_finder.FileFinderCondition( condition_type=rdf_file_finder.FileFinderCondition.Type.SIZE, size=rdf_file_finder.FileFinderSizeCondition( max_file_size=min(sizes) - 1)) regex_condition = rdf_file_finder.FileFinderCondition( condition_type=( rdf_file_finder.FileFinderCondition.Type.CONTENTS_REGEX_MATCH), contents_regex_match=rdf_file_finder. FileFinderContentsRegexMatchCondition( mode=(rdf_file_finder.FileFinderContentsRegexMatchCondition. Mode.ALL_HITS), bytes_before=10, bytes_after=10, regex="session opened for user .*?john")) for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults( action=action, conditions=[size_condition, regex_condition], expected_files=expected_files, non_expected_files=non_expected_files) # Check that order of conditions doesn't influence results for action in self.CONDITION_TESTS_ACTIONS: self.RunFlowAndCheckResults( action=action, conditions=[regex_condition, size_condition], expected_files=expected_files, non_expected_files=non_expected_files)
def testMatchOsxBitsUnset(self): params = rdf_file_finder.FileFinderCondition() params.ext_flags.osx_bits_unset = self.UF_NODUMP | self.UF_IMMUTABLE condition = conditions.ExtFlagsCondition(params) self._Chflags(["hidden", "uappend"]) try: self.assertTrue(condition.Check(self.Stat())) finally: # Make the test file deletable. self._Chflags(["nouappend"])
def testMaxTime(self): time = rdfvalue.RDFDatetime.FromHumanReadable("2125-12-28 18:45") params = rdf_file_finder.FileFinderCondition() params.modification_time.max_last_modified_time = time condition = conditions.ModificationTimeCondition(params) self.Touch("-m", "211811111200") # 2118-11-11 12:00 self.assertTrue(condition.Check(self.Stat())) self.Touch("-m", "222510201500") # 2225-10-20 15:00 self.assertFalse(condition.Check(self.Stat()))
def testMinTime(self): time = rdfvalue.RDFDatetime.FromHumanReadable("2017-12-24 19:00:00") params = rdf_file_finder.FileFinderCondition() params.modification_time.min_last_modified_time = time condition = conditions.ModificationTimeCondition(params) self.Touch("-m", "201712240100") # 2017-12-24 1:30 self.assertFalse(condition.Check(self.Stat())) self.Touch("-m", "201806141700") # 2018-06-14 17:00 self.assertTrue(condition.Check(self.Stat()))