-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
adb.cpp
213 lines (159 loc) · 5.67 KB
/
adb.cpp
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
#include <cassert>
#include <cstdio>
#include <vector>
#include "adb.h"
#include "common.h"
#include "crypto.h"
#include "comparator.h"
#include "cryptopp/sha.h"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/string_file.hpp"
#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
#include <dpapi.h>
#endif
#ifdef __APPLE__
#include <CoreFoundation/CFString.h>
#include <IOKit/IOKitLib.h>
#endif
// CrashPlan Home:
static const std::string OBFUSCATION_KEY = "HWANToDk3L6hcXryaU95X6fasmufN8Ok";
static Code42AES256RandomIV aes256;
static std::vector<std::string> platformKeys(1, OBFUSCATION_KEY);
std::pair<std::string, std::string> makeMacPlatformIDFromSerial(const std::string &serial) {
std::string output = serial + serial + serial + serial + "\n";
return std::make_pair(output, output.substr(0, 32));
}
#ifdef __APPLE__
static std::pair<std::string, std::string> getMacPlatformID() {
char buffer[64];
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0);
IOObjectRelease(ioRegistryRoot);
if (!CFStringGetCString(uuidCf, buffer, sizeof(buffer), kCFStringEncodingMacRoman)) {
throw std::runtime_error("Serial number too long for buffer");
}
CFRelease(uuidCf);
return makeMacPlatformIDFromSerial(std::string(buffer));
}
#endif
std::pair<std::string, std::string> makeLinuxPlatformIDFromSerial(const std::string &serial) {
return std::make_pair(serial, serial.substr(0, 32));
}
static std::pair<std::string, std::string> getLinuxPlatformID() {
std::string id1 = "", id2 = "";
try {
boost::filesystem::load_string_file(boost::filesystem::path("/var/lib/dbus/machine-id"), id1);
} catch (...) {
}
try {
boost::filesystem::load_string_file(boost::filesystem::path("/etc/machine-id"), id2);
} catch (...) {
}
std::string serial = id1 + id2;
return makeLinuxPlatformIDFromSerial(serial);
}
/**
* Attempt decryption using DPAPI and return true if successful
*/
static bool deobfuscateWin32(const std::string &input, std::string &output) {
#ifdef _WIN32
DATA_BLOB in, out;
in.cbData = input.length();
in.pbData = (BYTE*) input.data();
if (CryptUnprotectData(&in, NULL, NULL, NULL, NULL, 0, &out)) {
std::string result((const char *)out.pbData, out.cbData);
LocalFree(out.pbData);
output = result;
return true;
}
#endif
return false;
}
void adbInitPlatformKeys(const std::string &macOSSerial, const std::string &linuxSerial) {
std::pair<std::string, std::string> platformID;
if (macOSSerial.length() > 0) {
platformID = makeMacPlatformIDFromSerial(macOSSerial);
} else if (linuxSerial.length() > 0) {
platformID = makeLinuxPlatformIDFromSerial(linuxSerial);
} else {
#ifdef __APPLE__
platformID = getMacPlatformID();
#else
#ifdef _WIN32
// Windows uses DPAPI instead
return;
#else
// Linux, etc.
platformID = getLinuxPlatformID();
#endif
#endif
}
if (platformID.first.length() < 32) {
return;
}
platformKeys.push_back(generateSmallBusinessKeyV2(platformID.first, platformID.second));
}
static std::string adbDeobfuscate(const std::string &value) {
std::string result;
if (deobfuscateWin32(value, result)) {
return result;
}
for (std::string key : platformKeys) {
try {
return aes256.decrypt(value, key);
} catch (BadPaddingException &e) {
}
}
throw std::runtime_error("Failed to deobfuscate values from ADB, bad serial number? "
"If you're using CrashPlan for Small Business, please see the readme for instructions.");
}
leveldb::DB* adbOpen(std::string adbPath) {
leveldb::DB* db;
leveldb::Options options;
Code42Comparator *comp = new Code42Comparator();
options.create_if_missing = false;
options.compression = leveldb::CompressionType::kNoCompression;
options.comparator = comp;
leveldb::Status status = leveldb::DB::Open(options, adbPath, &db);
if (!status.ok()) {
throw std::runtime_error(status.ToString());
}
return db;
}
std::string adbReadKey(leveldb::DB* db, std::string key) {
std::string value;
leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value);
if (status.ok()) {
return adbDeobfuscate(value);
} else {
throw std::runtime_error("Failed to fetch " + key + ": " + status.ToString());
}
}
bool adbKeyExists(leveldb::DB* db, std::string key) {
std::string value;
leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value);
return status.ok();
}
std::string adbReadSecureKey(leveldb::DB* db, std::string key, std::string password) {
std::string value = base64Decode(adbReadKey(db, key));
if (passwordUnlocksSecureDataKey(value, password)) {
return decryptSecureDataKey(value, password);
} else {
throw std::runtime_error("The provided password couldn't decrypt the " + key + ", is the password correct?");
}
}
bool adbReadAllKeys(leveldb::DB* db, std::vector<std::pair<std::string, std::string>> &result) {
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
/*
* We avoid calling decryptAndPrintValueForKey here, because if our comparator is wrong then we expect iteration
* to find keys that ->Get() can't see.
*/
result.push_back(std::pair<std::string,std::string>(it->key().ToString(), adbDeobfuscate(it->value().ToString())));
}
bool success = it->status().ok();
delete it;
return success;
}