This repository was archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnmap_api.pl
More file actions
689 lines (609 loc) · 21.4 KB
/
Copy pathnmap_api.pl
File metadata and controls
689 lines (609 loc) · 21.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#!/usr/bin/env perl
###############################################################################
#
# API para controle e execucao do NMAP
#
###############################################################################
#
# @ Manoel Domingues Junior mdjunior@ufrj.br
#
###############################################################################
###############################################################################
package Utils;
###############################################################################
use strict;
use warnings;
use Net::Syslog;
use Mojo::Log;
use Readonly;
use Scalar::Util::Numeric qw/isint/;
use Data::Validate::IP qw/is_ipv4 is_ipv6/;
###############################################################################
Readonly my $IPV6_BLOCO_MIN => 128;
Readonly my $IPV6_BLOCO_MAX => 128;
Readonly my $IPV4_BLOCO_MIN => 24;
Readonly my $IPV4_BLOCO_MAX => 32;
Readonly my $PORT_MIN => 1;
Readonly my $PORT_MAX => 65_535;
###############################################################################
my %error = (
'100' => 'Versao invalida!',
'101' => 'Colecao nao utilizada!',
'102' => 'Endereco IP invalido!',
'103' => 'Registro duplicado no banco!',
'104' => 'Arquivo XML invalido!',
'105' => 'Ja existem informacoes sobre esse host nesse timestamp!',
'106' => 'Documento sobre host duplicado no banco!',
'107' => 'Sem dados sobre host!',
'108' => 'Host nao possui esse scan associado!',
'109' => 'Bloco IP invalido!',
'110' => 'Porta invalida (fora do range permitido)!',
'111' => 'Porta invalida (nao eh inteiro)!',
'112' => 'Servico invalido (caracteres nao permitidos)!',
);
#######################################
# Funcao que loga
#######################################
sub log_wrapper {
my $log = shift;
if ($ENV{NMAP_API_LOG} eq 'LOCAL') {
my $log_local = Mojo::Log->new;
$log_local->info($log);
}
elsif ($ENV{NMAP_API_LOG} eq 'NET') {
my $log_net = Net::Syslog->new(
Name => 'NMAP_API',
Facility => 'local7',
Priority => 'debug',
SyslogPort => $ENV{NMAP_API_SYSLOG_PORT},
SyslogHost => $ENV{NMAP_API_SYSLOG_HOST},
);
$log_net->send($log, Priority => 'info');
}
return;
}
#######################################
# Funcao que retorna os erros
#######################################
sub error {
my $code = shift;
my $info = shift;
if (!defined $info) { $info = q{}; }
Utils::log_wrapper("code=|$code| desc=|$error{$code}| info=|$info|");
my %hash = (result => 'error', code => $code, message => $error{$code},);
return \%hash;
}
sub valida_bloco {
my $net = shift;
my $mask = shift;
if (!defined $mask) {
if (is_ipv4($net)) {
$mask = $IPV4_BLOCO_MAX;
}
elsif (is_ipv6($net)) {
$mask = $IPV6_BLOCO_MAX;
}
}
if (is_ipv4($net)) {
if ($mask > $IPV4_BLOCO_MAX || $mask < $IPV4_BLOCO_MIN) {
return Utils::error('109', "NET:$net MASK:$mask");
}
}
elsif (is_ipv6($net)) {
if ($mask > $IPV6_BLOCO_MAX || $mask < $IPV6_BLOCO_MIN) {
return Utils::error('109', "NET:$net MASK:$mask");
}
}
my %hash = (result => 'success',);
return \%hash;
}
sub valida_port {
my $port = shift;
if ($port < $PORT_MIN && $port > $PORT_MAX) {
return Utils::error('110', "PORT: $port");
}
elsif (!isint($port)) {
return Utils::error('111', "PORT: $port");
}
my %hash = (result => 'success',);
return \%hash;
}
sub valida_service {
my $service = shift;
if ($service !~ /[\w?-]+/smx) {
return Utils::error('112', "SERVICE: $service");
}
my %hash = (result => 'success',);
return \%hash;
}
###############################################################################
package Model;
###############################################################################
use strict;
use warnings;
use XML::Twig;
use Nmap::Parser;
use MongoDB;
use Mojo::JSON qw(j);
use Hash::Merge qw( merge );
use NetAddr::IP;
my $client = MongoDB::MongoClient->new(
host => $ENV{NMAP_API_MONGO_HOST},
port => $ENV{NMAP_API_MONGO_PORT}
);
my $db = $client->get_database($ENV{NMAP_API_DATABASE});
#######################################
# Funcao que conta a quantidade atuais de documentos em uma colecao
#######################################
sub count {
my $collection = shift;
my $count = $db->get_collection($collection)->count;
my %hash = (result => 'success', total => $count,);
return \%hash;
}
#######################################
# Funcao que retorna os hosts cadastrados
#######################################
sub get_hosts {
my $hosts = $db->run_command(
[
distinct => $ENV{NMAP_API_HOST_COLLECTION},
key => 'addr',
query => {}
]
);
return $hosts->{values};
}
#######################################
# Funcao que retorna os scans cadastrados
#######################################
sub get_scans {
my $scans = $db->run_command(
[
distinct => $ENV{NMAP_API_SCAN_COLLECTION},
key => 'timestamp',
query => {}
]
);
return $scans->{values};
}
#######################################
# Funcao que retorna informacoes sobre um host
#######################################
sub get_host_info {
my $addr = shift;
my $scan_number = shift;
my $port = shift;
my $service = shift;
my $hosts = $db->get_collection($ENV{NMAP_API_HOST_COLLECTION})
->find({addr => $addr});
my $num = $hosts->count;
if ($num > 1) {
return Utils::error('103', "IP:$addr HOSTS_NUM:$num");
}
elsif ($num < 1) {
return Utils::error('107', "IP:$addr HOSTS_NUM:$num");
}
my $doc = $hosts->next; # Guarda informacoes sobre host
# verifica se exixte numero de scan associado
if (defined $scan_number ) {
Utils::log_wrapper(
"function=|get_host_info| action=|using_parameter_scan| desc=|| addr=|$addr| scan_number=|$scan_number|"
);
}
else {
# Obtendo dados mais recentes de varredura
my @array_scans = reverse sort { $a <=> $b } @{$doc->{scans}};
$scan_number = $array_scans[0]; # Guarda scan mais recentes
Utils::log_wrapper(
"function=|get_host_info| action=|no_using_parameter_scan| desc=|| addr=|$addr| scan_number=|$scan_number|"
);
}
# buscando dados do scan no mongo
my $scans = $db->get_collection($ENV{NMAP_API_SCAN_COLLECTION})
->find({timestamp => "$scan_number"});
# verificando se somente um scan foi retornado
my $scan_count = $scans->count;
if ($scan_count != 1) {
return Utils::error('108', "IP:$addr SCAN:$scan_number");
}
my $scan = $scans->next; # carregando o scan
# separando os dados do addr passado para a funcao
my $scan_info;
foreach my $host (@{$scan->{hosts}}){
if ($host->{addr} eq $addr) {
$scan_info = $host;
}
}
# filtrando pelos criterios de porta/servico/nada
if (defined $port && defined $service) {
Utils::log_wrapper(
"function=|get_host_info| action=|using_parameters_port_service| desc=|| addr=|$addr| info=|PORT:$port SERVICE:$service|"
);
# conferindo se host tem porta e servico
foreach my $data (@{$scan_info->{data}}) {
if ($data->{port} eq $port && $data->{service} eq $service) {
return $scan_info;
}
}
}
elsif (defined $port) {
Utils::log_wrapper(
"function=|get_host_info| action=|using_parameter_port| desc=|| addr=|$addr| info=|PORT:$port|"
);
# conferindo se host tem porta
foreach my $data (@{$scan_info->{data}}) {
if ($data->{port} eq $port) {
return $scan_info;
}
}
}
elsif (defined $service) {
Utils::log_wrapper(
"function=|get_host_info| action=|using_parameter_service| desc=|| addr=|$addr| info=|SERVICE:$service|"
);
# conferindo se host tem porta e servico
foreach my $data (@{$scan_info->{data}}) {
if ($data->{service} eq $service) {
return $scan_info;
}
}
}
else {
Utils::log_wrapper(
"function=|get_host_info| action=|no_using_parameter| desc=|| addr=|$addr| info=||"
);
# nao tem nenhum criterio, so retornar informacao do host
return $scan_info;
}
return {'result' => 'not found'};
}
#######################################
# Funcao que retorna informacoes sobre uma rede
#######################################
sub net {
my ($net, $mask, $port, $service) = @_;
my %net_info; # Hash para guardar as informações da rede
my $SLASH = q{/};
my $n = NetAddr::IP->new($net . $SLASH . $mask);
Utils::log_wrapper(
"function=|net| action=|list| desc=|| info=|$net $mask|");
my $bits = $n->bits();
for my $ip (@{$n->splitref($bits)}) { # laco com cada IP da rede
my $host_hash = get_host_info($ip->addr, undef, $port, $service);
if (!defined $host_hash->{result}) {
$net_info{$ip->addr} = $host_hash;
}
}
# verificando numero de resultados
my $hash_size = scalar keys %net_info;
Utils::log_wrapper(
"function=|net| action=|result| desc=|| info=|$hash_size|");
return \%net_info;
}
#######################################
# Funcao que gera as estruturas para importacao de dados
# Atencao: Essa funcao chama outras para armazenar as estruturas no banco
#######################################
sub import {
my $xml = shift;
# Validando XML
my $twig = XML::Twig->new();
my $res = $twig->safe_parse($xml);
if (!defined $res) {
$twig->purge;
return Utils::error('104', $xml);
}
$twig->purge;
# Parseando com o NMAP
my $nmap = Nmap::Parser->new();
$nmap->parse($xml);
# Informacoes do SCAN
my $session = $nmap->get_session();
my %session_info = (
timestamp => $session->start_time(),
finish_time => $session->finish_time(),
scan_version => $session->nmap_version(),
num_services => $session->numservices(),
scan_types => $session->scan_types(),
);
# Para cada host
my @hosts;
for my $host ($nmap->all_hosts()) {
my $os = $host->os_sig;
my @hostnames = $host->all_hostnames();
my @services;
# TCP
for my $svcs ($host->tcp_open_ports()) {
my $svc = $host->tcp_service($svcs);
my %additional_info;
for my $nse_script_name ($svc->scripts()) {
# Sanitizando nome que nao pode conter pontos
my $new_script_name = $nse_script_name;
$new_script_name =~ s/[.]/_/smxg;
$additional_info{$new_script_name}
= $svc->scripts($nse_script_name);
}
my %service_info = (
service => $svc->name(),
proto => 'tcp',
port => $svc->port(),
product => $svc->product(),
fingerprint => $svc->fingerprint(),
version => $svc->version(),
rpc => $svc->rpcnum(),
additional_info => \%additional_info,
);
push @services, \%service_info;
}
# UDP
for my $svcs ($host->udp_open_ports()) {
my $svc = $host->udp_service($svcs);
my %additional_info;
for my $nse_script_name ($svc->scripts()) {
# Sanitizando nome que nao pode conter pontos
my $new_script_name = $nse_script_name;
$new_script_name =~ s/[.]/_/smxg;
$additional_info{$new_script_name}
= $svc->scripts($nse_script_name);
}
my %service_info = (
service => $svc->name(),
proto => 'udp',
port => $svc->port(),
product => $svc->product(),
fingerprint => $svc->fingerprint(),
version => $svc->version(),
rpc => $svc->rpcnum(),
additional_info => \%additional_info,
);
push @services, \%service_info;
}
# Informacoes agregadas
my %host_info = (
hostnames => \@hostnames,
status => $host->status(),
addr => $host->addr(),
os => $os->name(),
os_family => $os->osfamily(),
status => $host->status(),
mac_addr => $host->mac_addr(),
distance => $host->distance(),
mac_vendor => $host->mac_vendor(),
uptime => $host->uptime_seconds(),
data => \@services,
);
push @hosts, \%host_info;
# Atualizando colecao de hosts
my %update_info = (
addr => $host_info{addr},
scan_timestamp => $session_info{timestamp},
status => $host_info{status},
os => $os->name(),
hostnames => \@hostnames,
);
my $result = add_scan_in_host(\%update_info);
Utils::log_wrapper(
"function=|import| action=|add_scan_in_host| desc=|adicionando scan a host| info=|IP:$host_info{addr} RESULT:$result->{result}|"
);
}
# Atualizando colecao de scans
$session_info{hosts} = \@hosts; # Guarda na collection scans
my $result = add_scan(\%session_info);
Utils::log_wrapper(
"function=|import| action=|add_scan| desc=|adicionando scan a colecao| info=|TIMESTAMP:$session_info{timestamp} RESULT:$result->{result}|"
);
return $result;
}
#######################################
# Funcao que recebe os dados de um scan e armazena no banco
#######################################
sub add_scan {
my $scan_info = shift;
my $scan_str = j($scan_info);
$scan_str =~ s/\n//smxg;
Utils::log_wrapper(
"function=|add_scan| action=|add_scan| desc=|adicionando scan a colecao| info=|$scan_str|"
);
my $scan = $db->get_collection($ENV{NMAP_API_SCAN_COLLECTION});
$scan->insert($scan_info);
my %result = (result => 'success',);
return \%result;
}
#######################################
# Funcao que recebe os dados de um host e armazena no banco
#######################################
#######################################
sub add_scan_in_host {
my $update_info = shift;
# Estrutura do update_info
# %update_info = (
# addr => $,
# scan_timestamp => $,
# status => $,
# os => $,
# hostnames => \@,
# );
# Verificando se ja existe registro do host
my $hosts = $db->get_collection($ENV{NMAP_API_HOST_COLLECTION})
->find({addr => $update_info->{addr}});
my $num = $hosts->count;
if ($num == 0) {
MongoDB::force_int($update_info->{scan_timestamp});
my %hash = (
addr => $update_info->{addr},
status => $update_info->{status},
os => $update_info->{os},
hostnames => $update_info->{hostnames},
scans => [$update_info->{scan_timestamp}],
);
my $host = $db->get_collection($ENV{NMAP_API_HOST_COLLECTION});
Utils::log_wrapper(
"function=|add_scan_in_host| action=|add_host| desc=|adicionando host| info=|IP:$update_info->{addr}|"
);
$host->insert(\%hash);
my %result = (result => 'success',);
return \%result;
}
elsif ($num == 1) {
my $doc = $hosts->next;
my @array_scans = reverse sort { $a <=> $b } @{$doc->{scans}};
if ($array_scans[0] < $update_info->{scan_timestamp})
{ # Se o scan eh mais atual que os existentes
my @scans = @array_scans;
push @scans, scalar $update_info->{scan_timestamp};
my %hash = (
addr => $update_info->{addr},
status => $update_info->{status},
os => $update_info->{os},
hostnames => $update_info->{hostnames},
scans => \@scans,
);
my $host = $db->get_collection($ENV{NMAP_API_HOST_COLLECTION});
Utils::log_wrapper(
"function=|add_scan_in_host| action=|update_host_all| desc=|atualizando host| info=|IP:$update_info->{addr}|"
);
$host->update({'addr' => $update_info->{addr}},
{'$set' => \%hash});
my %result = (result => 'success',);
return \%result;
}
elsif ($array_scans[0] > $update_info->{scan_timestamp})
{ # Se o scan eh mais antigo que os existentes
my $host = $db->get_collection($ENV{NMAP_API_HOST_COLLECTION});
Utils::log_wrapper(
"function=|add_scan_in_host| action=|update_host_scan_info| desc=|atualizando informacao de scan| info=|IP:$update_info->{addr}|"
);
$host->update({'addr' => $doc->{addr}},
{'$push' => {'scans' => $update_info->{scan_timestamp}}});
my %result = (result => 'success',);
return \%result;
}
else { # Se ja existe um scan com mesmo timestamp entre existentes
return Utils::error('105',
"IP:$update_info->{addr} TIMESTAMP:$update_info->{scan_timestamp}"
);
}
}
else {
return Utils::error('106', "IP:$update_info->{addr} HOSTS_NUM:$num");
}
}
###############################################################################
package Main;
###############################################################################
use strict;
use warnings;
use Mojolicious::Lite;
use Mojo::JSON;
use Mojo::Headers;
our $VERSION = 1.0;
get '/api/#version/:collection/count' => sub {
my $self = shift;
if ($self->param('version') != $VERSION) {
$self->render(json => Utils::error('100', $self->param('version')));
return;
}
if ( $self->param('collection') ne $ENV{NMAP_API_HOST_COLLECTION}
&& $self->param('collection') ne $ENV{NMAP_API_SCAN_COLLECTION})
{
$self->render(json => Utils::error('101', $self->param('collection')));
return;
}
$self->render(json => Model::count($self->param('collection')));
};
get '/api/#version/hosts' => sub {
my $self = shift;
$self->render(json => Model::get_hosts());
return;
};
get '/api/#version/hosts/#addr' => sub {
my $self = shift;
if ($self->param('version') != $VERSION) {
$self->render(json => Utils::error('100', $self->param('version')));
return;
}
my $validacao = Utils::valida_bloco($self->param('addr'));
if ($validacao->{result} ne 'success') {
$self->render(json => $validacao);
return;
}
if (defined $self->param('scan')) {
$self->render(
json => Model::get_host_info(
$self->param('addr'),
$self->param('scan')
)
);
return;
}
$self->render(json => Model::get_host_info($self->param('addr')));
};
get '/api/#version/net/#addr/:mask' => sub {
my $self = shift;
my $port = $self->param('port');
my $service = $self->param('service');
if ($self->param('version') != $VERSION) {
$self->render(json => Utils::error('100', $self->param('version')));
return;
}
my $validacao
= Utils::valida_bloco($self->param('addr'), $self->param('mask'));
if ($validacao->{result} ne 'success') {
$self->render(json => $validacao);
return;
}
if (defined $self->param('port')) {
my $validacao_port = Utils::valida_port($self->param('port'));
if ($validacao_port->{result} ne 'success') {
$self->render(json => $validacao_port);
return;
}
}
else { $port = undef; }
if (defined $self->param('service')) {
my $validacao_service = Utils::valida_service($self->param('service'));
if ($validacao_service->{result} ne 'success') {
$self->render(json => $validacao_service);
return;
}
}
else { $service = undef; }
$self->render(
json => Model::net(
$self->param('addr'),
$self->param('mask'),
$port, $service
)
);
};
get '/api/#version/scans' => sub {
my $self = shift;
$self->render(json => Model::get_scans());
return;
};
put '/api/#version/scans' => sub {
my $self = shift;
if ($self->param('version') != $VERSION) {
$self->render(json => Utils::error('100', $self->param('version')));
return;
}
$self->render(json => Model::import($self->req->body), status => 201);
};
app->config(
hypnotoad => {
listen => ["$ENV{NMAP_API_URL}"],
workers => $ENV{NMAP_API_WORKERS},
clients => 1, # is a blocking API
lock_file => 'run/nmap_api.lock',
pid_file => 'run/nmap_api.pid',
user => $ENV{NMAP_API_USER},
group => $ENV{NMAP_API_GROUP},
}
);
app->start;
__DATA__
@@ exception.html.ep
{"result": "error"}
@@ not_found.html.ep
{"result": "error"}