]>
git.ipfire.org Git - thirdparty/xtables-addons.git/blob - extensions/xt_TARPIT.c
2 * "TARPIT" target extension to Xtables
3 * Kernel module to capture and hold incoming TCP connections using
4 * no local per-connection resources.
6 * Copyright © Aaron Hopkins <tools [at] die net>, 2002
8 * Based on ipt_REJECT.c and offering functionality similar to
9 * LaBrea <http://www.hackbusters.net/LaBrea/>.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * - Allow incoming TCP connections to be established.
29 * - Passing data should result in the connection being switched to the
30 * persist state (0 byte window), in which the remote side stops sending
31 * data and asks to continue every 60 seconds.
32 * - Attempts to shut down the connection should be ignored completely, so
33 * the remote side ends up having to time it out.
36 * - Reply to TCP SYN,!ACK,!RST,!FIN with SYN-ACK, window 5 bytes
37 * - Reply to TCP SYN,ACK,!RST,!FIN with RST to prevent spoofing
38 * - Reply to TCP !SYN,!RST,!FIN with ACK, window 0 bytes, rate-limited
42 #include <linux/module.h>
43 #include <linux/skbuff.h>
44 #include <linux/version.h>
45 #include <linux/netfilter_ipv6.h>
46 #include <linux/netfilter/x_tables.h>
47 #ifdef CONFIG_BRIDGE_NETFILTER
48 # include <linux/netfilter_bridge.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_checksum.h>
52 #include <net/ip6_route.h>
54 #include <net/route.h>
56 #include "compat_xtables.h"
57 #include "xt_TARPIT.h"
58 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
62 static bool xttarpit_tarpit(struct tcphdr
*tcph
, const struct tcphdr
*oth
)
64 /* No replies for RST, FIN or !SYN,!ACK */
65 if (oth
->rst
|| oth
->fin
|| (!oth
->syn
&& !oth
->ack
))
67 tcph
->seq
= oth
->ack
? oth
->ack_seq
: 0;
69 /* Our SYN-ACKs must have a >0 window */
70 tcph
->window
= (oth
->syn
&& !oth
->ack
) ? htons(5) : 0;
71 if (oth
->syn
&& oth
->ack
) {
73 tcph
->ack_seq
= false;
77 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
);
80 /* Rate-limit replies to !SYN,ACKs */
81 if (!oth
->syn
&& oth
->ack
)
82 if (!xrlim_allow(&ort
->dst
, HZ
))
89 static bool xttarpit_honeypot(struct tcphdr
*tcph
, const struct tcphdr
*oth
,
92 /* Do not answer any resets regardless of combination */
93 if (oth
->rst
|| oth
->seq
== 0xDEADBEEF)
95 /* Send a reset to scanners. They like that. */
96 if (oth
->syn
&& oth
->ack
) {
100 tcph
->ack_seq
= 0xdeadbeef; /* see if they ack it */
101 tcph
->seq
= oth
->ack_seq
;
106 if (oth
->syn
&& !oth
->ack
) {
109 tcph
->window
= oth
->window
&
110 ((prandom_u32() & 0x1f) - 0xf);
111 tcph
->seq
= htonl(prandom_u32() & ~oth
->seq
);
112 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
);
116 if (oth
->ack
&& (!(oth
->fin
|| oth
->syn
))) {
119 tcph
->window
= oth
->window
&
120 ((prandom_u32() & 0x1f) - 0xf);
121 tcph
->ack_seq
= payload
> 100 ?
122 htonl(ntohl(oth
->seq
) + payload
) :
124 tcph
->seq
= oth
->ack_seq
;
129 * We cannot terminate gracefully so just be abrupt.
133 tcph
->seq
= oth
->ack_seq
;
134 tcph
->ack_seq
= oth
->ack_seq
;
143 static void xttarpit_reset(struct tcphdr
*tcph
, const struct tcphdr
*oth
)
149 tcph
->seq
= oth
->ack_seq
;
150 tcph
->ack_seq
= oth
->seq
;
153 static bool tarpit_generic(struct tcphdr
*tcph
, const struct tcphdr
*oth
,
154 uint16_t payload
, unsigned int mode
)
157 case XTTARPIT_TARPIT
:
158 if (!xttarpit_tarpit(tcph
, oth
))
161 case XTTARPIT_HONEYPOT
:
162 if (!xttarpit_honeypot(tcph
, oth
, payload
))
166 xttarpit_reset(tcph
, oth
);
173 static void tarpit_tcp4(struct net
*net
, struct sk_buff
*oldskb
,
174 unsigned int hook
, unsigned int mode
)
176 struct tcphdr _otcph
, *tcph
;
177 const struct tcphdr
*oth
;
178 unsigned int addr_type
= RTN_UNSPEC
;
179 struct sk_buff
*nskb
;
180 const struct iphdr
*oldhdr
;
182 uint16_t tmp
, payload
;
184 /* A truncated TCP header is not going to be useful */
185 if (oldskb
->len
< ip_hdrlen(oldskb
) + sizeof(struct tcphdr
))
188 oth
= skb_header_pointer(oldskb
, ip_hdrlen(oldskb
),
189 sizeof(_otcph
), &_otcph
);
193 /* Check checksum. */
194 if (nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), IPPROTO_TCP
))
198 * Copy skb (even if skb is about to be dropped, we cannot just
199 * clone it because there may be other things, such as tcpdump,
202 nskb
= skb_copy_expand(oldskb
, LL_MAX_HEADER
,
203 skb_tailroom(oldskb
), GFP_ATOMIC
);
207 /* This packet will not be the same as the other: clear nf fields */
208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
213 skb_nfmark(nskb
) = 0;
214 skb_init_secmark(nskb
);
215 skb_shinfo(nskb
)->gso_size
= 0;
216 skb_shinfo(nskb
)->gso_segs
= 0;
217 skb_shinfo(nskb
)->gso_type
= 0;
218 oldhdr
= ip_hdr(oldskb
);
219 tcph
= (struct tcphdr
*)(skb_network_header(nskb
) + ip_hdrlen(nskb
));
221 /* Swap source and dest */
223 niph
->daddr
= xchg(&niph
->saddr
, niph
->daddr
);
225 tcph
->source
= tcph
->dest
;
228 /* Calculate payload size?? */
229 payload
= nskb
->len
- ip_hdrlen(nskb
) - sizeof(struct tcphdr
);
231 /* Truncate to length (no data) */
232 tcph
->doff
= sizeof(struct tcphdr
) / 4;
233 skb_trim(nskb
, ip_hdrlen(nskb
) + sizeof(struct tcphdr
));
234 niph
->tot_len
= htons(nskb
->len
);
237 ((u_int8_t
*)tcph
)[13] = 0;
239 if (!tarpit_generic(tcph
, oth
, payload
, mode
))
242 /* Adjust TCP checksum */
244 tcph
->check
= tcp_v4_check(sizeof(struct tcphdr
), niph
->saddr
,
245 niph
->daddr
, csum_partial((char *)tcph
,
246 sizeof(struct tcphdr
), 0));
249 niph
->frag_off
= htons(IP_DF
);
250 if (mode
== XTTARPIT_TARPIT
|| mode
== XTTARPIT_RESET
)
252 else if (mode
== XTTARPIT_HONEYPOT
)
253 niph
->id
= ~oldhdr
->id
+ 1;
255 #ifdef CONFIG_BRIDGE_NETFILTER
256 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
257 if (hook
!= NF_INET_FORWARD
|| ((struct nf_bridge_info
*)skb_ext_find(nskb
, SKB_EXT_BRIDGE_NF
) != NULL
&&
258 ((struct nf_bridge_info
*)skb_ext_find(nskb
, SKB_EXT_BRIDGE_NF
))->physoutdev
))
260 if (hook
!= NF_INET_FORWARD
|| (nskb
->nf_bridge
!= NULL
&&
261 nskb
->nf_bridge
->physoutdev
!= NULL
))
264 if (hook
!= NF_INET_FORWARD
)
266 addr_type
= RTN_LOCAL
;
268 if (ip_route_me_harder(net
, nskb
, addr_type
))
273 nskb
->ip_summed
= CHECKSUM_NONE
;
276 if (mode
== XTTARPIT_HONEYPOT
)
279 niph
->ttl
= ip4_dst_hoplimit(skb_dst(nskb
));
281 /* Adjust IP checksum */
283 niph
->check
= ip_fast_csum(skb_network_header(nskb
), niph
->ihl
);
285 /* "Never happens" */
286 if (nskb
->len
> dst_mtu(skb_dst(nskb
)))
289 nf_ct_attach(nskb
, oldskb
);
290 NF_HOOK(NFPROTO_IPV4
, NF_INET_LOCAL_OUT
, net
, nskb
->sk
, nskb
, NULL
,
291 skb_dst(nskb
)->dev
, dst_output
);
299 static void tarpit_tcp6(struct net
*net
, struct sk_buff
*oldskb
,
300 unsigned int hook
, unsigned int mode
)
302 struct sk_buff
*nskb
;
303 struct tcphdr
*tcph
, oth
;
304 unsigned int otcplen
;
306 const struct ipv6hdr
*oip6h
= ipv6_hdr(oldskb
);
307 struct ipv6hdr
*ip6h
;
308 const uint8_t tclass
= 0;
313 proto
= oip6h
->nexthdr
;
314 tcphoff
= ipv6_skip_exthdr(oldskb
,
315 (uint8_t *)(oip6h
+ 1) - oldskb
->data
, &proto
, &frag_off
);
317 if (tcphoff
< 0 || tcphoff
> oldskb
->len
) {
318 pr_debug("Cannot get TCP header.\n");
322 otcplen
= oldskb
->len
- tcphoff
;
324 /* IP header checks: fragment, too short. */
325 if (proto
!= IPPROTO_TCP
|| otcplen
< sizeof(struct tcphdr
)) {
326 pr_debug("proto(%d) != IPPROTO_TCP, "
327 "or too short. otcplen = %d\n",
332 if (skb_copy_bits(oldskb
, tcphoff
, &oth
, sizeof(struct tcphdr
))) {
337 /* Check checksum. */
338 if (csum_ipv6_magic(&oip6h
->saddr
, &oip6h
->daddr
, otcplen
, IPPROTO_TCP
,
339 skb_checksum(oldskb
, tcphoff
, otcplen
, 0))) {
340 pr_debug("TCP checksum is invalid\n");
344 nskb
= skb_copy_expand(oldskb
, LL_MAX_HEADER
,
345 skb_tailroom(oldskb
), GFP_ATOMIC
);
348 pr_debug("cannot alloc skb\n");
352 /* This packet will not be the same as the other: clear nf fields */
353 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
358 skb_nfmark(nskb
) = 0;
359 skb_init_secmark(nskb
);
360 skb_shinfo(nskb
)->gso_size
= 0;
361 skb_shinfo(nskb
)->gso_segs
= 0;
362 skb_shinfo(nskb
)->gso_type
= 0;
363 skb_put(nskb
, sizeof(struct ipv6hdr
));
364 ip6h
= ipv6_hdr(nskb
);
365 *(__be32
*)ip6h
= htonl(0x60000000 | (tclass
<< 20));
366 ip6h
->nexthdr
= IPPROTO_TCP
;
367 ip6h
->saddr
= oip6h
->daddr
;
368 ip6h
->daddr
= oip6h
->saddr
;
371 if (mode
== XTTARPIT_HONEYPOT
) {
372 ip6h
->hop_limit
= 128;
374 ip6h
->hop_limit
= ip6_dst_hoplimit(skb_dst(nskb
));
377 tcph
= (struct tcphdr
*)(skb_network_header(nskb
) +
378 sizeof(struct ipv6hdr
));
380 /* Truncate to length (no data) */
381 skb_trim(nskb
, sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
));
382 tcph
->doff
= sizeof(struct tcphdr
)/4;
383 tcph
->source
= oth
.dest
;
384 tcph
->dest
= oth
.source
;
387 ((uint8_t *)tcph
)[13] = 0;
389 payload
= nskb
->len
- sizeof(struct ipv6hdr
) - sizeof(struct tcphdr
);
390 if (!tarpit_generic(&oth
, tcph
, payload
, mode
))
393 ip6h
->payload_len
= htons(sizeof(struct tcphdr
));
396 /* Adjust TCP checksum */
397 tcph
->check
= csum_ipv6_magic(&ipv6_hdr(nskb
)->saddr
,
398 &ipv6_hdr(nskb
)->daddr
, sizeof(struct tcphdr
),
400 csum_partial(tcph
, sizeof(struct tcphdr
), 0));
402 if (ip6_route_me_harder(net
, nskb
))
405 nskb
->ip_summed
= CHECKSUM_NONE
;
407 nf_ct_attach(nskb
, oldskb
);
408 NF_HOOK(NFPROTO_IPV6
, NF_INET_LOCAL_OUT
, net
, nskb
->sk
, nskb
, NULL
,
409 skb_dst(nskb
)->dev
, dst_output
);
418 tarpit_tg4(struct sk_buff
*skb
, const struct xt_action_param
*par
)
420 const struct iphdr
*iph
= ip_hdr(skb
);
421 const struct rtable
*rt
= skb_rtable(skb
);
422 const struct xt_tarpit_tginfo
*info
= par
->targinfo
;
424 /* Do we have an input route cache entry? (Not in PREROUTING.) */
428 /* No replies to physical multicast/broadcast */
429 /* skb != PACKET_OTHERHOST handled by ip_rcv() */
430 if (skb
->pkt_type
!= PACKET_HOST
)
433 /* Now check at the protocol level */
434 if (rt
->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))
438 * Our naive response construction does not deal with IP
439 * options, and probably should not try.
441 if (ip_hdrlen(skb
) != sizeof(struct iphdr
))
444 /* We are not interested in fragments */
445 if (iph
->frag_off
& htons(IP_OFFSET
))
447 tarpit_tcp4(par_net(par
), skb
, par
->state
->hook
, info
->variant
);
453 tarpit_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
455 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
456 const struct rt6_info
*rt
= (struct rt6_info
*)skb_dst(skb
);
457 const struct xt_tarpit_tginfo
*info
= par
->targinfo
;
461 /* Do we have an input route cache entry? (Not in PREROUTING.) */
463 pr_debug("Dropping no input route cache entry\n");
467 /* No replies to physical multicast/broadcast */
468 /* skb != PACKET_OTHERHOST handled by ip_rcv() */
469 if (skb
->pkt_type
!= PACKET_HOST
) {
470 pr_debug("type != PACKET_HOST");
475 * Our naive response construction does not deal with IP
476 * options, and probably should not try.
478 proto
= iph
->nexthdr
;
479 if (ipv6_skip_exthdr(skb
, skb_network_header_len(skb
), &proto
,
480 &frag_off
) != sizeof(struct ipv6hdr
))
483 if ((!(ipv6_addr_type(&iph
->saddr
) & IPV6_ADDR_UNICAST
)) ||
484 (!(ipv6_addr_type(&iph
->daddr
) & IPV6_ADDR_UNICAST
))) {
485 pr_debug("addr is not unicast.\n");
488 tarpit_tcp6(par_net(par
), skb
, par
->state
->hook
, info
->variant
);
493 static struct xt_target tarpit_tg_reg
[] __read_mostly
= {
497 .family
= NFPROTO_IPV4
,
498 .hooks
= (1 << NF_INET_LOCAL_IN
) | (1 << NF_INET_FORWARD
),
499 .proto
= IPPROTO_TCP
,
500 .target
= tarpit_tg4
,
501 .targetsize
= sizeof(struct xt_tarpit_tginfo
),
508 .family
= NFPROTO_IPV6
,
509 .hooks
= (1 << NF_INET_LOCAL_IN
) | (1 << NF_INET_FORWARD
),
510 .proto
= IPPROTO_TCP
,
511 .target
= tarpit_tg6
,
512 .targetsize
= sizeof(struct xt_tarpit_tginfo
),
518 static int __init
tarpit_tg_init(void)
520 return xt_register_targets(tarpit_tg_reg
, ARRAY_SIZE(tarpit_tg_reg
));
523 static void __exit
tarpit_tg_exit(void)
525 xt_unregister_targets(tarpit_tg_reg
, ARRAY_SIZE(tarpit_tg_reg
));
528 module_init(tarpit_tg_init
);
529 module_exit(tarpit_tg_exit
);
530 MODULE_DESCRIPTION("Xtables: \"TARPIT\", capture and hold TCP connections");
531 MODULE_AUTHOR("Jan Engelhardt ");
532 MODULE_LICENSE("GPL");
533 MODULE_ALIAS("ipt_TARPIT");
534 MODULE_ALIAS("ip6t_TARPIT");