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 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
|
/***************************************************************************/
/* S e n d I C M P N a s t y G a r b a g e */
/***************************************************************************/
/* - Program that allow to send the following ICMP packets fully */
/* customized: */
/* a) ICMP information: */
/* - Echo Request. Sent on by default. */
/* - Echo Reply. */
/* - Address Mask Request. */
/* - Timestamp. */
/* - Information Request. */
/* - Router Solicitation (Router Discovery). */
/* - Router Advertisement (Router Discovery). */
/* b) ICMP errors: */
/* - Redirect. */
/* - Source Quench. */
/* - Time Exceeded. */
/* - Destination Unreach. */
/* - Parameter Problem. */
/***************************************************************************/
/* Copyright (C) 1999, 2000, 20001 Alfredo Andres <aandres@s21sec.com> */
/* 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 2 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, write to the Free Software */
/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/***************************************************************************/
/* echo "Ideas & comments" | /bin/mail aandres@s21sec.com */
/* echo "Destructive opinions & flames" > /dev/null */
/***************************************************************************/
#ifndef lint
static const char rcsid[] =
"$Id: sing.c,v 1.24 2001/04/18 07:42:59 slay Exp $";
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#else
# ifdef HAVE_NETINET_IN_SYSTEM_H
# include <netinet/in_system.h>
# endif
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_BSTRING_H
# include <bstring.h>
#endif
#include <setjmp.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
#endif
#ifdef LINUX
# include<linux/version.h>
#ifndef KERNEL_VERSION
# define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif
#else
# define LINUX_VERSION_CODE 0
# define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif
#include <stdarg.h>
#include <pcap.h>
#include <libnet.h>
#include "sing.h"
/*********************/
/* Initial function. */
/*********************/
int
main(int argc, char **argv)
{
struct protoent *proto;
initmem( buf, sizeof(buf) );
if ( argc == 1 )
{
printf("GNU %s %s %s\n", PACKAGE, VERSION, vers_date);
printf("Try '%s -h' to display the help.\n",PACKAGE);
exit(0);
}
init_packet_struct( (struct my_pack *)&packet );
parse_args( argc, argv, (struct my_pack *)&packet );
packet.name_dst = argv[argc-1];
if (packet.logfile)
init_log(argc,argv);
#if defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) || defined(SOLARIS)
if (set_rf && !packet.mac_src)
{
#ifdef SOLARIS
write_log(1,"<*> Solaris systems can't set he IP header flags (without libnet) ;) <*>\n");
#else
write_log(1,"<*> Using promisc mode to jump over *BSD Reserved bit problem! <*>\n");
#endif
}
#endif
if (packet.mac_src) /* MAC spoofing, use libnet */
{
if ((network = libnet_open_link_interface(packet.iface2route.name, err_buf)) == NULL)
libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf);
packet.builder = build_pack_libnet;
}
else
{
if ( ( proto = getprotobyname("icmp")) == NULL )
go_out(1,"Unknown ICMP protocol, plz, vrfy your /etc/protocols");
sock = socket( AF_INET, SOCK_RAW, proto->p_proto);
if ( sock < 0 )
go_out_error(1,"Can't build RAW sockets");
}
if ( !geteuid() )
setuid( getuid() );
uid=getuid();
if (!packet.mac_src)
adjust_sock(sock);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)) || defined(FREEBSD) || defined(NETBSD)
if ( !packet.spoof && packet.ipopt && !packet.rr )
{
if ( vrfy_sr() == 0 )
{
if (!Quiet)
{
printf("\n * Ouch! Your kernel seems to have Source Routing disabled!\n");
printf(" * You can enable it using:\n *\n");
#if defined(FREEBSD) || defined(NETBSD)
printf(" * # sysctl -w net.inet.ip.accept_sourceroute=1\n");
#else
printf(" * # echo 1 > /proc/sys/net/ipv4/conf/%s/accept_source_route\n",
packet.listen2dev?packet.listen2dev:packet.iface2route.name);
printf(" * # echo 1 > /proc/sys/net/ipv4/conf/all/accept_source_route\n");
#endif
printf(" *\n * or force SING to use libpcap spoofing your own IP address (-S).\n\n");
}
exit(1);
}
}
#endif
handle_signals();
if ( icmp_info_or_err[packet.tipo_icmp].class ) /* ICMP error ... */
tam_icmp = build_icmp_err( (struct my_pack *)&packet );
else
tam_icmp = build_icmp_info( (struct my_pack *)&packet );
write_log(2,"SINGing to %s (%s): %d data bytes\n", argv[argc-1],
inet_ntoa( packet.destino.sin_addr), tam_icmp );
#ifdef DEBUG
printf(" -> ICMP total size = %d bytes\n", tam_icmp);
#endif
build_ip( sock, packet.ip_spoof, packet.ttl, packet.tos,
IPPROTO_ICMP, tam_icmp );
if ( !icmp_info_or_err[packet.tipo_icmp].class &&
(packet.tipo_icmp != ICMP_ROUTER_ADVERT) && packet.tipo_icmp )
read_icmp( buf );
exit(2);
}
/********************************/
/* Adjust the raw socket values */
/* Send buffer, broadcasting... */
/********************************/
void
adjust_sock( int sock )
{
int on = 1, tam_sock_buf = SIZE_BIG;
if ( setsockopt( sock, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on) )
== -1 )
go_out_error(1,"setsockopt IP_HDRINCL error");
if ( setsockopt( sock, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof(on) )
== -1 )
{
#ifdef DEBUG
fprintf(stderr,"%s: SO_BROADCAST -> %s\n", PACKAGE, sys_errlist[errno] );
#endif
}
if (setsockopt( sock, SOL_SOCKET, SO_SNDBUF, (char *)&tam_sock_buf,
sizeof(tam_sock_buf) ) == -1 )
{
#ifdef DEBUG
fprintf(stderr,"%s: SO_SNDBUF -> %s\n", PACKAGE, sys_errlist[errno] );
#endif
}
}
/****************************************/
/* Build an ICMP information packet and */
/* put it into the address 'buffer'. */
/* Return the packet length in bytes. */
/****************************************/
int
build_icmp_info( struct my_pack *packet )
{
int tam = TCAB_ICMP_MSG;
struct icmp2 *icp = (struct icmp2 *) buf;
speed.seq = &icp->icmp_seq2;
speed.time = (u_long *)icp->icmp_data2;
speed.cksum = &icp->icmp_cksum;
icp->icmp_type = packet->tipo_icmp;
icp->icmp_code = packet->cod_icmp;
icp->icmp_cksum = 0;
if (packet->info_id)
icp->icmp_id2 = packet->info_id;
else
icp->icmp_id2 = getpid();
switch ( packet->tipo_icmp )
{
case ICMP_TIMESTAMP: icp->icmp_rtime2 = 0;
icp->icmp_ttime2 = 0;
tam += TDATA_TIMESTAMP;
if ( packet->size_pattern )
{
copymem( packet->pattern, icp->icmp_data2 +
TDATA_TIMESTAMP, packet->size_pattern );
tam += packet->size_pattern;
}
break;
case ICMP_INFO_REQUEST: speed.time=NULL;
if ( packet->size_pattern )
{
copymem( packet->pattern, icp->icmp_data2,
packet->size_pattern );
tam += packet->size_pattern;
}
break;
case ICMP_ADDRESS: icp->icmp_mask2 = htonl(packet->maskaddr);
tam += TDATA_ADDRESS;
speed.time=NULL;
if ( packet->size_pattern )
{
copymem( packet->pattern, icp->icmp_data2 +
TDATA_ADDRESS, packet->size_pattern );
tam += packet->size_pattern;
}
break;
case ICMP_ROUTER_SOLICIT: icp->icmp_reserved = 0;
speed.seq = NULL;
speed.time = NULL;
break;
case ICMP_ROUTER_ADVERT: speed.seq = NULL;
speed.time = NULL;
icp->icmp_num_addr = packet->num_routers;
icp->icmp_addr_entry_size = 2;
icp->icmp_lifetime2 = htons(packet->lifetime);
put_routers( packet, (struct id_rdiscovery *) (buf +
sizeof(struct icmp_hdr) + TCAB_RDISC) );
tam = sizeof(struct icmp_hdr) + TCAB_RDISC +
(TDATA_RDISC * packet->num_routers) ;
break;
case ICMP_ECHO_REPLY:
case ICMP_ECHO_REQUEST: tam += sizeof(struct timeval);
if ( packet->size_pattern )
{
copymem( packet->pattern, icp->icmp_data2 +
sizeof(struct timeval),
packet->size_pattern );
tam += packet->size_pattern;
}
break;
}
tam = add_garbage( packet->garbage, tam);
if ( packet->tipo_icmp == ICMP_ROUTER_SOLICIT ||
packet->tipo_icmp == ICMP_ROUTER_ADVERT )
icp->icmp_cksum = in_cksum( (u_short *)icp, tam );
return( tam );
}
/**************************/
/* Control the garbage. */
/* Return the size of the */
/* garbage added to the */
/* packet + the ICMP hdr. */
/**************************/
int
add_garbage( u_long garbage, int tam )
{
if ( garbage && ( tam < SIZE_BIG) )
{
if ( garbage > ( SIZE_BIG - TCAB_IP - tam ) )
{
if ( verbose )
if ( garbage != SIZE_BIG )
write_log(1," -> Size of data garbage too big, using maximum (%d bytes)\n",
SIZE_BIG - TCAB_IP - tam);
fill_garbage( buf + tam, (SIZE_BIG - TCAB_IP - tam ) );
tam = (SIZE_BIG - TCAB_IP);
}
else
{
fill_garbage( buf + tam, garbage );
tam += garbage;
}
}
return tam;
}
/***************************/
/* Fill the buffer with */
/* "data" size of garbage. */
/***************************/
void
fill_garbage( char *buf, u_long data )
{
u_char flag=0;
#ifdef DEBUG
printf(" -> Data garbage size = %ld bytes\n", data );
#endif
while ( data-- )
*buf++ = ((flag=!flag) ? 33 : 72);
}
/***************************/
/* Fetch the routers from */
/* the linked list and put */
/* them into the buffer. */
/***************************/
void
put_routers( struct my_pack *packet, struct id_rdiscovery *data)
{
struct router *cursor = packet->router;
while (cursor)
{
data->router_addr.s_addr = cursor->address;
data->pref_level.s_addr = htonl(cursor->pref);
cursor = cursor->next;
++data;
}
}
/**************************/
/* Return the time of day */
/* in milisecs. */
/**************************/
u_long
day2milisecs( void )
{
struct timeval tiempo;
gettimeofday( (struct timeval *) &tiempo, NULL);
return ( (tiempo.tv_sec%86400) * 1000 /* From 2day secs 2 milisecs */
+
tiempo.tv_usec / 1000 /* From microsecs 2 milisecs */
); /* Milisegundos en el mismo segundo */
}
/**************************************************/
/* Build an ICMP error packet and put it into the */
/* address 'buffer'. */
/* Also build the IP header and the 64 data bits */
/* of the original datagram. */
/* Return the packet length in bytes. */
/**************************************************/
int
build_icmp_err ( struct my_pack *packet )
{
struct icmp2 *icp = (struct icmp2 *) buf;
struct protoent *proto;
union data_hdr *mix = (union data_hdr *) malloc(TCAB_64DATA);
int elemento, tam;
if ( !mix )
go_out(3,"Out of memory on union data_hdr memory");
initmem( mix, TCAB_64DATA);
icp->icmp_type = packet->tipo_icmp;
icp->icmp_code = packet->cod_icmp;
icp->icmp_cksum = 0;
icp->icmp_gwaddr2.s_addr = 0;
icp->icmp_ip.ip_hl = TCAB_IP >> 2;
icp->icmp_ip.ip_v = 4;
icp->icmp_ip.ip_tos = 0x0000;
icp->icmp_ip.ip_len = htons( TCAB_IP + TCAB_ICMP + TCAB_64DATA);
icp->icmp_ip.ip_id = htons(0x4A2F);
icp->icmp_ip.ip_off = 0x0000;
icp->icmp_ip.ip_ttl = TTL_DFL;
icp->icmp_ip.ip_p = packet->protocol;
#ifdef HAVE_IP_IP_CSUM
icp->icmp_ip.ip_csum = 0x0000;
#else
icp->icmp_ip.ip_sum = 0x0000;
#endif
switch( packet->tipo_icmp )
{
case ICMP_REDIRECT: icp->icmp_ip.ip_src.s_addr = packet->orig;
icp->icmp_ip.ip_dst.s_addr = packet->dest_red;
icp->icmp_gwaddr2.s_addr = packet->gway;
break;
case ICMP_PARAM_PROB: icp->icmp_pptr2 = packet->pointer;
default: icp->icmp_ip.ip_src.s_addr = packet->destino.sin_addr.s_addr;
icp->icmp_ip.ip_dst.s_addr = packet->orig;
}
#ifdef HAVE_IP_IP_CSUM
icp->icmp_ip.ip_csum = in_cksum( (u_short *)(&(icp->icmp_ip)), TCAB_IP );
#else
icp->icmp_ip.ip_sum = in_cksum( (u_short *)(&(icp->icmp_ip)), TCAB_IP );
#endif
for ( elemento=0; elemento < MAX_C(data_protocols); elemento++ )
{
if ( packet->protocol == data_protocols[elemento].proto )
{
(*data_protocols[elemento].func_proto)( mix, packet->p_origen,
packet->p_destino);
elemento=32767;
break;
}
}
if ( elemento != 32767)
{
if ( verbose )
{
write_log(1, " -> Embedded IP header with unimplemented protocol");
if ( (proto = getprotobynumber( packet->protocol )) == NULL )
write_log(1, " (%d)\n", packet->protocol);
else
write_log(1, " (%s)\n", proto->p_name);
}
proto_unknown( mix );
}
copymem( mix, icp + 1, TCAB_64DATA );
tam = add_garbage( packet->garbage, (TCAB_ICMP + TCAB_64DATA) );
icp->icmp_cksum = in_cksum( (u_short *)icp, tam );
return( tam );
}
/***********************************************/
/* Fulfill the TCP 32 bits inside the embedded */
/* IP header on an ICMP error */
/***********************************************/
void
proto_tcp64( union data_hdr *mix, u_short porto, u_short portd)
{
mix->cab_tcp.source = htons(porto); /* Source port */
mix->cab_tcp.dest = htons(portd); /* Destination port */
mix->cab_tcp.seq = htonl(0xC010C005); /* TCP sequence number */
}
/***********************************************/
/* Fulfill the UDP 32 bits inside the embedded */
/* IP header on an ICMP error */
/***********************************************/
void
proto_udp64( union data_hdr *mix, u_short porto, u_short portd )
{
mix->cab_udp.source = htons(porto); /* Source port */
mix->cab_udp.dest = htons(portd); /* Destination port */
mix->cab_udp.uh_ulen = htons(12); /* Length */
mix->cab_udp.uh_sum = htons(0xFA13); /* Checksum */
}
/************************************************/
/* Fulfill the ICMP 32 bits inside the embedded */
/* IP header on an ICMP error */
/************************************************/
void
proto_icmp64( union data_hdr *mix, u_short identif, u_short num_seq )
{
mix->cab_echo.type = ICMP_ECHO_REQUEST;
mix->cab_echo.code = 0x0; /* Echo Request */
mix->cab_echo.cksum = 0x0000; /* Checksum */
mix->cab_echo.id = identif; /* Echo Request ID */
mix->cab_echo.seq = num_seq; /* Echo Request sequence number */
mix->cab_echo.cksum = in_cksum( (u_short *)mix, TCAB_64DATA );
}
/**********************************************/
/* Fulfill the 32 bits inside the embedded IP */
/* header (with unknown protocol field) on an */
/* ICMP error */
/**********************************************/
void
proto_unknown( union data_hdr *mix )
{
#define DATA 0xFF
register int i;
for ( i=0; i < 8; i++)
mix->cab_byte[i]= DATA;
}
/******************************************/
/* Build an IP datagram adding the data */
/* 'paquete' and sending it to the socket */
/* 's'. */
/* Make the fragmentation if necessary, */
/* getting the MTU value of the outgoing */
/* interface (linuz only). */
/******************************************/
void
build_ip( int s, u_long orig, int ttl, int tos, int prot, int lenpack )
{
u_int mtu = SIZE_BIG-(TCAB_IP+packet.len_ipopt), len_pack = lenpack;
u_int len_ipopt = packet.len_ipopt;
struct ip2 *ip_p = (struct ip2 *) (buf_ip+LIBNET_ETH_H);
initmem( buf_ip, sizeof(buf_ip));
ip_p->ip_hl = (TCAB_IP + len_ipopt) >> 2;
ip_p->ip_v = 4;
ip_p->ip_tos = tos;
ip_p->ip_ttl = ttl;
ip_p->ip_id = htons(0x3372);
ip_p->ip_p = prot;
/* If you want to calculate the IP header checksum
* you must delete the comments of the next line
* because the Linux kernel fill in automatically:
*
* ip_p->ip_sum = in_cksum( (u_short *)ip_p, TCAB_IP ); */
#ifdef HAVE_IP_IP_CSUM
ip_p->ip_csum = 0x0000;
if (packet.mac_src ) /* Used MAC spoofing...*/
ip_p->ip_csum = in_cksum( (u_short *)ip_p, TCAB_IP+ len_ipopt );
#else
ip_p->ip_sum = 0x0000;
if (packet.mac_src ) /* Used MAC spoofing...*/
ip_p->ip_sum = in_cksum( (u_short *)ip_p, TCAB_IP+ len_ipopt );
#endif
ip_p->ip_src.s_addr = orig;
ip_p->ip_dst.s_addr = packet.destino.sin_addr.s_addr;
#ifdef CAN_FRAGMENT
mtu = packet.iface2route.mtu - ( TCAB_IP + len_ipopt );
#else
if ( packet.mac_src )
mtu = packet.iface2route.mtu - ( TCAB_IP + len_ipopt );
#endif
if ( packet.mtu )
mtu = ( packet.mtu > mtu ) ? mtu : packet.mtu ;
#ifdef DEBUG
printf( " -> MTU = %d bytes\n", mtu + TCAB_IP + len_ipopt);
printf(" -> Total packet size (ICMP + IP + IPOPT) = %d bytes\n",
len_pack + TCAB_IP + len_ipopt );
#endif
#ifdef SOLARIS
if ( !packet.mac_src ) /* If not used MAC spoofing */
{
if ( setsockopt( s, IPPROTO_IP, IP_OPTIONS, packet.ipopt, packet.len_ipopt )
== -1 )
go_out_error(1, "setsockopt IP_OPTIONS error");
len_ipopt = 0;
}
#endif
/* To improve speed on subsequents packets */
speed.s = s;
speed.len_pack = len_pack;
speed.mtu = mtu;
speed.len_ipopt = len_ipopt;
copymem((char *)ip_p, (char *)&speed.ip_p, sizeof(struct ip2));
/* Send a gratuitous ARP if used MAC spoofing */
if ( packet.mac_src)
send_arp(ip_p->ip_src.s_addr,packet.mac_dst);
if (icmp_info_or_err[packet.tipo_icmp].class == ICMP_ERROR ||
packet.tipo_icmp == ICMP_ROUTER_ADVERT || !packet.tipo_icmp )
(*packet.builder)();
}
/*******************************/
/* This routine will be called */
/* every time we send a packet */
/* I've tried to improve the */
/* program speed. */
/*******************************/
void
build_pack(void)
{
#ifdef CAN_FRAGMENT
u_int first = 1;
u_int len_ipopt = speed.len_ipopt;
#endif
u_int len, offset_pack = 0;
u_int len_pack = speed.len_pack;
u_char *paquete=buf, *punt = (buf_ip+LIBNET_ETH_H);
struct ip2 *ip_p = (struct ip2 *)punt;
copymem((char *)&speed.ip_p, punt, sizeof(struct ip2));
if (speed.seq || speed.time)
{
*speed.seq = stats.nsent + packet.info_seq;
CLR(*speed.seq % mx_dup_ck);
if (speed.time)
{
if ( (packet.tipo_icmp == ICMP_ECHO_REQUEST) ||
!packet.tipo_icmp )
gettimeofday((struct timeval *)speed.time, NULL);
else
*speed.time = htonl(day2milisecs());
}
*speed.cksum = 0;
if (!bad_cksum)
*speed.cksum = in_cksum( (u_short *)buf, tam_icmp );
else
*speed.cksum = 0x6666; /* The Checksum of the Beast xDD */
}
while ( len_pack > 0 )
{
len = len_pack;
if ( len > speed.mtu )
len = speed.mtu;
if ( ( speed.mtu > 7 ) & ( len < len_pack ) )
len &= ~7;
#ifdef CAN_FRAGMENT
/* All fragments must carry IP options when LSRR|SSRR */
if ( packet.len_ipopt && (first || !packet.rr) )
copymem( packet.ipopt, ip_p + 1, speed.len_ipopt );
else
ip_p->ip_hl = TCAB_IP >> 2;
#endif
/* Copy the data...*/
copymem( paquete, punt + sizeof(struct ip2) + speed.len_ipopt, len );
#ifdef SOLARIS_26
ip_p->ip_len = FIX_IT(TCAB_IP + len);
#else
ip_p->ip_len = FIX_IT(TCAB_IP + packet.len_ipopt + len);
#endif
ip_p->ip_off = FIX_IT(offset_pack >> 3);
if (set_df)
ip_p->ip_off |= FIX_IT(IP_DF);
if (set_rf)
ip_p->ip_off |= FIX_IT(IP_RF);
len_pack-=len;
#ifdef CAN_FRAGMENT /* 65535 - 20 minimum IP header */
if ( (offset_pack + speed.mtu) <= ( 65515 - len_ipopt ) )
{
#endif
if ( len_pack > 0 )
ip_p->ip_off |= FIX_IT(IP_MF);
#if defined(LINUX) || defined(SOLARIS_26) || defined(SOLARIS_27) || defined(NETBSD) || defined(FREEBSD)
send2sock( speed.s, buf_ip+LIBNET_ETH_H, UNFIX_IT(ip_p->ip_len), (struct sockaddr *)&packet.destino );
#else
send2sock( speed.s, buf_ip+LIBNET_ETH_H, UNFIX_IT(ip_p->ip_len) - packet.len_ipopt,
(struct sockaddr *)&packet.destino );
#endif
#ifdef CAN_FRAGMENT
}
else
{ /* Last packet must *NOT* have the IP_MF */
send2sock( speed.s, buf_ip+LIBNET_ETH_H, UNFIX_IT(ip_p->ip_len), (struct sockaddr *)&packet.destino );
break;
}
first = 0;
if ( packet.rr || !packet.len_ipopt )
len_ipopt = 0;
#endif
paquete+=len;
offset_pack+=len;
}
stats.nsent++;
}
/***************************/
/* Send datalen 'len' from */
/* 'buf_ip' to socket 's'. */
/***************************/
void
send2sock( int s, char *buf_ip, int len, struct sockaddr *host )
{
int n;
n = sendto( s, buf_ip, len, 0, host, sizeof(struct sockaddr_in) );
if ( n < 0 )
go_out_error(1, "Error sending packet on send2sock");
#ifdef DEBUG
if (n != len)
printf("Warning!! -> Bytes sent = %d\n",n);
#endif
}
/*******************************/
/* This routine will be called */
/* every time we send a packet */
/* using MAC spoofing. */
/*******************************/
void
build_pack_libnet(void)
{
u_int first = 1;
u_int len_ipopt = speed.len_ipopt;
u_int len, offset_pack = 0;
u_int len_pack = speed.len_pack;
u_int sended;
u_char *paquete=buf, *punt = (buf_ip+LIBNET_ETH_H);
struct ip2 *ip_p = (struct ip2 *)punt;
copymem((char *)&speed.ip_p, punt, sizeof(struct ip2));
if (speed.seq || speed.time)
{
*speed.seq = stats.nsent + packet.info_seq;
CLR(*speed.seq % mx_dup_ck);
if (speed.time)
{
if ( (packet.tipo_icmp == ICMP_ECHO_REQUEST) ||
!packet.tipo_icmp )
gettimeofday((struct timeval *)speed.time, NULL);
else
*speed.time = htonl(day2milisecs());
}
*speed.cksum = 0;
if (!bad_cksum)
*speed.cksum = in_cksum( (u_short *)buf, tam_icmp );
else
*speed.cksum = 0x6666; /* The Checksum of the Beast xDD */
}
while ( len_pack > 0 )
{
len = len_pack;
if ( len > speed.mtu )
len = speed.mtu;
if ( ( speed.mtu > 7 ) & ( len < len_pack ) )
len &= ~7;
/* All fragments must carry IP options when LSRR|SSRR */
if ( packet.len_ipopt && (first || !packet.rr) )
copymem( packet.ipopt, ip_p + 1, speed.len_ipopt );
else
ip_p->ip_hl = TCAB_IP >> 2;
/* Copy the data...*/
copymem( paquete, punt + sizeof(struct ip2) + speed.len_ipopt, len );
ip_p->ip_len = htons(TCAB_IP + packet.len_ipopt + len);
ip_p->ip_off = htons(offset_pack >> 3);
if (set_df)
ip_p->ip_off |= htons(IP_DF);
if (set_rf)
ip_p->ip_off |= htons(IP_RF);
len_pack-=len;
if ( (offset_pack + speed.mtu) <= ( 65515 - len_ipopt ) )
{
if ( len_pack > 0 )
ip_p->ip_off |= htons(IP_MF);
#ifdef HAVE_IP_IP_CSUM
ip_p->ip_csum = 0x0000;
ip_p->ip_csum = in_cksum( (u_short *)ip_p, TCAB_IP+speed.len_ipopt );
#else
ip_p->ip_sum = 0x0000;
ip_p->ip_sum = in_cksum( (u_short *)ip_p, TCAB_IP+speed.len_ipopt );
#endif
if ( libnet_build_ethernet( packet.mac_dst, packet.mac_src,
ETHERTYPE_IP, buf_ip+LIBNET_ETH_H, ntohs(ip_p->ip_len),
buf_ip
) == -1)
go_out(1,"libnet_build_ethernet error");
sended = libnet_write_link_layer(network, packet.iface2route.name,
buf_ip, ntohs(ip_p->ip_len)+LIBNET_ETH_H);
if (sended < (ntohs(ip_p->ip_len)+LIBNET_ETH_H) )
go_out(1,"libnet_write_link_layer only wrote %d bytes", sended);
}
else
{ /* Last packet must *NOT* have the IP_MF */
#ifdef HAVE_IP_IP_CSUM
ip_p->ip_csum = 0x0000;
ip_p->ip_csum = in_cksum( (u_short *)ip_p, TCAB_IP );
#else
ip_p->ip_sum = 0x0000;
ip_p->ip_sum = in_cksum( (u_short *)ip_p, TCAB_IP );
#endif
sended = libnet_write_link_layer(network, packet.iface2route.name,
buf_ip, ntohs(ip_p->ip_len)+LIBNET_ETH_H);
if (sended < (ntohs(ip_p->ip_len)+LIBNET_ETH_H))
go_out(1,"libnet_write_link_layer only wrote %d bytes", sended);
break;
}
first = 0;
if ( packet.rr || !packet.len_ipopt )
len_ipopt = 0;
paquete+=len;
offset_pack+=len;
}
stats.nsent++;
}
/**************************/
/* Prepare and create the */
/* libpcap handler */
/**************************/
void
prepare_libpcap( void )
{
u_int localnet, netmask;
struct bpf_program fcode;
char msg[PCAP_ERRBUF_SIZE];
char filter[512];
char *expr=NULL;
struct in_addr ppp;
struct mi_ifaz iface;
struct sockaddr_in *aux;
if (packet.mac_src) /* MAC spoofing? */
expr="arp or icmp and dst host %s";
else
expr="icmp and dst host %s";
if ( packet.listen2dev )
{
if ( !strncmp(packet.listen2dev, LOOPB, 2) )
filter[0]=0;
else
{
ppp.s_addr = packet.ip_spoof;
sprintf(filter, expr, inet_ntoa( ppp ) );
}
}
else
{
iface.name[0]=0;
aux = (struct sockaddr_in *)&iface.ip;
aux->sin_addr.s_addr = packet.destino.sin_addr.s_addr;
if ( !look4dev( (struct mi_ifaz *)&iface) )
{
ppp.s_addr = packet.ip_spoof;
sprintf(filter, expr, inet_ntoa( ppp ) );
packet.listen2dev = packet.iface2route.name;
}
else
{
filter[0]=0;
packet.listen2dev = LOOPB;
}
}
if ( !(phandler = pcap_open_live(packet.listen2dev, 128, 1, 1, msg)))
go_out(1,"pcap_open_live error -> %s",msg);
if ( pcap_lookupnet(packet.listen2dev, &localnet, &netmask, msg) < 0 )
go_out(1,"pcap_lookupnet error -> %s",msg);
if ( pcap_compile(phandler, &fcode, filter, 0, netmask) < 0 )
go_out(1,"pcap_compile error -> %s",pcap_geterr(phandler));
if ( pcap_setfilter(phandler, &fcode) < 0 )
go_out(1,"pcap_setfilter error -> %s",pcap_geterr(phandler));
}
/*************************/
/* Read ICMP packets ... */
/*************************/
void
read_icmp( char *buf_snd )
{
u_char *buf_rcv=NULL;
u_char buf_data[128];
int n, tam = sizeof(struct sockaddr);
struct sockaddr origen;
struct icmp2 *icp = (struct icmp2 *) buf_snd;
if ( packet.spoof ) /*If spoof use libpcap */
prepare_libpcap();
else
buf_rcv = buf_data;
while(packet.flood--)
(*packet.builder)();
timeout_func(1);
for ( ; ; )
{
if ( packet.spoof) /* If spoof use libpcap */
{
buf_rcv = read_pcap( phandler, &n );
gettimeofday(&trecv,NULL);
}
else
{
if (( (n = recvfrom( sock, buf_rcv, sizeof(buf_data), 0,
&origen, &tam)) == -1 ) && (errno==EINTR))
continue;
gettimeofday(&trecv,NULL);
}
print_pack( buf_snd, buf_rcv, n, icp->icmp_type, icp->icmp_code,
icp->icmp_id2);
if (packet.count_rx && stats.nrecv >= packet.count_rx)
break;
}
term(1);
}
/*************************/
/* Read a libpcap packet */
/*************************/
char *
read_pcap( pcap_t *phandler, u_int *n )
{
int offset=0;
struct pcap_pkthdr pack_begin;
char *data=NULL;
char *mac_src=NULL;
int linktype;
if (!phandler)
go_out(1,"NULL libpcap handler");
if ( (linktype = pcap_datalink(phandler)) < 0)
go_out(1,"pcap_datalink error -> %s",pcap_geterr(phandler));
switch(linktype)
{
case DLT_EN10MB: offset = 14;
break;
case DLT_IEEE802: offset = 22;
break;
case DLT_SLIP:
case DLT_SLIP_BSDOS: offset = 16;
break;
case DLT_PPP:
case DLT_PPP_BSDOS:
#ifdef SOLARIS
offset = 8;
#else
offset = 4;
#endif
break;
case DLT_FDDI: offset = 13; /* Not really correct */
break;
case DLT_NULL: offset = 4;
break;
case DLT_RAW: offset = 0;
break;
default: go_out(1,"libpcap datalink type -> %d", linktype);
}
do
{
data = (char *) pcap_next(phandler, &pack_begin );
if ( data )
{
data += offset;
if (packet.mac_src) /* Using MAC spoofing? */
{
if ((*data & 0x40) != 0x40)
{
mac_src=(data+7);
if (*mac_src == 1) /* Ok. ARP-REQUEST :) */
if (!memcmp((mac_src+17),(u_char *)&packet.orig,4))
send_arp(packet.orig, mac_src);
}
}
}
} while( !data || ((*data & 0x40) != 0x40) );
*n = pack_begin.caplen - offset;
return data;
}
/********************************/
/* Print a received ICMP packet */
/********************************/
void
print_pack( char *buf_snd, char *buf_rcv, int n, u_char type, u_char code,
u_short id)
{
struct icmp2 *icmp_rcv;
struct ip2 *ip = (struct ip2 *) buf_rcv;
struct ip2 *ip2;
char *ptr_snd, *ptr_rcv;
icmp_rcv = (struct icmp2 *)((char *)ip + (ip->ip_hl << 2) );
if ( ip->ip_hl > 5 ) /* Yeah, there are IP Options */
{
ip_opt_rcv = (char *)(ip + 1); /* Point to IP Options Header */
opt_len_rcv = (ip->ip_hl << 2) - 20;
}
else
ip_opt_rcv = NULL;
if ( icmp_rcv->icmp_type > MAX_C(icmp_info_or_err) )
return;
if ( icmp_info_or_err[icmp_rcv->icmp_type].class == ICMP_ERROR )
{
ptr_snd = (char *)buf_snd;
ip2 = (struct ip2 *)( ((char *)icmp_rcv) + 8); /* Skip ICMP header and unused bits */
ptr_rcv = (char *)((char *)ip2 + (ip2->ip_hl << 2) ); /* Skip the embedded IP header */
if (!memcmp( ptr_rcv, ptr_snd, 8) )
{
/* Go to the appropiate ICMP print Error function ...*/
(*icmp_info_or_err[icmp_rcv->icmp_type].print_packet)
( ip, icmp_rcv );
}
return;
}
if ( icmp_rcv->icmp_type != icmp_info_or_err[type].reply )
return;
if ( (icmp_rcv->icmp_type != ICMP_ROUTER_ADVERT) &&
(icmp_rcv->icmp_type != ICMP_ECHO_REPLY) )
{
if ( icmp_rcv->icmp_id2 != id ) /* Control of ID can't be made */
return; /* within an ICMP Router Advertisement */
}
#if defined(FREEBSD) || defined(NETBSD) || defined (OPENBSD)
if (!packet.spoof)
{
df = ip->ip_off & IP_DF;
rf = ip->ip_off & IP_RF;
}
else
{
#endif
df = ntohs(ip->ip_off) & IP_DF;
rf = ntohs(ip->ip_off) & IP_RF;
#if defined(FREEBSD) || defined(NETBSD) || defined (OPENBSD)
}
#endif
/* Go to the appropiate ICMP print reply function ...*/
(*icmp_info_or_err[icmp_rcv->icmp_type].print_packet)
( ip, icmp_rcv );
if ( packet.tipo_icmp != ICMP_ROUTER_SOLICIT )
{
if (TST(icmp_rcv->icmp_seq2 % mx_dup_ck))
{
++stats.nrep;
write_log(1," (DUP!)");
}
else
{
stats.nrecv++;
SET(icmp_rcv->icmp_seq2 % mx_dup_ck);
}
}
else
stats.nrecv++;
if ( ip_opt_rcv )
print_ip_opt();
write_log(1,"\n");
}
void
dont_print( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
write_log(1, "%d bytes from %s: %s%sttl=%d TOS=%d (%s 0x%X) -> Uuuu?",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
inet_ntoa(ip->ip_src),
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl,
ip->ip_tos,
icmp_info_or_err[icmp_rcv->icmp_type].name,
icmp_rcv->icmp_type);
}
/***********************************/
/* Print an ICMP Info Reply packet */
/***********************************/
void
print_info_reply( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
os_finger = 12;
write_log(1,"%d bytes from %s: seq=%d %s%sttl=%d TOS=%d %s",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
inet_ntoa(ip->ip_src),
icmp_rcv->icmp_seq2,
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl, ip->ip_tos,
icmp_info_or_err[icmp_rcv->icmp_type].name);
}
/*****************************************/
/* Print an ICMP Time stamp Reply packet */
/*****************************************/
void
print_timestamp_reply( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
u_long otime, ttime, now;
static u_long last=0;
static u_short osfixed=0;
long diff=0;
char *text;
otime = ntohl(icmp_rcv->icmp_otime2);
ttime = ntohl(icmp_rcv->icmp_ttime2);
now = (ttime & ((unsigned long) (2 << 30) - 1)) - otime;
if (ttime & (unsigned long) (2 << 30))
{
if (do_fingerprint &&!osfixed)
{
if (!last)
{
os_finger=8; /* Win/Cisco */
last=now;
}
else
{
diff=last-now;
if (diff<0)
diff*=(-1);
osfixed=1;
if (diff>999) /* Only Window$ is totally crazy! */
os_finger=2; /* Win */
else
os_finger=3; /* Cisco */
}
}
text ="*";
}
else
{
if (do_fingerprint && !osfixed)
{
if (!last)
{
os_finger=7; /* Win/Unix */
last=now;
}
else
{
diff=last-now;
if (diff<0)
diff*=(-1);
osfixed=1;
if (diff>999) /* Only Window$ is totally crazy! */
os_finger=2;/* Win */
else
os_finger=4; /* Unix */
}
}
text = "";
}
write_log(1,"%d bytes from %s: seq=%d %s%sttl=%d TOS=%d diff=%ld%s",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
inet_ntoa(ip->ip_src), icmp_rcv->icmp_seq2,
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl, ip->ip_tos,
now,
text );
}
/*******************************************/
/* Print an ICMP Address Mask Reply packet */
/*******************************************/
void
print_address_reply( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
struct in_addr mascara;
char src[16];
mascara.s_addr = icmp_rcv->icmp_mask2;
strncpy(src,inet_ntoa(ip->ip_src),sizeof(src)); /* Fuck static alloc */
if (do_fingerprint)
{
if (packet.mtu && !(mascara.s_addr))
os_finger=6;
else
{
if ( packet.mtu && mascara.s_addr && (ip->ip_ttl<129) )
os_finger=10;
else
{
if (df)
os_finger=6;
else
os_finger=9;
}
}
}
write_log(1, "%d bytes from %s: seq=%d %s%sttl=%d TOS=%d mask=%s",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
src,
icmp_rcv->icmp_seq2,
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl, ip->ip_tos,
inet_ntoa(mascara)
);
}
/*********************************************/
/* Print an ICMP Router Advertisement packet */
/*********************************************/
void
print_router_reply( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
int entry;
u_int life;
struct id_rdiscovery *data_rdisc;
struct hostent *thishost;
char life_str[16];
static char radvert[ 10 * sizeof(struct id_rdiscovery)];
static int radvert_len;
life = ntohs(icmp_rcv->icmp_lifetime2);
if ( life < 60 )
sprintf( life_str, "%02d secs", life);
else
{
if ( life < 3600 )
sprintf( life_str, "%02d:%02d min", life / 60,
life % 60);
else
sprintf( life_str, "%02d:%02d:%02d hours", life / 3600,
(life % 3600) / 60, life % 60);
}
write_log(1,"%d bytes from %s: %s%sttl=%d TOS=%d Advert Life %s",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
inet_ntoa(ip->ip_src),
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl, ip->ip_tos, life_str
);
data_rdisc = (struct id_rdiscovery *) &icmp_rcv->icmp_rdiscovery;
if ( ( (icmp_rcv->icmp_num_addr * sizeof(struct id_rdiscovery)) == radvert_len)
&& !memcmp((void *)data_rdisc, radvert, radvert_len) )
{
write_log(1," (same song)");
return;
}
radvert_len = icmp_rcv->icmp_num_addr * sizeof(struct id_rdiscovery);
copymem((char *)data_rdisc, radvert, radvert_len);
for ( entry=1; entry <= icmp_rcv->icmp_num_addr; entry++, data_rdisc++)
{
if ( resolve )
{
thishost = gethostbyaddr((char *)&data_rdisc->router_addr, 4,
AF_INET );
if ( !thishost )
write_log(1, "\n R%d = %s", entry,
inet_ntoa(data_rdisc->router_addr));
else
write_log(1, "\n R%d = %s", entry,
thishost->h_name);
}
else
write_log(1, "\n R%d = %s", entry,
inet_ntoa(data_rdisc->router_addr));
#if defined(SOLARIS_27) || defined(NETBSD) || defined(OPENBSD)
write_log(1, "/%d", ntohl(data_rdisc->pref_level.s_addr) );
#else
write_log(1, "/%ld", ntohl(data_rdisc->pref_level.s_addr) );
#endif
} /* for...next advert entry */
}
/***********************************/
/* Print an ICMP Echo Reply packet */
/***********************************/
void
print_echo_reply( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
float rtt;
tsend=(struct timeval *)icmp_rcv->icmp_data2;
if ( (trecv.tv_usec -= tsend->tv_usec) < 0 )
{
--trecv.tv_sec;
trecv.tv_usec += 1000000;
}
trecv.tv_sec -= tsend->tv_sec;
rtt = trecv.tv_sec*1000.0 + trecv.tv_usec/1000.0;
if (do_fingerprint)
{
if ( packet.cod_icmp && !icmp_rcv->icmp_code ) /* Win box */
{
if (!ip->ip_tos)
os_finger = 15; /* Win2K */
else
os_finger = 10; /* Other*/
}
else
{
if (df)
os_finger = 11;
else
{
if (ip->ip_ttl < 65)
os_finger = 13;
else
{ /* UNIX box */
if (!ip->ip_tos)
os_finger=14;
else
os_finger = 5;
}
}
}
}
write_log(1, "%d bytes from %s: seq=%d %s%sttl=%d TOS=%d time=%.3f ms",
#ifdef LINUX
ntohs(ip->ip_len)-(ip->ip_hl << 2),
#else
packet.spoof?ntohs(ip->ip_len)-(ip->ip_hl << 2):ip->ip_len,
#endif
inet_ntoa(ip->ip_src),
icmp_rcv->icmp_seq2,
rf ? "RF! " : "",
df ? "DF! " : "",
ip->ip_ttl,
ip->ip_tos,
rtt
);
if ( rtt > stats.rtt_max)
stats.rtt_max = rtt;
if ( rtt < stats.rtt_min )
stats.rtt_min = rtt;
stats.rtt_tot+=rtt;
}
/********************************************/
/* Print an ICMP Destination Unreach packet */
/********************************************/
void
print_dst_unreach( struct ip2 *ip2, struct icmp2 *icmp_rcv )
{
u_char *host_orig, *dst_port;
struct protoent *proto;
struct hostent *thishost;
struct ip2 *ip = (struct ip2 *)&(icmp_rcv->icmp_ip);
write_log(1, "Ouch!! %s sings", inet_ntoa(ip2->ip_src));
if ( icmp_rcv->icmp_code >= max_cod_u )
{
write_log(1," Destination Unreach %d :?!!\n",icmp_rcv->icmp_code);
return;
}
if ( resolve )
{
thishost = gethostbyaddr((char *)&ip->ip_dst, 4, AF_INET );
if ( !thishost )
host_orig = inet_ntoa(ip->ip_dst);
else
host_orig = thishost->h_name;
}
else
host_orig = inet_ntoa(ip->ip_dst);
switch(icmp_rcv->icmp_code)
{
case 0: write_log(1," net %s unreachable!!\n",host_orig);
break;
case 1: write_log(1," host %s unreachable!!\n",host_orig);
break;
case 2: if ( !(proto = getprotobynumber(ip->ip_p) ) )
write_log(1," protocol %d on host %s is unreachable!!\n", ip->ip_p, host_orig);
else
write_log(1," protocol %s on host %s is unreachable!!\n", proto->p_name, host_orig);
break;
case 3: dst_port = (u_char *)((char *)ip + (ip->ip_hl << 2) );
dst_port+=2;
if ( !(proto = getprotobynumber(ip->ip_p) ) )
write_log(1," port %d (proto %d) on host %s is unreachable!!\n", *dst_port, ip->ip_p, host_orig);
else
write_log(1," port %d/%s on host %s is unreachable!!\n", *dst_port, proto->p_name, host_orig);
break;
case 4: write_log(1," %s is unreachable 'cause Fragment Needed but DF was Set!!\n", host_orig);
break;
case 5: write_log(1," Source Routing Failed!!\n");
break;
case 6: write_log(1," net %s Unknown!!\n", host_orig);
break;
case 7: write_log(1," host %s Unknown!!\n", host_orig);
break;
case 8: write_log(1," %s is unreachable 'cause source host is isolated!!\n", host_orig);
break;
case 9: write_log(1," communication with net %s administratively prohibited (maybe firewalled?)!!\n", host_orig);
break;
case 10:write_log(1," communication with host %s administratively prohibited (maybe firewalled?)!!\n", host_orig);
break;
case 11:write_log(1," Type Of Service %d to reach net %s is not allowed!!\n", ip->ip_tos, host_orig);
break;
case 12:write_log(1," Type Of Service %d to reach host %s is not allowed!!\n", ip->ip_tos, host_orig);
break;
case 13:write_log(1," communication with %s is administratively prohibited (maybe firewalled?)!!\n", host_orig);
break;
case 14:write_log(1," %s is unreachable 'cause IP precedence is not allowed!!\n", host_orig);
break;
case 15:write_log(1," %s is unreachable 'cause IP precedence is smaller than allowed!!\n", host_orig);
break;
}
}
/**************************************/
/* Print an ICMP Source Quench packet */
/**************************************/
void
print_src_quench( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
write_log(1,"Ouch!! %s sings Source Quench!!\n", inet_ntoa(ip->ip_src));
}
/*********************************/
/* Print an ICMP Redirect packet */
/*********************************/
void
print_redirect( struct ip2 *ip2, struct icmp2 *icmp_rcv )
{
u_char dest_host[64], gway_host[64], aux_name[16];
struct hostent *gwayhost, *desthost;
struct ip2 *ip = (struct ip2 *)&(icmp_rcv->icmp_ip);
strncpy(aux_name,inet_ntoa(ip2->ip_src), sizeof(aux_name));
write_log(1,"Ouch!! %s sings", aux_name);
if ( icmp_rcv->icmp_code >= max_cod_r )
{
write_log(1," Redirect(%d)!!\n", icmp_rcv->icmp_code);
return;
}
if ( resolve )
{
desthost = gethostbyaddr((char *)&ip->ip_dst, 4, AF_INET );
if ( !desthost )
strncpy(dest_host,inet_ntoa(ip->ip_dst), sizeof(dest_host));
else
strncpy(dest_host,desthost->h_name, sizeof(dest_host));
gwayhost = gethostbyaddr((char *)&icmp_rcv->icmp_gwaddr2, 4, AF_INET );
if ( !gwayhost )
strncpy(gway_host, inet_ntoa(icmp_rcv->icmp_gwaddr2), sizeof(gway_host));
else
strncpy(gway_host, gwayhost->h_name, sizeof(gway_host));
}
else
{
strncpy(dest_host,inet_ntoa(ip->ip_dst),sizeof(dest_host));
strncpy(gway_host, inet_ntoa(icmp_rcv->icmp_gwaddr2), sizeof(gway_host));
}
switch(icmp_rcv->icmp_code)
{
case 0: write_log(1," redirect %s to net %s!!\n", dest_host, gway_host );
break;
case 1: write_log(1," redirect %s to host %s!!\n", dest_host, gway_host);
break;
case 2: write_log(1," redirect-tos %s to net %s!!\n", dest_host, gway_host );
break;
case 3: write_log(1," redirec-tos %s to host %s!!\n", dest_host, gway_host );
break;
}
}
/**************************************/
/* Print an ICMP Time Exceeded packet */
/**************************************/
void
print_time_xcd( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
if (icmp_rcv->icmp_code >= max_l_cod_t )
write_log(1,"Ouch!! %s sings Time Exceeded(%d)!!\n", inet_ntoa(ip->ip_src),
icmp_rcv->icmp_code);
else
write_log(1,"Ouch!! %s sings %s!!\n", inet_ntoa(ip->ip_src),
l_cod_t[icmp_rcv->icmp_code]);
}
/******************************************/
/* Print an ICMP Parameter Problem packet */
/******************************************/
void
print_param_prob( struct ip2 *ip, struct icmp2 *icmp_rcv )
{
write_log(1,"Ouch!! %s sings Param Problem on byte %d!!\n", inet_ntoa(ip->ip_src),
icmp_rcv->icmp_pptr2);
}
/**********************************************/
/* This function has been obtained from the */
/* book "UNIX NETWORK PROGRAMMING" (1st Ed.), */
/* by Richard W. Stevens (Hyper-Guru). */
/* ;) */
/**********************************************/
int
in_cksum( u_short *p, int n)
{
register u_short answer;
register long sum = 0;
u_short odd_byte = 0;
while( n > 1 )
{
sum += *p++;
n -= 2;
}
/* mop up an odd byte, if necessary */
if( n == 1 )
{
*(u_char *)(&odd_byte) = *(u_char *)p;
sum += odd_byte;
}
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* ones-complement, truncate*/
return (answer);
}
char
*pasa( struct sockaddr_in *sin )
{
return ((char *) inet_ntoa(sin->sin_addr));
}
/***************************************/
/* Exit from program without printing */
/* a system error. The returned code */
/* == num_err. */
/***************************************/
void
go_out( short int num_err, char *msg, ... )
{
va_list ap; /* Variable argument list...*/
if (!Quiet)
{
if ( num_err )
{
fprintf(stderr,"%s: ", PACKAGE);
va_start(ap,msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr,"\n");
}
}
exit(num_err);
}
/**********************************/
/* Exit from program printing the */
/* system error. */
/* Return num_err to the system. */
/**********************************/
void
go_out_error( short int num_err, char *msg )
{
if (!Quiet)
fprintf(stderr,"%s: %s -> %s\n", PACKAGE, msg,
sys_errlist[errno] );
exit(num_err);
}
/***********************************/
/* Write to logfile (mode=0) or to */
/* the stdout & logfile (mode=1), */
/***********************************/
void
write_log( u_short mode, char *msg, ... )
{
va_list ap; /* Variable argument list...*/
va_start(ap,msg);
if ((!quiet && mode) || (!Quiet && mode==2))
vfprintf(stdout, msg, ap);
if (packet.logfile)
vfprintf(packet.logfile, msg,ap);
va_end(ap);
}
/**********************/
/* Init the log file. */
/**********************/
void
init_log(int argc, char **argv)
{
u_short i;
u_char this_name[128];
time_t this_time;
this_time = time(NULL);
gethostname(this_name,sizeof(this_name));
write_log(0,"# %s v%s initiated on %s at %s", PACKAGE, VERSION, this_name,
ctime(&this_time));
write_log(0,"# Command line:\n# -> ");
for(i=0;i<argc;i++)
write_log(0,"%s ",argv[i]);
write_log(0,"\n");
}
/************************/
/* Finish the log file. */
/************************/
void
finish_log(void)
{
time_t this_time;
this_time = time(NULL);
write_log(0,"# %s finished at %s\n",PACKAGE, ctime(&this_time));
fflush(packet.logfile);
fclose(packet.logfile);
}
/************************/
/* Init packet struct */
/* with default values. */
/************************/
void
init_packet_struct( struct my_pack *packet )
{
packet->logfile = NULL;
packet->ip_spoof = 0x0000;
packet->spoof = 0;
packet->destino.sin_family = AF_INET;
packet->destino.sin_addr.s_addr = 0x0001;
initmem((char *)&packet->iface2route, sizeof(struct mi_ifaz));
packet->listen2dev = NULL;
packet->gway = 0x0001;
packet->dest_red = 0x0001;
packet->orig = 0x0001;
packet->cod_icmp = 0;
packet->tipo_icmp = 255;
packet->protocol = IPPROTO_TCP;
packet->p_origen = 0;
packet->p_destino = 0;
packet->maskaddr = 0;
packet->router = NULL;
packet->lifetime = LIFETIME_DFL;
packet->num_routers = 0;
packet->size_pattern = 0;
packet->timeout = TIMEOUT_DFL;
packet->flood = 0;
packet->garbage = 0;
packet->pointer = 0;
packet->mtu = 0;
packet->rr = 0;
packet->ipopt = 0;
packet->len_ipopt = 0;
packet->count_rx = 0;
packet->ttl = TTL_DFL;
packet->tos = TOS_DFL;
packet->info_id = 0;
packet->info_seq = 0;
packet->mac_src = NULL;
packet->mac_dst = NULL;
packet->builder = build_pack;
stats.nsent = 0;
stats.nrecv = 0;
stats.nrep = 0;
stats.rtt_min = 99999999.9;
stats.rtt_max = 0.0;
stats.rtt_tot = 0.0;
}
/********************/
/* Signals handling */
/********************/
void
handle_signals( void )
{
posix_signal( SIGIO, SIG_IGN );
posix_signal( SIGURG, SIG_IGN );
posix_signal( SIGTSTP, SIG_IGN );
posix_signal( SIGQUIT, SIG_IGN );
posix_signal( SIGPIPE, SIG_IGN );
posix_signal( SIGCHLD, SIG_IGN );
posix_signal( SIGTERM, term );
posix_signal( SIGINT, term );
posix_signal( SIGALRM, timeout_func );
}
/*****************************/
/* SIGINT interrupt handler */
/*****************************/
void
term( int signal )
{
write_log(2,"\n--- %s sing statistics ---\n", packet.name_dst);
write_log(2,"%ld packets transmitted, %ld packets received,",
stats.nsent, stats.nrecv);
if (stats.nrep)
write_log(2," +%ld duplicates,",stats.nrep);
write_log(2," %ld%% packet loss\n",
stats.nrecv?((stats.nsent-stats.nrecv)*100)/stats.nsent:100);
if ( packet.tipo_icmp == ICMP_ECHO_REQUEST && stats.nrecv )
write_log(2,"round-trip min/avg/max = %.3f/%.3f/%.3f ms\n", stats.rtt_min,
stats.rtt_tot/stats.nrecv, stats.rtt_max);
if (do_fingerprint)
{
if (stats.nrecv)
{
write_log(2,"\n<*> Remote OS on %s %s\n\n", packet.name_dst, osfingers[os_finger]);
}
else
write_log(2,"\n<*> Ouch!! I can't guess the remote OS without a reply!\n\n");
}
if (packet.spoof)
pcap_close(phandler);
if (packet.logfile)
finish_log();
stats.nrecv?exit(0):exit(2);
}
/*****************************/
/* SIGALRM interrupt handler */
/*****************************/
void
timeout_func( int nothing )
{
if (!packet.count_rx || stats.nsent < packet.count_rx )
{
(*packet.builder)();
alarm(packet.timeout);
}
else
{
if (done)
term(1);
done=1;
if (stats.nrecv)
alarm(packet.timeout);
else
alarm(TIMEOUT_MAX);
}
return;
}
/****************************/
/* POSIX calls with signals */
/****************************/
int
posix_signal( int signo, void (*handler)() )
{
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset( &act.sa_mask );
switch( signo )
{
case SIGALRM: break;
case SIGINT:
case SIGTERM: sigaddset( &act.sa_mask, SIGALRM );
default: act.sa_flags |= SA_RESTART;
break;
}
if ( sigaction( signo, &act, NULL ) == -1 )
return -1;
return 0;
}
/********************************/
/* Send an ARP Reply to the MAC */
/* 'mac_dst' */
/********************************/
void
send_arp(u_long orig, u_char *mac_dst)
{
u_char *arp_pack=(u_char *)calloc(LIBNET_ARP_H+LIBNET_ETH_H+18,1);
if (!arp_pack)
go_out(2,"Out of memory on arp_packet");
libnet_build_ethernet( mac_dst, packet.mac_src,
ETHERTYPE_ARP, NULL, 0, arp_pack );
libnet_build_arp( ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REPLY,
packet.mac_src, (u_char *)&orig, mac_dst,
(u_char *)&packet.destino.sin_addr.s_addr, NULL, 0,
arp_pack + ETH_H);
libnet_write_link_layer( network, packet.iface2route.name, arp_pack,
LIBNET_ARP_H + LIBNET_ETH_H+18);
free(arp_pack);
}
|