def _SignedBinaryIDFromURN(binary_urn): """Converts an AFF4 URN for a signed binary to a SignedBinaryID.""" if binary_urn.RelativeName(GetAFF4PythonHackRoot()): return rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.PYTHON_HACK, path=binary_urn.RelativeName(GetAFF4PythonHackRoot())) elif binary_urn.RelativeName(GetAFF4ExecutablesRoot()): return rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.EXECUTABLE, path=binary_urn.RelativeName(GetAFF4ExecutablesRoot())) else: raise ValueError("Unable to determine type of signed binary: %s." % binary_urn)
def testCorrectlyConvertsExecutableURN(self): self.assertEqual( signed_binary_utils.SignedBinaryIDFromURN( rdfvalue.RDFURN("aff4:/config/executables/foo")), rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.EXECUTABLE, path="foo"))
def testCorrectlyConvertsPythonHackURN(self): self.assertEqual( signed_binary_utils.SignedBinaryIDFromURN( rdfvalue.RDFURN("aff4:/config/python_hacks/foo")), rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.PYTHON_HACK, path="foo"))
def ReadIDsForAllSignedBinaries(self, cursor=None): """Returns ids for all signed binaries in the DB.""" cursor.execute( "SELECT binary_type, binary_path FROM signed_binary_references") return [ rdf_objects.SignedBinaryID(binary_type=binary_type, path=binary_path) for binary_type, binary_path in cursor.fetchall() ]
def testWriteAndReadLongUnicodePath(self): test_id = rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.EXECUTABLE, path="linux/" + "🚀" * 1000 + "/hello") self.db.WriteSignedBinaryReferences(test_id, _test_references1) stored_hash_id, stored_timestamp = self.db.ReadSignedBinaryReferences( test_id) self.assertEqual(stored_hash_id, _test_references1) self.assertGreater(stored_timestamp.AsMicrosecondsSinceEpoch(), 0) self.assertEqual(self.db.ReadIDsForAllSignedBinaries(), [test_id])
def _binary_id(self): return rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.EXECUTABLE, path=self.args.binary_path)
def _SignedBinaryIDFromKey(binary_key): """Converts a tuple representing a signed binary to a SignedBinaryID.""" return rdf_objects.SignedBinaryID(binary_type=binary_key[0], path=binary_key[1])
#!/usr/bin/env python """Tests for signed-binary DB functionality.""" from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals from grr_response_server import db from grr_response_server.rdfvalues import objects as rdf_objects _test_id1 = rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.EXECUTABLE, path="linux/test/hello") _test_id2 = rdf_objects.SignedBinaryID( binary_type=rdf_objects.SignedBinaryID.BinaryType.PYTHON_HACK, path="windows/test/hello") _test_references1 = rdf_objects.BlobReferences(items=[ rdf_objects.BlobReference(offset=0, size=2, blob_id=b"\xaa" * 32), rdf_objects.BlobReference(offset=2, size=3, blob_id=b"\xbb" * 32), ]) _test_references2 = rdf_objects.BlobReferences(items=[ rdf_objects.BlobReference(offset=0, size=3, blob_id=b"\xcc" * 32), rdf_objects.BlobReference(offset=3, size=2, blob_id=b"\xdd" * 32), ]) class DatabaseTestSignedBinariesMixin(object): """Mixin that adds tests for signed binary DB functionality.""" def testReadSignedBinaryReferences(self): self.db.WriteSignedBinaryReferences(_test_id1, _test_references1) stored_hash_id, stored_timestamp = self.db.ReadSignedBinaryReferences( _test_id1)