Ejemplo n.º 1
0
    def InitFromKeyValue(self, key, value):
        self.key = key

        # Convert primitive types to rdf values so they can be serialized.
        if isinstance(value, float) and not value.is_integer():
            # TODO(user): Do not convert float values here and mark them invalid
            # later. ATM, we do not have means to properly represent floats. Change
            # this part once we have a RDFFloat implementation.
            pass
        elif rdfvalue.RDFInteger.IsNumeric(value):
            value = rdfvalue.RDFInteger(value)
        elif isinstance(value, unicode):
            value = rdfvalue.RDFString(value)
        elif isinstance(value, bytes):
            value = rdfvalue.RDFBytes(value)
        elif isinstance(value, bool):
            value = rdfvalue.RDFBool(value)

        if isinstance(value, rdfvalue.RDFValue):
            self.type = value.__class__.__name__
            self.value = value
        else:
            self.invalid = True

        return self
Ejemplo n.º 2
0
    def StoreMBR(self, responses):
        """This method stores the MBR."""

        if not responses.success:
            msg = "Could not retrieve MBR: %s" % responses.status
            self.Log(msg)
            raise flow.FlowError(msg)

        response = responses.First()

        self.state.buffers.append(response.data)
        self.state.bytes_downloaded += len(response.data)

        if self.state.bytes_downloaded >= self.args.length:
            mbr_data = "".join(self.state.buffers)
            self.state.buffers = None

            mbr = aff4.FACTORY.Create(self.client_urn.Add("mbr"),
                                      aff4_grr.VFSFile,
                                      mode="w",
                                      token=self.token)
            mbr.write(mbr_data)
            mbr.Close()
            self.Log("Successfully stored the MBR (%d bytes)." % len(mbr_data))
            self.SendReply(rdfvalue.RDFBytes(mbr_data))
Ejemplo n.º 3
0
 def ParseFile(
     self,
     knowledge_base: rdf_client.KnowledgeBase,
     pathspec: rdf_paths.PathSpec,
     filedesc: file_store.BlobStream,
 ) -> Iterable[rdfvalue.RDFBytes]:
     del knowledge_base, pathspec  # Unused.
     return [rdfvalue.RDFBytes(filedesc.Read())]
Ejemplo n.º 4
0
 def Done(self, responses):
   response = responses.First()
   if not responses.success:
     raise flow.FlowError("Execute Python hack failed: %s" % responses.status)
   if response:
     result = utils.SmartStr(response.return_val)
     # Send reply with full data, but only log the first 200 bytes.
     str_result = result[0:200]
     if len(result) >= 200:
       str_result += "...[truncated]"
     self.Log("Result: %s" % str_result)
     self.SendReply(rdfvalue.RDFBytes(utils.SmartStr(response.return_val)))
Ejemplo n.º 5
0
  def testAnyValueWithoutTypeCallback(self):
    test_pb = AnyValueWithoutTypeFunctionTest()

    for value_to_assign in [
        rdfvalue.RDFString("test"),
        rdfvalue.RDFInteger(1234),
        rdfvalue.RDFBytes(b"abc"),
        rdf_flows.GrrStatus(status="WORKER_STUCK", error_message="stuck")
    ]:
      test_pb.dynamic = value_to_assign
      serialized = test_pb.SerializeToString()
      self.assertEqual(
          AnyValueWithoutTypeFunctionTest.FromSerializedString(serialized),
          test_pb)
Ejemplo n.º 6
0
    def testRDFBytesConverter(self):
        data = rdfvalue.RDFBytes(b"foobar")

        converter = rdf_primitives.RDFBytesToExportedBytesConverter()
        results = list(converter.Convert(self.metadata, data))

        self.assertNotEmpty(results)

        exported_bytes = [
            r for r in results if r.__class__.__name__ == "ExportedBytes"
        ]
        self.assertLen(exported_bytes, 1)

        self.assertEqual(exported_bytes[0].data, data)
        self.assertEqual(exported_bytes[0].length, 6)
Ejemplo n.º 7
0
  def StoreMBR(self, responses):
    """This method stores the MBR."""

    if not responses.success:
      msg = "Could not retrieve MBR: %s" % responses.status
      self.Log(msg)
      raise flow_base.FlowError(msg)

    response = responses.First()

    self.state.buffers.append(response.data)
    self.state.bytes_downloaded += len(response.data)

    if self.state.bytes_downloaded >= self.args.length:
      mbr_data = b"".join(self.state.buffers)
      self.state.buffers = None

      self.Log("Successfully collected the MBR (%d bytes)." % len(mbr_data))
      self.SendReply(rdfvalue.RDFBytes(mbr_data))
Ejemplo n.º 8
0
 def GenerateSample(self, number=0):
     return rdfvalue.RDFBytes(b"\x00hello%s\x01" %
                              str(number).encode("ascii"))
Ejemplo n.º 9
0
 def testFromRDFBytes(self):
   foo = rdf_objects.BlobID(rdfvalue.RDFBytes(b"12345678" * 4))
   self.assertEqual(foo, b"12345678" * 4)
Ejemplo n.º 10
0
 def GenerateSample(self, number=0):
     return rdfvalue.RDFBytes(b"\x00hello%s\x01" % number)