Skip to content

Commit

Permalink
Remove use of g_output_format and g_scan_output_format as globals
Browse files Browse the repository at this point in the history
  • Loading branch information
matricali committed Aug 15, 2024
1 parent 6cb81d7 commit 9f770dc
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 51 deletions.
8 changes: 5 additions & 3 deletions src/bruteforce_ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,10 @@ int bruteforce_ssh_try_login(btkg_context_t *context, const char *hostname,
log_info("\033[32m[+]\033[0m %s:%d %s %s", hostname, port,
_username, _password);
if (context->output != NULL) {
btkg_log_successfull_login(context->output, hostname,
port, _username, _password);
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,
Expand Down Expand Up @@ -313,7 +315,7 @@ static void *btkg_bruteforce_worker(void *ptr)
combo->password);
}
}
pthread_exit(NULL);

return NULL;
}

Expand Down
31 changes: 13 additions & 18 deletions src/cbrutekrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ SOFTWARE.
#include "str.h"
#include "target.h"

char *g_output_format = NULL;
char *g_scan_output_format = NULL;

#define OPTIONAL_ARGUMENT_IS_PRESENT \
((optarg == NULL && optind < argc && argv[optind][0] != '-') ? \
(bool)(optarg = argv[optind++]) : \
Expand All @@ -61,6 +58,7 @@ char *g_scan_output_format = NULL;
/* Long options for getopt_long */
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "config", required_argument, NULL, 'c' },
{ "verbose", no_argument, NULL, 'v' },
{ "verbose-sshlib", no_argument, NULL, 'V' },
{ "scan", no_argument, NULL, 's' },
Expand Down Expand Up @@ -229,9 +227,9 @@ int main(int argc, char **argv)
options->max_threads = (size_t)tempint;
break;
case 'f':
g_output_format = strdup(optarg);
options->bruteforce_output_format = strdup(optarg);
btkg_str_replace_escape_sequences(
g_output_format);
options->bruteforce_output_format);
break;
case 'o':
output_filename = strdup(optarg);
Expand All @@ -243,9 +241,9 @@ int main(int argc, char **argv)
scan_output_filename = strdup(optarg);
break;
case 'F':
g_scan_output_format = strdup(optarg);
options->scanner_output_format = strdup(optarg);
btkg_str_replace_escape_sequences(
g_scan_output_format);
options->scanner_output_format);
break;
case 'D':
options->dry_run = 1;
Expand Down Expand Up @@ -349,8 +347,8 @@ int main(int argc, char **argv)
}

/* Output Format */
if (g_output_format == NULL) {
g_output_format = strdup(
if (options->bruteforce_output_format == NULL) {
options->bruteforce_output_format = strdup(
"%DATETIME%\t%HOSTNAME%:%PORT%\t%USERNAME%\t%PASSWORD%\n");
}

Expand Down Expand Up @@ -392,10 +390,11 @@ int main(int argc, char **argv)
scan_output_filename);
exit(EXIT_FAILURE);
}
free(scan_output_filename);

/* Scanner Output Format */
if (g_scan_output_format == NULL) {
g_scan_output_format =
if (options->scanner_output_format == NULL) {
options->scanner_output_format =
strdup("%HOSTNAME%:%PORT%\t%BANNER%\n");
}
}
Expand All @@ -410,9 +409,9 @@ int main(int argc, char **argv)
context.scan_output = NULL;
}

if (g_scan_output_format != NULL) {
free(g_scan_output_format);
g_scan_output_format = NULL;
if (options->scanner_output_format != NULL) {
free(options->scanner_output_format);
options->scanner_output_format = NULL;
}

btkg_progress_watcher_wait(&progress_watcher);
Expand Down Expand Up @@ -453,10 +452,6 @@ int main(int argc, char **argv)
_finalize:
btkg_context_destroy(&context);

if (g_output_format != NULL) {
free(g_output_format);
g_output_format = NULL;
}

return EXIT_SUCCESS;
}
2 changes: 2 additions & 0 deletions src/cbrutekrag.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ typedef struct {
int non_openssh;
int allow_honeypots;
char *check_http;
char *bruteforce_output_format;
char *scanner_output_format;
} btkg_options_t;

typedef struct {
Expand Down
13 changes: 13 additions & 0 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void btkg_options_init(btkg_options_t *options)
options->non_openssh = 0;
options->allow_honeypots = 0;
options->check_http = NULL;
options->bruteforce_output_format = NULL;
options->scanner_output_format = NULL;
}

/**
Expand Down Expand Up @@ -111,4 +113,15 @@ void btkg_context_destroy(btkg_context_t *context)
fclose(context->scan_output);
context->scan_output = NULL;
}

btkg_options_t *options = &context->options;

free(options->check_http);
options->check_http = NULL;

free(options->bruteforce_output_format);
options->bruteforce_output_format = NULL;

free(options->scanner_output_format);
options->scanner_output_format = NULL;
}
1 change: 0 additions & 1 deletion src/credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ void btkg_credentials_list_destroy(btkg_credentials_list_t *credentials_list)
free(credentials_list->credentials);
credentials_list->credentials = NULL;
credentials_list->length = 0;
credentials_list = NULL;
}
7 changes: 4 additions & 3 deletions src/detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ int detection_detect_ssh(btkg_context_t *context, const char *hostname,
log_info("[+] %s:%d - %s", hostname, port, banner);

if (context->scan_output != NULL) {
btkg_log_target_found(context->scan_output, hostname, port,
banner);
btkg_log_target_found(context->scan_output,
options->scanner_output_format, hostname,
port, banner);
}

ssh_disconnect(session);
Expand Down Expand Up @@ -281,7 +282,7 @@ void *detection_process(void *ptr)
pthread_mutex_unlock(&mutex);
}
}
pthread_exit(NULL);

return NULL;
}

Expand Down
29 changes: 12 additions & 17 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ SOFTWARE.
/** Global verbosity level. */
static int g_verbose;

/** Global output format string. */
extern char *g_output_format;

/** Global scanner output format string. */
extern char *g_scan_output_format;

#define TIMESTAMP_BUFFER_SIZE 20

/**
Expand Down Expand Up @@ -123,11 +117,12 @@ void log_output(FILE *stream, const char *format, ...)
* @param username The username used in the login attempt.
* @param password The password used in the login attempt.
*/
void btkg_log_successfull_login(FILE *stream, const char *hostname, int port,
void btkg_log_successfull_login(FILE *stream, const char *format,
const char *hostname, int port,
const char *username, const char *password)
{
if (g_output_format == NULL) {
log_error("g_output_format is NULL");
if (format == NULL) {
log_error("bruteforce_output_format is NULL");
return;
}

Expand All @@ -137,15 +132,15 @@ void btkg_log_successfull_login(FILE *stream, const char *hostname, int port,
snprintf(strport, sizeof(strport), "%d", port);

// Allocation
size_t output_len = strlen(g_output_format) + 1;
size_t output_len = strlen(format) + 1;
char *output = malloc(output_len);

if (output == NULL) {
log_error("Error allocating memory");
return;
}

snprintf(output, output_len, "%s", g_output_format);
snprintf(output, output_len, "%s", format);

output = btkg_str_replace_placeholder(output, "%DATETIME%",
get_current_timestamp());
Expand Down Expand Up @@ -190,11 +185,11 @@ void btkg_log_successfull_login(FILE *stream, const char *hostname, int port,
* @param banner The server banner.
* @param password The password used in the login attempt.
*/
void btkg_log_target_found(FILE *stream, const char *hostname, int port,
const char *banner)
void btkg_log_target_found(FILE *stream, const char *format,
const char *hostname, int port, const char *banner)
{
if (g_scan_output_format == NULL) {
log_error("g_scan_output_format is NULL");
if (format == NULL) {
log_error("scanner_output_format is NULL");
return;
}

Expand All @@ -204,15 +199,15 @@ void btkg_log_target_found(FILE *stream, const char *hostname, int port,
snprintf(strport, sizeof(strport), "%d", port);

// Allocation
size_t output_len = strlen(g_scan_output_format) + 1;
size_t output_len = strlen(format) + 1;
char *output = malloc(output_len);

if (output == NULL) {
log_error("Error allocating memory");
return;
}

snprintf(output, output_len, "%s", g_scan_output_format);
snprintf(output, output_len, "%s", format);

output = btkg_str_replace_placeholder(output, "%DATETIME%",
get_current_timestamp());
Expand Down
7 changes: 4 additions & 3 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ void log_set_level(int level);
* @param username The username used in the login attempt.
* @param password The password used in the login attempt.
*/
void btkg_log_successfull_login(FILE *stream, const char *hostname, int port,
void btkg_log_successfull_login(FILE *stream, const char *format,
const char *hostname, int port,
const char *username, const char *password);

/**
Expand All @@ -157,7 +158,7 @@ void btkg_log_successfull_login(FILE *stream, const char *hostname, int port,
* @param banner The server banner.
* @param password The password used in the login attempt.
*/
void btkg_log_target_found(FILE *stream, const char *hostname, int port,
const char *banner);
void btkg_log_target_found(FILE *stream, const char *format,
const char *hostname, int port, const char *banner);

#endif // LOGGER_H
3 changes: 0 additions & 3 deletions tests/test-credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ SOFTWARE.

#include "../src/credentials.h"

char *g_output_format = NULL;
char *g_scan_output_format = NULL;

typedef struct {
const char *input;
const char *username;
Expand Down
3 changes: 0 additions & 3 deletions tests/test-targets.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ SOFTWARE.

#include "../src/target.h"

char *g_output_format = NULL;
char *g_scan_output_format = NULL;

typedef struct {
const char *input;
btkg_target_t *expected;
Expand Down

0 comments on commit 9f770dc

Please sign in to comment.