-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_shell_server.cpp
More file actions
377 lines (316 loc) · 9.21 KB
/
remote_shell_server.cpp
File metadata and controls
377 lines (316 loc) · 9.21 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
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <thread>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define PORT 8080
#define BUFFER_SIZE 1024
/*
* authenticate client
* send shell commands to server
* receive command output
*/
void Handle_client(int client_fd)
{
char buffer[BUFFER_SIZE];
// handle authentication
const std::string password = "testpass";
const char *prompt = "Enter password:\n";
send(client_fd, prompt, strlen(prompt), 0);
char password_buffer[BUFFER_SIZE] = {0};
// block and wait
ssize_t bytes = read(client_fd, password_buffer, BUFFER_SIZE - 1);
if (bytes <= 0)
{
// connection closed || error occurred
close(client_fd);
return;
}
// add a null terminator
password_buffer[bytes] = '\0';
std::string received_password(password_buffer);
// remove newline characters
received_password.erase(received_password.find_last_not_of("\r\n") + 1);
if (received_password != password)
{
const char *denied = "Access denied!\n";
send(client_fd, denied, strlen(denied), 0);
close(client_fd);
std::cout << "Client failed to authenticate\n";
return;
}
const char *granted = "Access granted! You may now enter commands\n";
send(client_fd, granted, strlen(granted), 0);
while (true)
{
memset(buffer, 0, BUFFER_SIZE);
ssize_t bytes_received = read(client_fd, buffer, BUFFER_SIZE - 1);
if (bytes_received <= 0)
{
std::cout << "Client disconnected!\n";
break;
}
buffer[bytes_received] = '\0';
std::string command(buffer);
if (command == "exit\n" || command == "exit\r\n")
{
std::cout << "Client requested to exit\n";
break;
}
std::cout << "Received command: " << command;
// run raw -- TODO : add whitelisting
FILE* pipe = popen(command.c_str(), "r");
if (!pipe)
{
const char *err = "Failed to run command!\n";
send(client_fd, err, strlen(err), 0);
}
else
{
char output[BUFFER_SIZE];
// read output from pipe
while (fgets(output, sizeof(output), pipe) != nullptr)
{
send(client_fd, output, strlen(output), 0);
}
pclose(pipe);
}
}
close(client_fd);
}
int Run_OneClient()
{
/*
* test client server communication : (one client)
*/
int server_fd, client_fd;
struct sockaddr_in server_addr{}, client_addr{};
socklen_t client_len = sizeof(client_addr);
// create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket failed!");
return 1;
}
// bind to port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed!");
close(server_fd);
return 1;
}
// listen for connections
if (listen(server_fd, 5) < 0) {
perror("listen failed!");
close(server_fd);
return 1;
}
std::cout << "Server listening on port " << PORT << "...\n";
// accept one client
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept failed!");
close(server_fd);
return 1;
}
std::cout << "client connected\n";
Handle_client(client_fd);
close(server_fd);
return 0;
}
int Run_MultipleClients()
{
int server_fd;
struct sockaddr_in server_addr{}, client_addr{};
socklen_t client_len = sizeof(client_addr);
// create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("Socket failed!");
return 1;
}
// bind to port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed!");
close(server_fd);
return 1;
}
// listen for connections
if (listen(server_fd, 5) < 0) {
perror("Listen failed!");
close(server_fd);
return 1;
}
std::cout << "Server listening on port " << PORT << "...\n";
// accept clients on loop
while (true) {
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("Accept failed!");
continue;
}
std::cout << "New client connected\n";
// handle each client in a separate thread
std::thread(Handle_client, client_fd).detach();
}
close(server_fd);
return 0;
}
void Handle_client_SSL(SSL* ssl)
{
char buffer[BUFFER_SIZE];
const std::string correct_password = "testpass";
// prompt to enter password
const char *prompt = "Enter password:\n";
SSL_write(ssl, prompt, strlen(prompt));
char password_buffer[BUFFER_SIZE] = {0};
ssize_t bytes = SSL_read(ssl, password_buffer, BUFFER_SIZE - 1);
if (bytes <= 0)
{
SSL_shutdown(ssl);
SSL_free(ssl);
return;
}
password_buffer[bytes] = '\0';
std::string received_password(password_buffer);
received_password.erase(received_password.find_last_not_of("\r\n") + 1);
if (received_password != correct_password)
{
const char *denied = "Access denied!\n";
SSL_write(ssl, denied, strlen(denied));
SSL_shutdown(ssl);
SSL_free(ssl);
std::cout << "Client failed to authenticate\n";
return;
}
const char *granted = "Access granted! You may now enter commands\n";
SSL_write(ssl, granted, strlen(granted));
std::cout << "Client authenticated\n";
while (true)
{
memset(buffer, 0, BUFFER_SIZE);
ssize_t bytes_received = SSL_read(ssl, buffer, BUFFER_SIZE - 1);
if (bytes_received <= 0)
{
std::cout << "Client disconnected\n";
break;
}
buffer[bytes_received] = '\0';
std::string command(buffer);
if (command == "exit\n" || command == "exit\r\n")
{
std::cout << "Client requested to exit\n";
break;
}
std::cout << "Received command: " << command;
FILE *pipe = popen(command.c_str(), "r");
if (!pipe)
{
const char *err = "Failed to run command\n";
SSL_write(ssl, err, strlen(err));
}
else
{
// send command output
char output[BUFFER_SIZE];
while (fgets(output, sizeof(output), pipe) != nullptr)
{
SSL_write(ssl, output, strlen(output));
}
pclose(pipe);
}
}
// clean up
SSL_shutdown(ssl);
SSL_free(ssl);
}
int Run_MultipleClients_WithSSL()
{
int server_fd;
struct sockaddr_in address;
socklen_t addr_len = sizeof(address);
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1)
{
perror("socket failed!");
return 1;
}
// bind to any available address on the specified port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("bind failed!");
return 1;
}
// init openssl
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
// create context
const SSL_METHOD *method = TLS_server_method();
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx)
{
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// load certificate and private key
if (SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) <= 0 ||
SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// start listening
if (listen(server_fd, 5) < 0)
{
perror("listen");
return 1;
}
std::cout << "Server listening on port " << PORT << "...\n";
// handle every authenticated client in a thread
while (true)
{
int client_fd = accept(server_fd, (struct sockaddr *)&address, &addr_len);
if (client_fd < 0)
{
perror("accept");
continue;
}
// create new ssl object
SSL* ssl = SSL_new(ctx);
SSL_set_fd(ssl, client_fd);
// accept the ssl handshake
if (SSL_accept(ssl) <= 0)
{
ERR_print_errors_fp(stderr);
close(client_fd);
SSL_free(ssl);
continue;
}
std::cout << "New client connected\n";
std::thread client_thread(Handle_client_SSL, ssl);
// run independently
client_thread.detach();
}
close(server_fd);
return 0;
}
int main()
{
// one client-server
// return Run_OneClient();
// multiple clients_server
// return Run_MultipleClients();
// multiple clients with ssl
// return Run_MultipleClients_WithSSL();
}