Shammer's Philosophy

My private adversaria

Linux iptables samples - 20141129

iptablesはこれまでも使用していたが、情報としてまとめていなかったのでまとめることにしました。

tcp-flagsについて

tcp-flags は2つの引数を取る。最初はチェック対象のTCPフラグ、2つ目はどのフラグがONのパケットを対象にするか。指定可能なフラグは以下。

      • SYN
      • ACK
      • FIN
      • RST
      • URG
      • PSH
      • ALL
      • NONE

samples for a filter table

Drop incoming PSH/ACK packets from specific ip address

Using -s option, this can determine the source ip address.

# iptables -A INPUT -s 172.16.1.10 -p tcp --tcp-flags ALL PSH,ACK -j DROP
Drop incoming FIN/ACK packets to specific port

Using --dport option, this can determine the destination port.

# iptables -A INPUT -p tcp --tcp-flags ALL FIN,ACK --dport 80 -j DROP

If tcp-flags is ALL FIN, FIN/ACK packets are not filtered.

Drop outgoing RST
# iptables -A OUTPUT -p tcp --tcp-flags rst rst -j DROP
Drop outgoing SYN/ACK to specific ip address from specific port

Using -d option can determine the destination ip address and --sport option can determine the source port.

# iptables -A OUTPUT -d 192.168.1.2 -p tcp --tcp-flags ALL SYN,ACK --sport 80 -j DROP

samples for a nat table

Enable IP MASQUERADE

Required that ip_forward should be 1. Using -s option can determine the source segment from packets should be transferred with IP MASQUERADE.

# iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE