系统之家 - Windows操作系统&装机软件下载网站!

当前位置: 首页  >  教程资讯  >  电脑教程 Linux防火墙iptables简明教程

Linux防火墙iptables简明教程

时间:2023-06-02 16:16:41 来源: 人气:

   前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔除了许多冗余的内容,提取出尽量多的精华部分成文,和大家共同学习,本文涉及的内容包括如下,  Linux防火墙iptables简明教程,  1.安装iptables,  2.查看现有的iptables规则,  3.删除某iptables规则,  4.清除现有iptables规则,  5.创建规则,  6.设置开机启动,  7.保存iptables规则,  8.iptables在手动防CC攻击中的简单应用,  1.安装iptables,  很多Linux已经默认安装iptables,可使用后文的查看命令测试是否安装,  CentOS/RedHat下执行:,  yum install iptablesDebian/Ubuntu下执行:,  apt-get install iptables,  2.查看现有的iptables规则,  命令后面的line-number为显示行号(将规则一则一则输出,并显示行号),可选,方便后文的删除指令。,  iptables -L -n --line-numbers,  3.删除某iptables规则,  例如,删除第12行的规则,行号可由之前的命令查看,  iptables -D INPUT 12,  4.清除现有iptables规则,  iptables -F,  iptables -X,  iptables -Z,  5.创建规则,  a).开放端口,  命令iptables -A INPUT -j REJECT将屏蔽其他未授权的端口,因此请务必开放22端口以保障SSH连接正常~,  代码如下:,  #允许本机访问,  iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT,  # 允许已建立的或相关连的通行,  iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT,  #允许所有本机向外的访问,  iptables -A OUTPUT -j ACCEPT,  # 允许访问22端口,  iptables -A INPUT -p tcp --dport 22 -j ACCEPT,  #允许访问80端口,  iptables -A INPUT -p tcp --dport 80 -j ACCEPT,  #允许FTP服务的21和20端口,  iptables -A INPUT -p tcp --dport 21 -j ACCEPT,  iptables -A INPUT -p tcp --dport 20 -j ACCEPT,  #如果有其他端口的话,规则也类似,稍微修改上述语句就行,  #禁止其他未允许的规则访问,  iptables -A INPUT -j REJECT,  iptables -A FORWARD -j REJECT,  b).屏蔽ip,  iptables -I INPUT -s 123.123.123.123 -j DROP可通过更换上述ip为ip段来达到屏蔽ip段的目的~,  若需屏蔽整个ip段(123.0.0.1到123.255.255.254)则换为123.0.0.0/8,  若需屏蔽ip段123.123.0.1到123.123.255.254,则换为124.123.0.0/16,  若需屏蔽ip段123.123.123.1到123.123.123.254则换为123.123.123.0/24,  6.设置开机启动,  一般在安装iptables完成后,开机启动会自动设置成功,但在个别CentOS系统上,貌似还有些问题,可以使用如下命令手动设置,  chkconfig --level 345 iptables on,  7.保存iptables规则,  service iptables save,  8.iptables在手动防CC攻击中的简单应用,  关于获取攻击者ip的方法,可以通过很多方法获取,如查看网站日志等,本文不再赘述。,  a).建立要屏蔽的ip/ip段文件,名为ip.txt,  #屏蔽的ip,  123.4.5.6,  #屏蔽的ip段(编写方法,同前文),  123.4.5.6/24b).建立block_ip.sh脚本文件,  代码如下:,  #!/bin/sh,  # Filename: block_ip.sh,  # Purpose: blocks all IP address/network found in a text file,  # The text file must have one IP address or network per line,  #################################################################,  # Change the following path/filename to match yours,  IP_LIST_FILE=/path/to/ip.txt,  #################################################################,  # Dont change anything below unless you are a smarty pant!,  #################################################################,  IPTABLES_BIN=/sbin/iptables,  # Get the IP address/network from the file and ignore any line starting with # (comments),  BAD_IP_ADDR_LIST=$(grep -Ev "^#" $IP_LIST_FILE),  # Now loop through the IP address/network list and ban them using iptabels,  for i in $BAD_IP_ADDR_LIST,  do,  echo -n "Blocking $i ...";,  $IPTABLES_BIN -A INPUT -s $i -j DROP,  $IPTABLES_BIN -A OUTPUT -d $i -j DROP,  echo "DONE.";,  done,  ##################################################################,  # END OF SCRIPT - NOTHING TO SEE HERE - THATS ALL FOLKS!,  ##################################################################,  c).运行脚本,  sh /path/to/block_ip.sh,  d).查看iptables规则是否生效/正确,这一步的命令,之前有提到哦,开动脑筋,实在忘了,点击此处~,

作者

教程资讯

电脑教程排行

系统教程

系统主题