系统的INPUT和OUTPUT默认策略为DROP,请完成以下关于iptables的题目;

1、限制本地主机的web服务器在周一不允许访问;新请求的速率不能超过100个每秒;web服务器包含了admin字符串的页面不允许访问;web服务器仅允许响应报文离开本机;

[root@C1 ~]# iptables -F    #清空filter表上的所有规则链
[root@C1 ~]# iptables -X    #删除用户自定链
[root@C1 ~]# iptables -t filter -A INPUT -d 192.168.1.71 -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT    #放行SSH和HTTP的SYN报文
[root@C1 ~]# iptables -t filter -I INPUT -d 192.168.1.71 -m state --state ESTABLISHED -j ACCEPT    #放行所有目标为192.168.1.71的已建立链接的请求报文
[root@C1 ~]# iptables -t filter -A OUTPUT -s 192.168.1.71 -m state --state ESTABLISHED -j ACCEPT    #只放行已建立链接的出部请求
[root@C1 ~]# iptables -P INPUT DROP    #INPUT链的默认规置为则为DROP
[root@C1 ~]# iptables -P OUTPUT DROP    #OUTPUT链的默认规置为则为DROP
[root@C1 ~]# iptables -t filter -I INPUT -d 192.168.1.71 -p tcp --dport 80 -m time --weekdays Mon -j REJECT    #周一禁止访问httpd服务
[root@C1 ~]# iptables -t filter -I INPUT -d 192.168.1.71 -m limit --limit-burst 200 --limit 100/second -j ACCEPT    #设置可以接受的请求报文的速率为平均100个/秒,峰值为200个。
[root@C1 ~]# iptables -R OUTPUT 1 -s 192.168.1.71 -p tcp --sport 80 -m string --algo bm --string "admin" -j REJECT    #拦截http响应报文中包含“admin”字符串的报文

2、在工作时间,即周一到周五的8:30-18:00,开放本机的ftp服务给172.16.0.0网络中的主机访问;数据下载请求的次数每分钟不得超过5个;

 (1). 检查并装载nf_conntrack_ftp模块:

[root@C1 ~]# ll /lib/modules/3.10.0-327.el7.x86_64/kernel/net/netfilter/nf_conntrack_ftp.ko 
-rw-r--r--. 1 root root 26181 11月 20 2015 /lib/modules/3.10.0-327.el7.x86_64/kernel/net/netfilter/nf_conntrack_ftp.ko
[root@C1 ~]# modprobe nf_conntrack_ftp
[root@C1 ~]# lsmod | grep ftp
nf_conntrack_ftp       18638  0 
nf_conntrack          105745  3 xt_conntrack,nf_conntrack_ftp,nf_conntrack_ipv4

 (2). 在1题的基础上进行修改iptables:

[root@C1 ~]# iptables -L -n -v --line-number
Chain INPUT (policy DROP 18 packets, 2321 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1030 74542 ACCEPT     all  --  *      *       0.0.0.0/0            192.168.1.71         limit: avg 100/sec burst 200
2        0     0 REJECT     tcp  --  *      *       0.0.0.0/0            192.168.1.71         tcp dpt:80 TIME on Mon UTC reject-with icmp-port-unreachable
3     2616  210K ACCEPT     all  --  *      *       0.0.0.0/0            192.168.1.71         state ESTABLISHED
4        7   420 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.71         multiport dports 22,80 state NEW
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
Chain OUTPUT (policy DROP 28 packets, 8506 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       22  7434 REJECT     tcp  --  *      *       192.168.1.71         0.0.0.0/0            tcp spt:80 STRING match  "admin" ALGO name bm TO 65535 reject-with icmp-port-unreachable
2     2290  413K ACCEPT     all  --  *      *       192.168.1.71         0.0.0.0/0            state ESTABLISHED
[root@C1 ~]# iptables -D INPUT 1    #删除INPUT链中的1-2行规则
[root@C1 ~]# iptables -D INPUT 1
[root@C1 ~]# iptables -R INPUT 1 -d 192.168.1.71 -s 192.168.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@C1 ~]# iptables -I INPUT -d 192.168.1.71 -s 192.168.1.0/24 -p tcp --dport 21 -m state --state NEW -m time --weekdays 1,2,3,4,5 --timestart 8:30 --timestop 18:00 -j ACCEPT
[root@C1 ~]# iptables -L -n --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.1.0/24       192.168.1.71         tcp dpt:21 state NEW TIME from 08:30:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri UTC
2    ACCEPT     tcp  --  192.168.1.0/24       192.168.1.71         state RELATED,ESTABLISHED
3    ACCEPT     tcp  --  0.0.0.0/0            192.168.1.71         multiport dports 22,80 state NEW
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    REJECT     tcp  --  192.168.1.71         0.0.0.0/0            tcp spt:80 STRING match  "admin" ALGO name bm TO 65535 reject-with icmp-port-unreachable
2    ACCEPT     all  --  192.168.1.71         0.0.0.0/0            state ESTABLISHED   #确保ESTABLISHED链接可以出栈

3、开放本机的ssh服务给172.16.x.1-172.16.x.100中的主机,x为你的座位号,新请求建立的速率一分钟不得超过2个;仅允许响应报文通过其服务端口离开本机;

 本题中172.16.x替换为192.168.x

 (1). 初始化iptables:

[root@C1 ~]# iptables -P INPUT ACCEPT
[root@C1 ~]# iptables -P OUTPUT ACCEPT
[root@C1 ~]# iptables -F
[root@C1 ~]# iptables -X

 (2). 设置链规则:

[root@C1 ~]# iptables -A INPUT -d 192.168.1.71 -m iprange --src-range 192.168.1.1-192.168.1.100 -p tcp --dport 22 -m limit --limit 2/minute -m state --state NEW -j ACCEPT
[root@C1 ~]# iptables -I INPUT -d 192.168.1.71 -p tcp --dport 22 -m state --state ESTABLISHED -j ACCEPT
[root@C1 ~]# iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT
[root@C1 ~]# iptables -P INPUT DROP
[root@C1 ~]# iptables -P OUTPUT DROP
[root@C1 ~]# iptables -L -n -v --line-number
Chain INPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      279 19728 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.71         tcp dpt:22 state ESTABLISHED
2        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.71         source IP range 192.168.1.1-192.168.1.100 tcp dpt:22 limit: avg 2/min burst 5 state NEW
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
Chain OUTPUT (policy DROP 1 packets, 76 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       87  8008 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state ESTABLISHED

4、拒绝TCP标志位全部为1及全部为0的报文访问本机;

[root@C1 ~]# iptables -A  INPUT -d 192.168.1.71 -p tcp --tcp-flags ALL ALL -j DROP
[root@C1 ~]# iptables -A  INPUT -d 192.168.1.71 -p tcp --tcp-flags ALL NONE -j DROP

5、允许本机ping别的主机;但不开放别的主机ping本机;

# iptables -I OUTPUT -s 192.168.1.71 -p icmp --icmp-type 8 -j ACCEPT
# iptables -I INPUT -d 192.168.1.71 -p icmp --icmp-type 0 -j ACCEPT
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP

6、判断下述规则的意义:

# iptables -N clean_in    //创建名为“clean_in”的自定义链

# iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP    //禁止广播ping

# iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP    //禁止对172.16网段的主机进行广播ping


# iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP    //禁止非TCP第一次握手请求报文中状态为NEW的所有链接

# iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP    //禁止TCP标志位所有为1的报文

# iptables -A clean_in -p tcp --tcp-flags ALL NONE -j DROP    //禁止TCP标志位中所有为0的报文

# iptables -A clean_in -d 172.16.100.7 -j RETURN    //返回目标主机为172.16.100.7的报文到主链



# iptables -A INPUT -d 172.16.100.7 -j clean_in    //配置目标主机为172.16.100.7的报文交于clean_in链进行检查


# iptables -A INPUT -i lo -j ACCEPT    //接受来自回环接口的流量

# iptables -A OUTPUT -o lo -j ACCEPT    //允许去住回环接口的流量



# iptables -A INPUT -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP    //拒绝eth0接口上的TCP 53,113,135,137,139,445流量(DNS及Samba相关)

# iptables -A INPUT -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP    //拒绝eth0接口上的UDP 53,113,135,137,139,445流量(DNS及Samba相关)

# iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROP    //拒绝eth0接口上的cap协议

# iptables -A INPUT -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP    //拒绝eth0接口上的MSSQL和远程控制报文


# iptables -A INPUT -p icmp -m limit --limit 10/second -j ACCEPT    //限制ping流量为10/秒

7、通过tcp_wrapper控制vsftpd仅允许172.16.0.0/255.255.0.0网络中的主机访问,但172.16.100.3除外;对所被被拒绝的访问尝试都记录在/var/log/tcp_wrapper.log日志文件中;

声明:此题中的172.16.0.0/16改为192.168.1.0/24,172.16.100.3改为192.168.1.72

[root@C1 ~]# grep -v '^#' /etc/hosts.deny 
vsftpd: ALL :spawn /bin/echo `date` login attempt from %c to %s, %d >> /var/log/tcp_wrapper.log
[root@C1 ~]# grep -v '^#' /etc/hosts.allow 
vsftpd: 192.168.1. EXCEPT 192.168.1.72
[root@C1 ~]# tail -2 /var/log/tcp_wrapper.log 
Sun Nov 27 22:46:38 CST 2016 login attempt from 192.168.1.72 to vsftpd@192.168.1.71, vsftpd
Sun Nov 27 22:46:54 CST 2016 login attempt from 192.168.1.72 to vsftpd@192.168.1.71, vsftpd

8、删除/boot/grub/grub.conf文件中所有行的行首的空白字符;

# sed -i 's@^[[:space:]]\+@@' grub.conf

9、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符;

# grep -i 's@^#[[:space:]]\+@@' fstab

10、把/etc/fstab文件的奇数行另存为/tmp/fstab.3;

# cat -n /etc/fstab | sed -n '1~2w /tmp/fstab.3'

11、echo一个文件路径给sed命令,取出其基名;进一步地,取出其路径名;

基名: 

# echo /etc/sysconfig/network-scripts/ | sed -r 's@^.*/([^/]+)/?$@\1@'   #-r:使用扩展正则表达式
network-scripts

路径名:

# echo /etc/sysconfig/network-scripts/ | sed 's@[^/]\+/\?$@@'
/etc/sysconfig/

12、统计当前系统上所有tcp连接的各种状态的个数;

# netstat -tan | awk '/^tcp\>/{state[$NF]++} END{for(i in state) {print i,state[i]}}'
LISTEN 2
ESTABLISHED 1

13、统计指定的web访问日志中各ip的资源访问次数:

# awk '{ip[$1]++} END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log
192.168.1.103 139
192.168.1.104 19
::1 29

14、授权centos用户可以运行fdisk命令完成磁盘管理,以及使用mkfs或mke2fs实现文件系统管理;

在/etc/sudoers中添加对centos用户的授权,授权centos用户只能以root身份执行/usr/sbin/{fdisk,mkfs,mke2fs}命令:

# tail -n 3 /etc/sudoers
User_Alias  POWERUSER = centos
Cmnd_Alias  DISKMANAGE = /usr/sbin/fdisk,/usr/sbin/mkfs,/usr/sbin/mke2fs
POWERUSERALL=(root)NOPASSWD: DISKMANAGE

验证:

[centos@C1 ~]$ sudo -l
匹配此主机上 centos 的默认条目:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS",
    env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT
    LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS
    _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
用户 centos 可以在该主机上运行以下命令:
    (root) NOPASSWD: /usr/sbin/fdisk, /usr/sbin/mkfs, /usr/sbin/mke2fs
[centos@C1 ~]$ sudo fdisk -l
磁盘 /dev/sda:85.9 GB, 85899345920 字节,167772160 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000a33c9
   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048     5222399     2098176   82  Linux swap / Solaris
/dev/sda3         5222400   167772159    81274880   83  Linux

15、授权gentoo用户可以运行逻辑卷管理的相关命令;

[root@C1 ~]# tail -4 /etc/sudoers
Cmnd_Alias  DISKMANAGE = /usr/sbin/fdisk,/usr/sbin/mkfs,/usr/sbin/mke2fs
Cmnd_AliasLVMCOMMAND = /usr/sbin/pv*, /usr/sbin/lv*, /usr/sbin/vg*
POWERUSERALL=(root)NOPASSWD: DISKMANAGE
gentooALL=(root)NOPASSWD: LVMCOMMAND,DISKMANAGE

验证:

[gentoo@C1 ~]$ sudo -l
匹配此主机上 gentoo 的默认条目:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS",
    env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT
    LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS
    _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
用户 gentoo 可以在该主机上运行以下命令:
    (root) NOPASSWD: /usr/sbin/pv*, /usr/sbin/lv*, /usr/sbin/vg*, (root) /usr/sbin/fdisk, /usr/sbin/mkfs, /usr/sbin/mke2fs
[gentoo@C1 ~]$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/sdb1       lvm2 ---  500.00m 500.00m
  /dev/sdb2       lvm2 ---  500.00m 500.00m
  /dev/sdb3       lvm2 ---  500.00m 500.00m

16、基于pam_time.so模块,限制用户通过sshd服务远程登录只能在工作时间进行;

(1).修改pam_time.so模块对应的配置文件:

配置文件:/etc/security/time.conf

语法:services;ttys;users;times

# egrep -v '^#|^$' /etc/security/time.conf
sshd;pts/*;gentoo;Wd0000-2400 | Wk1800-0800   #禁止gentoo用户通过sshd服务在周末以及工作日的18-8点登陆到pts终端

(2). 修改sshd调用PAM通用框架时的配置文件,以启用pam_time.so模块进行检测

配置文件:/etc/pam.d/sshd

起始处添加以下行:

auth        required     pam_time.so


说明:用户登陆时强制使用pam_time.so认证,若通过需进一步检查其他项目,若不通过直结拒绝

验证:

$ ssh gentoo@192.168.1.71
gentoo@192.168.1.71's password: 
Permission denied, please try again.
gentoo@192.168.1.71's password: 
Permission denied, please try again.
gentoo@192.168.1.71's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).   #即使输入正常的密码也无法登陆

17、基于pam_listfile.so模块,定义仅某些用户,或某些组内的用户可登录系统;

(1).pam_listfile.so模块没有专用的配置文件,可通过在调用该模块时为其指定模块变量来实现功能说明

在sshd的PAM定义文件/etc/pam.d/sshd中的起始处添加下行:

auth        required    pam_listfile.so item=user sense=deny file=/etc/pam.d/users.deny onerr=succeed

说明:

item=user:定义对哪些项目启用规则,可选项有[tty|user|rhost|ruser|group|shell]

sense=deny:定义匹配文件中的用户后拒绝,即黑名单

file=/etc/pam.d/users.deny:指定检查的匹配文件

onerr=succeed:定义了当出现错误(比如无法打开配置文件)时的缺省返回值

(2).创建pam_listfile.so模块的匹配文件

# echo centos > /etc/pam.d/users.deny
# ll /etc/pam.d/users.deny
-rw-r--r-- 1 root root 7 12月  4 00:40 /etc/pam.d/users.deny
# chmod 600 /etc/pam.d/users.deny

验证:

yinkai@yinkai-NB-X230 ~ $ ssh gentoo@192.168.1.71
gentoo@192.168.1.71's password: 
Last login: Sun Dec  4 01:07:42 2016 from 192.168.1.104
[gentoo@C1 ~]$ exit
登出
Connection to 192.168.1.71 closed.
yinkai@yinkai-NB-X230 ~ $ ssh centos@192.168.1.71
centos@192.168.1.71's password: 
Permission denied, please try again.
centos@192.168.1.71's password: 
Permission denied, please try again.
centos@192.168.1.71's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).