Beispiel #1
0
    def testGrepLength(self):
        data = "X" * 100 + "HIT"

        MockVFSHandlerFind.filesystem[self.filename] = data

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            "HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].offset, 100)

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            "HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0
        request.length = 100

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), 0)
Beispiel #2
0
  def ContentsLiteralMatchCondition(self, response, condition_options,
                                    condition_index):
    """Applies literal match condition to responses."""
    if not (self.args.process_non_regular_files or
            stat.S_ISREG(int(response.stat_entry.st_mode))):
      return

    options = condition_options.contents_literal_match
    grep_spec = rdf_client_fs.GrepSpec(
        target=response.stat_entry.pathspec,
        literal=options.literal.AsBytes(),
        mode=options.mode,
        start_offset=options.start_offset,
        length=options.length,
        bytes_before=options.bytes_before,
        bytes_after=options.bytes_after,
        xor_in_key=options.xor_in_key,
        xor_out_key=options.xor_out_key)

    self.CallClient(
        server_stubs.Grep,
        request=grep_spec,
        next_state=compatibility.GetName(self.ProcessGrep),
        request_data=dict(
            original_result=response, condition_index=condition_index + 1))
Beispiel #3
0
    def testGrepOffset(self):
        data = b"X" * 10 + b"HIT" + b"X" * 100

        MockVFSHandlerFind.filesystem[self.filename] = data

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            b"HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].offset, 10)

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            b"HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 5

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), 1)
        # This should still report 10.
        self.assertEqual(result[0].offset, 10)

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            b"HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 11

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), 0)
Beispiel #4
0
    def testOffsetAndLength(self):

        data = b"X" * 10 + b"HIT" + b"X" * 100 + b"HIT" + b"X" * 10
        MockVFSHandlerFind.filesystem[self.filename] = data

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            b"HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 11
        request.length = 100

        result = self.RunAction(searching.Grep, request)
        self.assertEmpty(result)
Beispiel #5
0
    def testGrepRegex(self):
        # Use the real file system.
        vfs.VFSInit().Run()

        request = rdf_client_fs.GrepSpec(
            regex="1[0]",
            xor_out_key=self.XOR_OUT_KEY,
            start_offset=0,
            target=rdf_paths.PathSpec(path=os.path.join(
                self.base_path, "numbers.txt"),
                                      pathtype=rdf_paths.PathSpec.PathType.OS))

        result = self.RunAction(searching.Grep, request)
        hits = [x.offset for x in result]
        self.assertEqual(hits, [
            18, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 329, 729,
            1129, 1529, 1929, 2329, 2729, 3129, 3529, 3888
        ])
        for x in result:
            self.assertTrue("10" in utils.Xor(x.data, self.XOR_OUT_KEY))
Beispiel #6
0
    def testHitLimit(self):
        limit = searching.Grep.HIT_LIMIT

        hit = "x" * 10 + "HIT" + "x" * 10
        data = hit * (limit + 100)
        MockVFSHandlerFind.filesystem[self.filename] = data

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            "HIT", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0
        request.bytes_before = 10
        request.bytes_after = 10

        result = self.RunAction(searching.Grep, request)
        self.assertEqual(len(result), limit + 1)
        error = "maximum number of hits"
        self.assertTrue(error in utils.Xor(result[-1].data, self.XOR_OUT_KEY))
Beispiel #7
0
    def testGrep(self):
        # Use the real file system.
        vfs.Init()

        request = rdf_client_fs.GrepSpec(literal=utils.Xor(
            b"10", self.XOR_IN_KEY),
                                         xor_in_key=self.XOR_IN_KEY,
                                         xor_out_key=self.XOR_OUT_KEY)
        request.target.path = os.path.join(self.base_path, "numbers.txt")
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0

        result = self.RunAction(searching.Grep, request)
        hits = [x.offset for x in result]
        self.assertEqual(hits, [
            18, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 329, 729,
            1129, 1529, 1929, 2329, 2729, 3129, 3529, 3888
        ])
        for x in result:
            self.assertIn(b"10", utils.Xor(x.data, self.XOR_OUT_KEY))
            self.assertEqual(request.target.path, x.pathspec.path)
Beispiel #8
0
    def ContentsRegexMatchCondition(self, response, condition_options,
                                    condition_index):
        """Applies contents regex condition to responses."""
        if not (self.args.process_non_regular_files
                or stat.S_ISREG(response.stat_entry.st_mode)):
            return

        options = condition_options.contents_regex_match
        grep_spec = rdf_client_fs.GrepSpec(target=response.stat_entry.pathspec,
                                           regex=options.regex,
                                           mode=options.mode,
                                           start_offset=options.start_offset,
                                           length=options.length,
                                           bytes_before=options.bytes_before,
                                           bytes_after=options.bytes_after)

        self.CallClient(server_stubs.Grep,
                        request=grep_spec,
                        next_state="ProcessGrep",
                        request_data=dict(original_result=response,
                                          condition_index=condition_index + 1))
Beispiel #9
0
  def testBufferBoundaries(self):

    for offset in range(-20, 20):

      data = b"X" * (1000 + offset) + b"HIT" + b"X" * 100
      MockVFSHandlerFind.filesystem[self.filename] = data

      request = rdf_client_fs.GrepSpec(
          literal=utils.Xor(b"HIT", self.XOR_IN_KEY),
          xor_in_key=self.XOR_IN_KEY,
          xor_out_key=self.XOR_OUT_KEY)
      request.target.path = self.filename
      request.target.pathtype = rdf_paths.PathSpec.PathType.OS
      request.start_offset = 0

      result = self.RunAction(searching.Grep, request)
      self.assertLen(result, 1)
      self.assertEqual(result[0].offset, 1000 + offset)
      expected = b"X" * 10 + b"HIT" + b"X" * 10
      self.assertLen(expected, result[0].length)
      self.assertEqual(utils.Xor(result[0].data, self.XOR_OUT_KEY), expected)
Beispiel #10
0
  def testGrepEverywhere(self):

    for offset in range(500):
      data = b"X" * offset + b"HIT" + b"X" * (500 - offset)
      MockVFSHandlerFind.filesystem[self.filename] = data

      request = rdf_client_fs.GrepSpec(
          literal=utils.Xor(b"HIT", self.XOR_IN_KEY),
          xor_in_key=self.XOR_IN_KEY,
          xor_out_key=self.XOR_OUT_KEY)
      request.target.path = self.filename
      request.target.pathtype = rdf_paths.PathSpec.PathType.OS
      request.start_offset = 0
      request.bytes_before = 10
      request.bytes_after = 10

      result = self.RunAction(searching.Grep, request)
      self.assertLen(result, 1)
      self.assertEqual(result[0].offset, offset)
      expected = data[max(0, offset - 10):offset + 3 + 10]
      self.assertLen(expected, result[0].length)
      self.assertEqual(utils.Xor(result[0].data, self.XOR_OUT_KEY), expected)
Beispiel #11
0
  def testSnippetSize(self):

    data = b"X" * 100 + b"HIT" + b"X" * 100
    MockVFSHandlerFind.filesystem[self.filename] = data

    for before in [50, 10, 1, 0]:
      for after in [50, 10, 1, 0]:
        request = rdf_client_fs.GrepSpec(
            literal=utils.Xor(b"HIT", self.XOR_IN_KEY),
            xor_in_key=self.XOR_IN_KEY,
            xor_out_key=self.XOR_OUT_KEY)
        request.target.path = self.filename
        request.target.pathtype = rdf_paths.PathSpec.PathType.OS
        request.start_offset = 0
        request.bytes_before = before
        request.bytes_after = after

        result = self.RunAction(searching.Grep, request)
        self.assertLen(result, 1)
        self.assertEqual(result[0].offset, 100)
        expected = b"X" * before + b"HIT" + b"X" * after
        self.assertLen(expected, result[0].length)
        self.assertEqual(utils.Xor(result[0].data, self.XOR_OUT_KEY), expected)