馬哥2016全新Linux+Python高端營運班-Iptables 防火牆基礎練習,tcp_wrapper

來源:互聯網
上載者:User

標籤:iptables   tcp_wrapper   pam.d   

本周作業內容:


系統的INPUT和OUTPUT預設策略為DROP,請完成以下關於iptables的題目;

先設定預設策略為DROP

# iptables -P OUTPUT DROP

# iptables -P INPUT DROP

1、限制本地主機的web伺服器在周一不允許訪問;新請求的速率不能超過100個每秒;web伺服器包含了admin字串的頁面不允許訪問;web伺服器僅允許響應報文離開本機;

 # iptables -A  INPUT -d 192.168.137.30 -p tcp --dport 80 -m time ! --weekdays  Mon  -m state --state NEW -m limit --limit 100/s -j ACCEPT

 # iptables -I OUTPUT -s 192.168.137.30 -m string --algo bm --string ‘admin‘ -j REJECT

 #  iptables -A  OUTPUT -s 192.168.137.30 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

 

2、在工作時間,即周一到周五的8:30-18:00,開放原生ftp服務給172.16.0.0網路中的主機訪問;資料下載請求的次數每分鐘不得超過5個;


首先這台伺服器:裝載ftp追蹤時的專用的模組:

# modprobe nf_conntrack_ftp

# iptables -A INPUT -d 192.168.137.30 -s 172.16.0.0/16 -p tcp --dport 21 -m time --weekdays 1,2,3,4,5 -m time --timestart 8:30 --timestop 18:00 -m state --state NEW,ESTABLISHED -j ACCEPT

# iptables -A INPUT -d 192.168.137.30 -s 172.16.0.0/16 -p tcp --dport 21 -m state --state RELATED,ESTABLISHED -j ACCEPT

# iptables -A OUTPUT -s 192.168.137.30 -d 172.16.0.0/16 -p tcp -m state --state ESTABLISHED -j ACCEPT


3、開放原生ssh服務給172.16.x.1-172.16.x.100中的主機,x為你的座位號,新請求建立的速率一分鐘不得超過2個;僅允許響應報文通過其服務連接埠離開本機;

# iptables -A INPUT -d 192.168.137.30 -p tcp --dport 22 -m iprange --src-range 172.16.2.1-172.16.2.100 -m limit --limit 2/minute -j ACCEPT

# iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT


4、拒絕TCP標誌位全部為1及全部為0的報文訪問本機;

iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP


5、允許本機ping別的主機;但不開放別的主機ping本機;

# iptables -A INPUT -d 192.168.137.30 -p icmp --icmp-type 0 -jACCEPT

# iptables -A OUTPUT -s 192.168.137.30 -p icmp --icmp-type 8 -jACCEPT



6、判斷下述規則的意義:


# iptables -N clean_in

新建立一條自訂鏈叫clean_in

# iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP

丟棄廣播域的包

# iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP

丟棄172.16網路段廣播報文

# iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP

丟棄syn狀態不為1且為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的 就返回 clean_in鏈

# 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的報文丟棄

# 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的報文丟棄

# iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROP

指定流入報文介面為eth0,協議為udp,目標連接埠為1026的報文丟棄

# iptables -A INPUT -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP

指定流入報文介面為eth0,協議為tcp,指定多個連接埠1433,4899,報文丟棄


# 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記錄檔中;


vim /etc/hosts.allow

vsftpd:172.16.0.0/255.255.0.0 EXCEPT 172.16.100.3  ##子網路遮罩必須為長格式

vim /etc/hosts.deny

vsftpd:ALL :spawn /bin/echo `date` login attempt from %c to %s, %d >> /var/log/tcp_wrapper.log



8、刪除/boot/grub/grub.conf檔案中所有行的行首的空白字元;

# sed ‘[email protected]^[[:space:]]\[email protected]@‘ /etc/grub2.cfg


9、刪除/etc/fstab檔案中所有以#開頭,後跟至少一個空白字元的行的行首的#和空白字元;

# sed ‘[email protected]^#[[:space:]]\[email protected]@‘ /etc/fstab


10、把/etc/fstab檔案的奇數行另存新檔/tmp/fstab.3;

# sed ‘n;d‘ /etc/fstab >> /tmp/fstab.3

11、echo一個檔案路徑給sed命令,取出其基名;進一步地,取出其路徑名;

# echo "/etc/fstab" | sed ‘[email protected][^/]\+/\[email protected]@‘

12、統計當前系統上所有tcp串連的各種狀態的個數;

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

13、統計指定的web訪問日誌中各ip的資源訪問次數:

# awk ‘{ip[$1]++}END{for(i in ip) {print i,ip[i]}}‘ /var/log/httpd/access_log

14、授權centos使用者可以運行fdisk命令完成磁碟管理,以及使用mkfs或mke2fs實現檔案系統管理;

# visudo 添加如下一行

# centos  ALL=(root)   NOPASSWD: /sbin/fdisk, /sbin/mke2fs, /sbin/mkfs

15、授權gentoo使用者可以運行邏輯卷管理的相關命令;

# gentoo  ALL=(root)   lvm

16、基於pam_time.so模組,限制使用者通過sshd服務遠程登入只能在工作時間進行;

(1).# vim /etc/pam.d/sshd

在account required pam_nologin.so上插入一行:

account required pam_time.so

(2).編輯pam_time.so模組的設定檔

# vim /etc/security/time.conf

*;*;*;MoTuWeThFr0900-1800

上面表示工作時間的9點到下午6點允許訪問ssh

17、基於pam_listfile.so模組,定義僅某些使用者,或某些組內的使用者可登入系統;

建立一個使用者的列表檔案,例如/etc/sshd_userlist,然後編輯檔案

root 

centos

gentoo

然後修改檔案的許可權和屬主

# chmod 600 /etc/sshd_userlist 

# chown root /etc/sshd_userlist

再編輯/etc/pam.d/sshd檔案,加入以下一行內容:

auth required pam_listfile.so item=user sense=allow file=/etc/sshd_userlist onerr=succeed


本文出自 “Ljohn” 部落格,請務必保留此出處http://ljohn.blog.51cto.com/11932290/1874279

馬哥2016全新Linux+Python高端營運班-Iptables 防火牆基礎練習,tcp_wrapper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.