-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlog.c
266 lines (220 loc) · 7.7 KB
/
log.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
/*
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 <stdarg.h> /* va_list, va_start, va_end */
#include <stdio.h> /* fprintf, vfprintf, stderr */
#include <string.h> /* strlen, malloc */
#include <time.h> /* time_t, time, tm, localtime, strftime */
#include "cbrutekrag.h" /* CBRUTEKRAG_VERBOSE_MODE */
#include "log.h"
#include "str.h" /* btkg_str_replace_placeholder */
/** Global verbosity level. */
static int g_verbose;
#define TIMESTAMP_BUFFER_SIZE 20
/**
* @brief Get the current timestamp in the format YYYY/MM/DD HH:MM:SS.
*
* @return A pointer to a static buffer containing the timestamp.
*/
static inline const char *get_current_timestamp(void)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
static char buffer[TIMESTAMP_BUFFER_SIZE];
buffer[strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", tm)] =
'\0';
return buffer;
}
/**
* @brief Print formatted output to a specified stream with optional logging
* information such as file and line number.
*
* @param level The logging level of the message (e.g., LOG_DEBUG).
* @param file The source file from which the log is coming.
* @param line The line number in the source file.
* @param head A prefix string to be printed before the log message.
* @param tail A suffix string to be printed after the log message.
* @param stream The output stream (e.g., stdout or stderr).
* @param format The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void print_output(int level, const char *file, int line, const char *head,
const char *tail, FILE *stream, const char *format, ...)
{
if (level == LOG_DEBUG && !(g_verbose & CBRUTEKRAG_VERBOSE_MODE)) {
return;
}
va_list arg;
fprintf(stream, "\033[2K\r%s[%s] ", head, get_current_timestamp());
#ifndef DEBUG
if (level == LOG_DEBUG)
#endif
fprintf(stream, "%s:%d ", file, line);
va_start(arg, format);
vfprintf(stream, format, arg);
va_end(arg);
fprintf(stream, "%s\n", tail);
fflush(stream);
}
/**
* @brief Print formatted output to a specified stream with the current timestamp.
*
* @param stream The output stream (e.g., stdout or stderr).
* @param format The format string for the log message.
* @param ... Additional arguments for the format string.
*/
void log_output(FILE *stream, const char *format, ...)
{
va_list arg;
fprintf(stream, "%s ", get_current_timestamp());
va_start(arg, format);
vfprintf(stream, format, arg);
va_end(arg);
fflush(stream);
}
/**
* @brief Log a successful login attempt with detailed information such as
* hostname, port, username, and password, formatted according to
* a global output format string.
*
* @param stream The output stream (e.g., stdout or stderr).
* @param hostname The hostname or IP address where the login was successful.
* @param port The port number used in the login attempt.
* @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 *format,
const char *hostname, int port,
const char *username, const char *password)
{
if (format == NULL) {
log_error("bruteforce_output_format is NULL");
return;
}
int port_len = snprintf(NULL, 0, "%d", port);
char strport[port_len + 1]; // +1 for the null terminator
snprintf(strport, sizeof(strport), "%d", port);
// Allocation
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", format);
output = btkg_str_replace_placeholder(output, "%DATETIME%",
get_current_timestamp());
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%HOSTNAME%", hostname);
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%USERNAME%", username);
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%PASSWORD%", password);
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%PORT%", strport);
if (output == NULL)
goto error;
// Print buffer
fprintf(stream, "%s", output);
free(output);
fflush(stream);
return;
error:
log_error("Error replacing placeholders");
free(output);
}
/**
* @brief Log elegible target found with detailed information formatted
* according to a global output format string.
*
* @param stream The output stream (e.g., stdout or stderr).
* @param hostname The hostname or IP address where the login was successful.
* @param port The port number used in the login attempt.
* @param banner The server banner.
* @param password The password used in the login attempt.
*/
void btkg_log_target_found(FILE *stream, const char *format,
const char *hostname, int port, const char *banner)
{
if (format == NULL) {
log_error("scanner_output_format is NULL");
return;
}
int port_len = snprintf(NULL, 0, "%d", port);
char strport[port_len + 1]; // +1 for the null terminator
snprintf(strport, sizeof(strport), "%d", port);
// Allocation
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", format);
output = btkg_str_replace_placeholder(output, "%DATETIME%",
get_current_timestamp());
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%HOSTNAME%", hostname);
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%PORT%", strport);
if (output == NULL)
goto error;
output = btkg_str_replace_placeholder(output, "%BANNER%", banner);
if (output == NULL)
goto error;
// Print buffer
fprintf(stream, "%s", output);
free(output);
fflush(stream);
return;
error:
log_error("Error replacing placeholders");
free(output);
}
/**
* @brief Sets the logging verbosity level.
*
* This function sets the global logging verbosity level based on the given
* level parameter. The verbosity level can be used to control the amount of
* detail output in log messages.
*
* The verbosity level is typically a bitwise OR of different flags that
* enable various levels of detail. For example, the following flags might
* be combined:
* - `CBRUTEKRAG_VERBOSE_MODE` (0x1): Enables basic verbose logging.
* - `CBRUTEKRAG_VERBOSE_SSHLIB` (0x2): Enables verbose logging for the SSH library.
*
* Example usage:
* @code
* options->verbose |= CBRUTEKRAG_VERBOSE_MODE;
* log_set_level(options->verbose);
* @endcode
*
* @param level The verbosity level to set. This is typically a combination of
* flags defined as bitwise values (e.g., `CBRUTEKRAG_VERBOSE_MODE`).
*/
void log_set_level(int level)
{
g_verbose = level;
}