четверг, 8 декабря 2016 г.

/etc/init.d/iptables

#!/bin/sh
# /etc/init.d/iptables

IPTABLES=/sbin/iptables

case "$1" in
  start)
    echo "Starting iptables"

    # Обнуляем правила
    $IPTABLES -F
    $IPTABLES -X
    $IPTABLES -F -t nat
    $IPTABLES -X -t nat

    # Политика по умолчанию
    # Все запрещено
    $IPTABLES -P INPUT DROP
    $IPTABLES -P OUTPUT DROP
    $IPTABLES -P FORWARD DROP

        # Loopback
    $IPTABLES -A INPUT -i lo -j ACCEPT
    #$IPTABLES -A INPUT -m geoip --src-cc PS,FR,CA -j DROP
    $IPTABLES -A OUTPUT -o lo -j ACCEPT
    $IPTABLES -A INPUT -i lo -d 127.0.0.0/8 -j REJECT
    #$IPTABLES -A INPUT ! -i lo -m geoip ! --src-cc RU,DE,US -j DROP
    #$IPTABLES -A INPUT -p udp --dport 5060 -m recent --update --seconds 1 --hitcount 20 --name SIP -j LOG --log-prefix "SIP flood detected: "
    #$IPTABLES -A INPUT -p udp --dport 5060 -m recent --update --seconds 1 --hitcount 20 --name SIP -j DROP
    $IPTABLES -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
# Разрешаем SSH
    $IPTABLES -t nat -N DOCKER
    $IPTABLES -t filter -N DOCKER
    $IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 10000:12000 -j ACCEPT
    $IPTABLES -A INPUT -p udp -m state --state NEW -m udp --dport 10000:12000 -j ACCEPT
    $IPTABLES -A INPUT -p udp -m state --state NEW -m udp --dport 5060:5064 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 5060:5064 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 18765 -j ACCEPT
   # $IPTABLES -A INPUT -p udp -m state --state NEW --dport 53 -j ACCEPT
   # $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 53 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 3080 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 3306 -j ACCEPT
    # Разрешаем Asterisk
    $IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 12020:12025 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 2020:2021 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 20:21 -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type fragmentation-needed -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type echo-request -m limit --limit 10/sec -j ACCEPT
    #$IPTABLES -A INPUT -p udp -m state --state NEW -m udp --dport 53 -j ACCEPT
    #$IPTABLES -A INPUT -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT
    #$IPTABLES -A INPUT -m geoip --src-cc CN,TW -j DROP
    # Логгируем
    #$IPTABLES -A INPUT -m state --state NEW -p udp --dport 111 -j ACCEPT
    #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 111 -j ACCEPT
    #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 2049 -j ACCEPT
    #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 32803 -j ACCEPT
    #$IPTABLES -A INPUT -m state --state NEW -p udp --dport 32769 -j ACCEPT
       $IPTABLES -A INPUT -p udp --dport 5060 -m recent --update --seconds 1 --hitcount 20 --name SIP -j LOG --log-prefix "SIP flood detected: "
    $IPTABLES -A INPUT -p udp --dport 5060 -m recent --update --seconds 1 --hitcount 20 --name SIP -j DROP

    $IPTABLES -A INPUT -p tcp -m state --state NEW -j LOG --log-level debug --log-prefix 'iptables new: '
    $IPTABLES -A INPUT -p udp -m state --state NEW -m multiport --dport '67,137,138,1947,17500' -j LOG --log-level debug --log-prefix 'iptables new: '

 # Разрешаем весь исходящий трафик
    $IPTABLES -A OUTPUT -t mangle -p udp -m udp --dport 5060:5064 -j DSCP --set-dscp-class cs3 # SIP
    $IPTABLES -A OUTPUT -t mangle -p tcp -m tcp --dport 5060:5064 -j DSCP --set-dscp-class cs3 #SIP
    $IPTABLES -A OUTPUT -t mangle -p udp -m udp --sport 10000:12000 -j DSCP --set-dscp-class ef # RTP
    $IPTABLES -A OUTPUT -p all -j ACCEPT

    # Разрешаем установленные соединения
    #$IPTABLES -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT

    $IPTABLES -t nat -A PREROUTING -i wlan1 -p udp -m udp --dport 10000:12000 -j DNAT --to-destination 192.168.0.100
    $IPTABLES -t nat -A PREROUTING -i wlan1 -p udp -m udp --dport 5060 -j DNAT --to-destination 192.168.0.100

    # Запрещаем forwarding
    sysctl net.ipv4.ip_forward=1

    # Нужные модули
    modprobe nf_conntrack_ftp
    modprobe nf_conntrack_pptp
    modprobe nf_conntrack_proto_gre
    ;;
  stop)
    echo "Stopping iptables"

    # Обнуляем правила
    $IPTABLES -F
    $IPTABLES -X
    $IPTABLES -F -t nat
    $IPTABLES -X -t nat

    # Политика по умолчанию
    # Все разрешено
    $IPTABLES -P INPUT ACCEPT
    $IPTABLES -P OUTPUT ACCEPT
    $IPTABLES -P FORWARD ACCEPT
    ;;
  *)
    echo "Usage: /etc/init.d/iptables {start|stop}"
    exit 1
    ;;
esac

exit 0

How to update kernel to the latest.

 
apt-cache search linux-image
 
sudo apt-get install linux-image-[version]
 
sudo apt-get install linux-headers-[version]-generic
 
sudo apt-get install linux-image-extra-[version]-generic   
 
 
OR:

  1. Go here: http://kernel.ubuntu.com/~kernel-ppa/mainline/  
  2. Download 3 (maybe 4) debs to a folder somewhere:

    linux-headers-VERSION-NUMBER_all.deb
    linux-headers-VERSION-NUMBER_amd64.deb
    linux-image-VERSION-NUMBER_amd64.deb
    linux-image-extra-VERSION-NUMBER_amd64.deb   # if available
     
  3. Install the debs with whatever package manager front-end you use (is gdebi still around?), or use these commands:

    cd /path/to/folder/where/you/put/the/debs
    sudo dpkg -i *.deb