forked from thenickdude/PlanC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanc.cpp
More file actions
967 lines (754 loc) · 31.4 KB
/
planc.cpp
File metadata and controls
967 lines (754 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
#define __STDC_FORMAT_MACROS
#define _FILE_OFFSET_BITS 64
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <ctype.h>
#include <sstream>
#include <thread>
#include <atomic>
#include "zstr/src/zstr.hpp"
#include "leveldb/db.h"
#include "boost/algorithm/hex.hpp"
#include "boost/asio/post.hpp"
#include "boost/asio/thread_pool.hpp"
#include "boost/program_options.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/iostreams/stream.hpp"
#include "boost/iostreams/device/null.hpp"
#include "boost/iostreams/filtering_streambuf.hpp"
#include "boost/iostreams/copy.hpp"
#include "boost/iostreams/filter/gzip.hpp"
#include "boost/date_time.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/system/error_code.hpp"
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include "cryptopp/md5.h"
#include "cryptopp/sha.h"
#include "cryptopp/channels.h"
#include "cryptopp/filters.h"
#include "cryptopp/files.h"
#include "common.h"
#include "backup.h"
#include "adb.h"
#include "properties.h"
using namespace CryptoPP;
using namespace std;
namespace po = boost::program_options;
std::string formatDateTime(time_t time, const std::string &format) {
boost::posix_time::time_facet *facet = new boost::posix_time::time_facet();
boost::posix_time::ptime p = boost::posix_time::from_time_t(time);
std::stringstream stream;
facet->format(format.c_str());
// std::locale is responsible for destroying the facet:
stream.imbue(std::locale(std::locale::classic(), facet));
stream << p;
return stream.str();
}
time_t parseDateTime(const std::string &time) {
return boost::posix_time::to_time_t(boost::posix_time::ptime(boost::posix_time::time_from_string(time)));
}
time_t archiveTimestampToUnix(time_t time) {
return time / 1000;
}
void printFileRevision(const FileManifestHeader &file, const ArchivedFileVersion &version) {
time_t revisionTimestamp = archiveTimestampToUnix(version.timestamp);
string revisionTime = formatDateTime(revisionTimestamp, "%Y-%m-%d %H:%M:%S");
time_t lastModifiedTimestamp = archiveTimestampToUnix(version.sourceLastModified);
string lastModifiedTime = formatDateTime(lastModifiedTimestamp, "%Y-%m-%d %H:%M:%S");
string checksum;
if (version.isDeleted()) {
checksum = "X";
} else if (version.isDirectory()) {
checksum = "-";
} else {
checksum = binStringToHex(string((char *) version.sourceChecksum, sizeof(version.sourceChecksum)));
}
printf("%.*s %" PRId64 " %.*s %.*s %.*s\n",
(int) file.path.length(), file.path.data(),
version.sourceLength,
(int) revisionTime.length(), revisionTime.data(),
(int) lastModifiedTime.length(), lastModifiedTime.data(),
(int) checksum.length(),
checksum.data()
);
}
enum class FileListDetailLevel {
basic, detailed
};
enum class TimeMode {
latest,
atTime,
all
};
void listBackupFiles(BackupArchive &archive, BackupArchive::iterator &begin, BackupArchive::iterator &end,
FileListDetailLevel detailLevel, bool includeDeleted, TimeMode timeMode, time_t atTime) {
while (begin != end) {
FileManifestHeader file = *begin;
++begin;
if (detailLevel == FileListDetailLevel::basic) {
// Just printing all filenames, we don't even need to fetch the history to see if the file was deleted or not
printf(
"%.*s\n",
(int) file.path.length(), file.path.data()
);
} else if (file.hasHistory()) {
FileHistory fileHistory = archive.getFileHistory(file);
if (!fileHistory.versions.empty()) {
int i;
switch (timeMode) {
case TimeMode::all:
for (auto & revision : fileHistory.versions) {
printFileRevision(file, revision);
}
break;
case TimeMode::latest:
if (includeDeleted || !fileHistory.versions.back().isDeleted()) {
printFileRevision(file, fileHistory.versions.back());
}
break;
case TimeMode::atTime:
for (i = 0; i < fileHistory.versions.size(); i++) {
if (archiveTimestampToUnix(fileHistory.versions[i].timestamp) > atTime) {
// This snapshot is newer than the one we want to see
break;
}
}
if (i > 0 && (includeDeleted || !fileHistory.versions[i - 1].isDeleted())) {
printFileRevision(file, fileHistory.versions[i - 1]);
}
break;
}
}
} else {
// Not sure why this would happen unless database is corrupt (special files-that-arent-files as flags?)
cerr << "Error: No revision history found for '" << file.path << "'" << endl;
}
}
}
void readFileRevisionData(const BackupArchive &archive,
const FileManifestHeader &file, const ArchivedFileVersion &version,
const vector<int64_t> &blockList,
CryptoPP::BufferedTransformation &output) {
CryptoPP::Weak::MD5 hasher;
bool hasCorruptBlocks = false;
for (int64_t blockNumber : blockList) {
DataBlock block = archive.blockDirectories.readBlockHeader(blockNumber);
std::string archivedData = archive.blockDirectories.readBlockData(blockNumber, block.backupLen);
std::string decryptedData;
uint8_t cipher = block.getCipher();
if (block.isEncrypted() || block.isCompressed()) {
// Check that the archived block isn't corrupt before we try something interesting like decryption or decompression
CryptoPP::byte archivedHash[CryptoPP::Weak::MD5::DIGESTSIZE];
hasher.Update((const CryptoPP::byte*) archivedData.data(), archivedData.length());
hasher.Final(archivedHash);
if (memcmp(archivedHash, block.backupMD5, sizeof(archivedHash)) != 0) {
/*
* Since we daren't decrypt or decompress this block, replace its position in the file with a string
* of nul bytes of the same original length:
*/
int bytesToPad = block.sourceLen;
const int PADDING_BUFFER_SIZE = 16 * 1024;
auto padding = new uint8_t[PADDING_BUFFER_SIZE](); // Zero-initialised by the ()
while (bytesToPad > 0) {
int padThisLoop = bytesToPad < PADDING_BUFFER_SIZE ? bytesToPad : PADDING_BUFFER_SIZE;
output.Put((const CryptoPP::byte *) padding, padThisLoop);
bytesToPad -= padThisLoop;
}
delete [] padding;
hasCorruptBlocks = true;
continue;
}
}
retryDecrypt:
if (block.isEncrypted() && isValidCipherCode(cipher)) {
try {
archivedData = code42Ciphers[cipher]->decrypt(archivedData, archive.key);
} catch (BadPaddingException & e) {
if (cipher == CIPHER_CODE_BLOWFISH_448) {
cipher = CIPHER_CODE_BLOWFISH_128;
goto retryDecrypt;
}
throw;
}
}
if (block.isCompressed()) {
try {
archivedData = maybeDecompress(archivedData);
} catch (std::exception & e) {
if (block.type != DATA_BLOCK_TYPE_UNKNOWN) {
throw;
}
/* If the "compressed" MD5 is the same as the source MD5, it was never compressed in the first
* place and we can just pass it through.
*/
CryptoPP::byte compressedHash[CryptoPP::Weak::MD5::DIGESTSIZE];
hasher.Update((const CryptoPP::byte*) archivedData.data(), archivedData.length());
hasher.Final(compressedHash);
if (memcmp(compressedHash, block.sourceMD5, sizeof(compressedHash)) != 0) {
throw;
}
}
}
// Check that the hash of the restored block is the same as what it was raw on disk when first backed up
CryptoPP::byte restoredHash[CryptoPP::Weak::MD5::DIGESTSIZE];
hasher.Update((const CryptoPP::byte*) archivedData.data(), archivedData.length());
hasher.Final(restoredHash);
if (memcmp(restoredHash, block.sourceMD5, sizeof(restoredHash)) != 0) {
hasCorruptBlocks = true;
}
// Finally write it to the destination
output.Put((const CryptoPP::byte *) archivedData.data(), archivedData.length());
}
if (hasCorruptBlocks) {
throw std::runtime_error("Some blocks in this file did not restore correctly (bad MD5)");
}
}
void restoreFileRevision(const BackupArchive &archive,
const FileManifestHeader &file, const ArchivedFileVersion &version,
const BlockList &blockList,
const boost::filesystem::path &destDirectory,
bool dryRun = true,
bool destSupportsColons = true) {
boost::filesystem::path destFilename;
if (destSupportsColons) {
destFilename = destDirectory / boost::filesystem::path(file.path);
} else {
std::string mungedPath(file.path);
for (int i = 0; i < mungedPath.length(); i++) {
if (mungedPath[i] == ':') {
mungedPath[i] = '-';
}
}
destFilename = destDirectory / boost::filesystem::path(mungedPath);
}
if (!dryRun) {
boost::filesystem::create_directories(destFilename.parent_path());
}
if (version.isRegularFile()) {
std::string tempFilename = destFilename.string() + "._planc_temp";
std::string fileMD5;
CryptoPP::Weak::MD5 md5Hasher;
CryptoPP::HashFilter hashFilter(md5Hasher, new CryptoPP::StringSink(fileMD5));
CryptoPP::FileSink *outputSink = nullptr;
CryptoPP::ChannelSwitch cs;
// The restored file is hashed as it is decoded:
cs.AddDefaultRoute(hashFilter);
// And written to a file if this isn't a dry run:
if (!dryRun) {
outputSink = new CryptoPP::FileSink(tempFilename.c_str(), true);
cs.AddDefaultRoute(*outputSink);
}
// Do the restore now:
readFileRevisionData(archive, file, version, blockList, cs);
cs.MessageEnd();
if (!dryRun) {
delete outputSink;
}
// Now we need to turn that temporary file into the destination file:
switch (version.handlerId) {
case FILE_VERSION_HANDLER_COMPRESS_FIRST_128: {
if (dryRun) {
throw std::runtime_error("Dry run not implemented for this filetype");
}
// Decompress the temp file using bzip
// Note I've never tested this path since this format appears to be deprecated (my client doesn't generate it)
ifstream inFile(tempFilename, ios_base::in | ios_base::binary);
ofstream outFile(destFilename.string(), std::ofstream::binary | std::ofstream::trunc);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inFilters;
inFilters.push(boost::iostreams::gzip_decompressor());
inFilters.push(inFile);
boost::iostreams::copy(inFilters, outFile);
outFile.close();
fileMD5 = "";
FileSource fs(tempFilename.c_str(), true /* PumpAll */,
new HashFilter(md5Hasher, new StringSink(fileMD5))
);
if (memcmp(fileMD5.data(), version.sourceChecksum, sizeof(version.sourceChecksum)) != 0) {
throw std::runtime_error("MD5 of restored file is incorrect!");
}
}
break;
default:
if (memcmp(fileMD5.data(), version.sourceChecksum, sizeof(version.sourceChecksum)) != 0) {
throw std::runtime_error("MD5 of restored file is incorrect!");
}
if (!dryRun) {
boost::filesystem::rename(boost::filesystem::path(tempFilename), destFilename);
}
}
} else if (version.isSymlink()) {
std::string symlinkContents;
StringSink sink(symlinkContents);
readFileRevisionData(archive, file, version, blockList, sink);
if (!dryRun) {
try {
boost::filesystem::create_symlink(boost::filesystem::path(symlinkContents), destFilename);
} catch (boost::filesystem::filesystem_error &e) {
throw std::runtime_error(
"Failed to create symlink to " + symlinkContents + " at " + destFilename.string() + ": "
+ e.what());
}
}
} else if (version.isDirectory()) {
if (!dryRun) {
try {
boost::filesystem::create_directory(destFilename);
} catch (boost::filesystem::filesystem_error &e) {
throw std::runtime_error(
"Failed to create output directory " + destFilename.string() + ": " + e.what());
}
}
} else {
throw std::runtime_error("Unsupported filetype " + std::to_string(version.fileType) + " for restore of '" + file.path + "', is this a device file or resource fork?");
}
if (!dryRun) {
try {
boost::filesystem::last_write_time(destFilename, archiveTimestampToUnix(version.sourceLastModified));
} catch (boost::filesystem::filesystem_error &e) {
std::cerr << "Failed to update timestamp of '" << destFilename.string() << "': " << e.what() << std::endl;
}
}
// Successfully restored this file
cout << file.path << endl;
}
static bool directorySupportsColons(const boost::filesystem::path &path) {
boost::filesystem::path testNoColon(path / "._planc-test");
boost::filesystem::path testColon(path / "._planc:test");
boost::system::error_code err;
boost::filesystem::create_directory(testNoColon, err);
if (err.value()) {
// It seems that we can't create any files in the destination, so we can't check for colon support.
// Just say it's supported.
return true;
}
boost::filesystem::remove(testNoColon);
boost::filesystem::create_directory(testColon, err);
if (err.value()) {
return false;
}
boost::filesystem::remove(testColon);
return true;
}
bool restoreBackupFiles(BackupArchive &archive, BackupArchive::iterator &begin, BackupArchive::iterator &end,
const boost::filesystem::path &destDirectory,
bool includeDeleted, TimeMode timeMode, time_t atTime,
bool dryRun = true,
bool destSupportsColons = true) {
bool success = true;
// For every matched file in the manifest:
while (begin != end) {
FileManifestHeader file = *begin;
++begin;
if (file.hasHistory()) {
try {
FileHistory fileHistory = archive.getFileHistory(file);
FileHistorySnapshot previous;
FileHistorySnapshot previousNotDeleted;
bool hasPrevious = false;
bool hasPreviousNotDeleted = false;
// Locate the revision we want to restore:
for (auto iterator = fileHistory.begin(); iterator != fileHistory.end(); ++iterator) {
if (timeMode == TimeMode::atTime && archiveTimestampToUnix(iterator->version.timestamp) > atTime) {
break;
}
previous = *iterator;
hasPrevious = true;
if (!iterator->version.isDeleted()) {
previousNotDeleted = *iterator;
hasPreviousNotDeleted = true;
}
}
if (includeDeleted && hasPreviousNotDeleted) {
restoreFileRevision(archive, file, previousNotDeleted.version, previousNotDeleted.blockList, destDirectory, dryRun, destSupportsColons);
} else if (hasPrevious && !previous.version.isDeleted()) {
restoreFileRevision(archive, file, previous.version, previous.blockList, destDirectory, dryRun, destSupportsColons);
}
} catch (std::exception &e) {
success = false;
cerr << "Error: Failures occurred while restoring '" << file.path << "': " << e.what() << endl;
}
} else {
// Not sure why this would happen unless database is corrupt (special files-that-aren't-files as flags?)
success = false;
cerr << "Error: No revision history found for '" << file.path << "'" << endl;
}
}
return success;
}
std::string readInputLine() {
char buffer[1024];
char *newLine;
fgets(buffer, sizeof(buffer), stdin);
if ((newLine = strchr(buffer, '\r'))
|| (newLine = strchr(buffer, '\n'))) {
*newLine = '\0';
}
return std::string(buffer);
}
std::string recoverADBKey(leveldb::DB *adb) {
std::string keyType;
try {
keyType = adbReadKey(adb, "\x01" "ArchiveSecurityKeyType");
} catch (const std::runtime_error &e) {
keyType = "AccountPassword";
}
try {
std::string key = adbReadKey(adb, "\x01" "ArchiveDataKey");
if (key.length() == 0) {
throw std::runtime_error("Read key was empty");
}
return key;
} catch (std::runtime_error &e) {
cerr << "Failed to read ArchiveDataKey from ADB: " << e.what() << endl;
try {
// Check we can both read and de-obfuscate it:
adbReadKey(adb, "\x01" "ArchiveSecureDataKey");
} catch (...) {
throw e;
}
cerr << endl
<< "It looks like there is an ArchiveSecureDataKey available instead, which is encrypted with "
"your CrashPlan Account Password or Archive Password. Enter that password now to attempt decryption "
"of the key:" << endl;
cerr << "? ";
string accountPassword(readInputLine());
try {
std::string key = adbReadSecureKey(adb, "\x01" "ArchiveSecureDataKey", accountPassword);
return key;
} catch (std::runtime_error &e) {
cerr << "Failed to read ArchiveSecureDataKey from ADB: " << e.what() << endl;
throw;
}
}
}
std::string recoverCPPropertiesKey(const std::string &filename) {
ifstream inFile(filename, ios_base::in | ios_base::binary);
std::string propertyFile = readStreamAsString(inFile);
std::string secureDataKey = propertiesReadField(propertyFile, "secureDataKey");
if (secureDataKey.length() == 0) {
throw std::runtime_error(
"Failed to read secureDataKey field from cp.properties file, does it actually contain a field with that name?");
}
secureDataKey = base64Decode(secureDataKey);
cerr << "The secureDataKey field in cp.properties is encrypted with your CrashPlan Account Password or Archive Password. Enter that password now to attempt decryption "
"of the key:" << endl;
cerr << "? ";
std::string password(readInputLine());
if (!passwordUnlocksSecureDataKey(secureDataKey, password)) {
throw std::runtime_error("The provided password couldn't decrypt the secureDataKey, is the password correct?");
}
return decryptSecureDataKey(secureDataKey, password);
}
std::string deriveKeyFromPasswordPrompt(std::string cpProperties, int32_t maxUserID) {
cerr << "Enter your Crashplan user ID (a number, can be found in conf/my.service.xml or in log files, grep for \"userId\"), or press enter if you don't know it:" << endl;
cerr << "? ";
const std::string userID(readInputLine());
if (userID.length() == 0) {
cerr << "Since you didn't provide a userid, it will be recovered using a brute-force search instead" << endl;
if (cpProperties.length() == 0) {
cerr
<< "You must supply a --cpproperties argument which points to the cp.properties file in your backup archive"
<< endl;
exit(EXIT_FAILURE);
}
}
cerr << endl
<< "Enter your passphrase:" << endl;
cerr << "? ";
const std::string customPassword(readInputLine());
cerr << endl;
if (userID.length() > 0) {
return deriveCustomArchiveKeyV2(userID, customPassword);
}
ifstream inFile(cpProperties, ios_base::in | ios_base::binary);
std::string propertyFile = readStreamAsString(inFile);
std::string dataKeyChecksumStr = propertiesReadField(propertyFile, "dataKeyChecksum");
CryptoPP::byte dataKeyChecksum[CryptoPP::Weak::MD5::DIGESTSIZE];
if (dataKeyChecksumStr.length() == 0) {
throw std::runtime_error(
"Failed to read dataKeyChecksum field from cp.properties file, does it actually contain a field with that name?");
}
if (dataKeyChecksumStr.length() != sizeof(dataKeyChecksum) * 2) {
throw std::runtime_error(
"Expected dataKeyChecksum to be " + std::to_string(sizeof(dataKeyChecksum)) + " characters, " + std::to_string(dataKeyChecksumStr.length()) + " found");
}
boost::algorithm::unhex(dataKeyChecksumStr, dataKeyChecksum);
std::cerr << "Brute-forcing your userID now (up to a maximum of " << std::to_string(maxUserID) << ")... expect this to take 30 minutes per million scanned on a workstation" << std::endl;
boost::asio::thread_pool pool;
const int chunkSize = std::max(maxUserID / 1024, 50);
std::atomic<int> recoveredUserID(0);
for (int chunkStart = 1; chunkStart <= maxUserID; chunkStart += chunkSize) {
boost::asio::post(pool, [chunkStart, chunkSize, customPassword, dataKeyChecksum, &recoveredUserID]() {
for (int userID = chunkStart; userID < chunkStart + chunkSize; userID++) {
if (recoveredUserID.load() != 0) {
// Another thread already found the prize
break;
}
std::string userIDString = std::to_string(userID);
std::string key = deriveCustomArchiveKeyV2(userIDString, customPassword);
CryptoPP::Weak::MD5 hasher;
CryptoPP::byte currentHash[CryptoPP::Weak::MD5::DIGESTSIZE];
hasher.Update((const CryptoPP::byte *) key.data(), key.length());
hasher.Final(currentHash);
bool found = true;
for (int i = 0; i < sizeof(currentHash); i++) {
if (currentHash[i] != dataKeyChecksum[i]) {
found = false;
break;
}
}
if (found) {
recoveredUserID = userID;
break;
}
}
});
}
pool.join();
if (recoveredUserID == 0) {
cerr << "Failed to brute-force userID, password is probably incorrect" << std::endl;
exit(EXIT_FAILURE);
}
cout << "Recovered user ID: " << std::to_string(recoveredUserID) << std::endl;
return deriveCustomArchiveKeyV2(std::to_string(recoveredUserID), customPassword);
}
int main(int argc, char **argv) {
po::options_description mainOptions("Options");
mainOptions.add_options()
("help", "shows this page")
("adb", po::value<string>(),
"path to CrashPlan's 'adb' directory to recover a decryption key from (e.g. /Library/Application Support/CrashPlan/conf/adb. Optional)")
("mac-serial", po::value<string>(),
"serial number of the Mac that matches the adb directory (for CrashPlan Small Business, optional)")
("linux-serial", po::value<string>(),
"serial number of the Linux machine that matches the adb directory (for CrashPlan Small Business, optional)")
("cpproperties", po::value<string>(),
"path to a cp.properties file containing a 'secureDataKey' field to recover a decryption key from (Optional)")
("max-userid", po::value<int32_t>(),
"maximum user ID to consider when performing a brute-force search with derive-key (default 10000000)")
("key", po::value<string>(), "your backup decryption key (Hexadecimal, not your password. Optional)")
("key64", po::value<string>(), "backup decryption key in base64 (76 characters long. Optional)")
("archive", po::value<string>(), "the root of your CrashPlan backup archive")
("command", po::value<string>(), "command to run (recover-key,list,restore,etc)")
;
po::options_description filterOptions("Which archived files to operate on");
filterOptions.add_options()
("prefix", po::value<string>(), "prefix of the archived filepath to operate on")
("filename", po::value<string>(), "exact archived filepath to operate on")
("include-deleted", "include deleted files")
("at", po::value<string>(), "restore/list files at the given date (yyyy-mm-dd hh:mm:ss), if omitted will use the newest version")
;
po::options_description restoreOptions("Restore options");
restoreOptions.add_options()
("dest", po::value<string>(), "destination directory for restored files")
/* This does everything except actually write the restored file to disk (decrypts, decompresses and verifies MD5) so it
* should be an excellent integrity check:
*/
("dry-run", "verify integrity of restored files without actually writing them to disk. Filenames are printed to stdout and "
"errors to stderr.")
;
po::positional_options_description positionalOptions;
positionalOptions.add("command", -1);
po::options_description allOptions;
allOptions.add(mainOptions).add(filterOptions).add(restoreOptions);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(allOptions).positional(positionalOptions).run(), vm);
po::notify(vm);
} catch (std::exception &e) {
cerr << "Error parsing options: " << e.what() << endl;
return EXIT_FAILURE;
}
if (vm.count("help") || !vm.count("command")) {
cout << "Plan C" << endl;
cout << allOptions << endl;
cout << "Commands:" << endl;
cout << " recover-key - Recover your backup encryption key from a CrashPlan ADB directory or cp.properties file" << endl;
cout << " derive-key - Derive an encryption key from an archive password" << endl;
cout << " list - List all filenames that were ever in the backup (incl deleted)" << endl;
cout << " list-detailed - List the newest version of files in the backup (add --at for other times)" << endl;
cout << " list-all - List all versions of the files in the backup" << endl;
cout << " restore - Restore files" << endl;
return EXIT_FAILURE;
}
string adbPath;
string key;
FilenameMatchMode matchMode = FilenameMatchMode::none;
string matchString;
if (vm.count("adb")) {
adbPath = vm["adb"].as<string>();
}
if (vm.count("prefix")) {
matchString = vm["prefix"].as<string>();
matchMode = FilenameMatchMode::prefix;
}
if (vm.count("filename")) {
if (matchMode != FilenameMatchMode::none) {
cerr << "You can't combine the --prefix and --filename flags" << endl;
return EXIT_FAILURE;
}
matchString = vm["filename"].as<string>();
matchMode = FilenameMatchMode::equals;
}
if (vm.count("key")) {
key = hexStringToBin(vm["key"].as<string>());
}
if (vm.count("key64")) {
key = base64Decode(vm["key64"].as<string>());
}
if (adbPath.length() == 0) {
for (auto &path : {"/Library/Application Support/CrashPlan/conf/adb", "/usr/local/crashplan/conf/adb"}) {
if (boost::filesystem::is_directory(path)) {
adbPath = path; // Although we probably can't read this directory without being root
break;
}
}
}
if (vm["command"].as<string>() == "derive-key") {
key = deriveKeyFromPasswordPrompt(
vm.count("cpproperties") ? vm["cpproperties"].as<string>() : "",
vm.count("max-userid") ? vm["max-userid"].as<int32_t>() : 10000000
);
}
if (key.length() == 0 && vm.count("cpproperties")) {
try {
key = recoverCPPropertiesKey(vm["cpproperties"].as<string>());
} catch (std::runtime_error &e) {
cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
}
if (key.length() == 0 && adbPath.length() > 0) {
leveldb::DB *adb;
try {
adb = adbOpen(adbPath);
} catch (std::runtime_error &e) {
cerr << "Failed to open ADB (" + adbPath + ") to recover your decryption key:" << endl;
cerr << e.what() << endl << endl;
cerr << "You may need to run 'sudo ./plan-c recover-key' to have enough permission to read that directory, then pass the recovered key to the --key option." << endl << endl;
cerr << "Also check that the CrashPlan service is not running (it holds a lock on ADB), try one of these:" << endl;
cerr << " macOS - sudo launchctl unload /Library/LaunchDaemons/com.code42.service.plist" << endl;
cerr << " Windows - net stop \"Code42 Service\"" << endl;
cerr << " Linux - sudo /usr/local/crashplan/bin/service.sh stop" << endl;
cerr << " Other - https://support.code42.com/Incydr/Agent/Troubleshooting/Stop_and_start_the_Code42_app_service" << endl;
return EXIT_FAILURE;
}
adbInitPlatformKeys(vm.count("mac-serial") ? vm["mac-serial"].as<string>() : "", vm.count("linux-serial") ? vm["linux-serial"].as<string>() : "");
if (vm["command"].as<string>() == "recover-keys") {
cerr << "All deobfuscated values from adb:" << endl;
std::vector<std::pair<std::string, std::string>> values;
adbReadAllKeys(adb, values);
for (auto pair : values) {
bool printable = true;
for (int i = 0; i < pair.second.length(); i++) {
if (!isprint(pair.second[i])) {
printable = false;
break;
}
}
if (printable) {
cout << pair.first << "= " << pair.second << endl;
} else {
cout << pair.first << " (hex) = " << binStringToHex(pair.second) << endl;
}
}
cerr << endl;
return EXIT_SUCCESS;
}
key = recoverADBKey(adb);
}
if (key.length() == 0 && adbPath.length() == 0) {
cerr << "Couldn't find your decryption key automatically, you must supply one of the --adb, --cpproperties, --key or --key64 options" << endl;
return EXIT_FAILURE;
}
if (vm["command"].as<string>() == "recover-key" || vm["command"].as<string>() == "recover-keys" || vm["command"].as<string>() == "derive-key") {
cerr << "Here's your recovered decryption key (for use with --key):" << endl;
cout << binStringToHex(key) << endl;
return EXIT_SUCCESS;
}
if (vm["command"].as<string>() == "list" || vm["command"].as<string>() == "list-detailed"
|| vm["command"].as<string>() == "list-all" || vm["command"].as<string>() == "restore") {
if (!vm.count("archive")) {
cerr << "You must supply the --archive option" << endl;
return EXIT_FAILURE;
}
bool includeDeleted = vm.count("include-deleted") > 0;
time_t at = vm.count("at") ? parseDateTime(vm["at"].as<string>()) : 0;
boost::filesystem::path archivePath(vm["archive"].as<string>());
BackupArchive *backupArchive;
try {
backupArchive = new BackupArchive(archivePath, key);
} catch (std::runtime_error & e) {
cerr << "Fatal error opening backup archive: " << e.what() << endl;
return EXIT_FAILURE;
}
if (vm["command"].as<string>() == "list" || vm["command"].as<string>() == "list-detailed"
|| vm["command"].as<string>() == "list-all") {
FileListDetailLevel detailLevel;
TimeMode timeMode;
if (vm["command"].as<string>() == "list-all") {
detailLevel = FileListDetailLevel::detailed;
timeMode = TimeMode::all;
} else if (vm["command"].as<string>() == "list-detailed") {
detailLevel = FileListDetailLevel::detailed;
if (vm.count("at")) {
timeMode = TimeMode::atTime;
} else {
timeMode = TimeMode::latest;
}
} else {
detailLevel = FileListDetailLevel::basic;
timeMode = TimeMode::latest;
}
auto begin = backupArchive->begin(matchMode, matchString);
auto end = backupArchive->end();
listBackupFiles(*backupArchive, begin, end, detailLevel, includeDeleted, timeMode, at);
return EXIT_SUCCESS;
} else if (vm["command"].as<string>() == "restore") {
bool dryRun = vm.count("dry-run") > 0;
boost::filesystem::path destDirectory;
bool colonSupport = true;
if (!dryRun) {
if (!vm.count("dest")) {
cerr << "You must a --dest to specify where restored files should be saved to" << endl;
return EXIT_FAILURE;
}
destDirectory = boost::filesystem::path(vm["dest"].as<string>());
if (!boost::filesystem::is_directory(destDirectory)) {
cerr << "Destination '" + destDirectory.string() + "' is not a directory." << endl;
return EXIT_FAILURE;
}
colonSupport = directorySupportsColons(destDirectory);
if (!colonSupport) {
cerr << "Destination filesystem does not support ':' characters in filenames, replacing those with '-'" << endl;
}
}
cerr << "Caching block indexes in memory..." << endl;
backupArchive->cacheBlockIndex();
if (dryRun) {
cerr << "Verifying archive integrity without restoring (dry-run)..." << endl;
} else {
cerr << "Restoring files..." << endl;
}
auto begin = backupArchive->begin(matchMode, matchString);
auto end = backupArchive->end();
TimeMode timeMode = vm.count("at") ? TimeMode::atTime : TimeMode::latest;
bool success = restoreBackupFiles(*backupArchive, begin, end, destDirectory, includeDeleted, timeMode, at, dryRun, colonSupport);
if (success) {
cerr << "Done!" << endl;
return EXIT_SUCCESS;
} else {
cerr << "Errors were encountered during this restore" << endl;
return EXIT_FAILURE;
}
}
}
cerr << "Missing command to run" << endl;
}