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

  1. /*
  2.  * IGMP/ICMP/IPIP/IDP/RSVP/IPIP/IPPROTO_RAW KERNEL CHECKER
  3.  *
  4.  * These protocols have the same pr_usrreqs... This is a kld to monitor
  5.  * their packets via FreeBSD kernel.
  6.  *
  7.  * idea & code by pIGpEN [pigpen@s0ftpj.org, deadhead@sikurezza.org ]
  8.  *
  9.  * Tested on FreeBSD CURRENT 4.0
  10.  *
  11.  * s0ftpr0ject - digital security for y2k
  12.  * www.s0ftpj.org
  13.  *
  14.  * sikurezza.org - italian security mailing list
  15.  * www.sikurezza.org
  16.  *
  17.  */
  18.  
  19. #include <sys/param.h>
  20. #include <sys/systm.h>
  21. #include <sys/kernel.h>
  22. #include <sys/malloc.h>
  23. #include <sys/mbuf.h>
  24. #include <sys/proc.h>
  25. #include <sys/protosw.h>
  26. #include <sys/socket.h>
  27. #include <sys/socketvar.h>
  28. #include <sys/sysctl.h>
  29.  
  30. #include <vm/vm_zone.h>
  31.  
  32. #include <net/if.h>
  33. #include <net/route.h>
  34.  
  35. #define _IP_VHL
  36. #include <netinet/in.h>
  37. #include <netinet/in_systm.h>
  38. #include <netinet/ip.h>
  39. #include <netinet/in_pcb.h>
  40. #include <netinet/in_var.h>
  41. #include <netinet/ip_var.h>
  42. #include <netinet/ip_mroute.h>
  43.  
  44.  
  45. #define        print_ip(a)    printf("%d.%d.%d.%d\n",            \
  46.                     (int)(ntohl(a) >>24) & 0xFF,    \
  47.                     (int)(ntohl(a) >>16) & 0xFF,    \
  48.                     (int)(ntohl(a) >> 8) & 0xFF,    \
  49.                     (int)(ntohl(a))    & 0xFF);
  50.  
  51.  
  52. static int      rip_new_send      __P((struct socket *, int, struct mbuf *,
  53.                      struct sockaddr *, struct mbuf *,
  54.                      struct proc *));
  55.  
  56. static int      (*old_rip_send) __P((struct socket *, int, struct mbuf *,
  57.                      struct sockaddr *, struct mbuf *,
  58.                      struct proc *));
  59.  
  60. static int      rip_my_output      __P((register struct mbuf *, 
  61.                      struct socket *, u_long));
  62.  
  63. static int      s_load      __P((struct module *, int, void *));
  64.  
  65.  
  66.  
  67. /* il controllo si poteva fare direttamente su questa funzione.... non l'ho
  68.    fatto perche' avevo gia' messo la mia rip_my_output nel sorgente :) */
  69.  
  70.  
  71. static int
  72. rip_new_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  73.      struct mbuf *control, struct proc *p)
  74. {
  75.     struct inpcb *inp = sotoinpcb(so);
  76.     register u_long dst;
  77.  
  78.     if (so->so_state & SS_ISCONNECTED) {
  79.         if (nam) {
  80.             m_freem(m);
  81.             return EISCONN;
  82.         }
  83.         dst = inp->inp_faddr.s_addr;
  84.     } else {
  85.         if (nam == NULL) {
  86.             m_freem(m);
  87.             return ENOTCONN;
  88.         }
  89.         dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
  90.     }
  91.     return rip_my_output(m, so, dst);
  92. }
  93.  
  94. static int
  95. rip_my_output(m, so, dst)
  96.     register struct mbuf *m;
  97.     struct socket *so;
  98.     u_long dst;
  99. {
  100.     register struct ip *ip;
  101.     register struct inpcb *inp = sotoinpcb(so);
  102.     int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
  103.  
  104.  
  105.  
  106.     switch(inp->inp_ip_p) {
  107.         case IPPROTO_RAW:  printf("RAW");
  108.                    break;
  109.         case IPPROTO_ICMP: printf("ICMP");
  110.                    break;
  111.         case IPPROTO_IGMP: printf("IGMP");
  112.                    break;
  113.         case IPPROTO_RSVP: printf("RSVP");
  114.                    break;
  115.         case IPPROTO_IPIP: printf("IPIP");
  116.                    break;
  117.         case IPPROTO_IDP:  printf("IDP");
  118.                    break;                   
  119.         default:       printf("#%d", inp->inp_ip_p);
  120.                    break;
  121.     }
  122.     
  123.     printf(" -> ");           print_ip(dst);
  124.     
  125.     /*
  126.      * If the user handed us a complete IP packet, use it.
  127.      * Otherwise, allocate an mbuf for a header and fill it in.
  128.      */
  129.     if ((inp->inp_flags & INP_HDRINCL) == 0) {
  130.         if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
  131.             m_freem(m);
  132.             return(EMSGSIZE);
  133.         }
  134.         M_PREPEND(m, sizeof(struct ip), M_WAIT);
  135.         ip = mtod(m, struct ip *);
  136.         ip->ip_tos = 0;
  137.         ip->ip_off = 0;
  138.         ip->ip_p = inp->inp_ip_p;
  139.         ip->ip_len = m->m_pkthdr.len;
  140.         ip->ip_src = inp->inp_laddr;
  141.         ip->ip_dst.s_addr = dst;
  142.         ip->ip_ttl = MAXTTL;
  143.     } else {
  144.         if (m->m_pkthdr.len > IP_MAXPACKET) {
  145.             m_freem(m);
  146.             return(EMSGSIZE);
  147.         }
  148.         ip = mtod(m, struct ip *);
  149.         /* don't allow both user specified and setsockopt options,
  150.            and don't allow packet length sizes that will crash */
  151.         if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 
  152.              && inp->inp_options)
  153.             || (ip->ip_len > m->m_pkthdr.len)
  154.             || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
  155.             m_freem(m);
  156.             return EINVAL;
  157.         }
  158.         if (ip->ip_id == 0)
  159.             ip->ip_id = htons(ip_id++);
  160.         /* XXX prevent ip_output from overwriting header fields */
  161.         flags |= IP_RAWOUTPUT;
  162.         ipstat.ips_rawout++;
  163.     }
  164.  
  165.  
  166.     
  167.     return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
  168.               inp->inp_moptions));
  169. }
  170.  
  171.  
  172.  
  173. extern struct     protosw     inetsw[];
  174.  
  175. static int
  176. s_load (struct module *module, int cmd, void *arg)
  177. {
  178.  int s;
  179.  
  180.  switch(cmd) {
  181.     case MOD_LOAD:    
  182.       s=splnet();
  183.       old_rip_send = inetsw[ip_protox[IPPROTO_ICMP]].pr_usrreqs->pru_send;
  184.       inetsw[ip_protox[IPPROTO_ICMP]].pr_usrreqs->pru_send = rip_new_send;
  185.         splx(s);
  186.       break;
  187.             
  188.     case MOD_UNLOAD:
  189.       s=splnet();
  190.       inetsw[ip_protox[IPPROTO_ICMP]].pr_usrreqs->pru_send = old_rip_send;
  191.        splx(s);
  192.       break;
  193.  }
  194.  
  195.  return 0;
  196. }
  197.  
  198. static moduledata_t s_mod_1 = {
  199.             "raww_mod",
  200.             s_load,
  201.             0
  202. };
  203.  
  204. DECLARE_MODULE(raww_mod, s_mod_1, SI_SUB_PSEUDO, SI_ORDER_ANY);
  205.