-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexploithawk.c
More file actions
352 lines (302 loc) · 11.4 KB
/
exploithawk.c
File metadata and controls
352 lines (302 loc) · 11.4 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Developer: Sreeraj
// GitHub: https://github.com/s-r-e-e-r-a-j
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // for strcasestr
#include <pthread.h>
#include <dirent.h>
#include <ncurses.h>
#include <unistd.h>
#define MAX_RESULTS 1000
#define MAX_PATH 512
#define MAX_TITLE 512
#define MAX_SOURCE 32
#define TITLE_WRAP 50 // preferred width for title column
typedef struct {
char source[MAX_SOURCE];
char title[MAX_TITLE];
char path[MAX_PATH];
} Exploit;
Exploit results[MAX_RESULTS];
int result_count = 0;
pthread_mutex_t lock;
int use_colors = 0; // flag for color support
const char *exploitDBPaths[] = {
"/usr/share/exploitdb/files_exploits.csv",
"/usr/share/exploitdb/files_shellcodes.csv",
"/var/lib/exploitdb/files_exploits.csv",
"/var/lib/exploitdb/files_shellcodes.csv",
"/opt/exploitdb/files_exploits.csv",
"/opt/exploitdb/files_shellcodes.csv",
};
const char *exploitDirs[] = {
"/usr/share/exploitdb/exploits",
"/usr/share/exploitdb/shellcodes",
"/var/lib/exploitdb/exploits",
"/var/lib/exploitdb/shellcodes",
"/opt/exploitdb/exploits",
"/opt/exploitdb/shellcodes",
"/usr/local/share/exploitdb/exploits",
"/usr/local/share/exploitdb/shellcodes",
"/usr/share/metasploit-framework/modules/exploits",
"/usr/share/metasploit-framework/modules/auxiliary",
"/usr/share/metasploit-framework/modules/post",
"/usr/share/metasploit-framework/modules/payloads",
"/usr/share/exploits",
"/usr/local/share/exploits",
"/opt/exploits",
"/var/exploits"
};
const char *exploitDBRoots[] = {
"/usr/share/exploitdb",
"/var/lib/exploitdb",
"/opt/exploitdb"
};
char search_term[128];
static void trim_newline(char *s) {
if (!s) return;
size_t n = strlen(s);
while (n > 0 && (s[n-1] == '\n' || s[n-1] == '\r')) { s[n-1] = '\0'; n--; }
}
// Check if all ^-separated parts exist in str (case-insensitive)
int matches_search(const char *str, const char *term) {
if (!str || !term) return 0;
char tmp[128];
strncpy(tmp, term, sizeof(tmp));
tmp[sizeof(tmp)-1] = 0;
char *part = strtok(tmp, "^");
while (part != NULL) {
if (!strcasestr(str, part)) return 0;
part = strtok(NULL, "^");
}
return 1;
}
// Add a result safely
void add_result(const char *source, const char *title, const char *path) {
pthread_mutex_lock(&lock);
if (result_count < MAX_RESULTS) {
strncpy(results[result_count].source, source, sizeof(results[result_count].source)-1);
strncpy(results[result_count].title, title, sizeof(results[result_count].title)-1);
strncpy(results[result_count].path, path, sizeof(results[result_count].path)-1);
results[result_count].source[MAX_SOURCE-1] = 0;
results[result_count].title[MAX_TITLE-1] = 0;
results[result_count].path[MAX_PATH-1] = 0;
result_count++;
}
pthread_mutex_unlock(&lock);
}
void *load_csv_thread(void *arg) {
const char *path = (const char *)arg;
FILE *f = fopen(path, "r");
if (!f) return NULL;
char line[2048];
while (fgets(line, sizeof(line), f)) {
char *cols[10];
int col = 0;
char *p = strtok(line, ",");
while (p && col < 10) {
while (*p == ' ' || *p == '\t') p++;
trim_newline(p);
cols[col++] = p;
p = strtok(NULL, ",");
}
if (col >= 3 && matches_search(cols[2], search_term)) {
trim_newline(cols[1]);
trim_newline(cols[2]);
char fullpath[MAX_PATH] = "";
// Find first existing ExploitDB root
for (int r = 0; r < sizeof(exploitDBRoots)/sizeof(exploitDBRoots[0]); r++) {
snprintf(fullpath, sizeof(fullpath), "%s/%s", exploitDBRoots[r], cols[1]);
if (access(fullpath, F_OK) == 0) { // file exists
add_result("ExploitDB", cols[2], fullpath);
break;
}
}
}
}
fclose(f);
return NULL;
}
// Scan directory recursively
void scan_dir(const char *dir) {
DIR *d = opendir(dir);
if (!d) return;
struct dirent *entry;
while ((entry = readdir(d)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char subdir[MAX_PATH];
snprintf(subdir, sizeof(subdir), "%s/%s", dir, entry->d_name);
scan_dir(subdir);
}
} else {
if (matches_search(entry->d_name, search_term)) {
char fullpath[MAX_PATH];
snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, entry->d_name);
add_result("Local FS", entry->d_name, fullpath);
}
}
}
closedir(d);
}
void *scan_dir_thread(void *arg) {
scan_dir((const char *)arg);
return NULL;
}
int wrap_text(const char *text, char lines[][MAX_TITLE], int width) {
if (!text) {
lines[0][0] = '\0';
return 1;
}
int len = strlen(text);
if (len == 0) {
lines[0][0] = '\0';
return 1;
}
int count = 0;
int i = 0;
while (i < len && count < 10) {
int chunk = (len - i > width) ? width : len - i;
strncpy(lines[count], text + i, chunk);
lines[count][chunk] = '\0';
count++;
i += chunk;
}
return (count == 0) ? 1 : count;
}
void display_results() {
initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
if (has_colors()) {
start_color();
use_colors = 1;
init_pair(1, COLOR_YELLOW, COLOR_BLACK); // source
init_pair(2, COLOR_CYAN, COLOR_BLACK); // title
init_pair(3, COLOR_GREEN, COLOR_BLACK); // path
init_pair(4, COLOR_MAGENTA, COLOR_BLACK); // header
} else {
use_colors = 0;
}
int ch, start = 0, rows, cols;
getmaxyx(stdscr, rows, cols);
int content_source = 10;
int content_title = TITLE_WRAP;
int content_path = cols - (content_source + content_title + 10); // 10 = extra chars from separators/spaces
if (content_path < 10) {
// shrink title to make space, but keep minimum widths
content_title = cols - (content_source + 10 + 10);
if (content_title < 10) content_title = 10;
content_path = cols - (content_source + content_title + 10);
if (content_path < 10) content_path = 10;
}
int x_source_field = 2;
int x_title_field = x_source_field + content_source + 3;
int x_path_field = x_title_field + content_title + 3;
while (1) {
clear();
char header_buf[1024];
snprintf(header_buf, sizeof(header_buf),
"| %-*.*s | %-*.*s | %-*.*s |",
content_source, content_source, "Source",
content_title, content_title, "Title",
content_path, content_path, "Path");
// clear header line then print
move(0,0); clrtoeol();
mvprintw(0, 0, "%s", header_buf);
if (use_colors) {
attron(COLOR_PAIR(4) | A_BOLD);
mvprintw(0, x_source_field, "%-*.*s", content_source, content_source, "Source");
mvprintw(0, x_title_field, "%-*.*s", content_title, content_title, "Title");
mvprintw(0, x_path_field, "%-*.*s", content_path, content_path, "Path");
attroff(COLOR_PAIR(4) | A_BOLD);
}
// separator line under header
mvhline(1, 0, '-', cols);
int printed_rows = 0;
for (int i = start; i < result_count && printed_rows < rows - 3; i++) {
char title_lines[10][MAX_TITLE];
char path_lines[10][MAX_PATH];
int title_count = wrap_text(results[i].title, title_lines, content_title);
int path_count = wrap_text(results[i].path, path_lines, content_path);
int max_lines = (title_count > path_count) ? title_count : path_count;
for (int l = 0; l < max_lines && printed_rows < rows - 3; l++) {
int line_y = printed_rows + 2;
char row_buf[2048];
const char *src_display = (l == 0) ? results[i].source : "";
const char *title_display = (l < title_count) ? title_lines[l] : "";
const char *path_display = (l < path_count) ? path_lines[l] : "";
// build base row string with exact widths
snprintf(row_buf, sizeof(row_buf),
"| %-*.*s | %-*.*s | %-*.*s |",
content_source, content_source, src_display,
content_title, content_title, title_display,
content_path, content_path, path_display);
// clear line then print base row
move(line_y, 0); clrtoeol();
mvprintw(line_y, 0, "%s", row_buf);
if (use_colors) {
// Source
attron(COLOR_PAIR(1));
mvprintw(line_y, x_source_field, "%-*.*s", content_source, content_source, src_display);
attroff(COLOR_PAIR(1));
// Title
attron(COLOR_PAIR(2));
mvprintw(line_y, x_title_field, "%-*.*s", content_title, content_title, title_display);
attroff(COLOR_PAIR(2));
// Path
attron(COLOR_PAIR(3));
mvprintw(line_y, x_path_field, "%-*.*s", content_path, content_path, path_display);
attroff(COLOR_PAIR(3));
}
printed_rows++;
}
}
// footer / help line
move(rows - 1, 0); clrtoeol();
mvprintw(rows - 1, 0, "Use Up/Down/PageUp/PageDown arrows to scroll, q to quit. Showing %d of %d results",
(result_count == 0 ? 0 : start + 1), result_count);
refresh();
ch = getch();
if (ch == 'q') break;
else if (ch == KEY_DOWN) {
if (start + 1 < result_count) start++;
} else if (ch == KEY_UP) {
if (start > 0) start--;
} else if (ch == KEY_NPAGE) { // page down
start += (rows - 4);
if (start >= result_count) start = (result_count > 0) ? result_count - 1 : 0;
} else if (ch == KEY_PPAGE) { // page up
start -= (rows - 4);
if (start < 0) start = 0;
}
}
endwin();
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <search_term>\n", argv[0]);
printf("Use ^ to separate name and version (e.g., apache^2.4.51)\n");
return 1;
}
strncpy(search_term, argv[1], sizeof(search_term)-1);
search_term[sizeof(search_term)-1] = 0; // Ensure null termination
pthread_mutex_init(&lock, NULL);
int num_threads = 0;
pthread_t threads[64];
// CSV threads
for (int i = 0; i < (int)(sizeof(exploitDBPaths)/sizeof(exploitDBPaths[0])); i++)
pthread_create(&threads[num_threads++], NULL, load_csv_thread, (void*)exploitDBPaths[i]);
// Directory threads
for (int i = 0; i < (int)(sizeof(exploitDirs)/sizeof(exploitDirs[0])); i++)
pthread_create(&threads[num_threads++], NULL, scan_dir_thread, (void*)exploitDirs[i]);
// Join all threads
for (int i = 0; i < num_threads; i++)
pthread_join(threads[i], NULL);
display_results();
pthread_mutex_destroy(&lock);
return 0;
}