forked from BrieflyX/ctf-pwns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics.cc
360 lines (283 loc) · 7.25 KB
/
lyrics.cc
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
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <unistd.h>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
namespace globals {
std::vector<int> records;
} // namespace globals
static void set_limits() {
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 2;
setrlimit(RLIMIT_CPU, &rlim);
rlim.rlim_cur = rlim.rlim_max = 64 * 1024;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 32;
setrlimit(RLIMIT_NOFILE, &rlim);
}
static void welcome() {
printf("Welcome to the Lyrics Explorer!\n");
}
static ssize_t read_line(int fd, char *buffer, size_t size) {
ssize_t bytes_read = 0;
while (size > 0) {
char c;
ssize_t ret = read(fd, &c, 1);
if (ret <= 0) {
break;
} else if (c == '\n') {
*buffer = '\0';
break;
}
*buffer++ = c;
bytes_read++;
size--;
}
return bytes_read;
}
static ssize_t read_line_buffered(int fd, char *buffer, size_t size) {
if (size == 0) {
return -1;
}
ssize_t ret = read(fd, buffer, size - 1);
if (ret <= 0) {
return ret;
}
buffer[ret] = '\0';
for (ssize_t i = 0; i < ret; i++) {
if (buffer[i] == '\0') {
buffer[i] = '.';
} else if (buffer[i] == '\n') {
buffer[i] = '\0';
lseek(fd, -(ret - i - 1), SEEK_CUR);
return i;
}
}
return ret;
}
static int load_int() {
char buffer[32] = { 0 };
ssize_t bytes_read = read_line(STDIN_FILENO, buffer, sizeof(buffer));
if (bytes_read <= 0) {
return 0;
}
return atoi(buffer);
}
static bool sanitize_path(char *buffer) {
if (strstr(buffer, "../") != NULL) {
return false;
}
return true;
}
static bool list_files(const char *path, std::vector<std::string> *files) {
files->clear();
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
continue;
}
files->push_back(ent->d_name);
}
closedir (dir);
return true;
} else {
perror ("[-] Error");
return false;
}
}
static bool list_bands() {
std::vector<std::string> bands;
if (!list_files("./data", &bands)) {
return false;
}
for (const auto& band : bands) {
printf("%s\n", band.c_str());
}
return true;
}
static bool list_songs() {
char buffer[32] = { /* zero padding */ };
printf("Band: ");
read_line(STDIN_FILENO, buffer, sizeof(buffer));
// Never trust user input!!
if (!sanitize_path(buffer)) {
printf("[-] Nice try!\n");
return false;
}
char path[48] = "./data/";
strncat(path, buffer, sizeof(path) - 7);
std::vector<std::string> songs;
if (!list_files(path, &songs)) {
return false;
}
for (const auto& song : songs) {
printf("%s\n", song.c_str());
}
return true;
}
static bool open_lyrics() {
// Don't allow opening too many lyrics at once.
if (globals::records.size() >= 16) {
return false;
}
// Load and sanitize user input.
char band[32];
printf("Band: ");
read_line(STDIN_FILENO, band, sizeof(band));
char song[32];
printf("Song: ");
read_line(STDIN_FILENO, song, sizeof(song));
// Hackers these days...
if (!sanitize_path(band) || !sanitize_path(song)) {
printf("[-] Nice try!\n");
return false;
}
// Form the final path of the lyrics in our database.
char path[128];
snprintf(path, sizeof(path), "./data/%s/%s", band, song);
// Open the path, make sure that it's a file (and not e.g. directory), and
// save the file descriptor.
int fd1 = open(path, O_RDONLY);
if (fd1 == -1) {
return false;
}
struct stat st;
if (fstat(fd1, &st) != 0 || !S_ISREG(st.st_mode)) {
return false;
}
globals::records.push_back(fd1);
// Better safe then sorry. Make sure that the path also doesn't point to a
// symbolic link.
int fd2 = open(path, O_RDONLY | O_NOFOLLOW);
if (fd2 == -1) {
printf("[-] Detected attempt to open a symbolic link!\n");
// Some kind of attack detected?
return true;
}
close(fd2);
// Extra check to protect the flag.
if (strstr(path, "flag") != NULL) {
printf("[-] Not today\n");
close(globals::records.back());
globals::records.pop_back();
return false;
}
printf("[+] Opened the lyrics as new record %zu\n",
globals::records.size() - 1);
return true;
}
static bool read_lyrics() {
printf("Record ID: ");
int idx = load_int();
if (idx < 0 || idx >= globals::records.size()) {
return false;
}
char buffer[4096];
ssize_t bytes_read = read_line_buffered(globals::records[idx],
buffer, sizeof(buffer));
// Let's make sure we're not disclosing any sensitive data due to potential
// bugs in the program.
if (bytes_read > 0) {
if (strstr(buffer, "DrgnS")) {
printf("[-] Attack detected and stopped!\n");
assert(close(globals::records[idx]) == 0);
memmove(&globals::records[idx], &globals::records[idx + 1],
(globals::records.size() - idx - 1) * sizeof(int));
globals::records.pop_back();
return true;
}
}
printf("%s\n", buffer);
return true;
}
static bool write_lyrics() {
// This feature is not fully tested, let's hope that it works...
printf("Record ID: ");
int idx = load_int();
if (idx < 0 || idx >= globals::records.size()) {
return false;
}
printf("Data length: ");
int length = load_int();
if (length < 0 || length > 1024) {
return false;
}
char buffer[1024];
printf("Data: ");
size_t bytes_read = read(STDIN_FILENO, buffer, length);
assert(bytes_read == length);
if (write(globals::records[idx], buffer, bytes_read) != bytes_read) {
return false;
}
return true;
}
static bool close_record() {
printf("Record ID: ");
int idx = load_int();
if (idx < 0 || idx >= globals::records.size()) {
return false;
}
close(globals::records[idx]);
memmove(&globals::records[idx], &globals::records[idx + 1],
(globals::records.size() - idx - 1) * sizeof(int));
globals::records.pop_back();
return true;
}
int main() {
// Disable stdout/stderr buffering.
setbuf(stdout, NULL);
setbuf(stderr, NULL);
// Set some safe limits on the resources used by the process.
set_limits();
// Print the welcome message.
welcome();
// Main program loop.
while (1) {
char buffer[32] = { 0 };
printf("Command> ");
read_line(STDIN_FILENO, buffer, sizeof(buffer));
if (!strcmp(buffer, "bands")) {
if (!list_bands()) {
break;
}
} else if (!strcmp(buffer, "songs")) {
if (!list_songs()) {
break;
}
} else if (!strcmp(buffer, "open")) {
if (!open_lyrics()) {
break;
}
} else if (!strcmp(buffer, "read")) {
if (!read_lyrics()) {
break;
}
} else if (!strcmp(buffer, "write")) {
if (!write_lyrics()) {
break;
}
} else if (!strcmp(buffer, "close")) {
if (!close_record()) {
break;
}
} else if (!strcmp(buffer, "exit")) {
break;
} else {
printf("[-] Unknown command\n");
break;
}
}
printf("Bye!\n");
return 0;
}