def testMissingFile(self): """Fail on missing file?""" path = os.path.join(self.base_path, "this file does not exist") p = rdfvalue.PathSpec(path=path, pathtype=rdfvalue.PathSpec.PathType.OS) self.assertRaises(IOError, self.RunAction, "FingerprintFile", rdfvalue.FingerprintRequest(pathspec=p))
def testHashFile(self): """Can we hash a file?""" path = os.path.join(self.base_path, "numbers.txt") p = rdfvalue.PathSpec(path=path, pathtype=rdfvalue.PathSpec.PathType.OS) result = self.RunAction("FingerprintFile", rdfvalue.FingerprintRequest(pathspec=p)) types = result[0].matching_types fingers = {} for f in result[0].results: fingers[f["name"]] = f generic_sha256 = fingers["generic"]["sha256"] self.assertEqual(generic_sha256, hashlib.sha256(open(path).read()).digest()) # Make sure all fingers are listed in types and vice versa. t_map = { rdfvalue.FingerprintTuple.Type.FPT_GENERIC: "generic", rdfvalue.FingerprintTuple.Type.FPT_PE_COFF: "pecoff" } ti_map = dict((v, k) for k, v in t_map.iteritems()) for t in types: self.assertTrue(t_map[t] in fingers) for f in fingers: self.assertTrue(ti_map[f] in types) self.assertEqual(result[0].pathspec.path, path)
def StartFileFetch(self, pathspec, vfs_urn, request_data=None): """The entry point for this flow mixin - Schedules new file transfer.""" request_data = request_data or {} request_data["vfs_urn"] = vfs_urn self.CallClient("StatFile", pathspec=pathspec, next_state="StoreStat", request_data=request_data) request = rdfvalue.FingerprintRequest(pathspec=pathspec) request.AddRequest( fp_type=rdfvalue.FingerprintTuple.Type.FPT_GENERIC, hashers=[rdfvalue.FingerprintTuple.Hash.MD5, rdfvalue.FingerprintTuple.Hash.SHA1, rdfvalue.FingerprintTuple.Hash.SHA256]) self.CallClient("FingerprintFile", request, next_state="ReceiveFileHash", request_data=request_data)
def FingerprintFile(self, pathspec, request_data=None): """Launch a fingerprint client action.""" request = rdfvalue.FingerprintRequest(pathspec=pathspec) # Generic hash. request.AddRequest( fp_type=rdfvalue.FingerprintTuple.Type.FPT_GENERIC, hashers=[rdfvalue.FingerprintTuple.Hash.MD5, rdfvalue.FingerprintTuple.Hash.SHA1, rdfvalue.FingerprintTuple.Hash.SHA256]) # Authenticode hash. request.AddRequest( fp_type=rdfvalue.FingerprintTuple.Type.FPT_PE_COFF, hashers=[rdfvalue.FingerprintTuple.Hash.MD5, rdfvalue.FingerprintTuple.Hash.SHA1, rdfvalue.FingerprintTuple.Hash.SHA256]) self.CallClient("FingerprintFile", request, next_state="ProcessFingerprint", request_data=request_data)
def Start(self): """Issue the fingerprinting request.""" request = rdfvalue.FingerprintRequest( pathspec=self.args.pathspec) # Generic hash. request.AddRequest( fp_type=rdfvalue.FingerprintTuple.Type.FPT_GENERIC, hashers=[rdfvalue.FingerprintTuple.Hash.MD5, rdfvalue.FingerprintTuple.Hash.SHA1, rdfvalue.FingerprintTuple.Hash.SHA256]) # Authenticode hash. request.AddRequest( fp_type=rdfvalue.FingerprintTuple.Type.FPT_PE_COFF, hashers=[rdfvalue.FingerprintTuple.Hash.MD5, rdfvalue.FingerprintTuple.Hash.SHA1, rdfvalue.FingerprintTuple.Hash.SHA256]) self.CallClient("FingerprintFile", request, next_state="Done")