Skip to contentSkip to conversation

SYN attack, with FIN flag set

DISCUSSION Denys Fedoryshchenko 4 participants 11 messages Dec 2, 2011 – Dec 4, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi

Recently i started to get SYN attacks, and managed them. syncookies didn't helped, here is "perf report" info:
- 26.89% swapper [kernel.kallsyms] [k] _raw_spin_lock
- _raw_spin_lock
- 94.97% tcp_v4_rcv ip_local_deliver_finish ip_local_deliver
ip_rcv_finish
ip_rcv
__netif_receive_skb
process_backlog
net_rx_action
__do_softirq
call_softirq
do_softirq
+ irq_exit

But then i got attack that made server to choke and bypassed "--syn" rule, and i was surprised, that stack are handling invalid combination of flags, SYN+FIN. Is it valid behaviour?

in tcp_input.c, tcp_rcv_state_process(), it just does check for rst (to discard), but maybe packet with fin set should be discarded too?

From http://www.whitehats.ca/main/members/Seeker/seeker_tcp_header/

SYN FIN is probably the best known illegal combination. Remember that SYN is used to start a connection, while FIN is used to end an existing connection. It is nonsensical to perform both actions at the same time. Many scanning tools use SYN FIN packets, because many intrusion detection systems did not catch these in the past, although most do so now. You can safely assume that any SYN FIN packets you see are malicious.

---
System administrator
Denys Fedoryshchenko
Virtual ISP S.A.L.

#1 lore raw
Quoted 19 lines · Denys Fedoryshchenko

Hi

Recently i started to get SYN attacks, and managed them. syncookies didn't helped, here is "perf report" info:
- 26.89% swapper [kernel.kallsyms] [k] _raw_spin_lock
- _raw_spin_lock
- 94.97% tcp_v4_rcv ip_local_deliver_finish ip_local_deliver
ip_rcv_finish
ip_rcv
__netif_receive_skb
process_backlog
net_rx_action
__do_softirq
call_softirq
do_softirq
+ irq_exit

Quoted 7 lines

But then i got attack that made server to choke and bypassed "--syn" rule, and i was surprised, that stack are handling invalid combination of flags, SYN+FIN.
Is it valid behaviour?

in tcp_input.c, tcp_rcv_state_process(), it just does check for rst (to discard), but maybe packet with fin set should be discarded too?

I believe netfilter tcp conntrack considers SYN|FIN as INVALID

Yes, we could drop SYN|FIN messages, but what prevents attacker to just use SYN messages to attack your machine ?

#2 lore raw
Quoted 2 lines · Eric Dumazet

I believe netfilter tcp conntrack considers SYN|FIN as INVALID

Or if you cannot afford conntracking, just do

iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

#3 lore raw
Quoted 8 lines · Eric Dumazet
Quoted 2 lines · Eric Dumazet

I believe netfilter tcp conntrack considers SYN|FIN as INVALID

Or if you cannot afford conntracking, just do

iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

Sure i did,thanks, but i just was curious, why connection with such flags are threated as SYN.

---
System administrator
Denys Fedoryshchenko
Virtual ISP S.A.L.

#4 lore raw
Quoted 6 lines · Denys Fedoryshchenko
Quoted 5 lines · Eric Dumazet
Quoted 2 lines · Eric Dumazet

I believe netfilter tcp conntrack considers SYN|FIN as INVALID

Quoted 7 lines
Quoted 3 lines

Or if you cannot afford conntracking, just do

iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

Sure i did,thanks, but i just was curious, why connection with such flags are threated as SYN.

TCP stack first tries to lookup a socket, given the tuple found in incoming packet.

This is where your machine is hit : we find the listener socket and lock it.

Then, once socket was found and locked, state machine handle various possible states.

In your case, you want to bypass the lookup, and eventually bypass the IP route lookup as well (to keep IP route cache small)

iptables -t raw -I PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

#5 lore raw

On 03.12.2011 12:53, Eric Dumazet wrote:
[]

Quoted 6 lines

TCP stack first tries to lookup a socket, given the tuple found in incoming packet.

This is where your machine is hit : we find the listener socket and lock it.

Quoted 7 lines

Then, once socket was found and locked, state machine handle various possible states.

In your case, you want to bypass the lookup, and eventually bypass the IP route lookup as well (to keep IP route cache small)

iptables -t raw -I PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

Maybe it makes some sence to add a basic "sanity" check rule before the socket lookup?

The question here is why SYN+FIN results in worse behavour than SYN alone - in the default setup, without iptables rules? As far as I understand, "regular" SYN attack is handled just fine, but SYN+FIN attack makes the machine to "choke", and it is not obvious how to fix it -- naive --syn iptables rule does not help.

The price for the sanity check appears to be small since there's already a check for RST.

Just asking, not suggesting anything... ;)

Thanks,

/mjt

#6 lore raw
Quoted 25 lines · Michael Tokarev

On 03.12.2011 12:53, Eric Dumazet wrote:
[]

Quoted 6 lines

TCP stack first tries to lookup a socket, given the tuple found in incoming packet.

This is where your machine is hit : we find the listener socket and lock it.

Quoted 7 lines

Then, once socket was found and locked, state machine handle various possible states.

In your case, you want to bypass the lookup, and eventually bypass the IP route lookup as well (to keep IP route cache small)

iptables -t raw -I PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

Maybe it makes some sence to add a basic "sanity" check rule before the socket lookup?

The question here is why SYN+FIN results in worse behavour than SYN alone - in the default setup, without iptables rules? As far as I understand, "regular" SYN attack is handled just fine, but SYN+FIN attack makes the machine to "choke", and it is not obvious how to fix it -- naive --syn iptables rule does not help.

Quoted 8 lines

The price for the sanity check appears to be small since there's already a check for RST.

Just asking, not suggesting anything... ;)

Thanks,

/mjt

No,no, as i understand it is just threating SYN+FIN as plain SYN.
I think if it incurr additional expenses (verification), no need maybe to fix it, it is job of iptables. But is FIN to listening socket - legitimate? Shouldn't it be dropped?

---
System administrator
Denys Fedoryshchenko
Virtual ISP S.A.L.

#7 lore raw
Quoted 5 lines · Denys Fedoryshchenko

No,no, as i understand it is just threating SYN+FIN as plain SYN. I think if it incurr additional expenses (verification), no need maybe to fix it, it is job of iptables.
But is FIN to listening socket - legitimate? Shouldn't it be dropped?

Host network stack is optimized for the fast path (legitimate traffic)

Firewalls are optimized to the opposite, to let protected machines doing their jobs without slow checks.

If we add all possible counter measures in tcp stack, it would be maybe 2 times slower than it is...

Remember than SYN|FIN messages are maybe 0.000001% of total Internet trafic, since they are not legitimate.

In the case of SYN packet, flow is :

1) IP stack : ip_rcv(), ip_route_input_noref(), ip_rcv_finish()

2) TCP stack :

	tcp_v4_rcv(),
	__inet_lookup_skb() [ find the listener socket ]
	bh_lock_sock_nested(sk) [ lock the socket ]
	tcp_v4_do_rcv()
	tcp_v4_hnd_req()
	inet_csk_search_req() [ try to find a previous SYN_RECV sock]
	tcp_rcv_state_process()
		tcp_v4_conn_request(sk, skb)

So it appears we go too far, we should drop FIN messages at this point.

What about following patch then ?

The added test is hit only for SYN messages, this seems not a very expensive test.

[PATCH] tcp: drop SYN+FIN messages

Denys Fedoryshchenko reported that SYN+FIN attacks were bringing his linux machines to their limits.

Dont call conn_request() if the TCP flags includes SYN flag

---
 net/ipv4/tcp_input.c |    2 ++
 1 file changed, 2 insertions(+)
net/ipv4/tcp_input.c +2 -0
index 52b5c2d..d54c942 100644
@@ -5809,6 +5809,8 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
 			goto discard;
 
 		if (th->syn) {
+			if (th->fin)
+				goto discard;
 			if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
 				return 1;
 
#8 lore raw
Quoted 7 lines · Eric Dumazet

[PATCH] tcp: drop SYN+FIN messages

Denys Fedoryshchenko reported that SYN+FIN attacks were bringing his linux machines to their limits.

Dont call conn_request() if the TCP flags includes SYN flag

Oh well this should be :

Dont call conn_request() if the TCP flags includes FIN flag

#9 lore raw
Quoted 6 lines · Eric Dumazet
Quoted 5 lines · Eric Dumazet

[PATCH] tcp: drop SYN+FIN messages

Denys Fedoryshchenko reported that SYN+FIN attacks were bringing his linux machines to their limits.

Quoted 6 lines
Quoted 2 lines

Dont call conn_request() if the TCP flags includes SYN flag

Oh well this should be :

Dont call conn_request() if the TCP flags includes FIN flag

Thats what i meant, maybe this will not slow down things (there is other conditions already available, such as rst, just one more), but will be somehow helpful for network administrators, since such attack are bypassing --syn iptables flag. I hope it doesn't break anything.

---
System administrator
Denys Fedoryshchenko
Virtual ISP S.A.L.

maintainer
#10 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Sat, 03 Dec 2011 10:41:42 +0100

Quoted 9 lines

[PATCH] tcp: drop SYN+FIN messages

Denys Fedoryshchenko reported that SYN+FIN attacks were bringing his linux machines to their limits.

Dont call conn_request() if the TCP flags includes SYN flag

Ok, I've applied this to net-next.