服务器配置札记五(iptables)

暴露在外网的服务器面临很大的安全隐患,因此应该加强安全防护。iptables和selinux是我目前所知的能够显著加强安全系统措施,下面说的是配置iptables。

前面所说的nis和nfs服务配置都是在selinux和iptables关闭的情形下才能成功,如果启用了selinux和iptables,可能需要额外的配置。

查看selinux状态: getenforce

查看iptables状态: service iptables status

更改selinux的状态可以编辑/etc/sysconfig/selinux文件。如果selinux已经启用,可以使用setenforce来改变临时状态。由于selinux是与系统集成的,在未启用的情形下是不能使用setenforce来改变的状态的。此时需要编辑/etc/sysconfig/selinux文件,然后重启来启动selinux。

iptables是作为系统服务,启用和停止方法和其他服务类似。iptables顾名思义应该是管理数据包的,其中对系统影响最大的当属进入系统的包。iptables使用表、链、规则的结构对包或者报文进行管理。常用的表示filter(过滤器),内置了INPUT,OUTPUT,FORWARD三条链。实际中只操作过filter这个表,下面内容只针对这张表。

使用iptables -L可以查看当前系统的过滤规则。永久性的规则保存在/etc/sysconfig/iptables文件里。文件架构如下:

*filter                                     <==iptables表名,即下面内容属于表filter

:INPUT ACCEPT [0:0]                          <== INPUT链的策略,默认是接受(即没有匹配规则就允许数据包通过)

:OUTPUT ACCEPT[0:0]                     <==OUTPUT链的策略,默认是接受

:FORWARD ACCEPT[0:0]

-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

….

这是文件中的结构示例。参数的含义可以使用iptables -h查看。常用的参数有:

-A  append的缩写: 将这条规则追加到链的末端。注意链的匹配是顺序进行的,有时候顺序就很重要。同理-D则是删除,-I插入到链的顶部,-R是替换,-F则是移除所有规则。

-m match的缩写:匹配状态,协议,地址等

-p procotol的缩写:包的协议,常用的是tcp和udp。icmp则是主机和路由之间的协议。

-P policy的缩写:链的规则,常用有ACCEPT,DROP,REJECT。:INPUT ACCEPT [0:0]即链的策略为接受。

-i interface的缩写:包流经的网口,包括lo,eth0,eth1等。

-s source的缩写:包的来源,ip地址或者主机名,则–sport指定包的端口

-d destination的缩写:包的去向,ip地址或主机名, –dport指定包的端口

-j jump的缩写:指定如果符合条件处理包的规则,常用为ACCEPT,DROP和REJECT。

配置实例:

  1. 清除所有规则: iptables -F

  2. 查看当前规则:iptables -L。这时候可以看到三张表都是空的,默认规则都是接受。还好默认规则是接受,否则ssh服务都将在iptables -F这个命令后就失去响应。

  3. 允许ssh的包经过: iptables -A INPUT -p tcp –dport 22 -j ACCEPT

  4. 允许内部链路通过: iptables -A INPUT -i lo -j ACCEPT

  5. 允许主机和路由通信的icmp协议通过: iptables -A INPUT -p icmp -j ACCEPT

6.允许已建立连接或者相关的包通过: iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT

  1. 允许192.168.1.0、24网段的包进入:iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

  2. 拒绝其他所有包: iptables -A INPUT -j REJECT –reject-with icmp-host prohibited

其中–reject-with 指示拒绝类型为主机禁止,如果不指定类型则默认为主机不可达。

由于对外只开放了22端口,如果主机上有ftp,web等服务将无法在192.168.1.0/24之外的网段访问。如果sshd监听多端口,可以测试也将无法连接。

由于INPUT链的策略默认是接受,为了主机安全应当只让受信任的主机或者指定端口接入。在链的最后添加iptables -A INPUT -j REJECT是一个很好的选择。但是应当谨记规则是逐条匹配的,对于INPUT链来说这条规则应该在最后。INPUT链中位于这条规则之后的追加规则都将失效。因为这个顺序问题在配置的时候可是郁闷了很多天,血淋淋的教训啊。当初天真的认为把规则写在COMMIT之前就能生效,默认的规则都没动,导致了悲剧的发生。

按照如此配置,将NIS和NFS的服务固定端口,然后开放端口即可。如果信任,可以直接允许24网段直接访问即可。

 

留言评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select * from vps_autoblog_queue where is_running >0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=6 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=6 and source_url_key='85f8e27ce55bd78fd33f47d2b1409198'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(6,'https://www.hijk.pw/post-sitemap.xml','85f8e27ce55bd78fd33f47d2b1409198',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=4 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=4 and source_url_key='54b1dccf98c13c81f546faf82132bc77'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(4,'https://ssr.tools/sitemap-posttype-post.2020.xml','54b1dccf98c13c81f546faf82132bc77',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=2 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=2 and source_url_key='91372ddcd9e4809b115feaa5b6f00815'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(2,'https://tlanyan.me/post-sitemap.xml','91372ddcd9e4809b115feaa5b6f00815',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=7 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=7 and source_url_key='569e17863f7693f1afa9e9256af9dcff'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(7,'https://aisoa.cn/sitemap.xml','569e17863f7693f1afa9e9256af9dcff',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=9 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=9 and source_url_key='cc9416f8df40d8c3a48e66856b1cd6fd'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(9,'https://www.v2rayssr.com/sitemap.xml','cc9416f8df40d8c3a48e66856b1cd6fd',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=10 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=10 and source_url_key='74714f67855820f0c701ce905b231f27'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(10,'https://doubibackup.com/sitemap.xml','74714f67855820f0c701ce905b231f27',1750610985,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,is_running,last_post_time,post_interval,not_check_stoped from vps_autoblog_queue WHERE is_running>0 and fetched=0 order BY id ASC

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,sn_id from vps_autoblog_queue WHERE is_running=0 and fetched=0 order BY id ASC LIMIT 1