static Tuple <RevisionList, List <Revision> > GetRevisionsSync(RevisionList rlResult, DriveService service, string sFileID, int iPageSize, string sFields, List <Revision> lAllrevisions) { Console.WriteLine("Start fetching revisions...\n"); while (true) { if (rlResult != null && string.IsNullOrWhiteSpace(rlResult.NextPageToken)) { break; } RevisionsResource.ListRequest listRequest = service.Revisions.List(sFileID); listRequest.PageSize = iPageSize; listRequest.Fields = sFields; if (rlResult != null) { listRequest.PageToken = rlResult.NextPageToken; } rlResult = listRequest.Execute(); lAllrevisions.AddRange(rlResult.Revisions); } return(Tuple.Create(rlResult, lAllrevisions)); }
private List <RestoreFileItem> getTargetFiles() { List <RestoreFileItem> results = new List <RestoreFileItem>(); /** CODE HERE **/ //Find all files FilesResource.ListRequest listRequest = service.Files.List(); listRequest.PageSize = 1000; //TODO CHANGE TO VERY LARGE VALUE listRequest.Q = "name contains 'b4bc'"; //SEARCH FOR RANSOMWARE listRequest.Fields = "nextPageToken, files(id, name, modifiedTime, mimeType, size, lastModifyingUser(me))"; // List files. IList <Google.Apis.Drive.v3.Data.File> files = listRequest.Execute() .Files; Console.WriteLine("Found " + files.Count + " files..."); if (files != null && files.Count > 0) { foreach (var file in files) { if (file.Size == null) { continue; // Only look at binary files } if (file.ModifiedTime < dateModifiedSince_value) { continue; // Only look at files recently changed } //if (!file.LastModifyingUser.Me.Value) continue; // Only look at files modified by current user (RANSOMWARE) if (!file.Name.Contains("b4bc")) { continue; // Only look for RANSOMWARE pattern 1 } //Create object for revisionresults RestoreFileItem result = new RestoreFileItem(); result.fileId = file.Id; result.current_modifiedDate = file.ModifiedTime.Value; result.current_name = file.Name; result.current_mimeType = file.MimeType; //Check if file has previous revisions... RevisionsResource.ListRequest revisionListRequest = service.Revisions.List(file.Id); revisionListRequest.PageSize = 10; revisionListRequest.Fields = "kind, revisions(id, modifiedTime, originalFilename)"; bool foundValueRevision = false; try { IList <Google.Apis.Drive.v3.Data.Revision> revisions = revisionListRequest.Execute().Revisions; if (revisions != null && revisions.Count > 1) //At least something to go back to other than current version { foreach (var revision in revisions) { if (revision.ModifiedTime < dateModifiedSince_value) { foundValueRevision = true; Console.WriteLine("Found valid revision for file " + file.Name + "... revision from date " + revision.ModifiedTime.ToString()); result.previous_revisionId = revision.Id; result.previous_modifiedDate = revision.ModifiedTime.Value; result.previous_name = revision.OriginalFilename; } } } else { //NO REVISIONS continue; } } catch (Exception e) { } if (foundValueRevision) { //Good revision found string toString = result.current_name + " (" + result.current_modifiedDate.ToString() + ") -> " + result.previous_name + " (" + result.previous_modifiedDate.ToString() + ")"; result.toString = toString; results.Add(result); } } } else { Console.WriteLine("No files found."); } /** END HERE **/ //Save results to disk for sanity var json = new JavaScriptSerializer().Serialize(results); System.IO.File.AppendAllText(downloadPath + "\\restorable.lst", "[date]" + dateModifiedSince_value.ToString() + "[/date]" + json); return(results); }
//ORIGINAL CODE FROM TESTING DO NOT EDIT private void consoleRun() { UserCredential credential; downloadPath = System.Environment.CurrentDirectory; using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { string credPath = System.Environment.CurrentDirectory; credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); // Define parameters of request. FilesResource.ListRequest listRequest = service.Files.List(); listRequest.PageSize = 100; listRequest.Fields = "nextPageToken, files(id, name, modifiedTime, mimeType)"; // List files. IList <Google.Apis.Drive.v3.Data.File> files = listRequest.Execute() .Files; Console.WriteLine("Files:"); if (files != null && files.Count > 0) { foreach (var file in files) { if (file.Name != "Sample document RANSOM.txt") { continue; } Console.WriteLine("{0} - {1} ({2})", file.ModifiedTime, file.Name, file.Id); RevisionsResource.ListRequest revisionListRequest = service.Revisions.List(file.Id); revisionListRequest.PageSize = 100; revisionListRequest.Fields = "kind, revisions(id, modifiedTime, originalFilename, md5Checksum)"; IList <Google.Apis.Drive.v3.Data.Revision> revisions = revisionListRequest.Execute().Revisions; if (revisions != null && revisions.Count > 0) { foreach (var revision in revisions) { Console.WriteLine("{0} - {1} ({2})", revision.ModifiedTime, revision.OriginalFilename, revision.Id); //Replace original file //Rename Google.Apis.Drive.v3.Data.File f = service.Files.Get(file.Id).Execute(); /*FilesResource.UpdateRequest fUpdate = new FilesResource.UpdateRequest(service, f, file.Id); * fUpdate.*/ //Download Revision MemoryStream stream = new MemoryStream(); RevisionsResource.GetRequest dlRevision = new RevisionsResource.GetRequest(service, file.Id, revision.Id); dlRevision.Download(stream); //service.Files.Get(revision.Id).Download(stream); //Replace revision //System.IO.FileStream fs = new FileStream(downloadPath + "\\download\\"+ revision.OriginalFilename, FileMode.Open); FilesResource.UpdateMediaUpload fUpload = service.Files.Update(f, file.Id, stream, f.MimeType); f.Name = revision.OriginalFilename; f.Id = null; fUpload.Fields = "name"; var uploadStatus = fUpload.Upload(); //Console.WriteLine(fUpload.ResponseBody.ToString()); //fs.Close(); break; //Download Revision /*System.IO.FileStream fs = new FileStream(downloadPath + "\\download\\"+ revision.OriginalFilename, FileMode.Create); * RevisionsResource.GetRequest dlRevision = new RevisionsResource.GetRequest(service, file.Id, revision.Id); * dlRevision.Download(fs); * service.Files.Get(revision.Id).Download(fs); * fs.Flush(); * fs.Close(); * break;*/ } } } } else { Console.WriteLine("No files found."); } Console.Read(); }