-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracestore.pl
executable file
·424 lines (314 loc) · 9.83 KB
/
tracestore.pl
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/perl -w
# Copyright 2011 Merijntje Tak
#
# 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, version 3 of the License.
#
# 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/>.
#
##############################################################################
#
# tracestore.pl - Store traceroutes based on NetFlow data
#
# For all information, please view the README file.
use strict;
use DBD::mysql;
use Net::Traceroute;
use Socket;
use Data::Dumper;
use threads ('exit' => 'threads_only'); # exit() in thread becomes threads->exit()
my $mysqlHost = 'localhost';
my $mysqlUser = 'mysqlUser';
my $mysqlPass = 'mysqlPass';
my $mysqlName = 'databaseName';
my $flowdFile = '/var/log/flowd'; # Flowd database file
my $flowdSudo = 1; # Use sudo for flowd-reader
my $flowdBin = '/opt/flowd/bin/flowd-reader'; # flowd-reader binary
my $sudoBin = '/usr/bin/sudo'; # sudo binary
my $processTime = 300; # Amount of seconds to process during each run, 300 is default
my $debugSwitch = 0;
my $debugSwitchThreads = 0;
#
# SQL functions INET_ATON and INET_NTOA are being used to
# convert the IP to an INT value
#
#
# getStartTime
# Determine the start time in epoch seconds for analysis
# Input: none
# Output: reference to start time in epoch secs
sub getStartTime {
my $startTime = time - $processTime;
return(\$startTime);
}
#
# processInput
# Process the argument given to this script
# Input: reference to $ARGV[0]
# Output: reference to IP address
#
sub processInput {
my $input_ref = shift;
my $ip;
if ( $$input_ref =~ m/[A-Za-z]+/o ) {
my $packed_ip = gethostbyname($$input_ref);
if (defined $packed_ip) {
$ip = inet_ntoa($packed_ip);
}
if ( !defined($ip) || $ip =~ m/[A-Za-z]+/o ) {
print "Address $$input_ref cannot be resolved. Aborting...\n" ;
exit(1);
}
} else {
$ip = $$input_ref;
}
return(\$ip);
}
#
# setupTrace
# Create the trace object
# Input: target IP address
# Output: Reference to traceroute object
#
sub setupTrace {
my $target_ref = shift;
my $trace = Net::Traceroute->new(host => $$target_ref);
return(\$trace);
}
#
# listHops
# Put all the hops in a reference
# Input: reference to trace object
# Output: reference to hash
# $data{$hopCount}{$queryCount}{'host'} = $hostname
# $data{$hopCount}{$queryCount}{'time'} = $RTT
#
sub listHops {
my $trace_ref = shift;
my %data;
my $tr = $trace_ref;
print "Trace endpoint found\n" if $debugSwitch == 1;
my $hops = $$tr->hops;
print "hops: $hops\n" if $debugSwitch == 1;
my $hopCount = 0;
while ( $hopCount++ < $hops ) {
my $queryCount = 0;
while ( $queryCount++ < 3 ) {
if ( $$tr->hop_query_host($hopCount, $queryCount) =~ m/^255.255.255.255$/o ) {
next;
}
$data{$hopCount}{$queryCount}{'host'} = $$tr->hop_query_host($hopCount, $queryCount);
$data{$hopCount}{$queryCount}{'time'} = $$tr->hop_query_time($hopCount, $queryCount);
}
}
return(\%data);
}
#
# createMysqlConn
# Creates connection to mysql database
# Input: mysqlHost, mysqlUser, mysqlPass, mysqlDb
# Output: reference to database handler or 1 if unsuccessfull
#
sub createMysqlConn {
my $dbhost_ref = shift;
my $dbuser_ref = shift;
my $dbpass_ref = shift;
my $dbname_ref = shift;
my $dbh = DBI->connect('DBI:mysql:'.$$dbname_ref.";host=".$$dbhost_ref, $$dbuser_ref, $$dbpass_ref, { PrintError => 1 } );
if ( defined($DBI::errstr) ) {
print "Error connecting to database:\n$DBI::errstr \n";
exit(1);
}
return(\$dbh);
}
#
# closeMysqlConn
# Closes mysql connection cleanly
# Input: reference to database handler
#
sub closeMysqlConn {
my $dbh_ref = shift;
$$dbh_ref->disconnect();
return(0);
}
#
# insertMetaData
# Create the SQL query for the traceroute table (metadata), do it, and return the inserted id
# Input: reference to database handler, reference to target
# Output: reference to inserted id
#
sub insertMetaData {
my $dbh_ref = shift;
my $target_ref = shift;
my $src = "10.100.0.70";
my $dst = $$target_ref;
my $insq = "INSERT INTO traceroutes (src, dest, time)
VALUES (INET_ATON(\"$src\"), INET_ATON(\"$dst\"), now());";
$$dbh_ref->do($insq);
my $sth = $$dbh_ref->prepare("SELECT LAST_INSERT_ID() AS id;");
$sth->execute;
my $res_ref = $sth->fetchrow_arrayref();
my $id = $$res_ref[0];
return(\$id);
}
#
# insertData
# Insert the trace data into the database
# Input: reference to database handler, traceroutes_id, hop number, ip, rtt
# Output: exit status
#
sub insertData {
my $dbh_ref = shift;
my $metaid_ref = shift;
my $hop_ref = shift;
my $ip_ref = shift;
my $rtt_ref = shift;
my $insq = "INSERT INTO traceresults (traceroutes_id, hop, ip, rtt)
VALUES ($$metaid_ref, $$hop_ref, INET_ATON(\"$$ip_ref\"), $$rtt_ref)";
$$dbh_ref->do($insq);
}
#
# prepareData
# Use the data from the trace in the insertData function
# Input: reference to data returned from listHops(), database handler
# Output: exit status
#
sub prepareData {
my $data_ref = shift;
my $dbh_ref = shift;
my $id_ref = shift;
foreach my $hop ( keys(%$data_ref) ) {
my $data_query_ref = \%{$$data_ref{$hop}};
foreach my $query ( keys(%$data_query_ref) ) {
my $ip = \$$data_query_ref{$query}{'host'};
my $rtt = \$$data_query_ref{$query}{'time'};
insertData($dbh_ref, $id_ref, \$hop, $ip, $rtt);
}
}
}
#
# getFlowdHosts
# Get list of hosts to process from flowd from the last x minutes
# Input: reference to start time in epoch seconds
# Output: reference to an array with hosts to check
#
sub getFlowdHosts {
my $starttime_ref = shift;
my %rawHosts;
my @hosts;
my $cmd = "$flowdBin -cv $flowdFile |";
if ( $flowdSudo == 1 ) {
open(FLOWD, $sudoBin." ".$cmd);
} else {
open(FLOWD, $cmd);
}
while (<FLOWD>) {
my $line = $_;
if ( $line =~ m/^#|^LOGFILE/o ) {
next;
}
my @lineSplit = split(/,/, $line);
if ( $lineSplit[0] < $$starttime_ref ) {
next;
}
$rawHosts{$lineSplit[10]} = 0;
}
close(FLOWD);
foreach my $host ( keys(%rawHosts) ) {
push(@hosts, $host);
}
return(\@hosts);
}
#
# main
# Main routine of the program
# Input: config values at the top of the file
# Output: none
#
sub main {
my $target_ref = processInput(\$ARGV[0]);
print "Target: ".$$target_ref."\n" if $debugSwitch == 1;
my $trace_ref = setupTrace($target_ref);
my $data_ref = listHops($trace_ref);
my $dbh_ref = createMysqlConn(\$mysqlHost,\$mysqlUser,\$mysqlPass,\$mysqlName);
my $id_ref = insertMetaData($dbh_ref, $target_ref);
print "Trace ID: $$id_ref\n" if $debugSwitch == 1;
prepareData($data_ref, $dbh_ref, $id_ref);
closeMysqlConn($dbh_ref);
}
#main();
# newMain is the old main with the flowd list included
sub newMain {
# Get the start time of the analysis run
my $starttime_ref = getStartTime;
# Get a list of hosts we need to traceroute
my $hosts_ref = getFlowdHosts($starttime_ref);
# Create a MySQL connection
my $dbh_ref = createMysqlConn(\$mysqlHost,\$mysqlUser,\$mysqlPass,\$mysqlName);
# Start tracing these hosts (threading needed!)
foreach my $host (@$hosts_ref) {
# Create a trace object
my $trace_ref = setupTrace(\$host);
# Get a list of hops
my $data_ref = listHops($trace_ref);
# Insert metadata about the trace into the database
my $id_ref = insertMetaData($dbh_ref, \$host);
print "Trace ID: $$id_ref\n" if $debugSwitch == 1;
# Prepare and insert actual data into the database
prepareData($data_ref, $dbh_ref, $id_ref);
}
# Close connection to MySQL
closeMysqlConn($dbh_ref);
}
#newMain();
# threading stuff
# newMain + threading support
# We need a thread routine before we run the main program
sub thread {
my $host_ref = shift;
print "Thread ".threads->tid()." started\n" if $debugSwitchThreads == 1;
# Create a trace object
my $trace_ref = setupTrace($host_ref);
# Get a list of hops
my $data_ref = listHops($trace_ref);
# Create a MySQL connection (db connection cannot be used by multiple threads)
my $dbh_ref = createMysqlConn(\$mysqlHost,\$mysqlUser,\$mysqlPass,\$mysqlName);
# Insert metadata about the trace into the database
my $id_ref = insertMetaData($dbh_ref, $host_ref);
print "Trace ID: $$id_ref\n" if $debugSwitch == 1;
# Prepare and insert actual data into the database
prepareData($data_ref, $dbh_ref, $id_ref);
# Close connection to MySQL
closeMysqlConn($dbh_ref);
threads->exit(0);
}
sub threadMain {
# Get the start time of the analysis run
my $starttime_ref = getStartTime;
# Get a list of hosts we need to traceroute
my $hosts_ref = getFlowdHosts($starttime_ref);
print "Total number of hosts: ".scalar(@$hosts_ref)."\n" if $debugSwitchThreads == 1;
# Start tracing these hosts
foreach my $host (@$hosts_ref) {
my $thr = threads->create('thread', \$host);
print "Thread ".$thr->tid()." stack size: ".$thr->get_stack_size()."\n" if $debugSwitchThreads == 1;
}
# Wait for all threads to finish
while ( threads->list(threads::running) > 0 ) {
print "Number of running threads: ".scalar(threads->list(threads::running))."\n" if $debugSwitchThreads == 1;
my @joinable = threads->list(threads::joinable);
foreach my $thr (@joinable) {
$thr->join();
print "Thread ".$thr->tid()." joined\n" if $debugSwitchThreads == 1;
}
sleep(1);
}
}
threadMain();