home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / knstat_openbsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  12.6 KB  |  373 lines

  1. /*
  2.  * Name: knstat for OpenBSD
  3.  * Date: Mon Feb 28 21:38:57 2000
  4.  * Author: pIGpEN [pigpen@s0ftpj.org, deadhead@sikurezza.org]
  5.  *
  6.  * SoftProject Digital Security for Y2K
  7.  * Sikurezza.org - Italian Security Mailinglist
  8.  *
  9.  * COFFEE-WARE LICENSE - This source code is like "THE BEER-WARE LICENSE" by
  10.  * Poul-Henning Kamp <phk@FreeBSD.ORG> but you can give me in return a coffee.
  11.  *
  12.  * Tested on: OpenBSD 2.6 kern#0 i386
  13.  *
  14.  * Little revision - Mar 08 2000 ( This code is very simple... ) 
  15.  *
  16.  * $ calendar
  17.  *
  18.  * Mar 08 Ron "Pigpen" McKernan (Grateful Dead) dies in California, 1973
  19.  *
  20.  * :(   
  21.  *                         Let His Memory Shine!
  22.  */
  23.  
  24. /*
  25.  * This is a simple porting of knstat on your OpenBSD box...
  26.  *
  27.  * compile with: cc filename -lkvm 
  28.  */ 
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include <err.h>
  34. #include <sysexits.h>
  35.  
  36. #include <sys/types.h>
  37. #include <sys/param.h>
  38. #include <sys/socket.h>
  39.  
  40. #include <netinet/in.h>
  41. #include <netinet/in_systm.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/ip_var.h>
  44. #include <netinet/udp.h>
  45. #include <netinet/udp_var.h>
  46. #include <netinet/tcp.h>
  47. #include <netinet/tcp_timer.h>
  48. #include <netinet/tcp_var.h>
  49. #include <netinet/ip_icmp.h>
  50. #include <netinet/icmp_var.h>
  51. #include <netinet/igmp.h>
  52. #include <netinet/igmp_var.h>
  53.  
  54. #include <nlist.h>
  55. #include <kvm.h>
  56.  
  57. #define        WAIT
  58. #define        Error(s)    err(EX_UNAVAILABLE, s);
  59. #define        E(s)        if(!strcmp(s, arg[1]))
  60.  
  61. void        usage        __P((char *));
  62. void        print_ip    __P((kvm_t *));
  63. void        print_udp    __P((kvm_t *));
  64. void        print_tcp    __P((kvm_t *));
  65. void        print_icmp    __P((kvm_t *));
  66. void        print_igmp    __P((kvm_t *));
  67.  
  68. struct prot {
  69.     char *name;
  70.     void (*funct) (kvm_t *);
  71. };
  72.     
  73.  
  74. struct prot protos[] = {
  75.     { "-ip"  , print_ip   },
  76.     { "-udp" , print_udp  },
  77.     { "-tcp" , print_tcp  },
  78.     { "-icmp", print_icmp },
  79.     { "-igmp", print_igmp },
  80. };
  81.  
  82. struct nlist list[] = {
  83.      {"_ipstat"},
  84.     {"_udpstat"},
  85.     {"_tcpstat"},
  86.     {"_icmpstat"},
  87.     {"_igmpstat"},
  88.     {NULL}
  89. };    
  90.     
  91. int main(int narg, char **arg)
  92. {
  93.     kvm_t *kd;
  94.     int i;
  95.     int len = sizeof(protos) / sizeof(struct prot);
  96.  
  97.     
  98.     if(narg != 2){
  99.         usage(arg[0]);
  100.         exit(0);
  101.     }
  102.  
  103.     /* Think different */
  104.     
  105.     for(i=0; i < len; i++) 
  106.         E(protos[i].name) {
  107.           if(!(kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL))) 
  108.             Error("kvm_openfiles()");
  109.           if(kvm_nlist(kd, list) != 0)
  110.             Error("kvm_nlist()");
  111.  
  112.             (*protos[i].funct) (kd);
  113.           kvm_close(kd);    
  114.            return 1;    
  115.         }
  116.  
  117.     usage(arg[0]);
  118.  
  119.     return 0; 
  120. }
  121.  
  122. void usage(char *cmdname)
  123. {
  124.     printf("Usage:\n"
  125.            "\t%s -option\n\n"
  126.            "Option:   -ip\n" 
  127.            "          -icmp\n"
  128.            "          -igmp\n"
  129.            "          -tcp\n"
  130.            "          -udp\n", cmdname);
  131. }           
  132.  
  133.  
  134.  
  135. /*
  136.  * You can cover printf() with a macro... but I'm fucking about... so
  137.  * I have time to spend... 
  138.  */
  139.  
  140. void print_ip(kvm_t *kd)
  141. {
  142.  struct ipstat i_stat;
  143.  
  144.  
  145.  if(!list[0].n_value)
  146.     Error("[print_ip] checking nlist");
  147.  
  148.  kvm_read(kd, list[0].n_value, &i_stat, sizeof i_stat);
  149.  
  150.  printf("IP Statistics\n\n");
  151.  
  152.  printf("\t\treceived         [ %ld ]\n", i_stat.ips_total);
  153.  printf("\t\tbad checksum     [ %ld ]\n", i_stat.ips_badsum);
  154.  printf("\t\tpkts too short   [ %ld ]\n", i_stat.ips_tooshort);
  155.  printf("\t\tno enough data   [ %ld ]\n", i_stat.ips_toosmall);
  156.  printf("\t\tiph len < data   [ %ld ]\n", i_stat.ips_badhlen);
  157.  printf("\t\tip len < iph len [ %ld ]\n", i_stat.ips_badlen);
  158.  printf("\t\tfragments        [ %ld ]\n", i_stat.ips_fragments);
  159.  printf("\t\tfrags dropped    [ %ld ]\n", i_stat.ips_fragdropped);
  160.  printf("\t\tfrags timeout    [ %ld ]\n", i_stat.ips_fragtimeout);
  161.  printf("\t\tforwarded        [ %ld ]\n", i_stat.ips_forward);
  162.  printf("\t\tcant forward     [ %ld ]\n", i_stat.ips_cantforward);
  163.  printf("\t\tredirect sent    [ %ld ]\n", i_stat.ips_redirectsent);
  164.  printf("\t\tproto unknown    [ %ld ]\n", i_stat.ips_noproto);
  165.  printf("\t\tiplen > maxpksz  [ %ld ]\n", i_stat.ips_toolong);
  166.  printf("\t\tip version != 4  [ %ld ]\n", i_stat.ips_badvers);
  167.  printf("\t\ttotal raw gen    [ %ld ]\n", i_stat.ips_rawout);
  168.  
  169. }
  170.  
  171. void print_udp(kvm_t *kd)
  172.  struct udpstat u_stat;
  173.  
  174.  
  175.  if(!list[1].n_value)
  176.     Error("[print_udp] checking nlist");
  177.  
  178.  kvm_read(kd, list[1].n_value, &u_stat, sizeof u_stat); 
  179.  
  180.  printf("UDP Statistics\n\n"); 
  181.  
  182.  printf("Total input  packets:      %ld\n",        u_stat.udps_ipackets);
  183.  printf("\t\t\t\tPacket shorter than header: %ld\n", u_stat.udps_hdrops);
  184.  printf("\t\t\t\tChecksum error:             %ld\n", u_stat.udps_badsum);
  185.  printf("\t\t\t\tData len larger than pkt:   %ld\n", u_stat.udps_badlen);
  186.  printf("\t\t\t\tNo socket on port:          %ld\n", u_stat.udps_noport);
  187.  printf("\t\t\t\tArrived as broadcast:       %ld\n", u_stat.udps_noportbcast);
  188.  printf("\t\t\t\tNot delivered:              %ld\n", u_stat.udps_fullsock);
  189.  printf("Total ouput packets:       %ld\n\n",       u_stat.udps_opackets);
  190. }    
  191.  
  192. void print_tcp(kvm_t *kd)
  193. {
  194.  struct tcpstat t_stat;
  195.  
  196.  
  197.  if(!list[2].n_value)
  198.     Error("[print_tcp] checking nvalue");  
  199.  
  200.  kvm_read(kd, list[2].n_value, &t_stat, sizeof t_stat);
  201.  
  202.  printf("TCP Statistics\n\n");
  203.  
  204.  printf("Connection:\n");
  205.  printf("\t\tinitiated:               [ %d ]\n", t_stat.tcps_connattempt);
  206.  printf("\t\taccepted:                [ %d ]\n", t_stat.tcps_accepts);
  207.  printf("\t\testabilished             [ %d ]\n", t_stat.tcps_connects);
  208.  printf("\t\tdropped                  [ %d ]\n", t_stat.tcps_drops);
  209.  printf("\t\tembryonic dropped        [ %d ]\n", t_stat.tcps_conndrops);
  210.  printf("\t\tkeepalive dropped        [ %d ]\n", t_stat.tcps_keepdrops);
  211.  printf("\t\tclosed                   [ %d ]\n", t_stat.tcps_closed);
  212.  printf("\nTimers:\n");
  213.  printf("\t\tsegs timed               [ %d ]\n", t_stat.tcps_segstimed);
  214.  printf("\t\trtt updated              [ %d ]\n", t_stat.tcps_rttupdated);
  215.  printf("\t\tdelayed acks sent        [ %d ]\n", t_stat.tcps_delack);
  216.  printf("\t\tdropped in rxmt timeouts [ %d ]\n", t_stat.tcps_timeoutdrop);
  217.  printf("\t\tretrasmit timeouts       [ %d ]\n", t_stat.tcps_rexmttimeo);
  218.  printf("\t\tpersist timeouts         [ %d ]\n", t_stat.tcps_persisttimeo);
  219.  printf("\t\tkeepalive timeouts       [ %d ]\n", t_stat.tcps_keeptimeo);
  220.  printf("\t\tkeepalive probes sent    [ %d ]\n", t_stat.tcps_keepprobe);
  221. #ifdef    WAIT 
  222.  getchar();
  223. #endif 
  224.  printf("\nPackets\n");
  225.  printf("\t\tsent                     [ %d ]\n", t_stat.tcps_sndtotal);
  226.  printf("\t\tdata pkt                 [ %d ]\n", t_stat.tcps_sndpack);
  227.  printf("\t\tdata bytes               [ %ld ]\n", 
  228.                     (long) t_stat.tcps_sndbyte);
  229.  printf("\t\tdata pkt retrasmitted    [ %d ]\n", t_stat.tcps_sndrexmitpack);
  230.  printf("\t\tdata bytes retrasmitted  [ %ld ]\n", 
  231.                     (long)t_stat.tcps_sndrexmitbyte);
  232.  printf("\t\tack only pkts            [ %d ]\n", t_stat.tcps_sndacks);
  233.  printf("\t\turg only pkts            [ %d ]\n", t_stat.tcps_sndurg);
  234.  printf("\t\twin update only pkt      [ %d ]\n", t_stat.tcps_sndwinup);
  235.  printf("\t\tsyn|fin|rst pkt          [ %d ]\n", t_stat.tcps_sndctrl);
  236.  printf("\t\twindow probes            [ %d ]\n", t_stat.tcps_sndprobe);
  237.  printf("\n\n");
  238. #ifdef    WAIT 
  239.  getchar();
  240. #endif 
  241.  printf("\t\treceived                 [ %ld ]\n", (long)
  242.                         t_stat.tcps_rcvtotal);
  243.  printf("\t\tpkt in sequence          [ %d ]\n", t_stat.tcps_rcvpack);
  244.  printf("\t\tbyte in sequence         [ %ld ]\n", (long) t_stat.tcps_rcvbyte);
  245.  printf("\t\tpkt with checksum errors [ %d ]\n", t_stat.tcps_rcvbadsum);
  246.  printf("\t\tpkt with bad offset      [ %d ]\n", t_stat.tcps_rcvbadoff);
  247.  printf("\t\tpkt received too short   [ %d ]\n", t_stat.tcps_rcvshort);
  248.  printf("\t\tduplicate only pkts      [ %d ]\n", t_stat.tcps_rcvduppack);
  249.  printf("\t\tduplicate only bytes     [ %ld ]\n", 
  250.                         (long) t_stat.tcps_rcvdupbyte);
  251.  printf("\t\tpartial duplicate data   [ %d ]\n", t_stat.tcps_rcvpartduppack);
  252.  printf("\t\tpartial duplicate bytes  [ %ld ]\n", 
  253.                     (long) t_stat.tcps_rcvpartdupbyte);
  254.  printf("\t\tout of order pkts        [ %d ]\n", t_stat.tcps_rcvoopack);
  255.  printf("\t\tout of order bytes       [ %ld ]\n", 
  256.                         (long) t_stat.tcps_rcvoobyte);
  257.  printf("\t\tpkts with data after win [ %d ]\n", t_stat.tcps_rcvpackafterwin);
  258.  printf("\t\tbytes received after win [ %ld ]\n", 
  259.                     (long) t_stat.tcps_rcvbyteafterwin);
  260.  printf("\t\tpkts rcvd after close    [ %d ]\n", t_stat.tcps_rcvafterclose);
  261.  printf("\t\tpkts rcvd win probe      [ %d ]\n", t_stat.tcps_rcvwinprobe);
  262.  printf("\t\tduplicate acks           [ %d ]\n", t_stat.tcps_rcvdupack);
  263.  printf("\t\tacks for unsent data     [ %d ]\n", t_stat.tcps_rcvacktoomuch);
  264.  printf("\t\tacks packets             [ %d ]\n", t_stat.tcps_rcvackpack);
  265.  printf("\t\tbytes acked by rcvd acks [ %d ]\n", t_stat.tcps_rcvwinupd);
  266.  printf("\t\tseg dropped due to PAWS  [ %d ]\n", t_stat.tcps_pawsdrop);
  267.  printf("\t\tbogus syn                [ %d ]\n", t_stat.tcps_badsyn);
  268.  
  269.  
  270. void print_icmp(kvm_t *kd)
  271. {
  272.  struct icmpstat i_stat;
  273.  
  274.  
  275.  if(!list[3].n_value) 
  276.     Error("[print_icmp] checking list n_value");
  277.  
  278.  kvm_read(kd, list[3].n_value, &i_stat, sizeof(i_stat));  
  279.  
  280.  printf("ICMP Statistics\n");
  281.  printf("\t\tNumber of calls to icmp_error  [ %ld ]\n", i_stat.icps_error);
  282.  printf("\t\ticmp_code out of range         [ %ld ]\n", i_stat.icps_badcode);
  283.  printf("\t\tpkts < ICMP_MINLEN             [ %ld ]\n", i_stat.icps_tooshort);
  284.  printf("\t\tbad checksum                   [ %ld ]\n", i_stat.icps_checksum);
  285.  printf("\t\tbad length                     [ %ld ]\n", i_stat.icps_badlen);
  286.  printf("\t\tnumber of responses            [ %ld ]\n", i_stat.icps_reflect);
  287.  printf("\t\tm/bcast echo requests dropped  [ %ld ]\n", i_stat.icps_bmcastecho);
  288.  printf("Input Packet\n\n");
  289.  printf("\tEcho Reply                         [ %ld ]\n",i_stat.icps_inhist[0]);
  290.  printf("\tDestination Unreachable            [ %ld ]\n",i_stat.icps_inhist[3]); 
  291.  printf("\tSource Quench                      [ %ld ]\n",
  292.                         i_stat.icps_inhist[4]);
  293.  printf("\tRedirect                           [ %ld ]\n",
  294.                         i_stat.icps_inhist[5]);
  295.  printf("\tEcho                               [ %ld ]\n",
  296.                         i_stat.icps_inhist[8]);
  297.  printf("\tRouter Advert                      [ %ld ]\n",
  298.                         i_stat.icps_inhist[9]);
  299.  printf("\tRouter Solicitation                [ %ld ]\n",
  300.                         i_stat.icps_inhist[10]);
  301.  printf("\tTime Exceed                        [ %ld ]\n",
  302.                         i_stat.icps_inhist[11]);
  303.  printf("\tTimestamp Request                  [ %ld ]\n",
  304.                         i_stat.icps_inhist[13]);
  305.  printf("\tTimestamp Reply                    [ %ld ]\n",
  306.                         i_stat.icps_inhist[14]);
  307.  printf("\tInformation Request                [ %ld ]\n",
  308.                         i_stat.icps_inhist[15]);
  309.  printf("\tInformation Reply                  [ %ld ]\n",
  310.                         i_stat.icps_inhist[16]);
  311.  printf("\tAddress Mask Request               [ %ld ]\n",
  312.                         i_stat.icps_inhist[17]);
  313.  printf("\tAddress Mask Reply                 [ %ld ]\n",
  314.                         i_stat.icps_inhist[18]);     
  315. #ifdef WAIT
  316.  getchar(); 
  317. #endif
  318.  printf("Output Packet\n\n");
  319.  printf("\tEcho Reply                        [ %ld ]\n",
  320.                         i_stat.icps_outhist[0]);
  321.  printf("\tDestination Unreachable           [ %ld ]\n",
  322.                         i_stat.icps_outhist[3]); 
  323.  printf("\tSource Quench                     [ %ld ]\n",
  324.                         i_stat.icps_outhist[4]);
  325.  printf("\tRedirect                          [ %ld ]\n",
  326.                         i_stat.icps_outhist[5]);
  327.  printf("\tEcho                              [ %ld ]\n",
  328.                         i_stat.icps_outhist[8]);
  329.  printf("\tRouter Advert                     [ %ld ]\n",
  330.                         i_stat.icps_outhist[9]);
  331.  printf("\tRouter Solicitation               [ %ld ]\n",
  332.                         i_stat.icps_outhist[10]);
  333.  printf("\tTime Exceed                       [ %ld ]\n",
  334.                         i_stat.icps_outhist[11]);
  335.  printf("\tTime Stamp Request                [ %ld ]\n",
  336.                         i_stat.icps_outhist[13]);
  337.  printf("\tTime Stamp Reply                  [ %ld ]\n",
  338.                         i_stat.icps_outhist[14]);
  339.  printf("\tInformation Request               [ %ld ]\n",
  340.                         i_stat.icps_outhist[15]);
  341.  printf("\tInformation Reply                 [ %ld ]\n",
  342.                         i_stat.icps_outhist[16]);
  343.  printf("\tMask Request                      [ %ld ]\n",
  344.                         i_stat.icps_outhist[17]);
  345.  printf("\tMask Reply                        [ %ld ]\n",
  346.                         i_stat.icps_outhist[18]);
  347. }
  348.  
  349. void print_igmp(kvm_t *kd)
  350. {
  351.  struct igmpstat i_stat;
  352.  
  353.  
  354.  if(!list[4].n_value) 
  355.     Error("[print_igmp] checking list nvalue");
  356.  
  357.  kvm_read(kd, list[4].n_value, (char *) &i_stat, sizeof i_stat);
  358.     
  359.  printf("IGMP Statistics\n\n");
  360.  
  361.  printf("\t\tmessages received       [ %ld ]\n", i_stat.igps_rcv_total);
  362.  printf("\t\trcvd with too few bytes [ %ld ]\n", i_stat.igps_rcv_tooshort);
  363.  printf("\t\trcvd with bad checksum  [ %ld ]\n", i_stat.igps_rcv_badsum);
  364.  printf("\t\trcvd membership queries [ %ld ]\n", i_stat.igps_rcv_queries);
  365.  printf("\t\trcvd invalid queries    [ %ld ]\n", i_stat.igps_rcv_badqueries);
  366.  printf("\t\trcvd membership reports [ %ld ]\n", i_stat.igps_rcv_reports);
  367.  printf("\t\trcvd invalid reports    [ %ld ]\n", i_stat.igps_rcv_badreports);
  368.  printf("\t\trcvd rep. for our grps  [ %ld ]\n", i_stat.igps_rcv_ourreports);
  369.  printf("\t\tsent membership reports [ %ld ]\n", i_stat.igps_snd_reports);
  370. }
  371.