-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.c
183 lines (140 loc) · 4.1 KB
/
main.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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <[email protected]>
*
* cSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cSploit. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <csploit/logger.h>
#include <csploit/file_logger.h>
#include "event.h"
#include "sniffer.h"
#include "notifier.h"
#include "resolver.h"
#include "prober.h"
#include "host.h"
#include "ifinfo.h"
#include "main.h"
#include "analyzer.h"
#include "scanner.h"
void stop(int signal) {
hosts.control.active = 0;
}
void update(int signal) {
full_scan();
}
/**
* @brief register main thread signal handlers
* @returns 0 on success, -1 on error.
*/
int register_signal_handlers() {
struct sigaction action;
action.sa_handler = stop;
sigemptyset(&(action.sa_mask));
action.sa_flags = 0;
if(sigaction(SIGINT, &action, NULL)) {
print( ERROR, "sigaction(SIGINT): %s", strerror(errno) );
return -1;
}
if(sigaction(SIGTERM, &action, NULL)) {
print( ERROR, "sigaction(SIGTERM): %s", strerror(errno) );
return -1;
}
action.sa_handler = update;
if(sigaction(SIGHUP, &action, NULL)) {
print( ERROR, "sigaction(SIGHUP): %s", strerror(errno) );
return -1;
}
return 0;
}
int init_controls() {
memset(&(events.control), 0, sizeof(struct data_control));
memset(&(hosts.control), 0, sizeof(struct data_control));
memset(&(sniffer_info.control), 0, sizeof(struct data_control));
memset(&(resolver_info.control), 0, sizeof(struct data_control));
memset(&(prober_info.control), 0, sizeof(struct data_control));
if(control_init(&(events.control)))
return -1;
if(control_init(&(hosts.control))) goto e1;
if(control_init(&(sniffer_info.control))) goto e2;
if(control_init(&(resolver_info.control))) goto e3;
if(control_init(&(prober_info.control))) goto e4;
if(control_init(&(analyze.control))) goto e5;
return 0;
e5:
control_destroy(&(prober_info.control));
e4:
control_destroy(&(resolver_info.control));
e3:
control_destroy(&(sniffer_info.control));
e2:
control_destroy(&(hosts.control));
e1:
control_destroy(&(events.control));
return -1;
}
void destroy_controls() {
control_destroy(&(prober_info.control));
control_destroy(&(resolver_info.control));
control_destroy(&(sniffer_info.control));
control_destroy(&(hosts.control));
control_destroy(&(events.control));
}
int main(int argc, char **argv) {
char *prog_name;
int ret;
if(argc < 2 || !strncmp(argv[1], "-h", 3) || !strncmp(argv[1], "--help", 7) ) {
prog_name = strrchr(argv[0], '/');
if(!prog_name)
prog_name = argv[0];
else
prog_name++;
print( ERROR, "Usage: %s <interface>\n", prog_name);
return EXIT_FAILURE;
}
if(open_logfile("./network-radar.log")) {
print( ERROR, "cannot open logfile");
return EXIT_FAILURE;
}
set_logger(file_logger);
if(get_ifinfo(argv[1]))
return EXIT_FAILURE;
if(register_signal_handlers())
return EXIT_FAILURE;
if(init_controls())
return EXIT_FAILURE;
if(init_prober()) goto exit;
if(start_analyzer()) goto exit;
if(start_notifier()) goto exit;
if(start_resolver()) goto exit;
if(start_sniff()) goto exit;
prober(NULL);
ret = 0;
exit:
stop_prober();
stop_analyzer();
stop_sniff();
stop_notifier();
stop_resolver();
fini_hosts();
destroy_controls();
return ret;
}