Linux系統平常管理2 tcpdump,iptables


                Linux系統平常管理2 tcpdump,iptables           linux

  1. Linux抓包工具bash


tcpdump 系統自帶抓包工具
網絡

若是沒有安裝,須要安裝以後纔可使用tcp

安裝:ide

[root@linux2 ~]# yum install -y tcpdump


tcpdump -nn工具

不轉換頓口的名字,直接顯示端口號spa


tcpdump -nn -i eth0 tcp and host 192.168.0.1 and port 80rest

抓取192.168.0.1ip地址的80端口的tcp包,而且不進行端口名字的轉換日誌

-i:指定網卡three

tcp:表示只抓取tcp的包

192.168.0.1:指定的ip地址

port 80:指定的,只抓取80端口的數據


tcpdump -nn -i eth0 tcp and host 192.168.0.1 and port 80 -w 1.txt

把抓取的數據包寫入到1.txt文件中

-w:寫入


tcpdump -nn -i eth0 tcp and host 192.168.0.1 and port 80 -c 10 -w 1.txt

抓取10個數據包,而且寫入到1.txt文件中

-c:指定抓取多少個包

-s0:抓取全部內容


[root@linux2 ~]# tcpdump -nn -i eth0 tcp and not port 80

過濾掉80端口


tcpdump -nn -vs0 tcp and port not 22 -c 100 -w 1.cap 

-w:寫入


strings:能夠查看二進制包裏面的內容


wireshark

默認沒有安裝,須要安裝

yum install -y wireshark


抓包分析http請求,查看80端口上面請求的一些東西

tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

在什麼狀況下使用:

   一、訪問日誌沒有記錄

   二、不知道配置文件在哪裏,不不想去配置日誌文件

   三、只是抓跑看看內容

   四、若是抓包沒有任何顯示的時候,須要指定網卡在

tshark -i eth0 -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

 

tcpdump和tshark都須要用root的身份才能夠執行


2. Selinux

getenforce:查看selinux狀態

配置文件 /etc/selinux/config 

三種狀態:

     enforcing:開啓

permissive:開啓可是不生效,觸碰規則的時候警告

disabled:關閉

要設置永久關閉,須要更改配置文件:

[root@linux2 ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing #改爲SELINUX=disabled,就永久關閉了
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

 更改以後重啓Linux系統就會生效


setenforce 0:臨時關閉,重啓後失效

setenforce 1:臨時啓用,可是若是SELINUX=disabled,是沒法啓用的


若是沒有getenforce這個包,須要安裝

使用rpm查看包名:

rpm -qf `which getenforce`

安裝

yum install -y libselinux-utils 

使用yum也能夠安裝

yum install *selinux*


3. netfilter --  iptables

iptables -nvL 查看規則

iptables -F 清除當前的規則,系統從啓後會失效,只是臨時的

iptables -Z 計數器清零

service iptables save 保存規則 保存的規則文件爲:/etc/sysconfig/iptables 

service iptables stop  能夠暫停防火牆,可是重啓後它會讀取/etc/sysconfig/iptables 從而啓動防火牆,另外即便咱們中止防火牆,但一旦咱們添加任何一條規則,它也會開啓。

iptables -t  指定表名,默認不加-t則是filter表 

filter 這個表主要用於過濾包的,是系統預設的表,內建三個鏈INPUT、OUTPUT以及FORWARD。INPUT做用於進入本機的包;OUTPUT做用於本機送出的包;FORWARD做用於那些跟本機無關的包。  


iptables -nvL -t nat

指定nat表

nat 主要用處是網絡地址轉換、端口映射,也有三個鏈。PREROUTING 鏈的做用是在包剛剛到達防火牆時改變它的目的地址,若是須要的話。OUTPUT鏈改變本地產生的包的目的地址。POSTROUTING鏈在包就要離開防火牆以前改變其源地址。 


mangle 主要用於修改數據包的TOS(Type Of Service,服務類型)、TTL(Time ToLive,生存週期)值以及爲數據包設置Mark標記,以實現Qos (Quality of Service,服務質量)調整以及策略路由等應用,因爲須要相應的路由設備支持,所以應用並不普遍。 五個鏈:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD


raw   對報文設置一個標誌,決定數據包是否被狀態跟蹤機制處理   只有兩個鏈:OUTPUT、PREROUTING 


iptables規則相關:


查看規則 iptables -t nat -nvL

清除規則 iptables -t nat -F


增長/刪除規則

iptables -A/-D INPUT -s 10.72.11.12 -p tcp --sport 1234 -d 10.72.137.159 --dport 80 -j DROP 

把10.72.11.12主機從1234端口出去到達目標10.72.137.159主80端口的包丟掉

-A/D:表示增長/刪除一條規則

-I:插入一條規則,跟A的效果相似

-p:地址協議,能夠是tcp、udp或icmp

--dport:指定目標端口,跟-p一塊兒使用

--sport:指定源端口,跟-p一塊兒使用

-s:指定源IP,也能夠是一個IP段

-d:指定目的IP或者IP段

-j:後面跟動做,有三個選項,容許ACCEPT,丟包DROR,拒絕REJECT

-i:表示指定網卡,不經常使用,偶爾用到


插入規則,使用了-I以後,插入的規則會在以前規則的前面

iptables -I INPUT -s 1.1.1.1 -j DROP/ACCEPT/REJECT 

越在前面越先匹配,應該說是在前面有優先匹配的權限吧


把來自192.168.21.99主機的數據包丟掉

[root@linux2 ~]#iptables -I INPUT -s 192.168.21.99 -j DROP


刪除192.168.21.99這條主機的規則

[root@linux2 ~]# iptables -D INPUT -s 192.168.21.99 -j DROP


通信協議使用的是tcp協議,而且是經過80端口來訪問本機,且IP地址是192.168.21.99主機的包丟掉

[root@linux2 ~]# iptables -I INPUT -s 192.168.21.99 -p tcp --dport 80 -j DROP


把本機訪問192.168.21.99主機22端口的包丟掉

[root@linux2 ~]# iptables -I INPUT -p tcp --dport 22 -d 192.168.21.99 -j DROP


容許192.168.21.0/24這個網段在eth0網卡上通訊

[root@linux2 ~]# iptables -A INPUT -s 192.168.21.0/24 -i eth0 -j ACCEPT


查看規則帶有id號

iptables -nvL --line-numbers 

[root@linux2 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1964  145K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5     3177  311K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
6        0     0 ACCEPT     tcp  --  *      *       1.1.1.1              0.0.0.0/0           tcp dpt:80

 

根據規則的id號刪除對應規則 

iptables -D INPUT 6 

[root@linux2 ~]# iptables -D INPUT 6
[root@linux2 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1981  146K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5     3177  311K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0

iptables -P INPUT [ACCEPT]  用來設定默認規則,默認是ACCEPT ,一旦設定爲DROP後,只能使用 iptables -P INPUT ACCEPT 才能恢復成原始狀態,而不能使用-F參數 


開放全部192.168.21.254主機的協議,就是說沒有任何端口的過濾和限制

[root@linux2 ~]# iptables -A INPUT -s 192.168.21.254 -j ACCEPT

iptables-save > 1.ipt

把規則重定向到1.ipt文件中,也能夠說成是備份


iptables-restore < 1.ipt

恢復以前備份的規則,使用反向重定向到iptables


禁止別人ping通你,可是你能夠ping通別人

[root@linux2 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP


實例:

針對filter表,預設策略INPUT鏈DROP,其餘兩個鏈ACCEPT,而後針對192.168.0.0/24開通22端口,對全部網段開放80端口,對全部網段開放21端口。 腳本以下:

#! /bin/bash 
ipt="/sbin/iptables" 
$ipt -F; $ipt -P INPUT DROP; 
$ipt -P OUTPUT ACCEPT; $ipt -P FORWARD ACCEPT; 
$ipt -A INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT 
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

icmp的包有常見的應用,本機ping通外網,外網ping不通本機 iptables -I INPUT -p icmp --icmp-type 8 -j DROP 


nat表應用:

路由器就是使用iptables的nat原理實現

假設您的機器上有兩塊網卡eth0和eth1,其中eth0的IP爲192.168.10.11,eth1的IP爲172.16.10.11 。eth0鏈接了intnet 但eth1沒有鏈接,如今有另外一臺機器(172.16.10.12)和eth1是互通的,那麼如何設置也可以讓鏈接eth1的這臺機器可以鏈接intnet? 

echo "1" > /proc/sys/net/ipv4/ip_forward 

iptables -t nat -A POSTROUTING -s 172.16.10.0/24 -o eth0 -j MASQUERADE 


規則備份與恢復:

service iptables save 這樣會保存到/etc/sysconfig/iptables 

iptables-save > myipt.rule 能夠把防火牆規則保存到指定文件中

iptables-restore < myipt.rule  這樣能夠恢復指定的規則

相關文章
相關標籤/搜索