-
Notifications
You must be signed in to change notification settings - Fork 64
/
fork_accept.c
196 lines (171 loc) · 5.08 KB
/
fork_accept.c
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
// A sample forking/listening CTF binary (shamelessly reverse engineered
// from an old Ghost in the Shellcode challenge). You don't have to use
// this exact code, but make sure that your forking/listening servers
// are not susceptible to the problems mentioned in the comments.
#include <arpa/inet.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
const uint16_t port = 1234;
// Remember to check return values carefully in this function.
// Don't want to accidentally give people root :-)
int drop_privs(char *username) {
struct passwd *pw = getpwnam(username);
if (pw == NULL) {
fprintf(stderr, "User %s not found\n", username);
return 1;
}
if (chdir(pw->pw_dir) != 0) {
perror("chdir");
return 1;
}
// Don't forget to drop supplemental groups. Forgetting this
// has led to people escalating to root in some past CTFs :-)
if (setgroups(0, NULL) != 0) {
perror("setgroups");
return 1;
}
if (setgid(pw->pw_gid) != 0) {
perror("setgid");
return 1;
}
if (setuid(pw->pw_uid) != 0) {
perror("setuid");
return 1;
}
return 0;
}
// It is recommended to have wrappers like this around send and recv.
// Remember that send/recv can return without reading all n requested
// bytes.
ssize_t recvlen(int fd, char *buf, size_t n) {
ssize_t rc;
size_t nread = 0;
while (nread < n) {
rc = recv(fd, buf + nread, n - nread, 0);
if (rc == -1) {
if (errno == EAGAIN || errno == EINTR) {
continue;
}
return -1;
}
if (rc == 0) {
break;
}
nread += rc;
}
return nread;
}
ssize_t sendlen(int fd, const char *buf, size_t n) {
ssize_t rc;
size_t nsent = 0;
while (nsent < n) {
rc = send(fd, buf + nsent, n - nsent, 0);
if (rc == -1) {
if (errno == EAGAIN || errno == EINTR) {
continue;
}
return -1;
}
nsent += rc;
}
return nsent;
}
ssize_t sendstr(int fd, const char *str) {
return sendlen(fd, str, strlen(str));
}
// The connection handling function.
// Put your vulnerable code here :-)
int handle(int fd) {
char buf[64];
size_t len;
recvlen(fd, (char *) &len, sizeof(len));
recvlen(fd, buf, len);
sendstr(fd, buf);
}
int main(int argc, char **argv) {
int rc;
int opt;
int sockfd;
int clientfd;
pid_t pid;
struct sockaddr_in saddr = {0};
// Setting the SIGCHLD handler to SIG_IGN prevents child
// processes from becoming zombies (so you do not need to
// call wait() on them).
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
fputs("Failed to set SIGCHLD handler.", stderr);
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) {
perror("socket");
return 1;
}
// Set SO_REUSEADDR. Otherwise, if the server crashes for
// any reason, you will have to wait for sockets to time
// out before you can reuse the port.
opt = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(opt)) != 0) {
perror("setsockopt");
return 1;
}
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr *) &saddr,
sizeof(saddr)) != 0) {
perror("bind");
return 1;
}
if (listen(sockfd, 20) != 0) {
perror("listen");
return 1;
}
while (1) {
clientfd = accept(sockfd, NULL, NULL);
if (clientfd == -1) {
perror("accept");
continue;
}
pid = fork();
if (pid == -1) {
perror("fork");
close(clientfd);
continue;
}
if (pid == 0) {
// Avoid tons of long-running processes sticking around.
alarm(15);
// If you do not close the socket fd, someone who
// exploits the service could call accept() on it and
// hijack other people's connections.
close(sockfd);
// The server is started as root and drops privileges
// after forking and before handling the request.
// Otherwise, someone who exploits the service can
// kill, ptrace, or otherwise interfere with the server.
rc = drop_privs("problemuser");
if (rc == 0) {
rc = handle(clientfd);
}
close(clientfd);
_exit(rc);
}
// If you forget to close the client fd, you could run
// out of file descriptors (it also makes the connection fd
// hard to predict, which can be annoying for someone
// writing an exploit - if you want to do this on purpose,
// use dup2 with a random fd instead :-P).
close(clientfd);
}
return 0;
}