Ejemplo n.º 1
0
    def testZipFileWithSymlink(self):
        """Test that symlinks are preserved when unpacking generated zips."""

        compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED]
        for compression in compressions:
            outfd = StringIO.StringIO()

            infd1 = StringIO.StringIO("this is a test string")
            infd2 = StringIO.StringIO("this is another test string")
            with utils.StreamingZipWriter(outfd,
                                          compression=compression) as writer:
                writer.WriteFromFD(infd1, "test1.txt")
                writer.WriteFromFD(infd2, "subdir/test2.txt")

                writer.WriteSymlink("test1.txt", "test1.txt.link")
                writer.WriteSymlink("subdir/test2.txt", "test2.txt.link")

            with utils.TempDirectory() as temp_dir:
                zip_path = os.path.join(temp_dir, "archive.zip")
                with open(zip_path, "w") as fd:
                    fd.write(outfd.getvalue())

                # Builtin python ZipFile implementation doesn't support symlinks,
                # so we have to extract the files with command line tool.
                subprocess.check_call(
                    ["unzip", "-x", zip_path, "-d", temp_dir])

                link_path = os.path.join(temp_dir, "test1.txt.link")
                self.assertTrue(os.path.islink(link_path))
                self.assertEqual(os.readlink(link_path), "test1.txt")

                link_path = os.path.join(temp_dir, "test2.txt.link")
                self.assertTrue(os.path.islink(link_path))
                self.assertEqual(os.readlink(link_path), "subdir/test2.txt")
Ejemplo n.º 2
0
  def CreateArchive(self, _):
    # Create an output zip or tar file in the temp space.
    with aff4.FACTORY.Create(None, "TempImageFile", token=self.token) as outfd:
      if self.args.format == self.args.ArchiveFormat.ZIP:
        file_extension = "zip"
      elif self.args.format == self.args.ArchiveFormat.TAR_GZ:
        file_extension = "tar.gz"
      else:
        raise ValueError("Unknown archive format: %s" % self.args.format)

      outfd.urn = outfd.urn.Add("%s_%X%X.%s" % (
          self.args.target_file_prefix, utils.PRNG.GetULong(),
          utils.PRNG.GetULong(), file_extension))

      self.Log("Will create output on %s" % outfd.urn)
      self.state.output_archive_urn = outfd.urn

      collection = aff4.FACTORY.Open(self.args.collection_urn, token=self.token)

      buffered_outfd = io.BufferedWriter(RawIOBaseBridge(outfd),
                                         buffer_size=1024 * 1024 * 12)
      if self.args.format == self.args.ArchiveFormat.ZIP:
        streaming_writer = utils.StreamingZipWriter(buffered_outfd, "w",
                                                    zipfile.ZIP_DEFLATED)
      elif self.args.format == self.args.ArchiveFormat.TAR_GZ:
        streaming_writer = utils.StreamingTarWriter(buffered_outfd, "w:gz")
      else:
        raise ValueError("Unknown archive format: %s" % self.args.format)

      with streaming_writer:
        self.DownloadCollectionFiles(collection, streaming_writer,
                                     self.args.target_file_prefix)

      self.state.output_size = rdfvalue.ByteSize(outfd.size)
    def CreateArchive(self, _):
        # Create an output zip or tar file in the temp space.
        with aff4.FACTORY.Create(None, "TempImageFile",
                                 token=self.token) as outfd:
            friendly_hunt_name = self.args.hunt_urn.Basename().replace(
                ":", "_")

            if self.args.format == self.args.ArchiveFormat.ZIP:
                file_extension = "zip"
            elif self.args.format == self.args.ArchiveFormat.TAR_GZIP:
                file_extension = "tar.gz"
            else:
                raise ValueError("Unknown archive format: %s" %
                                 self.args.format)

            outfd.urn = outfd.urn.Add(
                "hunt_%s_%X%X.%s" % (friendly_hunt_name, utils.PRNG.GetULong(),
                                     utils.PRNG.GetULong(), file_extension))

            self.Log("Will create output on %s" % outfd.urn)
            self.state.output_archive_urn = outfd.urn

            hunt = aff4.FACTORY.Open(self.args.hunt_urn,
                                     aff4_type="GRRHunt",
                                     token=self.token)
            hunt_output_urn = hunt.state.context.results_collection_urn

            collection = aff4.FACTORY.Open(hunt_output_urn,
                                           aff4_type="RDFValueCollection",
                                           token=self.token)

            buffered_outfd = io.BufferedWriter(RawIOBaseBridge(outfd),
                                               buffer_size=1024 * 1024 * 12)
            if self.args.format == self.args.ArchiveFormat.ZIP:
                streaming_writer = utils.StreamingZipWriter(
                    buffered_outfd, "w", zipfile.ZIP_DEFLATED)
            elif self.args.format == self.args.ArchiveFormat.TAR_GZIP:
                streaming_writer = utils.StreamingTarWriter(
                    buffered_outfd, "w:gz")
            else:
                raise ValueError("Unknown archive format: %s" %
                                 self.args.format)

            with streaming_writer:
                self.DownloadCollectionFiles(collection, streaming_writer,
                                             friendly_hunt_name)

            self.state.output_size = rdfvalue.ByteSize(outfd.size)
Ejemplo n.º 4
0
  def testZipFileWithOneFile(self):
    """Test the zipfile implementation."""
    compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED]
    for compression in compressions:
      outfd = StringIO.StringIO()

      # Write the zip into a file like object.
      infd = StringIO.StringIO("this is a test string")
      with utils.StreamingZipWriter(outfd, compression=compression) as writer:
        writer.WriteFromFD(infd, "test.txt")

      test_zip = zipfile.ZipFile(outfd, "r")
      test_zip.testzip()

      self.assertEqual(test_zip.namelist(), ["test.txt"])
      self.assertEqual(test_zip.read("test.txt"), infd.getvalue())
Ejemplo n.º 5
0
    def testZipFileWithSymlink(self):
        """Test that symlinks are preserved when unpacking generated zips."""

        compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED]
        for compression in compressions:
            outfd = StringIO.StringIO()

            infd1 = StringIO.StringIO("this is a test string")
            infd2 = StringIO.StringIO("this is another test string")
            with utils.StreamingZipWriter(outfd,
                                          compression=compression) as writer:
                writer.WriteFromFD(infd1, "test1.txt")
                writer.WriteFromFD(infd2, "subdir/test2.txt")

                writer.WriteSymlink("test1.txt", "test1.txt.link")
                writer.WriteSymlink("subdir/test2.txt", "test2.txt.link")

            with utils.TempDirectory() as temp_dir:
                zip_path = os.path.join(temp_dir, "archive.zip")
                with open(zip_path, "w") as fd:
                    fd.write(outfd.getvalue())

                zip_fd = zipfile.ZipFile(outfd, "r")

                link_info = zip_fd.getinfo("test1.txt.link")
                self.assertEqual(link_info.external_attr,
                                 (0644 | 0120000) << 16)
                self.assertEqual(link_info.create_system, 3)

                link_contents = zip_fd.read("test1.txt.link")
                self.assertEqual(link_contents, "test1.txt")

                link_info = zip_fd.getinfo("test2.txt.link")
                self.assertEqual(link_info.external_attr,
                                 (0644 | 0120000) << 16)
                self.assertEqual(link_info.create_system, 3)

                link_contents = zip_fd.read("test2.txt.link")
                self.assertEqual(link_contents, "subdir/test2.txt")