Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.49 KB | None | 0 0
  1. #!/sbin/runscript
  2.  
  3. # Main interfaces
  4. IF_WAN='eth0'
  5. IF_LAN='br0'
  6.  
  7. IP_WAN=`ifconfig ${IF_WAN} | awk '/inet addr/ {split ($2,A,":"); print A[2]}'`
  8. IP_LAN='192.168.1.1'
  9.  
  10. LAN='192.168.1.0/24'
  11.  
  12. # Hosts for port forwarding
  13.  
  14. caspair="192.168.1.2" # Desktop
  15. sachiel="192.168.1.4"
  16. iruel="192.168.1.10" # Windows Server 2003
  17.  
  18. # Getting DNS  server addresses
  19. DNS=`grep -i nameserver /etc/resolv.conf | cut -d' ' -f2`
  20.  
  21. ################################################################
  22.  
  23. depend() {
  24.     use net.eth0 net.br0
  25. }
  26.  
  27. start() {
  28.     ebegin "Running Routing and Firewall configuration"
  29.  
  30.     /sbin/modprobe iptable_filter
  31.     /sbin/modprobe xt_state
  32.    
  33.     # Firewall
  34.    
  35.     iptables -P INPUT DROP
  36.     iptables -P FORWARD DROP
  37.     iptables -P OUTPUT ACCEPT
  38.    
  39.     # Routing and masquerade
  40.     echo "1" > /proc/sys/net/ipv4/ip_forward
  41.    
  42.     iptables -t nat -A POSTROUTING -o $IF_WAN -j MASQUERADE
  43.     iptables -A FORWARD -i $IF_LAN -j ACCEPT
  44.    
  45.     iptables -A FORWARD -f -j ACCEPT
  46.    
  47.     # ICMP Ping blocking
  48.     #echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
  49.     # iptables -A INPUT -p icmp -j ACCEPT
  50.    
  51.     # SYN flooding protection
  52.     echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  53.    
  54.     # Smurf attack protection
  55.     echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  56.    
  57.     # Ignore ICMP errors
  58.     echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  59.    
  60.     # Log wierd packets
  61.     #echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
  62.    
  63.     # IP Conntrack
  64.     /sbin/modprobe xt_conntrack
  65.     /sbin/modprobe nf_conntrack_ftp
  66.    
  67.     # IRC
  68.     #/sbin/modprobe nf_nat_irc
  69.     #/sbin/modprobe nf_conntrack_irc
  70.     #/sbin/modprobe ip_nat_irc
  71.     #/sbin/modprobe ip_conntrack_irc
  72.    
  73.     # Local interface
  74.     iptables -A INPUT   -s 127.0.0.1 -j ACCEPT
  75.     iptables -A FORWARD -s 127.0.0.1 -j ACCEPT
  76.    
  77.     # Local network
  78.     iptables -A INPUT -s $LAN -j ACCEPT
  79.     iptables -A INPUT -d $LAN -j ACCEPT
  80.     iptables -A FORWARD -s $LAN -j ACCEPT
  81.    
  82.     # Blocked sites
  83.     #iptables -A INPUT -s 4chan.org -j DROP
  84.     #iptables -A OUTPUT -d 4chan.org -j DROP
  85.    
  86.     # Samba
  87.     iptables -A INPUT -p tcp -s $LAN --dport 137:139 -j ACCEPT
  88.    
  89.     # Sane
  90.     iptables -A INPUT -p tcp -s $LAN --dport 6566 -j ACCEPT
  91.    
  92.     # Passive ON
  93.     iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  94.     iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  95.    
  96.     # DNS
  97.     for SERVER in ${DNS}
  98.         do
  99.             iptables -A INPUT -s $SERVER -j ACCEPT
  100.             iptables -A FORWARD -s $SERVER -j ACCEPT
  101.             iptables -A FORWARD -d $SERVER -j ACCEPT
  102.         done
  103.    
  104.     # rtorrent
  105.     iptables -A INPUT -p TCP --dport 6881:6999 -i $IF_WAN -j ACCEPT
  106.     iptables -A INPUT -p UDP --dport 6881 -i $IF_WAN -j ACCEPT
  107.     iptables -A INPUT -p TCP --dport 7000:7100 -i $IF_WAN -j ACCEPT
  108.     iptables -A INPUT -p UDP --dport 7101 -i $IF_WAN -j ACCEPT
  109.    
  110.     # HTTP
  111.     port_open tcp 80
  112.     port_open udp 80
  113.    
  114.     # PostgreSQL
  115.     #port_open tcp 5432
  116.     #port_open udp 5432
  117.    
  118.     # SSH
  119.     port_open tcp 6666
  120.     port_open udp 6666
  121.    
  122.     # Icecast
  123.     port_open tcp 8000
  124.     port_open udp 8000
  125.    
  126.     # MPD
  127.     port_open tcp 6600 8000
  128.     port_open udp 6600 8000
  129.    
  130.     # Team Speak
  131.     #port_open tcp 51234 8767
  132.     #port_open udp 51234 8767
  133.    
  134.     ########### Port Forwarding ###########
  135.    
  136.     # DNS
  137.     #port_forward tcp $iruel 53
  138.     #port_forward udp $iruel 53
  139.  
  140.     # HTTP
  141.     port_forward tcp 80
  142.     port_forward udp 80
  143.    
  144.     # FTP
  145.     port_forward tcp $caspair 20 21
  146.     port_forward udp $caspair 20 21
  147.     /sbin/modprobe ip_nat_ftp
  148.    
  149.     # Poczta
  150.     # SMTP POP3
  151.     port_forward tcp 25 110
  152.     port_forward udp 25 110
  153.    
  154.     # Kadu (https)
  155.     port_forward tcp $caspair 442 8074
  156.     port_forward udp $caspair 442 8074
  157.     # Kadu - DCC
  158.     port_forward tcp $caspair 1550
  159.    
  160.     # VNC Server
  161.     #port_forward tcp $caspair 5666 5900
  162.     #port_forward udp $caspair 5666 5900
  163.    
  164.     # Jabber
  165.     #port_forward tcp $caspair 5222 5223
  166.     #port_forward udp $caspair 5222 5223
  167.    
  168.     # Gnutella
  169.     port_forward tcp $caspair 6346
  170.     port_forward udp $caspair 6346
  171.    
  172.    
  173.     # aMule
  174.     port_forward tcp $caspair 4661 4663 4711
  175.     port_forward udp $caspair 4661 4663 4672 4665
  176.    
  177.     # Skype
  178.     port_forward tcp $caspair 6533
  179.     port_forward udp $caspair 6533
  180.    
  181.     # Portal 2
  182.     port_forward tcp $caspair 27015
  183.    
  184.     port_forward udp $caspair 27015
  185.    
  186.     # Supreme Commander
  187.     port_forward tcp $caspair 2007
  188.     port_forward udp $caspair 2007
  189.    
  190.     # Shogun
  191.     port_forward tcp $caspair 8797 8793 18321
  192.     port_forward udp $caspair 8797 8793 18321
  193.    
  194.     # Tzar
  195.     port_forward tcp $caspair 23077 23078 23079
  196.     port_forward udp $caspair 23077 23078 23079
  197.    
  198.     # Defcon Service
  199.     port_forward tcp $caspair 5010 5011
  200.     port_forward udp $caspair 5010 5011
  201.    
  202.     # Torrent
  203.     port_forward tcp $caspair 6881
  204.     port_forward udp $caspair 6881
  205.     # sachiel
  206.     port_forward tcp $sachiel 6882
  207.     port_forward udp $sachiel 6882
  208.    
  209.     # Hamachi
  210.     port_forward tcp $caspair 67 68 1358 2587
  211.     port_forward udp $caspair 67 68 1358 2587
  212.    
  213.     eend $?
  214. }
  215.  
  216. flush() {
  217.     # Cleaning iptables
  218.     ebegin Erasing Routing and Firewall configuration
  219.  
  220.     iptables -P INPUT ACCEPT
  221.     iptables -P FORWARD ACCEPT
  222.     iptables -P OUTPUT ACCEPT
  223.     iptables -t nat -F
  224.     iptables -F
  225.     iptables -X
  226.     iptables -Z
  227.  
  228.     rmmod iptable_filter
  229.     rmmod nf_nat_ftp
  230.     rmmod xt_conntrack
  231.     rmmod nf_conntrack_ftp
  232.     rmmod xt_state
  233.    
  234.     eend $?
  235. }
  236.  
  237. reset() {
  238.     # reset all settings
  239.     ebegin "Resetting Routing and Firewall configuration"
  240.  
  241.     flush
  242.     start
  243.    
  244.     eend $?
  245. }
  246.  
  247. save() {
  248. # Saving iptables rules
  249.  
  250.     if [ -n $2 ]; then
  251.         ebegin "Saving Routing and Firewall configuration to $2"
  252.         iptables-save > $2
  253.     else
  254.         ebegin "Saving Routing and Firewall configuration"
  255.         iptbales-save > /etc/iptables/iptables_conf
  256.     fi
  257.    
  258.     eend $?
  259. }
  260.  
  261. load() {
  262. # Loading iptables rules
  263.  
  264.     if [ -n $2 ]; then
  265.         ebegin "Saving Routing and Firewall configuration to $2"
  266.         iptables-restore < $2
  267.     else
  268.         ebegin "Saving Routing and Firewall configuration"
  269.         iptbales-restore < /etc/iptables/iptables_conf
  270.     fi
  271.    
  272.     eend $?
  273. }
  274.  
  275. restart() {
  276.     # Full restart
  277.     ebegin "Restarting Routing and Firewall configuration"
  278.  
  279.     stop
  280.     start
  281.  
  282.     eend $?
  283. }
  284.  
  285. stop() {
  286. # Stop
  287.     ebegin "Stopping Routing and Firewall configuration"
  288.  
  289.     flush
  290.  
  291.     echo "0" > /proc/sys/net/ipv4/ip_forward
  292.     echo "0" > /proc/sys/net/ipv4/icmp_echo_ignore_all
  293.     echo "0" > /proc/sys/net/ipv4/tcp_syncookies
  294.     echo "0" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  295.     echo "0" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  296.     echo "0" > /proc/sys/net/ipv4/conf/all/log_martians
  297.  
  298.     eend $?
  299. }
  300.  
  301. ########### Functions ###########
  302.  
  303. function is_numeric {
  304.     [ "$(echo $*|grep -v "[^0-9]")" ]
  305. }
  306.  
  307. function port_forward {
  308.     prot=$1
  309.     shift
  310.     dest=""
  311.         if ! is_numeric $1; then
  312.             dest=$1
  313.             shift
  314.         fi
  315.     ports=$@
  316.    
  317.         for port in $ports; do
  318.             iptables -A FORWARD -p $prot -i $IF_WAN -o $IF_LAN --dport $port -j ACCEPT
  319.  
  320.                 if [ -n $dest ]; then
  321.                     iptables -t nat -A PREROUTING -p $prot -i $IF_WAN --dport $port -j DNAT --to $dest:$port
  322.                 fi
  323.             #echo "Frowarding port $port to $dest"
  324.         done
  325. }
  326.  
  327. function port_open {
  328.     prot=$1
  329.     shift
  330.     ports=$@
  331.         for port in $ports; do
  332.             #echo "Opening port $port"
  333.             iptables -A INPUT -i $IF_WAN -p $prot -s 0.0.0.0/0 --dport $port -j ACCEPT
  334.         done
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement