Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #!/bin/sh
  2. #######################################
  3. ### BEGIN INIT INFO
  4. # Provides: firewall
  5. # Required-Start: $local_fs $network $named $time $syslog $remote_fs
  6. # Required-Stop: $local_fs $network $named $time $syslog $remote_fs
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Description: Personal Firewall - Preventing attacks/open common ports
  10. ### END INIT INFO
  11.  
  12. ### Module loading ###
  13. /sbin/depmod -a
  14. ### Required modules ###
  15. /sbin/modprobe ip_tables
  16. /sbin/modprobe iptable_filter
  17. /sbin/modprobe ipt_REJECT
  18. /sbin/modprobe iptable_nat
  19. /sbin/modprobe ipt_LOG
  20. /sbin/modprobe ipt_limit
  21. ### Non-Required modules ###
  22. #/sbin/modprobe ipt_owner
  23. #/sbin/modprobe iptable_mangle
  24. #/sbin/modprobe ip_conntrack
  25. #/sbin/modprobe ip_conntrack_ftp
  26. #/sbin/modprobe ip_conntrack_irc
  27. #/sbin/modprobe ip_nat_ftp
  28. #/sbin/modprobe ip_nat_irc
  29. #/sbin/modprobe ipt_MASQUERADE
  30.  
  31. # To start the firewall
  32. start()
  33. {
  34. ### Allow Forward ip ###
  35. echo 1 > /proc/sys/net/ipv4/ip_forward
  36. ### Flush any Existing iptable Rules and start afresh ###
  37. iptables -F INPUT
  38. iptables -F OUTPUT
  39. iptables -F FORWARD
  40. iptables -F POSTROUTING -t nat
  41. iptables -F PREROUTING -t nat
  42.  
  43. ### Setting up Port Services ###
  44. iptables -A INPUT -p tcp --dport 25 -j ACCEPT //incoming mail
  45. iptables -A INPUT -p tcp --dport 53 -j ACCEPT //dns - udp for large queries
  46. iptables -A INPUT -p udp --dport 53 -j ACCEPT //dns - udp for small queries
  47. iptables -A INPUT -p tcp --dport 80 -j ACCEPT //apache
  48. iptables -A INPUT -p tcp --dport 443 -j ACCEPT //apache ssl
  49. iptables -A INPUT -p udp --dport 161 -j ACCEPT //snmpd
  50. iptables -A INPUT -p tcp --dport 953 -j ACCEPT //dns internal
  51. iptables -A INPUT -p tcp --dport 1080 -j ACCEPT //dante socks server
  52. iptables -A INPUT -p all --dport 3020 -j ACCEPT //cifs-smb
  53. iptables -A INPUT -p tcp --dport 3128 -j ACCEPT //squid
  54. iptables -A INPUT -p tcp --dport 4949 -j ACCEPT //munin stats
  55.  
  56. ### Setting up Local Ports ###
  57. iptables -A INPUT -d 192.168.1.120 -p udp --dport 9 -j ACCEPT //WOL (wake on lan)
  58. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 20:21 -j ACCEPT //ftp
  59. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 22 -j ACCEPT //sshd
  60. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 139 -j ACCEPT //samba
  61. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 993 -j ACCEPT //imaps
  62. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 3306 -j ACCEPT //mysql
  63. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 8000 -j ACCEPT //apache on phi
  64. iptables -A INPUT -s 192.168.1.120 -p tcp --dport 8080 -j ACCEPT //tomcat
  65. iptables -A INPUT -s 127.0.0.1 -p tcp --dport 111 -j ACCEPT //to speed up mail via courier. Identified via logging
  66. iptables -A INPUT -s 127.0.0.1 -p tcp --dport 143 -j ACCEPT //squirrelmail
  67.  
  68. ### Preventing Attacks ###
  69. iptables -A INPUT -p icmp -j ACCEPT //Allow ICMP Ping packets.
  70. iptables -A INPUT -p tcp --tcp-flags ACK ACK -j ACCEPT //Accept traffic with the ACK flag set
  71. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP //Deny all null packets
  72. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP //Deny all recon packets
  73. iptables -A INPUT -p tcp --tcp-flags ALL FIN -j DROP //nmap FIN stealth scan
  74. iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP //SYN + FIN
  75. iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP //SYN + RST
  76. iptables -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP //FIN + RST
  77. iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP //FIN + URG + PSH
  78. iptables -A INPUT -p tcp --tcp-flags ALL URG,ACK,PSH,RST,SYN,FIN -j DROP //XMAS
  79. iptables -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP //FIN without ACK
  80. iptables -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP //PSH without ACK
  81. iptables -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP //URG without ACK
  82. iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP //Deny SYN flood attack
  83. iptables -A INPUT -m state --state ESTABLISHED -m limit --limit 50/second --limit-burst 50 -j ACCEPT //Accept traffic with ESTABLISHED flag set (limit - DDoS prevent)
  84. iptables -A INPUT -m state --state RELATED -m limit --limit 50/second --limit-burst 50 -j ACCEPT //Accept traffic with RELATED flag set (limit - DDoS prevent)
  85. iptables -A INPUT -m state --state INVALID -j DROP //Deny traffic with the INVALID flag set
  86. #################################
  87.  
  88. ### PERSONALIZED RULES 80 PORT ###
  89. iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW -m limit --limit 50/minute --limit-burst 200 -j ACCEPT //Protection DDoS attacks
  90. ### PERSONALIZED RULES 22 PORT ###
  91. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 22 -m tcp -m state --state NEW -m recent --set --name SSH --rsource
  92. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 22 -m tcp -m recent --rcheck --seconds 30 --hitcount 4 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset //Protection bruteforce SSH
  93. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 22 -m tcp -m recent --rcheck --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j LOG --log-prefix "SSH brute force "
  94. iptables -A INPUT -d 192.168.1.120 -p tcp --dport 22 -m tcp -m recent --update --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset
  95.  
  96. ########### CLOSE ALL ############
  97. iptables -A INPUT -j REJECT //Close up firewall. All else blocked.
  98.  
  99. ######### PORT FORWARDING #######
  100. iptables -t nat -A PREROUTING -p tcp -d 192.168.1.120 --dport 8000 -j DNAT --to 1.2.3.4:80
  101. iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4 //for static ip
  102. #iptables -t nat -A POSTROUTING -d 1.2.3.4 -j MASQUERADE //for dynamic ip
  103. #################################
  104.  
  105. echo "--------------------------------------------------"
  106. echo "Firewall Loaded"
  107. echo "--------------------------------------------------"
  108. echo "Netstat output:"
  109. echo ""
  110. netstat -tuanp
  111. echo "Verify enabled rules with:"
  112. echo "filter) iptables -L -nvx"
  113. echo "nat) iptables -t nat -L -nvx"
  114. echo "script) firewall.sh status"
  115. EXT=0
  116. }
  117.  
  118. ### To stop the firewall ###
  119. stop()
  120. {
  121. ### Deny Forward ip ###
  122. echo 0 > /proc/sys/net/ipv4/ip_forward
  123. echo "--------------------------------------------------"
  124. echo "Firewall Stopped"
  125. echo "--------------------------------------------------"
  126. EXT=0
  127. }
  128.  
  129. ### To clear rules ###
  130. clear()
  131. {
  132. iptables -F INPUT
  133. iptables -F OUTPUT
  134. iptables -F FORWARD
  135. iptables -F POSTROUTING -t nat
  136. iptables -F PREROUTING -t nat
  137. EXT=0
  138. }
  139.  
  140. case $1 in
  141. start)
  142. clear
  143. start
  144. ;;
  145. stop)
  146. clear
  147. stop
  148. ;;
  149. restart)
  150. clear
  151. sleep 2
  152. start
  153. ;;
  154. status)
  155. echo "--------------------------------------------------"
  156. echo "--------------------------------------------------"
  157. echo "Status Firewall"
  158. echo "--------------------------------------------------"
  159. #iptables -L -n
  160. echo "--------------------------------------------------"
  161. echo "FILTER"
  162. echo "--------------------------------------------------"
  163. iptables -L -nvx
  164. echo "--------------------------------------------------"
  165. echo "NAT"
  166. echo "--------------------------------------------------"
  167. iptables -t nat -L -nvx
  168. EXT=0
  169. ;;
  170. *)
  171. echo "Usage: firewall.sh {start|stop|restart|status}"
  172. EXT=1
  173. ;;
  174. esac
  175. exit $EXT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement