Esempio n. 1
0
 def testReasonableInterval(self):
   # Check if the limit on maximum blocksize for processing still holds.
   dummy = io.BytesIO(b'')
   fp = fingerprint.Fingerprinter(dummy)
   big_finger = fingerprint.Finger(None, [fingerprint.Range(0, 1000001)], None)
   fp.fingers.append(big_finger)
   start, stop = fp._GetNextInterval()
   self.assertEqual(0, start)
   self.assertEqual(1000000, stop)
Esempio n. 2
0
    def _HashFile(self, fd):
        """Look for the required hashes in the file."""
        hashes = data_store_utils.GetFileHashEntry(fd)
        if hashes:
            found_all = True
            for fingerprint_type, hash_types in iteritems(self.HASH_TYPES):
                for hash_type in hash_types:
                    if fingerprint_type == "pecoff":
                        hash_type = "pecoff_%s" % hash_type
                    if not hashes.HasField(hash_type):
                        found_all = False
                        break
                if not found_all:
                    break
            if found_all:
                return hashes

        fingerprinter = fingerprint.Fingerprinter(fd)
        if "generic" in self.HASH_TYPES:
            hashers = self._GetHashers(self.HASH_TYPES["generic"])
            fingerprinter.EvalGeneric(hashers=hashers)
        if "pecoff" in self.HASH_TYPES:
            hashers = self._GetHashers(self.HASH_TYPES["pecoff"])
            if hashers:
                fingerprinter.EvalPecoff(hashers=hashers)

        if not hashes:
            hashes = fd.Schema.HASH()

        for result in fingerprinter.HashIt():
            fingerprint_type = result["name"]
            for hash_type in self.HASH_TYPES[fingerprint_type]:
                if hash_type not in result:
                    continue

                if hash_type == "SignedData":
                    # There can be several certs in the same file.
                    for signed_data in result[hash_type]:
                        hashes.signed_data.Append(revision=signed_data[0],
                                                  cert_type=signed_data[1],
                                                  certificate=signed_data[2])
                    continue

                # Set the hashes in the original object
                if fingerprint_type == "generic":
                    hashes.Set(hash_type, result[hash_type])

                elif fingerprint_type == "pecoff":
                    hashes.Set("pecoff_%s" % hash_type, result[hash_type])

                else:
                    logging.error("Unknown fingerprint_type %s.",
                                  fingerprint_type)

        return hashes
Esempio n. 3
0
 def testHashBlock(self):
   # Does it invoke a hash function?
   dummy = b'12345'
   fp = fingerprint.Fingerprinter(io.BytesIO(dummy))
   big_finger = fingerprint.Finger(None, [fingerprint.Range(0, len(dummy))],
                                   None)
   hasher = self.MockHasher()
   big_finger.hashers = [hasher]
   fp.fingers.append(big_finger)
   # Let's process the block
   fp._HashBlock(dummy, 0, len(dummy))
   self.assertEqual(hasher.seen, dummy)
Esempio n. 4
0
  def testSampleDataParsedCorrectly(self):
    for fname, expected in self.SAMPLE_LIST.items():
      path = package.ResourcePath(
          'grr-response-test',
          os.path.join('grr_response_test', 'test_data', 'fingerprint', fname))
      with io.open(path, 'rb') as f:
        fp = fingerprint.Fingerprinter(f)
        fp.EvalGeneric()
        fp.EvalPecoff()
        result = fp.HashIt()

        self.assertCountEqual(result, expected,
                              'Hashing results for %s do not match.' % fname)
Esempio n. 5
0
  def testAdjustments(self):
    dummy = io.BytesIO(b'')
    fp = fingerprint.Fingerprinter(dummy)
    big_finger = fingerprint.Finger(None, [fingerprint.Range(10, 20)], None)
    fp.fingers.append(big_finger)

    # The remaining range should not yet be touched...
    fp._AdjustIntervals(9, 10)
    self.assertEqual([fingerprint.Range(10, 20)], fp.fingers[0].ranges)
    # Trying to consume into the range. Blow up.
    self.assertRaises(RuntimeError, fp._AdjustIntervals, 9, 11)
    # We forgot a byte. Blow up.
    self.assertRaises(RuntimeError, fp._AdjustIntervals, 11, 12)
    # Consume a byte
    fp._AdjustIntervals(10, 11)
    self.assertEqual([fingerprint.Range(11, 20)], fp.fingers[0].ranges)
    # Consumed too much. Blow up.
    self.assertRaises(RuntimeError, fp._AdjustIntervals, 11, 21)
    # Consume exactly.
    fp._AdjustIntervals(11, 20)
    self.assertEmpty(fp.fingers[0].ranges)