-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinetmon.c
More file actions
323 lines (288 loc) · 9.48 KB
/
inetmon.c
File metadata and controls
323 lines (288 loc) · 9.48 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
/* inetmon- IP Network Monitor
*
* Copyright (C) 2021 ECLB Ltd
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <glib/gprintf.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pcap.h>
#include <curses.h>
#include "inetflow.h"
#include "ic.h"
static gchar *iface = NULL;
static gchar *filename = NULL;
static gchar *private = "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16";
static gchar *db_host = NULL;
static gchar *db_name = "inetmon";
static int interval = 1;
static gboolean running = TRUE;
static gboolean quiet = FALSE;
/* Counters */
static gint frames = 0;
static gint ipv4 = 0;
static gint ipv6 = 0;
static gint unknown = 0;
#define MAXIMUM_SNAPLEN 262144
static inline uint64_t
get_time_us (void)
{
struct timeval tv;
gettimeofday (&tv, NULL);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
}
typedef struct subnet {
unsigned short family;
unsigned short prefix;
struct sockaddr_storage sa;
} subnet;
static GList *private_subnets = NULL;
typedef struct host {
char name[INET6_ADDRSTRLEN];
unsigned long inbytes;
unsigned long outbytes;
} host;
static GHashTable* host_htable = NULL;
static bool
is_local_v4(struct sockaddr_storage *ss)
{
//TODO
// unsigned int mask = 0xFFFFFFFF << (32 - network_bits);
char lips[INET6_ADDRSTRLEN];
inet_ntop(((struct sockaddr_in *)ss)->sin_family, &((struct sockaddr_in *)ss)->sin_addr, lips, INET6_ADDRSTRLEN);
if (strncmp(lips, "192.168.", 8) == 0)
return TRUE;
return FALSE;
}
static void update_host(InetTuple *tuple, uint32_t bytes)
{
int family = inet_tuple_family(tuple);
char lips[INET6_ADDRSTRLEN];
char hostname[INET6_ADDRSTRLEN];
struct sockaddr_in *ip;
host *h;
if (is_local_v4(&tuple->src))
ip = (struct sockaddr_in *) &tuple->src;
else if (is_local_v4(&tuple->dst))
ip = (struct sockaddr_in *) &tuple->dst;
else
return;
inet_ntop(family, &ip->sin_addr, lips, INET6_ADDRSTRLEN);
h = g_hash_table_lookup(host_htable, lips);
if (!h) {
h = g_malloc0(sizeof(host));
if (getnameinfo((const struct sockaddr *)ip, sizeof(*ip), h->name, INET6_ADDRSTRLEN, NULL, 0, NI_NAMEREQD) != 0) {
strncpy(h->name, lips, INET6_ADDRSTRLEN);
}
g_hash_table_insert(host_htable, g_strdup(lips), h);
}
if (ip == (struct sockaddr_in *) &tuple->src)
h->outbytes += bytes;
else
h->inbytes += bytes;
}
static void process_frame(const uint8_t * frame, uint32_t length)
{
InetTuple tuple = {0};
frames++;
if (inet_flow_parse(frame, length, NULL, &tuple, FALSE) && (inet_tuple_family(&tuple) == AF_INET || inet_tuple_family(&tuple) == AF_INET6)) {
update_host(&tuple, length);
if (inet_tuple_family(&tuple) == AF_INET)
ipv4++;
else
ipv6++;
} else {
unknown++;
}
}
static void dump_host(gpointer key, gpointer value, gpointer user_data)
{
host *h = (host *)value;
if (!quiet && (h->inbytes || h->outbytes))
g_printf("host: %s (%s) %lu in %lu out\r\n", h->name, (char *) key, h->inbytes, h->outbytes);
if (db_host) {
char *tags = g_strdup_printf("host=%s", h->name);
ic_tags(tags);
free(tags);
ic_measure("traffic");
ic_long("inbytes", h->inbytes);
ic_long("outbytes", h->outbytes);
ic_measureend();
}
}
static void dump_state(void)
{
if (!quiet)
g_printf("\r\n%8d frames (IPv4:%d IPv6:%d Unknown:%d)\r\n", frames, ipv4, ipv6, unknown);
g_hash_table_foreach(host_htable, dump_host, NULL);
if (db_host)
ic_push();
}
static void clear_host(gpointer key, gpointer value, gpointer user_data)
{
host *h = (host *)value;
h->inbytes = 0;
h->outbytes = 0;
}
static void clear_state(void)
{
g_hash_table_foreach(host_htable, clear_host, NULL);
}
static void process_interface(const char *interface, int snaplen, int promisc, int to_ms)
{
char error_pcap[PCAP_ERRBUF_SIZE] = { 0 };
struct pcap_pkthdr hdr;
const uint8_t *frame;
pcap_t *pcap;
int status;
uint64_t lasttime;
int col, row;
pcap = pcap_open_live(interface, snaplen, promisc, to_ms, error_pcap);
if (pcap == NULL) {
g_printf("%s: Failed to open interface: %s\r\n", interface, error_pcap);
return;
}
g_printf("Reading from \"%s\"\r\n", interface);
lasttime = get_time_us();
if (!quiet) {
initscr();
getmaxyx(stdscr, row, col);
}
while (running && (frame = pcap_next(pcap, &hdr)) != NULL) {
process_frame(frame, hdr.caplen);
if (interval && ((get_time_us() - lasttime) / 1000000) > interval)
{
lasttime = get_time_us();
if (!quiet) {
clear();
refresh();
}
dump_state();
clear_state();
}
}
if (!quiet)
endwin();
dump_state();
pcap_close(pcap);
}
static void process_pcap(const char *filename)
{
char error_pcap[PCAP_ERRBUF_SIZE];
pcap_t *pcap;
const uint8_t *frame;
struct pcap_pkthdr hdr;
pcap = pcap_open_offline(filename, error_pcap);
if (pcap == NULL) {
g_printf("Invalid pcap file: %s\r\n", filename);
return;
}
g_printf("Reading \"%s\"\r\n", filename);
while (running && (frame = pcap_next(pcap, &hdr)) != NULL) {
process_frame(frame, hdr.caplen);
}
dump_state();
pcap_close(pcap);
}
void
parse_private_networks(const gchar *private)
{
char *ptr, *parameter, *subnets;
subnets = g_strdup (private);
parameter = strtok_r (subnets, ",", &ptr);
while (parameter)
{
subnet *sn = g_malloc0(sizeof(subnet));
char *slash = strchr(parameter, '/');
if (!slash)
g_error("Invalid subnet \"%s\"\n", parameter);
*slash = '\0';
slash++;
sn->prefix = atoi(slash);
if (inet_pton(AF_INET, parameter, &((struct sockaddr_in *)&sn->sa)->sin_addr) == 1)
{
sn->family = AF_INET;
if (sn->prefix < 8 || sn->prefix > 32)
g_error("Invalid IP mask \"%s\"\n", slash);
}
else if (inet_pton(AF_INET6, parameter, &((struct sockaddr_in6 *)&sn->sa)->sin6_addr) == 1)
{
sn->family = AF_INET6;
if (sn->prefix < 8 || sn->prefix > 128)
g_error("Invalid IPv6 mask \"%s\"\n", slash);
}
else
g_error("Invalid IP address \"%s\"\n", parameter);
private_subnets = g_list_append(private_subnets, (gpointer)sn);
parameter = strtok_r(NULL, ",", &ptr);
char str[INET6_ADDRSTRLEN];
if (sn->family == AF_INET)
inet_ntop(sn->family, &((struct sockaddr_in *)&sn->sa)->sin_addr, str, INET_ADDRSTRLEN);
else
inet_ntop(sn->family, &((struct sockaddr_in6 *)&sn->sa)->sin6_addr, str, INET_ADDRSTRLEN);
printf("Adding private %s subnet %s/%d\n", sn->family == AF_INET ? "IPv4" : "IPv6", str, sn->prefix);
}
g_free(subnets);
}
static GOptionEntry entries[] = {
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Quiet", NULL },
{ "filename", 'f', 0, G_OPTION_ARG_STRING, &filename, "Pcap file to use", NULL },
{ "interface", 'i', 0, G_OPTION_ARG_STRING, &iface, "Interface to capture on", NULL },
{ "timeout", 't', 0, G_OPTION_ARG_INT, &interval, "Display timeout", NULL },
{ "private", 'p', 0, G_OPTION_ARG_STRING, &private, "Private Subnets (defaults to \"10.0.0.0/8,172.16.0.0/12,192.168.0.0/16\")", NULL },
{ "db_host", 'd', 0, G_OPTION_ARG_STRING, &db_host, "InfluxDB database hostname", NULL },
{ "db_name", 'n', 0, G_OPTION_ARG_STRING, &db_name, "InfluxDB database name (defaults to \"inetmon\"", NULL },
{ NULL }
};
static void intHandler(int dummy)
{
running = FALSE;
}
int main(int argc, char **argv)
{
GError *error = NULL;
GOptionContext *context;
gint i, j;
/* Parse options */
context = g_option_context_new("- IP Network Monitor");
g_option_context_add_main_entries(context, entries, NULL);
if (!g_option_context_parse(context, &argc, &argv, &error)) {
g_print("%s", g_option_context_get_help(context, FALSE, NULL));
g_print("ERROR: %s\n", error->message);
exit(1);
}
if ((!filename && !iface) || filename && iface) {
g_print("%s", g_option_context_get_help(context, FALSE, NULL));
g_print("ERROR: Require interface or pcap file\n");
exit(1);
}
parse_private_networks(private);
host_htable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
if (db_host) {
// ic_debug(1);
ic_influx_database(db_host, 8086, db_name);
}
signal(SIGINT, intHandler);
if (filename)
process_pcap(filename);
else
process_interface(iface, MAXIMUM_SNAPLEN, 1, 1000);
g_hash_table_destroy(host_htable);
g_option_context_free(context);
return 0;
}