/usr/bin/scanpbnj is in pbnj 2.04-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 | #!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if 0; # not running under some shell
#
# Copyright (C) 2005-2006 Joshua D. Abraham (jabra@ccs.neu.edu)
#
# This program is released under the terms of the GNU General Public License
# (GPL), which is distributed with this software in the file "COPYING".
# The GPL specifies the terms under which users may copy and use this software.
#
# PBNJ 2.0
# (P)orts (B)anners N' (J)unk
#
# Author: Joshua D. Abraham
# Date: March 15, 2006
# Updated: November 15, 2006
# Version: 2.04
#
# ScanPBNJ - a program for running Nmap scans and storing the results in
# a PBNJ 2.0 database.
#
use strict;
use warnings;
use Shell;
use DBI;
use Nmap::Parser;
use Socket;
use Net::hostent;
use Sys::Hostname;
use Getopt::Long;
use FileHandle;
use YAML;
use File::Which;
use Term::ANSIColor qw(:constants);
use File::HomeDir;
use POSIX;
$Term::ANSIColor::AUTORESET = 1;
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//; # Truncate calling path from the prog name
my $AUTH = 'Joshua D. Abraham'; # author
my $VERSION = '2.04'; # version
my $np = new Nmap::Parser; # parser object
my $type = 'help'; # parse scan or file
my @ipRange; # ip Address or ip range to scan
my $iplist; # ip list file
my $xmlFile; # xml file to parse
my $Range = '1-1025'; # port range to scan
my $args = "-vv -O -P0 "; # scan args
my $scantype = "-sSV"; # default scantype
my $cargs = 'no'; # user changed args
my $outputdir = '.'; # output database directory
my $nmapPath = which('nmap'); # nmap path
my $interface = ""; # default interface to perform scan
my %options; # getopts hash
my $dbh; # database connection
my $dbconfig = 'config.yaml'; # database config
my $database; # database file
my $db; # db backend
my $hostname; # db host ip
my $port; # db port
my $user; # db username
my $passwd; # db password
my $datadb; # results from the config of the db
my $dir = "."; # dir of the config for the db
my $colors = 1; # print using colors
my $diff = 'version';
$options{test} = 0; # testing flag
$options{debug} = 0; # debug flag
my $configdir;
#chown() that reports errors
sub safe_chown {
my $uid = shift;
my $gid = shift;
my $file = shift;
if ( chown( $uid, $gid, $file ) != 1 ) {
error( 'Unable to change the owner of the file ' . $file . '.'
. "\n\n" );
}
}
if ( $^O eq 'MSWin32' || $^O =~ /cygwin/ ) {
$colors = 0;
require Win32;
import Win32;
Win32->import(qw(CSIDL_APPDATA));
my $dir = Win32::GetFolderPath( CSIDL_APPDATA() );
# 2.0
$configdir = $dir . "\\" . "pbnj-2.0";
if ( -e $configdir and -d $configdir ) {
if ( $options{test} > 2 ) {
print "$configdir exists\n";
}
}
else {
mkdir $configdir;
print "mkdir $configdir\n";
}
if ( !-e $dbconfig ) {
$dbconfig = "$configdir\\$dbconfig";
# check if the config exists in ~/.pbnj-2.0/config.yaml
if ( !-e $dbconfig ) {
open( CONFIG, ">$dbconfig" );
while (<DATA>) {
if ( defined($_) ) {
print CONFIG $_;
}
}
close(CONFIG);
print "$dbconfig generated\n";
}
}
}
else {
$configdir = File::HomeDir->my_home;
my $tmpuser = $configdir;
$configdir .= "/.pbnj-2.0";
$tmpuser =~ s/home//;
$tmpuser =~ s/\///g;
my $uid = getpwnam($tmpuser);
my $gid = id("-g $tmpuser");
if ( !defined($gid) ) {
error("gid not defined\n");
}
if ( !defined($uid) ) {
error("uid not defined\n");
}
if ( -e $configdir and -d $configdir ) {
if ( $options{test} > 2 ) {
print "$configdir exists\n";
}
}
else {
umask 077;
mkdir $configdir;
print "mkdir $configdir\n";
safe_chown( $uid, $gid, $configdir );
}
# check if config exists in the current directory
if ( !-e $dbconfig ) {
$dbconfig = "$configdir/$dbconfig";
# check if the config exists in ~/.pbnj-2.0/config.yaml
if ( !-e $dbconfig ) {
umask 077;
open( CONFIG, ">$dbconfig" );
while (<DATA>) {
if ( defined($_) ) {
print CONFIG $_;
}
}
close(CONFIG);
safe_chown( $uid, $gid, $dbconfig );
print "$dbconfig generated\n";
}
}
}
##############################################################################
#
# createMachinesTable: scalar -> scalar
# creates the table for machines in the database
# mid INTEGER PRIMARY KEY,
#
##############################################################################
sub createMachinesTable {
my $dbh = shift;
my $auto_increment = "AUTO_INCREMENT";
my $mid_type;
if ( $db eq "SQLite" ) {
$auto_increment = "AUTOINCREMENT";
$mid_type = "INTEGER PRIMARY KEY $auto_increment";
}
elsif ( $db eq "mysql" or $db eq "CSV" ) {
$mid_type = "INTEGER PRIMARY KEY";
}
elsif ( $db eq "Pg" ) {
$mid_type = "SERIAL PRIMARY KEY";
}
else {
print "db not supported\n";
exit 1;
}
eval {
$dbh->do(
"CREATE TABLE machines (
mid $mid_type,
ip TEXT,
host TEXT,
localh INTEGER,
os TEXT,
machine_created TEXT,
created_on TEXT)"
);
};
if ($@) {
return 0;
}
return 1;
}
##############################################################################
#
# createService: scalar -> scalar
# creates the table for services in the database
#
##############################################################################
sub createServicesTable {
my $dbh = shift;
#sid INTEGER PRIMARY KEY,
eval {
$dbh->do(
"CREATE TABLE services (
mid INTEGER,
service TEXT,
state TEXT,
port INTEGER,
protocol TEXT,
version TEXT,
banner TEXT,
machine_updated TEXT,
updated_on TEXT)"
);
};
if ($@) {
return 0;
}
return 1;
}
##############################################################################
#
# insertService: anoy hash -> scalar
# inserts information about a service into the database
#
##############################################################################
sub insertService {
my ($href) = @_;
die "insertService: mid not defined" unless defined $href->{mid};
die "insertService: service not defined" unless defined $href->{service};
die "insertService: state not defined" unless defined $href->{state};
die "insertService: port not defined" unless defined $href->{port};
die "insertService: proto not defined" unless defined $href->{proto};
die "insertService: ver not defined" unless defined $href->{ver};
die "insertService: banner not defined" unless defined $href->{banner};
die "insertService: updated not defined" unless defined $href->{updated};
die "insertService: machine_updated not defined"
unless defined $href->{machine_updated};
my $mid = $href->{mid};
my $service = $href->{service};
my $state = $href->{state};
my $port = $href->{port};
my $proto = $href->{proto};
my $ver = $href->{ver};
my $banner = $href->{banner};
my $updated = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $insert
= $dbh->prepare('INSERT INTO services VALUES (?,?,?,?,?,?,?,?,?)');
my $success = 1;
$success &&= $insert->execute(
$mid, $service, $state,
$port, $proto, $ver,
$banner, $machine_updated, $updated
);
return $success;
}
##############################################################################
#
# setLocal: anoy hash -> scalar
# set the localhost bit
#
##############################################################################
sub setLocal {
my ($href) = @_;
die "setLocal: ip not defined" unless defined $href->{ip};
die "setLocal: hostname not defined" unless defined $href->{hostname};
my $ip = $href->{ip};
my $hostname = $href->{hostname};
my $localh = 0;
if ( $hostname =~ m/localhost/ and $ip eq "127.0.0.1" ) {
if ( $options{test} > 0 or $options{debug} > 1 ) {
print "localhost scan\n";
}
$localh = 1;
}
return $localh;
}
##############################################################################
#
# insertMachine: anoy hash -> scalar
# inserts information about a machine into the database
#
##############################################################################
sub insertMachine {
my ($href) = @_;
die "insertMachine: ip not defined" unless defined $href->{ip};
die "insertMachine: host not defined" unless defined $href->{host};
die "insertMachine: localh not defined" unless defined $href->{localh};
die "insertMachine: os not defined" unless defined $href->{os};
die "insertMachine: created not defined" unless defined $href->{created};
die "insertMachine: machine_created not defined"
unless defined $href->{machine_created};
my $ip = $href->{ip};
my $host = $href->{host};
my $localh = $href->{localh};
my $os = $href->{os};
my $created = $href->{created};
my $machine_created = $href->{machine_created};
my $sth = $dbh->selectrow_hashref("select max(mid) from machines");
my $mid;
if ($db eq 'Pg') {
$mid = $sth->{'max'} ;
}
else {
$mid = $sth->{'max(mid)'};
}
# new mid needs to be 1 more than the max
$mid++;
my $insert = $dbh->prepare(
'INSERT INTO machines VALUES (?,?,?,?,?,?,?)');
my $success = 1;
$success &&= $insert->execute( $mid, $ip, $host, $localh, $os,
$machine_created, $created );
return $mid;
}
##############################################################################
#
# flushTables: ->
# flush the machines and services tables
#
##############################################################################
sub flushTables {
print "flushing tables\n";
$dbh->do("DROP TABLE machines");
$dbh->do("DROP TABLE services");
}
##############################################################################
#
# addHost: anoy hash -> scalar
# insert a host only if it is uniq
#
##############################################################################
sub addHost {
my ($href) = @_;
die "addHost: os not defined" unless defined $href->{os};
die "addHost: session not defined" unless defined $href->{session};
die "addHost: host not defined" unless defined $href->{host};
my $os = $href->{os};
my $session = $href->{session}; #a Nmap::Parser::Session object
my $host = $href->{host};
my $mid;
my $ip = $host->addr;
my $machine_created = $session->finish_time();
my $human_time = localtime($machine_created);
my $uniqhost = 0;
my $hostname = $host->hostname;
my $localh = setLocal( { ip => $ip, hostname => $host->hostname } );
my $uniq = $dbh->selectall_arrayref(
"select mid from
machines WHERE ip='$ip' AND os='$os' AND host='$hostname'
AND localh='$localh'"
);
if ( $options{test} > 1 ) {
print "uniq is $uniq\n";
}
foreach my $uniqtmp (@$uniq) {
my ($midtmp) = @$uniqtmp;
$uniqhost = 1;
$mid = $midtmp;
if ( $options{test} > 0 or $options{debug} > 1 ) {
print "non unique host\n";
print "mid shift is $mid\n";
}
}
if ( $uniqhost eq 0 ) {
if ( $colors eq 1 ) {
print BOLD GREEN "Inserting Machine\n";
}
else {
print "Inserting Machine\n";
}
$mid = insertMachine(
{ ip => $ip,
host => $hostname,
localh => $localh,
os => $os,
created => $human_time,
machine_created => $machine_created
}
);
if ( $options{test} > 0 or $options{debug} > 1 ) {
;
print "mid is $mid\n";
}
}
else {
print "Machine is already in the database\n";
print "Checking Current Services\n";
}
return ( $mid, $uniqhost );
}
##############################################################################
#
# insertServiceDB: anoy hash ->
# take information about a services from the database and insert it as
# as being down
#
##############################################################################
sub insertServiceDB {
my ($href) = @_;
die "insertServiceDB: mid not defined" unless defined $href->{mid};
die "insertServiceDB: port not defined" unless defined $href->{port};
die "insertServiceDB: state not defined" unless defined $href->{state};
die "insertServiceDB: proto not defined" unless defined $href->{proto};
die "insertSerciceDB: updated not defined"
unless defined $href->{updated};
die "insertSerciceDB: machine_updated not defined"
unless defined $href->{machine_updated};
my $mid = $href->{mid};
my $port = $href->{port};
my $state = $href->{state};
my $proto = $href->{proto};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $uniq_service = $dbh->selectall_arrayref(
"SELECT
service,version,protocol,banner
FROM services WHERE mid='$mid' AND port='$port'
AND protocol='$proto' AND state='up'
ORDER BY machine_updated DESC"
);
my ( $version, $service, $banner, $uniqservice );
foreach my $uniqtmp (@$uniq_service) {
( $service, $version, $proto, $banner ) = @$uniqtmp;
#print "service is $service\n";
}
if ( $colors eq 1 ) {
print BOLD RED "\t! Service $port:$proto $service is down\n";
}
else {
print "\t! Service $port:$proto $service is down\n";
}
insertService(
{ mid => $mid,
service => $service,
port => $port,
proto => $proto,
ver => $version,
state => $state,
banner => $banner,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
##############################################################################
#
# insertServiceScan: anoy hash ->
# take information about a services from the scan and insert it as
# as being up
#
##############################################################################
sub insertServiceScan {
my ($href) = @_;
die "insertServiceScan: host not defined" unless defined $href->{host};
die "insertServiceScan: mid not defined" unless defined $href->{mid};
die "insertServiceScan: port not defined" unless defined $href->{port};
die "insertServiceScan: state not defined" unless defined $href->{state};
die "insertServiceScan: proto not defined" unless defined $href->{proto};
die "insertServiceScan: change not defined"
unless defined $href->{change};
die "insertSerciceScan: updated not defined"
unless defined $href->{updated};
die "insertSerciceScan: machine_updated not defined"
unless defined $href->{machine_updated};
my $host = $href->{host};
my $mid = $href->{mid};
my $port = $href->{port};
my $state = $href->{state};
my $proto = $href->{proto};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $change = $href->{change};
my $service;
if ( $proto eq "tcp" ) {
$service = $host->tcp_service($port);
}
else {
$service = $host->udp_service($port);
}
my $name = "unknown name";
my $product = "unknown product";
my $version = "unknown version";
if ( defined( $service->name ) ) {
$name = $service->name;
}
if ( defined( $service->version ) ) {
$version = $service->version;
}
if ( defined( $service->product ) ) {
$product = $service->product;
}
if ( $change == 1 ) {
if ( $colors == 1 ) {
print BOLD GREEN "\tInserting Service on $port:$proto $name\n";
}
else {
print "\tInserting Service on $port:$proto $name\n";
}
}
insertService(
{ mid => $mid,
service => $name,
port => $port,
proto => $proto,
ver => $version,
state => $state,
banner => $product,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
##############################################################################
#
# cmpService: anoy hash -> scalar
# compare the service in the database and the service from the scan.
# Note:
# If the service is up the database & is not in the scan then the state is down.
# If the service is not in the database & is in the scan then the state is up.
#
##############################################################################
sub cmpService {
my ($href) = @_;
die "cmpService: host not defined" unless defined $href->{host};
die "cmpService: mid not defined" unless defined $href->{mid};
die "cmpService: port not defined" unless defined $href->{port};
die "cmpService: state not defined" unless defined $href->{state};
die "cmpService: proto not defined" unless defined $href->{proto};
die "cmpService: updated not defined" unless defined $href->{updated};
die "cmpService: machine_updated not defined"
unless defined $href->{machine_updated};
die "cmpService: db_updated not defined"
unless defined $href->{db_updated};
die "cmpService: db_machine_updated not defined"
unless defined $href->{db_machine_updated};
my $host = $href->{host};
my $mid = $href->{mid};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $state = $href->{state};
my $port = $href->{port};
my $proto = $href->{proto};
my $db_updated = $href->{db_updated};
my $db_machine_updated = $href->{db_machine_updated};
my $service;
if ( $proto eq "tcp" ) {
$service = $host->tcp_service($port);
}
elsif ( $proto eq "udp" ) {
$service = $host->udp_service($port);
}
else {
print "error in $proto\n";
exit 1;
}
my $banner_from_scan = "unknown product";
my $version_from_scan = "unknown version";
my $port_from_scan = $service->port;
my $service_name_from_scan = "unknown service";
if ( defined( $service->name ) ) {
$service_name_from_scan = $service->name;
}
if ( $options{test} eq 1 ) {
print "service name from scan is $service_name_from_scan\n";
}
if ( defined( $service->version ) ) {
$version_from_scan = $service->version;
if ( $options{test} > 2 ) {
print "version from scan is $version_from_scan\n";
}
}
if ( defined( $service->product ) ) {
$banner_from_scan = $service->product;
if ( $options{test} > 2 ) {
print "banner from scan is $banner_from_scan\n";
}
}
my ( $version_from_db, $service_name_from_db, $banner_from_db );
# note the version banner and service name are the only elements
# of the service that we can comparing
my $uniq_service = $dbh->selectall_arrayref(
"SELECT
service,version,banner FROM services
WHERE mid='$mid' AND
port='$port' AND state='$state'
AND protocol='$proto'
AND updated_on='$db_updated'
AND machine_updated='$db_machine_updated'"
);
foreach my $uniqtmp (@$uniq_service) {
my ( $nametmp, $vertmp, $bantmp ) = @$uniqtmp;
$service_name_from_db = $nametmp;
$version_from_db = $vertmp;
$banner_from_db = $bantmp;
}
if ( $options{test} > 1 ) {
print "state is $state\n";
print
"comparing service $service_name_from_db and $service_name_from_scan\n";
print "comparing version $version_from_db and $version_from_scan\n";
print "comparing banner $banner_from_db and $banner_from_scan\n";
}
if ( $diff eq "banner"
&& $version_from_db eq $version_from_scan
&& $banner_from_db eq $banner_from_scan
&& $service_name_from_db eq $service_name_from_scan )
{
print
"\t= $service_name_from_db:$port is ($version_from_db) $banner_from_db\n";
return 1;
}
elsif ($diff ne "banner"
&& $version_from_db eq $version_from_scan
&& $service_name_from_db eq $service_name_from_scan )
{
print
"\t= $service_name_from_db:$port is ($version_from_db) $banner_from_db\n";
return 1;
}
else {
if ( $colors == 1 ) {
print BOLD YELLOW
"\t! $service_name_from_db:$port is ($version_from_db) $banner_from_db\n";
}
else {
print
"\t! $service_name_from_db:$port is ($version_from_db) $banner_from_db\n";
}
if ( $service_name_from_db ne $service_name_from_scan ) {
if ( $colors == 1 ) {
print BOLD YELLOW
" Service Name was \"$service_name_from_db\" changed to \"$service_name_from_scan\"\n";
}
else {
print
" Service Name was \"$service_name_from_db\" changed to \"$service_name_from_scan\"\n";
}
}
elsif ( $version_from_db ne $version_from_scan ) {
if ( $colors == 1 ) {
print BOLD YELLOW
" Version was \"$version_from_db\" changed to \"$version_from_scan\"\n";
}
else {
print
" Version was \"$version_from_db\" changed to \"$version_from_scan\"\n";
}
}
elsif ( $diff eq 'banner' and $banner_from_db ne $banner_from_scan ) {
if ( $colors == 1 ) {
print BOLD YELLOW
" Banner was \"$banner_from_db\" changed to \"$banner_from_scan\"\n";
}
else {
print
" Banner was \"$banner_from_db\" changed to \"$banner_from_scan\"\n";
}
}
else {
print "version or banner difference\n";
}
# set time to earlier so insert works after old verison is down
$machine_updated = scalar($machine_updated);
$machine_updated -= 1;
insertService(
{ mid => $mid,
service => $service_name_from_db,
port => $port,
proto => $proto,
ver => $version_from_db,
state => 'down',
banner => $banner_from_db,
updated => $human_time,
machine_updated => $machine_updated,
}
);
return 0;
}
return 1;
}
##############################################################################
#
# setAndInsertService: anoy hash -> scalar
# store all the services in the scan into the database
#
##############################################################################
sub setAndInsertService {
my ($href) = @_;
die "setAndInsertService: mid not defined" unless defined $href->{mid};
die "setAndInsertService: proto not defined"
unless defined $href->{proto};
die "setAndInsertService: service not defined"
unless defined $href->{service};
die "setAndInsertService: updated not defined"
unless defined $href->{updated};
die "setAndInsertService: machine_updated not defined"
unless defined $href->{machine_updated};
my $mid = $href->{mid};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $service = $href->{service};
my $proto = $href->{proto};
my $state = 'up';
my $product_from_scan = "unknown product";
my $version_from_scan = "unknown version";
my $port_from_scan = $service->port;
my $name_from_scan = "unknown service";
if ( defined( $service->name ) ) {
$name_from_scan = $service->name;
}
if ( defined( $service->version ) ) {
$version_from_scan = $service->version;
}
if ( defined( $service->product ) ) {
$product_from_scan = $service->product;
}
print "Inserting Service on $port_from_scan:$proto $name_from_scan\n";
insertService(
{ mid => $mid,
service => $name_from_scan,
port => $port_from_scan,
proto => $proto,
ver => $version_from_scan,
state => $state,
banner => $product_from_scan,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
##############################################################################
#
# storeEverything: anoy hash -> scalar
# store all the services in the scan into the database
#
##############################################################################
sub storeEverything {
my ($href) = @_;
die "storeEverything: host not defined" unless defined $href->{host};
die "storeEverything: mid not defined" unless defined $href->{mid};
die "storeEverything: updated not defined"
unless defined $href->{updated};
die "storeEverything: machine_updated not defined"
unless defined $href->{machine_updated};
my $host = $href->{host};
my $mid = $href->{mid};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $state;
# $port is up in the database
# store all the services running on tcp ports
foreach ( $host->tcp_ports('open') ) {
my $state = $host->tcp_port_state($_);
if ( $options{test} eq 1 ) {
print "state is $state on port $_\n";
}
next if ( $state ne "open" );
my $service = $host->tcp_service($_);
next if ( !defined($service) );
#my $uniqservice = 1;
setAndInsertService(
{ proto => 'tcp',
service => $service,
mid => $mid,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
# store all the services running on tcp ports
foreach ( $host->udp_ports('open') ) {
my $state = $host->udp_port_state($_);
if ( $options{test} eq 1 ) {
print "in udp loop\n";
print "state is $state on port $_\n";
}
next if ( $state ne "open" );
my $service = $host->udp_service($_);
next if ( !defined($service) );
setAndInsertService(
{ proto => 'udp',
service => $service,
mid => $mid,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
return 0;
}
##############################################################################
#
# sortDB: anoy hash -> hash
# sort the database based on the time
#
##############################################################################
sub sortDB {
my ($href) = @_;
die "sortDB: mid not defined" unless defined $href->{mid};
die "sortDB: updated not defined" unless defined $href->{updated};
die "sortDB: machine_updated not defined"
unless defined $href->{machine_updated};
my $mid = $href->{mid};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my ( $port, $service, $banner, $state, $state_db );
# determines all ports that are in the database and not in the scan
# thus we assume these services went down
# may 30 added limit 1
my ( %port_db, %seen_ports, $scan_state, $scan_proto );
my $ports_db_ref = $dbh->selectall_arrayref(
"SELECT
port,state,protocol,machine_updated,updated_on FROM
services WHERE mid='$mid' ORDER BY machine_updated DESC "
);
foreach my $key (@$ports_db_ref) {
my ( $port, $state, $proto, $machine_updated, $updated ) = @$key;
if ( !$port_db{$port}{machine_updated} ) {
$port_db{$port}{machine_updated} = $machine_updated;
$port_db{$port}{updated} = $updated;
$port_db{$port}{port} = $port;
$port_db{$port}{state} = $state;
$port_db{$port}{proto} = $proto;
}
else {
#print "not less than and exists\n";
}
}
return %port_db;
}
##############################################################################
#
# setDown: anoy hash ->
# remove all services on a given protocol as the host has no ports on
# this protocol.
#
##############################################################################
sub setDown {
my ($href) = @_;
die "setDown: mid not defined" unless defined $href->{mid};
die "setDown: host not defined" unless defined $href->{host};
die "setDown: proto not defined" unless defined $href->{proto};
die "setDown: portdb not defined" unless defined $href->{portdb};
die "setDown: updated not defined" unless defined $href->{updated};
die "setDown: machine_updated not defined"
unless defined $href->{machine_updated};
my $host = $href->{host};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $mid = $href->{mid};
my $proto = $href->{proto};
my $db_ref = $href->{portdb};
my %portdb = %{$db_ref};
if ( $options{test} > 0 ) {
print "protocol is $proto\n";
}
my ( $scan_state, %seen_ports );
foreach my $port ( sort { $a cmp $b } keys %portdb ) {
#print "key is $port\n";
if ( $options{test} eq 1 ) {
print $portdb{$port}{state} . " and $port\n";
print "port db $port proto $proto\n";
}
$scan_state = "down";
if ( $options{test} eq 1 ) {
print "scan thinks the status of $port is $scan_state\n";
print "state is " . $portdb{$port}{state} . "\n";
}
if ( $scan_state eq "down"
&& $portdb{$port}{'state'} eq "up"
&& $portdb{$port}{proto}
&& $portdb{$port}{proto} eq $proto )
{
insertServiceDB(
{ mid => $mid,
port => $port,
state => $scan_state,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated
}
);
if ( $options{test} eq 1 ) {
print "pulling data about service from database\n";
}
}
}
}
##############################################################################
#
# cmpAndInsert: anoy hash ->
# add service changes to the database
#
##############################################################################
sub cmpAndInsert {
my ($href) = @_;
die "cmpAndInsert: mid not defined" unless defined $href->{mid};
die "cmpAndInsert: host not defined" unless defined $href->{host};
die "cmpAndInsert: proto not defined" unless defined $href->{proto};
die "cmpAndInsert: portdb not defined" unless defined $href->{portdb};
die "cmpAndInsert: updated not defined" unless defined $href->{updated};
die "cmpAndInsert: machine_updated not defined"
unless defined $href->{machine_updated};
my $host = $href->{host};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $mid = $href->{mid};
my $proto = $href->{proto};
my $db_ref = $href->{portdb};
my %portdb = %{$db_ref};
if ( $options{test} eq 1 or $options{debug} eq 1 ) {
print "protocol is $proto\n";
}
my ( $scan_state, %seen_ports );
foreach my $port ( sort { $a cmp $b } keys %portdb ) {
if ( $options{test} eq 1 ) {
print $portdb{$port}{state} . " and $port\n";
print "port db $port proto $proto\n";
}
my $tmp;
if ( $proto eq "tcp" ) {
$tmp = $host->tcp_port_state($port);
}
elsif ( $proto eq "udp" ) {
$tmp = $host->udp_port_state($port);
}
else {
next;
}
$seen_ports{$port} = "up";
if ($tmp) {
$scan_state = "up";
}
else {
$scan_state = "down";
}
if ( $options{test} eq 1 ) {
print "scan thinks the status of $port is $scan_state\n";
print "state is " . $portdb{$port}{state} . "\n";
}
if ( $portdb{$port}{state} eq "up"
&& $scan_state eq "up"
&& $portdb{$port}{proto}
&& $portdb{$port}{proto} eq $proto )
{
if ( $options{test} eq 1 ) {
print "states are both up\n";
print "checking versions and banners\n";
}
my $db_updated = $portdb{$port}{updated};
my $db_machine_updated = $portdb{$port}{machine_updated};
my $cmp = cmpService(
{ mid => $mid,
host => $host,
port => $port,
state => $scan_state,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated,
db_updated => $db_updated,
db_machine_updated => $db_machine_updated,
}
);
if ( $cmp eq 0 ) {
if ( $options{test} > 1 ) {
print "port is $port\n";
print "mark 1\n";
}
insertServiceScan(
{ mid => $mid,
host => $host,
port => $port,
state => $scan_state,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated,
change => $cmp,
}
);
}
else {
if ( $options{test} eq 1 ) {
print "no difference in name or version or banner\n";
}
}
}
else {
#print "scan state is $scan_state\n";
#print "database state is $port_db{$port}{'state'}\n";
if ( $scan_state eq "down"
&& $portdb{$port}{'state'} eq "up"
&& $portdb{$port}{proto}
&& $portdb{$port}{proto} eq $proto )
{
insertServiceDB(
{ mid => $mid,
port => $port,
state => $scan_state,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated
}
);
if ( $options{test} eq 1 ) {
print "pulling data about service from database\n";
}
}
elsif ($scan_state eq "up"
&& $portdb{$port}{'state'} eq "down"
&& $portdb{$port}{proto}
&& $portdb{$port}{proto} eq $proto )
{
if ( $options{test} > 1 ) {
print "port is $port\n";
print "mark 2\n";
}
insertServiceScan(
{ host => $host,
mid => $mid,
port => $port,
state => $scan_state,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated,
change => 1,
}
);
if ( $options{test} eq 1 ) {
print "pulling data about service from scan\n";
}
}
else {
if ( $portdb{$port}{proto}
&& $portdb{$port}{proto} eq $proto )
{
if ( $options{test} eq 1 ) {
print "states are both down\n";
}
}
else {
if ( $options{test} > 1 ) {
print "protocols are different\n";
}
}
}
}
chomp( $portdb{$port}{updated} );
#print "\n$port_db{$port}{port} keys is $port\n";
#print "\n$port_db{$port}{state} keys is $port\n";
#print "\n$port_db{$port}{updated} keys is $port\n";
}
# bug for seen ports on different protocols
if ( $proto eq "tcp" ) {
# handles ports that are open in the scan and don't exist in the database
foreach ( $host->tcp_ports('open') ) {
my $tmptest = $host->tcp_port_state($_);
if ( $options{test} eq 1 ) {
print "state is $tmptest\n";
}
next if ( $tmptest ne "open" );
my $port_tmp = $_;
my $state_tmp;
if ( $seen_ports{$port_tmp} && $seen_ports{$port_tmp} eq "up" ) {
if ( $options{test} eq 1 ) {
print "Port is already up\n";
}
}
else {
$state_tmp = "up";
if ( $options{test} > 1 ) {
print "inserting scan not in the database on $port_tmp\n";
print "mark 3\n";
}
insertServiceScan(
{ host => $host,
mid => $mid,
port => $port_tmp,
proto => $proto,
state => $state_tmp,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated,
change => 1,
}
);
}
}
}
else {
# handles ports that are open in the scan and don't exist in the database
foreach ( $host->udp_ports('open') ) {
my $tmptest = $host->udp_port_state($_);
if ( $options{test} eq 1 ) {
print "state is $tmptest\n";
}
next if ( $tmptest ne "open" );
my $port_tmp = $_;
my $state_tmp;
if ( $seen_ports{$port_tmp} && $seen_ports{$port_tmp} eq "up" ) {
if ( $options{test} eq 1 ) {
print "Port is already up\n";
}
}
else {
$state_tmp = "up";
if ( $options{test} > 1 ) {
print "inserting scan not in the database on $port_tmp\n";
print "mark 4\n";
}
insertServiceScan(
{ host => $host,
mid => $mid,
port => $port_tmp,
state => $state_tmp,
proto => $proto,
updated => $human_time,
machine_updated => $machine_updated,
change => 0,
}
);
}
}
}
}
##############################################################################
#
# addServices: anoy hash ->
# add service changes to the database
#
##############################################################################
sub addServices {
my ($href) = @_;
die "addServices: mid not defined" unless defined $href->{mid};
die "addServices: host not defined" unless defined $href->{host};
die "addServices: updated not defined" unless defined $href->{updated};
die "addServices: uniq not defined" unless defined $href->{uniq};
die "addServices: machine_updated not defined"
unless defined $href->{machine_updated};
my $host = $href->{host};
my $human_time = $href->{updated};
my $machine_updated = $href->{machine_updated};
my $mid = $href->{mid};
my $uniq = $href->{uniq};
my ( $port, $service, $banner, $state, $state_db );
my %port_db = sortDB(
{ host => $host,
mid => $mid,
updated => $human_time,
machine_updated => $machine_updated
}
);
# determines all ports that are in the database and not in the scan
# thus we assume these services went down
if ( $uniq eq 0 ) {
if ( $options{test} > 0 ) {
print "store everything from the scan\n";
}
storeEverything(
{ host => $host,
mid => $mid,
updated => $human_time,
machine_updated => $machine_updated
}
);
return 0;
}
my @tcp_ports = $host->tcp_open_ports();
my @udp_ports = $host->udp_open_ports();
if ( !defined( scalar(@tcp_ports) ) ) {
if ( $options{debug} > 0 or $options{test} eq 1 ) {
print "no TCP ports found\n";
}
setDown(
{ mid => $mid,
host => $host,
proto => "tcp",
portdb => \%port_db,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
else {
cmpAndInsert(
{ mid => $mid,
host => $host,
proto => "tcp",
portdb => \%port_db,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
if ( !defined( scalar(@udp_ports) ) ) {
if ( $options{debug} > 0 or $options{test} eq 1 ) {
print "no UDP ports found\n";
}
setDown(
{ mid => $mid,
host => $host,
proto => "udp",
portdb => \%port_db,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
else {
cmpAndInsert(
{ mid => $mid,
host => $host,
proto => "udp",
portdb => \%port_db,
updated => $human_time,
machine_updated => $machine_updated
}
);
}
}
##############################################################################
#
# storeData: scalar ->
# function to perform scans and insert hosts and services as needed
#
##############################################################################
sub storeData {
my $mTable = createMachinesTable($dbh);
if ( $mTable eq 0 ) {
print "creating Machines Table\n";
}
my $sTable = createServicesTable($dbh);
if ( $sTable eq 0 ) {
print "creating Services Table\n";
}
my $created_on = $np->get_session()->finish_time();
my $human_time = localtime($created_on);
for my $host ( $np->all_hosts() ) {
my $os = $host->os_sig();
my $os_scan;
if ( defined($os)
and defined( $os->osfamily )
and defined( $os->osgen ) )
{
$os_scan = $os->osfamily . " " . $os->osgen;
}
else {
$os_scan = "unknown os";
}
if ( !defined($os_scan) or $os_scan eq "" ) {
$os_scan = "unknown os";
}
print "\n--------------------------------------\n";
print "Starting Scan of " . $host->addr . "\n";
my ( $mid, $uniq ) = addHost(
{ os => $os_scan,
session => $np->get_session(),
host => $host,
created_on => $human_time,
machine_created => $created_on
}
);
addServices(
{ mid => $mid,
host => $host,
updated => $human_time,
machine_updated => $created_on,
uniq => $uniq,
}
);
print "Scan Complete for " . $host->addr . "\n";
print "--------------------------------------\n";
}
print "\n";
}
##############################################################################
#
# verify_windows: array ref ->
# verify that the windows machine isn't trying to scanning itself
# side effect: calls verify_scan and possibly quits
#
##############################################################################
sub verify_windows {
my $range = shift;
foreach (@$range) {
if ( $_ =~ m/127\.0\.0\.1/ ) {
print "Error: Win32 machines can't scan themselves.\n";
exit 1;
}
}
verify_scan();
}
##############################################################################
#
# verify_ip: anoy hash -> scalar
# determine if ip is in acceptable range
#
##############################################################################
sub verify_ip {
my ($href) = @_;
die "verify_ip: oct1 not defined" unless defined $href->{oct1};
die "verify_ip: oct2 not defined" unless defined $href->{oct2};
die "verify_ip: oct3 not defined" unless defined $href->{oct3};
die "verify_ip: oct4 not defined" unless defined $href->{oct4};
my $oct1 = $href->{oct1};
my $oct2 = $href->{oct2};
my $oct3 = $href->{oct3};
my $oct4 = $href->{oct4};
if ( $oct1 < 255 and $oct1 > 0 ) {
if ( ( $oct2 eq "*" or $oct2 eq "\* " )
or ( $oct2 == 0 )
or ( $oct2 < 256 and $oct2 > 0 ) )
{
if ( ( $oct3 eq "*" or $oct3 eq "\*" )
or ( $oct3 == 0 )
or ( $oct3 < 256 and $oct3 > 0 ) )
{
if ( ( $oct4 eq "*" or $oct4 eq "\*" )
or ( $oct4 =~ /0\/\d{1,2}/ )
or ( $oct4 < 256 and $oct4 > 0 ) )
{
return 1;
}
else {
print "fourth is $4\n" if ( $options{test} > 2 );
}
}
else {
print "third is $3\n" if ( $options{test} > 2 );
}
}
else {
print "second is $2\n" if ( $options{test} > 3 );
}
}
return 0;
}
##############################################################################
#
# verify_scan
# verifies that the nmap path is set
# side effect: quits if the nmap path is not set
#
##############################################################################
sub verify_scan {
if ( !defined($nmapPath) or $nmapPath eq "" ) {
print "Error: Nmap was not found in your path.\n";
print "Please download the latest version from\n";
print "http://www.insecure.org/nmap/download.html\n";
exit 1;
}
}
##############################################################################
#
# help ->
# display help information
# side effect: exits program
#
##############################################################################
sub help {
print "Usage: $PROG [Options] {target specification}
Target Specification:
Can pass hostnames, IP addresses, networks, etc.
Ex: microsoft.com, 192.168.0.1, 192.168.1.1/24, 10.0.0.1-254
-i --iplist <iplist> Scan using a list of IPs from a file
-x --xml <xml-file> Parse scan/info from Nmap XML file
Scan Options:
-a --args <args> Execute Nmap with args (needs quotes)
-e --extraargs <args> Add args to the default args (needs quotes)
--inter <interface> Perform Nmap Scan using non default interface
-m --moreports <ports> Add ports to scan ex: 8080 or 3306,5900
-n --nmap <path> Path to Nmap executable
-p --pingscan Ping Target then scan the host(s) that are alive
--udp Add UDP to the scan arguments
--rpc Add RPC to the scan arguments
-r --range <ports> Ports for scan [def 1-1025]
--diffbanner Parse changes of the banner
Config Options:
-d --dbconfig <config> Config for results database [def config.yaml]
--configdir <dir> Directory for the database config file
--data <file> SQLite Database override [def data.dbl]
--dir <dir> Directory for SQLite or CSV file [def . ]
General Options:
--nocolors Don't Print Colors
--test <level> Testing information
--debug <level> Debug information
-v --version Display version
-h --help Display this information
Send Comments to Joshua D. Abraham ( jabra\@ccs.neu.edu )\n";
exit;
}
##############################################################################
#
# print_version ->
# displays version
# side effect: exits program
#
##############################################################################
sub print_version {
print "$PROG version $VERSION by $AUTH\n";
exit;
}
##############################################################################
if ( @ARGV == 0 ) {
help;
exit;
}
GetOptions(
\%options,
'pingscan|p', 'scan|s', 'iplist|i=s', 'xml|x=s', 'range|r=s',
'moreports|m=s', 'nmap|n=s',
'dbconfig|d=s', 'dir=s', 'data=s', 'configdir=s', 'test=s',
'args|a=s', 'extraargs|e=s', 'udp', 'rpc', 'inter=s', 'diffbanner',
'help|h' => sub { help(); },
'version|v' => sub { print_version(); },
'nocolors' => sub { $colors = 0; },
'fruitycolors' => sub { $colors = 2; },
'debug=s',
)
or exit 1;
my $dephosts = "";
if ($options{'scan'}){
print "WARNING: --scan is deprecated\n";
print "Please use the following format: $PROG 127.0.0.1 \n";
$dephosts = $options{'scan'};
}
if ( ( $options{'iplist'} or $options{'pingscan'} )
and $options{'xml'} )
{
print "Invalid type of input\n";
exit 1;
}
if ( $options{'input'} and $options{'pingscan'} ) {
print "Conflicting type of Scan\n";
}
if ( $options{'dir'} ) {
$dir = $options{'dir'};
}
if ( $options{'configdir'} ) {
$configdir = $options{'configdir'};
}
if ( $options{'dbconfig'} ) {
my $tmpconfig;
if ( $^O eq 'MSWin32' || $^O =~ /cygwin/ ) {
$tmpconfig = $configdir . '\\' . $options{'dbconfig'};
}
else {
$tmpconfig = $configdir . "/" . $options{'dbconfig'};
}
if ( -e $tmpconfig && -r $tmpconfig ) {
$dbconfig = $tmpconfig;
}
}
chdir($dir) or die "Couldn't change to $dir directory\n";
if ( !defined($dbconfig)
or $dbconfig eq ""
or ( !-e $dbconfig or !-r $dbconfig ) )
{
print "Couldn't open $dbconfig for input\n";
exit;
}
if ( $options{'inter'} ) {
$interface = "-e " . $options{'inter'} . " ";
}
if ( $options{'nmap'} ) {
if ( -X $options{'nmap'} ) {
$nmapPath = $options{'nmap'};
}
else {
print $options{'nmap'} . " isn't executable using $nmapPath\n";
}
}
if ( $options{'iplist'} ) {
if ( -e $options{'iplist'} ) {
if ( -r $options{'iplist'} ) {
my $fh = new FileHandle("<$options{'iplist'}");
die "$options{'iplist'}:$!" unless defined $fh;
@ipRange = $fh->getlines();
chomp @ipRange;
$type = 'scan';
}
else {
print "File $options{'iplist'} isn't readable\n";
exit 1;
}
}
else {
print "File $options{'iplist'} doesn't exist\n";
exit 1;
}
}
if ( $options{'xml'} ) {
if ( -e $options{'xml'} ) {
if ( -r $options{'xml'} ) {
$xmlFile = $options{'xml'};
$type = 'file';
}
else {
print "Input File isn't readable\n";
exit 1;
}
}
else {
print "Input File doesn't exist\n";
exit 1;
}
}
if ( $options{'udp'} ) {
$scantype = $scantype . "U";
}
if ( $options{'rpc'} ) {
$scantype = $scantype . "R";
}
if ( $options{'args'} ) {
$args = "";
$scantype = "";
$args = $options{'args'};
$cargs = 'yes';
}
if ( $options{'extraargs'} ) {
$args = $args . $options{'extraargs'} . " ";
}
if ( $options{'dir'} ) {
$outputdir = $options{'dir'};
}
if ( $options{'range'} ) {
my (@ports) = split ',', $options{'range'};
foreach (@ports) {
chomp;
if ( $_ !~ /\d/ ) {
print "$_ not digit\n";
}
}
$Range = "";
for ( my $i = 0; $i < scalar(@ports); $i++ ) {
$Range .= $ports[$i];
if ( $i < scalar(@ports) - 1 ) {
$Range .= ",";
}
}
}
#@ports = @ports - 1;
if ( $options{'moreports'} ) {
my @morePorts = split ',', $options{'moreports'};
foreach (@morePorts) {
if (/(\d+)/) {
chomp;
if ( $_ =~ /\d/ ) {
$Range .= ",$_";
}
else {
print "MorePorts: Port $_ isn't between 1 and 65535\n";
exit 1;
}
}
else {
print "MorePorts: Port $_ isn't a number\n";
exit 1;
}
}
}
if ( $options{'diffbanner'} ) {
$diff = "banner";
}
my $tmp = scalar(@ARGV);
my @targets;
if ( $type ne 'file' ) {
foreach (@ARGV) {
s/\ //g;
my @args = split(',', $_);
foreach(@args){
push( @targets, $_ );
}
}
my @args = split(',', $dephosts);
foreach(@args){
push( @targets, $_ );
}
}
foreach my $host (@targets) {
if ($host) {
my $ipRange;
if ( $host =~ /[a-zA-Z]/ ) {
my $name = $host;
if ( $name eq 'localhost' ) {
$ipRange = '127.0.0.1';
}
else {
my ( $host, @addresses, $hent, $addr_ref );
if ( $hent = gethostbyname($name) ) {
$name = $hent->name(); # in case different
$addr_ref = $hent->addr_list();
@addresses = map { inet_ntoa($_) } @$addr_ref;
}
$ipRange = $addresses[0];
if ( !defined($ipRange) or $ipRange eq '' ) {
print "Invalid Ip Address being Resolved\n";
exit 1;
}
}
print "Resolved $host to $ipRange\n"
if ( $options{debug} > 3 );
$type = 'scan';
push( @ipRange, $ipRange );
}
elsif ( $host
=~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\-(\d{1,3}|\*|'*')/
)
{
my $max;
if ( $5 =~ m/\*/ ) {
$max = 255;
}
elsif ( $5 < 256 ) {
$max = $5;
}
else {
print "IP Address not in proper range\n";
exit 1;
}
if ( $4 < $5 ) {
for ( $4 .. $max ) {
if (verify_ip(
{ oct1 => $1,
oct2 => $2,
oct3 => $3,
oct4 => $_
}
) == 1
)
{
my $ip = join( ".", $1, $2, $3, $_ );
push( @ipRange, $ip );
$type = 'scan';
}
}
}
}
elsif ( $host
=~ /(\d{1,3})\.(\d{1,3}|\*)\.(\d{1,3}|\*)\.(0\/\d{1,2}|\d{1,3}|\*|'*')/
)
{
if ( $options{test} > 3 ) {
print "first is $1\n";
print "second is $2\n";
print "third is $3\n";
print "fourth is $4\n";
}
my $valid = verify_ip(
{ oct1 => $1,
oct2 => $2,
oct3 => $3,
oct4 => $4
}
);
if ( $valid == 1 ) {
$type = 'scan';
push( @ipRange, $host );
}
}
if ( !defined($host) ) {
print "IP Address not in proper range\n";
exit 1;
}
}
else {
print "Invalid IP Address\n";
exit 1;
print "scan is $host" if ( $options{debug} eq 1 );
}
}
if ( $options{'pingscan'} ) {
if ( $^O eq 'MSWin32' || $^O =~ /cygwin/ ) {
$nmapPath = "\"$nmapPath\"";
verify_windows( \@ipRange );
}
else {
verify_scan();
}
$np->parsescan( $nmapPath, "-R -sP", @ipRange );
my $livehosts = "";
for my $host ( $np->get_ips('up') ) {
if ( $livehosts eq "" ) {
$livehosts = $host;
}
else {
$livehosts = join( " ", $livehosts, $host );
}
}
if ( $livehosts eq "" ) {
print "Scanned @ipRange\n";
print "No Hosts Responding to Ping Scan\n";
exit;
}
else {
print "Live Hosts Found: $livehosts\n";
@ipRange = $livehosts;
}
}
# make sure something is passed
help() if ( $type ne 'file' and $type ne 'scan' );
if ( $type eq 'file' ) {
$np->parsefile($xmlFile);
}
elsif ( $type eq 'scan' ) {
$args = $args . $interface;
if ( $cargs eq 'no' ) {
$args = $args . $scantype . " -p $Range";
}
if ( $options{debug} > 2 or $options{test} > 0 ) {
print "nmap path is $nmapPath\n";
}
if ( $args =~ /-o(?:X|N|G)/ ) {
warn "$PROG Cannot pass option '-oX', '-oN' or '-oG'\n";
warn "Removing option\n";
$args =~ s/-o(?:X|N|G)//g;
print "args is now $args\n";
}
if ( $^O eq 'MSWin32' || $^O =~ /cygwin/ ) {
$nmapPath = "\"$nmapPath\"";
verify_windows( \@ipRange );
}
else {
verify_scan();
if ( $> ne 0 ) {
print "PBNJ Scans requires root privileges.\n";
exit 1;
}
}
if ( $options{debug} > 0 or $options{test} > 0 ) {
print "Scan Args are $args\n";
print "Scanning ";
foreach (@ipRange) { print "- $_ "; }
print "\n";
}
$np->parsescan( $nmapPath, $args, @ipRange );
}
else {
print "type error $type\n";
exit 1;
}
# only connect to the database when we need too.
if ( $type eq "scan" or $type eq "file" ) {
if ( $options{data} ) {
$db = "SQLite";
$database = $options{data};
}
else {
$datadb = YAML::LoadFile($dbconfig);
foreach my $tmp ( keys %$datadb ) {
$passwd = $$datadb{$tmp} if ( $tmp eq 'passwd' );
$user = $$datadb{$tmp} if ( $tmp eq 'user' );
$db = $$datadb{$tmp} if ( $tmp eq 'db' );
$database = $$datadb{$tmp} if ( $tmp eq 'database' );
$hostname = $$datadb{$tmp} if ( $tmp eq 'host' );
$port = $$datadb{$tmp} if ( $tmp eq 'port' );
}
}
# connection to database
if ( $db eq 'SQLite' or $db eq 'CSV' ) {
$dbh = DBI->connect(
"dbi:$db:$database",
$user, $passwd,
{ PrintError => 0,
RaiseError => 0,
AutoCommit => 1
}
)
|| die "Cannot connect: $DBI::errstr";
}
elsif ( $db eq 'mysql' ) {
my $dsn = "DBI:$db:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect(
$dsn, $user, $passwd,
{ PrintError => 0,
RaiseError => 0,
AutoCommit => 1
}
)
|| die "Cannot connect: $DBI::errstr";
}
elsif ( $db eq 'Pg' ) {
my $dsn = "DBI:$db:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect(
$dsn, $user, $passwd,
{ PrintError => 0,
RaiseError => 0,
AutoCommit => 1,
PrintWarn => 0
}
)
|| die "Cannot connect: $DBI::errstr";
}
else {
print "$db isn't supported\n";
}
}
storeData($dbh);
$dbh->disconnect;
__DATA__
# Config.yaml
#
# Copyright (C) 2005-2006 Joshua D. Abraham ( jabra@ccs.neu.edu )
#
# This config file is released under the terms of the GNU General
# Public License (GPL), which is distributed with this software in the
# file "COPYING". The GPL specifies the terms under which users
# may copy and use this software.
#
# PBNJ 2.0
# (P)orts (B)anners N' (J)unk
#
# Author: Joshua D. Abraham
# Date: March 15, 2006
# Updated: November 15, 2006
# Version: 2.04
#
# Configuration file for PBNJ 2.0
# YAML:1.0
#
# Config for connecting to a DBI database
# SQLite, mysql etc
db: SQLite
# for SQLite the name of the file. For mysql the name of the database
database: data.dbl
# Username for the database. For SQLite no username is needed.
user: ""
# Password for the database. For SQLite no password is needed.
passwd: ""
# Password for the database. For SQLite no host is needed.
host: ""
# Port for the database. For SQLite no port is needed.
port: ""
|