def testUpdateSignedBinary(self): binary1_data = b"\x00\x11\x22\x33" binary2_data = b"\x44\x55\x66\x77" self._WriteTestBinaryAndGetBlobIterator(binary1_data, 10) blob_iterator = self._WriteTestBinaryAndGetBlobIterator(binary2_data, 10) chunk_generator = signed_binary_utils.StreamSignedBinaryContents( blob_iterator, chunk_size=10) self.assertCountEqual(list(chunk_generator), [binary2_data])
def testStreamSignedBinary_SingleChunk(self): binary_data = b"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd" blob_iterator = self._WriteTestBinaryAndGetBlobIterator(binary_data, 5) # Stream binary content with a chunk size larger than the size of the # binary. chunk_generator = signed_binary_utils.StreamSignedBinaryContents( blob_iterator, chunk_size=15) self.assertCountEqual(list(chunk_generator), [binary_data])
def Handle(self, args, token=None): root_urn = _GetSignedBlobsRoots()[args.type] binary_urn = root_urn.Add(args.path) binary_size = signed_binary_utils.FetchSizeOfSignedBinary(binary_urn) blob_iterator, _ = signed_binary_utils.FetchBlobsForSignedBinary(binary_urn) chunk_iterator = signed_binary_utils.StreamSignedBinaryContents( blob_iterator, chunk_size=self.CHUNK_SIZE) return api_call_handler_base.ApiBinaryStream( filename=binary_urn.Basename(), content_generator=chunk_iterator, content_length=binary_size)
def testStreamSignedBinary_LargeBlobs(self): binary_data = b"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd" blob_iterator = self._WriteTestBinaryAndGetBlobIterator(binary_data, 5) # Stream binary content with a stream chunk size smaller than the # size of individual blobs. chunk_generator = signed_binary_utils.StreamSignedBinaryContents( blob_iterator, chunk_size=4) expected_chunks = [ b"\x00\x11\x22\x33", b"\x44\x55\x66\x77", b"\x88\x99\xaa\xbb", b"\xcc\xdd", ] self.assertCountEqual(list(chunk_generator), expected_chunks)
def testUploadPythonHack(self): with utils.TempDirectory() as dir_path: python_hack_path = os.path.join(dir_path, "hello_world.py") with open(python_hack_path, "wb") as f: f.write(b"print('Hello, world!')") config_updater_util.UploadSignedBinary( python_hack_path, objects_pb2.SignedBinaryID.BinaryType.PYTHON_HACK, "linux", upload_subdirectory="test") python_hack_urn = rdfvalue.RDFURN( "aff4:/config/python_hacks/linux/test/hello_world.py") blob_iterator, _ = signed_binary_utils.FetchBlobsForSignedBinaryByURN( python_hack_urn) uploaded_blobs = list( signed_binary_utils.StreamSignedBinaryContents(blob_iterator)) uploaded_content = b"".join(uploaded_blobs) self.assertEqual(uploaded_content, b"print('Hello, world!')")
def testUploadExecutable(self): with utils.TempDirectory() as dir_path: executable_path = os.path.join(dir_path, "foo.exe") with open(executable_path, "wb") as f: f.write(b"\xaa\xbb\xcc\xdd") config_updater_util.UploadSignedBinary( executable_path, objects_pb2.SignedBinaryID.BinaryType.EXECUTABLE, "windows", upload_subdirectory="anti-malware/registry-tools") executable_urn = rdfvalue.RDFURN( "aff4:/config/executables/windows/anti-malware/registry-tools/" "foo.exe") blob_iterator, _ = signed_binary_utils.FetchBlobsForSignedBinaryByURN( executable_urn) uploaded_blobs = list( signed_binary_utils.StreamSignedBinaryContents(blob_iterator)) uploaded_content = b"".join(uploaded_blobs) self.assertEqual(uploaded_content, b"\xaa\xbb\xcc\xdd")