safe_iptables.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env bash
  2. # IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB 特别感谢 TaterLi 指导
  3. # wget -qO safe_iptables.sh git.io/fhJrU
  4. # 初始化安全防火墙规则预设端口; 可以个性修改脚本; 或者 指定INPUT Chain 设置删除
  5. tcp_port="80,443"
  6. udp_port="9999,8000"
  7. # 保存防火墙规则文件路径 /etc/iptables/rules.v4 禁用ipv6
  8. mkdir -p /etc/iptables
  9. # 定义文字颜色
  10. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  11. # 检查系统
  12. check_sys(){
  13. if [[ -f /etc/redhat-release ]]; then
  14. release="centos"
  15. elif cat /etc/issue | grep -q -E -i "debian"; then
  16. release="debian"
  17. elif cat /etc/issue | grep -q -E -i "ubuntu"; then
  18. release="ubuntu"
  19. elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
  20. release="centos"
  21. elif cat /proc/version | grep -q -E -i "debian"; then
  22. release="debian"
  23. elif cat /proc/version | grep -q -E -i "ubuntu"; then
  24. release="ubuntu"
  25. elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
  26. release="centos"
  27. fi
  28. bit=`uname -m`
  29. }
  30. # 保存防火墙规则
  31. save_iptables(){
  32. if [[ ${release} == "centos" ]]; then
  33. service iptables save
  34. else
  35. iptables-save > /etc/iptables/rules.v4
  36. fi
  37. }
  38. # 设置防火墙规则,下次开机也生效
  39. set_iptables(){
  40. if [[ ${release} == "centos" ]]; then
  41. service iptables save
  42. chkconfig --level 2345 iptables on
  43. else
  44. iptables-save > /etc/iptables/rules.v4
  45. echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables/rules.v4' > /etc/network/if-pre-up.d/iptables
  46. chmod +x /etc/network/if-pre-up.d/iptables
  47. fi
  48. }
  49. no_use_passwd(){
  50. # 禁用密码登陆
  51. sed -i "s/#PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config
  52. sed -i "s/PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config
  53. # 重启ssh服务
  54. systemctl restart ssh
  55. }
  56. srgb18_ga_ddns(){
  57. # 下载 IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB
  58. wget -qO safe_iptables.sh git.io/fhJrU
  59. echo -e "${Red} 浏览器 HE.NET 动态DDNS更新IP示例: ${Font}"
  60. echo -e "${Green}https://srgb18.ga:[email protected]/nic/update?hostname=srgb18.ga&myip=35.235.96.85 ${Font}"
  61. curl -4 "srgb18.ga:[email protected]/nic/update?hostname=srgb18.ga"
  62. echo
  63. }
  64. # 隐藏的防火墙设置功能菜单 88
  65. hide_menu(){
  66. echo
  67. echo -e "${RedBG} 隐藏的高级防火墙设置功能 By 蘭雅sRGB ${Font}"
  68. echo -e "${Green}> 1. ss_kcp_speed_udp2raw 端口开放 防火墙规则"
  69. echo -e "> 2. ss brook 电报代理端口开放 防火墙规则"
  70. echo -e "> 3. frps_iptables 防火墙规则"
  71. echo -e "> 4. 菜单项1-2-3全功能开放"
  72. echo -e "> 5. 使用临时${GreenBG} srgb18.ga ${Font}${Green}域名(更新脚本)"
  73. echo -e "> 6. ${RedBG}禁止使用密码远程SSH登陆${Font}"
  74. echo
  75. read -p "请输入数字(1-6):" num_x
  76. case "$num_x" in
  77. 1)
  78. ss_kcp_speed_udp2raw
  79. ;;
  80. 2)
  81. ss_bk_tg
  82. ;;
  83. 3)
  84. frps_iptables
  85. ;;
  86. 4)
  87. ss_bk_tg_frps_iptables
  88. ss_kcp_speed_udp2raw
  89. ;;
  90. 5)
  91. srgb18_ga_ddns
  92. ;;
  93. 6)
  94. no_use_passwd
  95. ;;
  96. *)
  97. ;;
  98. esac
  99. }
  100. # ss_kcp_speed_udp2raw 端口防火墙规则
  101. ss_kcp_speed_udp2raw(){
  102. # ss+kcp+udp2raw 和 # wg+speed+udp2raw 环路设置
  103. iptables -I INPUT -s 127.0.0.1 -p tcp --dport 40000 -j ACCEPT
  104. iptables -I INPUT -s 127.0.0.1 -p udp -m multiport --dport 4000,8888,9999 -j ACCEPT
  105. # udp2raw 转接端口 1999 和 2999
  106. iptables -D INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  107. tcp_port="80,443,1999,2999"
  108. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  109. RELATED_ESTABLISHED
  110. wg-quick down wg0 >/dev/null 2>&1
  111. save_iptables
  112. # 重启 WireGuard
  113. wg-quick up wg0 >/dev/null 2>&1
  114. }
  115. # ss brook 电报代理端口开放 防火墙规则
  116. ss_bk_tg(){
  117. ss_bk_tg="2018,7731,7979"
  118. iptables -D INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  119. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port},${ss_bk_tg} -j ACCEPT
  120. RELATED_ESTABLISHED
  121. save_iptables
  122. }
  123. # frps_iptables 防火墙规则
  124. frps_iptables(){
  125. frps_port="7000,7500,8080,4443,11122,2222"
  126. iptables -D INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  127. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port},${frps_port} -j ACCEPT
  128. RELATED_ESTABLISHED
  129. save_iptables
  130. }
  131. # 菜单项1-2-3全功能开放
  132. ss_bk_tg_frps_iptables(){
  133. ss_bk_tg="2018,7731,7979"
  134. frps_port="7000,7500,8080,4443,11122,2222"
  135. iptables -D INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  136. tcp_port="80,443,1999,2999"
  137. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port},${ss_bk_tg},${frps_port} -j ACCEPT
  138. RELATED_ESTABLISHED
  139. save_iptables
  140. }
  141. # 安全防火墙规则: 只能Ping和SSH
  142. safe_iptables(){
  143. iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  144. ssh_port=$(cat /etc/ssh/sshd_config | grep -e 'Port ' | awk '{print $2}')
  145. if [ ${ssh_port} != '22' ]; then
  146. iptables -A INPUT -p tcp -m tcp --dport ${ssh_port} -j ACCEPT
  147. fi
  148. iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
  149. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  150. iptables -A INPUT -j DROP
  151. iptables -P FORWARD DROP
  152. iptables -P OUTPUT ACCEPT
  153. }
  154. # 建立相关链接的优先
  155. RELATED_ESTABLISHED(){
  156. iptables -D INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  157. iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  158. }
  159. # 禁止网卡IPV6功能
  160. disable_ipv6(){
  161. ni=$(ls /sys/class/net | awk {print} | grep -e eth. -e ens. -e venet.)
  162. echo 1 > /proc/sys/net/ipv6/conf/${ni}/disable_ipv6
  163. }
  164. # 初始化安全防火墙规则
  165. init_iptables(){
  166. # 清除防火墙规则
  167. iptables -F
  168. disable_ipv6
  169. # 添加 预置 tcp 和 udp端口
  170. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT
  171. iptables -I INPUT -p udp -m multiport --dport ${udp_port} -j ACCEPT
  172. safe_iptables
  173. set_iptables
  174. }
  175. add_tcp_chain(){
  176. echo -e "${GreenBG} 追加TCP端口段到 Chain INPUT ( multiport dports) ${Font}"
  177. read -p "请输入TCP端口段(示例: 7000,7500:7510 ): " port
  178. iptables -D INPUT -p tcp -m multiport --dport ${tcp_port} -j ACCEPT >/dev/null 2>&1
  179. iptables -I INPUT -p tcp -m multiport --dport ${tcp_port},${port} -j ACCEPT
  180. RELATED_ESTABLISHED
  181. save_iptables
  182. }
  183. add_udp_chain(){
  184. echo -e "${GreenBG} 追加UDP端口段到 Chain INPUT ( multiport dports) ${Font}"
  185. read -p "请输入UDP端口段(示例: 7000,7500:7510 ): " port
  186. iptables -D INPUT -p udp -m multiport --dport ${udp_port} -j ACCEPT >/dev/null 2>&1
  187. iptables -I INPUT -p udp -m multiport --dport ${udp_port},${port} -j ACCEPT
  188. RELATED_ESTABLISHED
  189. save_iptables
  190. }
  191. # 删除指定INPUT Chain 序号行
  192. del_chain(){
  193. iptables -nvL --line
  194. echo -e "${RedBG} 删除指定INPUT Chain 序号行 ${Font}"
  195. read -p "请检查INPUT Chain序号行,输入序号(2-X): " no_x
  196. if [[ ${no_x} -ge 2 ]] && [[ ${no_x} -le 20 ]]; then
  197. iptables -D INPUT ${no_x}
  198. else
  199. echo -e "${RedBG}:: INPUT Chain序号行选择错误,没有删除!${Font}"
  200. fi
  201. save_iptables
  202. }
  203. # 禁止ICMP,禁止Ping服务器
  204. no_ping(){
  205. iptables -D INPUT -p icmp --icmp-type echo-request -j ACCEPT
  206. }
  207. no_iptables(){
  208. # Debian 和 Centos 关闭防火墙命令分别是
  209. iptables -F && iptables-save > /etc/iptables/rules.v4 >/dev/null 2>&1
  210. iptables -F && service iptables save >/dev/null 2>&1
  211. }
  212. # 设置菜单
  213. start_menu(){
  214. echo
  215. echo -e "${GreenBG} IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB 特别感谢 TaterLi 指导 ${Font}"
  216. echo -e "${RedBG} 原则: 规则不宜超过10条,3-5条最好,每增加规则系统都忙很多。 ${Font}"
  217. echo -e "${Green}> 1. 追加 TCP 多端口到防火墙规则"
  218. echo -e "> 2. 追加 UDP 多端口到防火墙规则"
  219. echo -e "> 3. 删除指定INPUT Chain 序号行(原则: 精简规则)"
  220. echo -e "> 4. 禁止ICMP,禁止Ping服务器"
  221. echo -e "> 5. 重置初始化安全防火墙规则(首次需运行)"
  222. echo -e "> 6. 退出设置"
  223. echo -e "> 7. 关闭 IPTABLES 防火墙"
  224. echo -e "> 8. ${RedBG} 小白一键设置防火墙 ${Font}"
  225. echo
  226. read -p "请输入数字(1-8):" num
  227. case "$num" in
  228. 1)
  229. add_tcp_chain
  230. ;;
  231. 2)
  232. add_udp_chain
  233. ;;
  234. 3)
  235. del_chain
  236. ;;
  237. 4)
  238. no_ping
  239. ;;
  240. 5)
  241. init_iptables
  242. ;;
  243. 6)
  244. netstat -ltup
  245. exit 1
  246. ;;
  247. 7)
  248. no_iptables
  249. ;;
  250. 8)
  251. init_iptables
  252. ss_kcp_speed_udp2raw
  253. ;;
  254. 88)
  255. hide_menu
  256. ;;
  257. *)
  258. echo
  259. ;;
  260. esac
  261. iptables -nvL --line
  262. }
  263. check_sys
  264. start_menu