您的位置:首頁>正文

10分鐘教你解決伺服器被黑上不了網的問題

linux工作人員就是保證伺服器的安全, 如果LinuxLinux伺服器被黑了上不了網怎麼辦呢?應該如何解決呢??

首先, 大家需要瞭解解決問題的思路,

因為思路決定出路。

那麼下面, 我們來設計實戰情景。

實戰情景:

伺服器上能ping通閘道, 但是上不網, DNS配置正常

1、熟悉Linux網路相關命令

注:帶著大家用10分鐘左右, 快速過一下Linux網路基本命令

瞭解網路情況基本思路。 思路決定出路

0、查看網線, 是否正常

1、IP ping

2、閘道 IP

3、DNS

4、iptables規則

5、selinux

6、閘道MAC。 目的: 防止別人冒沖閘道。 ARP欺騙

7、IP位址衝突

現在在正常環境下進行測試:

[root@xuegod63 ~]#ping g.cn

PING g.cn (203.208.49.176) 56(84) bytes of data.

64 bytes from 203.208.49.176: icmp_seq=1 ttl=43 time=40.7 ms

64 bytes from 203.208.49.176: icmp_seq=2 ttl=43 time=50.2 ms

ping 參數:

-c #指定ping 的次數

-i # 指定ping 的頻率

例:

[root@xuegod63 ~]# ping -c 1192.168.1.1

[root@xuegod63 ~]# ping -i 0.001 192.168.1.1

1、查詢IP

[root@xuegod63 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E

inet addr:192.168.1.63

配置IP位址方法1:

[root@xuegod63 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

IPADDR=192.168.1.63#IP地址

NETMASK=255.255.255.0#子網路遮罩

GATEWAY=192.168.1.1#閘道

DNS1=202.106.46.151#DNS伺服器位址

ONBOOT=yes#開機啟動網卡

2、DNS

[root@xuegod63 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

或:

[root@xuegod63 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DNS1=202.106.46.151

例:查找DNS配置方法

[root@xuegod63 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

改:DNS1=8.8.8.8

[root@xuegod63 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

nameserver 202.106.46.151

search localhost

接下來:

[root@xuegod63 ~]# service network restart

Shutting down interface eth0: [ OK ]

Shutting down loopback interface: [ OK ]

Bringing up loopback interface: [ OK ]

Bringing up interface eth0: [ OK ]

[root@xuegod63 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

nameserver 8.8.8.8

search localhost

原因:

[root@xuegod63 ~]# tail -f /var/log/messages

May 13 09:47:16 localhost NET[3653]:/etc/sysconfig/network-scripts/ifup-post: updated /etc/resolv.conf

Ifup-post會去找DNS1, cat這個檔

3、閘道

查看閘道:

或:

[root@xuegod63 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=192.168.1.1

169.254.0.0/16 是公網:1 私網:2

擴展:

A類私有地址: 10.0.0.0/8

B 類私有網段: 172. 16.0.0 - 172.31.255.255 /16

C類私有網段:192.168.0.0 /24

169.254.0.0/16 :在windows下DHCP獲取不到IP的時候, 會分配169.254.的IP。 這個微軟買斷的一個網段。

4、iptables 規則

拒絕192.168.1.64訪問xuegod63上的80埠

[root@xuegod63 ~]# iptables -A INPUT -s 192.168.1.64 -p tcp --dport 80 -j DROP [root@xuegod63 ~]# iptables -L -n

Chain INPUT (policy ACCEPT)

target prot opt source destination

Chain FORWARD (policy ACCEPT)

target prot opt source destination

Chain OUTPUT (policy ACCEPT)

target prot opt source destination

# -n 不解析IP和埠號

不加-n 顯示:

[root@xuegod63 ~]# iptables -L

Chain INPUT (policy ACCEPT)

target prot opt source destination

DROP tcp --xuegod64.cnanywhere tcp dpt:http

如何解析IP地址為功能變數名稱?

[root@xuegod63 ~]#cat /etc/hosts#IP地址解析, 通過hosts檔或DNS

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.63 xuegod63.cn

192.168.1.64 xuegod64.cn

如何解析埠為協議名?

[root@xuegod63 ~]# vim /etc/services

作用:

1、查看埠號

2、iptables 解析埠號參考的文件

公開課: 主要是實戰 聽老師風格, 擴展你的經驗 從頭學來vip

6、閘道MAC 。 目的: 防止別人冒充閘道。 ARP欺騙

網路執法官/ p2p終結者 用過:1 沒有用過:2

在路由器:

查看閘道有沒有被冒充:

在windows下:

Linux如何知道別人冒充閘道?

[root@xuegod63 ~]# arping192.168.1.1# 測試192.168.1.1 此IP地址是否有衝突

ARPING 192.168.1.1 from 192.168.1.63 eth0

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 2.124ms

Unicast reply from 192.168.1.1 [14:75:90:15:23:12] 5.333ms

7、IP位址衝突

公司局域網中, IP位址衝突後, 換一個IP,再ping, 但是在IDC機房可以隨意換IP位址???

現在被黑了:

實戰情景:

伺服器上能ping通閘道, 但是上不網, DNS配置正常

網路排錯: 重點思路。 沒有思路, 就沒有出路。

實戰拓撲圖:

現在遠端連接不上伺服器A,如何遠端排查? 還有一台伺服器

解決思路:

[root@xuegod63 ~]# ping g.cn

ping: unknown host g.cn

先ping 自己的IP地址:

[root@xuegod63 ~]# ping 192.168.1.63

PING 192.168.1.63 (192.168.1.63) 56(84) bytes of data.

64 bytes from 192.168.1.63: icmp_seq=1 ttl=1 time=0.114 ms

64 bytes from 192.168.1.63: icmp_seq=2 ttl=1 time=0.072 ms

ping閘道:

[root@xuegod63 ~]# ping 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.38 ms

64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.65 ms

關閉iptables:

[root@xuegod63 ~]# iptables -L -n

[root@xuegod63 ~]# /etc/init.d/iptables stop

查看DNS:

[root@xuegod63 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

nameserver 202.106.46.151

search localhost

查看冒沖閘道:

[root@xuegod63 ~]# arping 192.168.1.1#有沒有冒沖閘道

ARPING 192.168.1.1 from 192.168.1.63 eth0

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.878ms

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.365ms

查看閘道配置是否正確: 正確

[root@xuegod63 ~]# arping 192.168.1.1

ARPING 192.168.1.1 from 192.168.1.63 eth0

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.878ms

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.365ms

查看防火牆:

[root@xuegod63 ~]# iptables -L -n

Chain INPUT (policy ACCEPT)

target prot opt source destination

Chain FORWARD (policy ACCEPT)

target prot opt source destination

Chain OUTPUT (policy ACCEPT)

target prot opt source destination

[root@xuegod63 ~]#

思路: ping 不通功能變數名稱,ping通閘道。 需要ping公網IP位址

[root@xuegod63 ~]# ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

From 192.168.1.1 icmp_seq=1 Time to live exceeded

From 192.168.1.1 icmp_seq=2 Time to live exceeded

From 192.168.1.1 icmp_seq=3 Time to live exceeded

TTL:資料包的生存週期。

預設linux作業系統值:64.每經過一個路器節點,TTL值減1。TTL值為0時,說明目標位址不可達並返回:Time to live exceeded

作用: 防止資料包,無限制在公網中轉發。

終極大招: 抓包

tcpdump

參數:

-i指定網卡介面

-n: -n 不解析IP地址為功能變數名稱

-v :顯示包中詳細資訊

-t : 指定協定類型

網路不通,抓什麼協定?

ping 使用的是ICMP協定

抓包:

[root@xuegod63 ~]# tcpdump -i eth0 -nv -t icmp

然後另一個終端上執行:[root@xuegod63 ~]#ping -c 1 192.168.1.1

查看抓包的結果:

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>192.168.1.1:ICMP echo request, id 52761, seq 1, length 64

IP (tos 0x0, ttl 64, id 7167, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.1>192.168.1.63:ICMP echo reply, id 52761, seq 1, length 64

注: ping 包特點,一去一回。

[root@xuegod63 ~]# ping-c 1192.168.1.1#ping 一次

如圖:

測試:

[root@xuegod63 ~]# ping -c 1 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

From 192.168.1.1 icmp_seq=1 Time to live exceeded

[root@xuegod63 ~]#tcpdump -i eth0 -nv -t icmp

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>8.8.8.8: ICMPecho request, id 37658, seq 1, length 64

IP (tos 0x0, ttl 64, id 7168, offset 0, flags [DF], proto ICMP (1), length 112)

192.168.1.1>192.168.1.63: ICMPtime exceededin-transit, length 92

IP (tos 0x0, ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

修改:TTL值:

[root@xuegod63 ~]# echo 64 > /proc/sys/net/ipv4/ip_default_ttl

再次抓包:

[root@xuegod63 ~]# echo 64 > /proc/sys/net/ipv4/ip_default_ttl

[root@xuegod63 ~]#tcpdump -i eth0 -nv -t icmp

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>8.8.8.8: ICMPecho request, id 62490, seq 1, length 64

IP (tos 0x90, ttl 30, id 0, offset 0, flags [none], proto ICMP (1), length 84)

8.8.8.8>192.168.1.63: ICMPecho reply,id 62490, seq 1, length 64

[root@xuegod63 ~]# ping g.cn

PING g.cn (203.208.48.148) 56(84) bytes of data.

64 bytes from 203.208.48.148: icmp_seq=1 ttl=43 time=41.7 ms

64 bytes from 203.208.48.148: icmp_seq=2 ttl=43 time=43.5 ms

解決思路:

[root@xuegod63 ~]# ping g.cn

ping: unknown host g.cn

先ping 自己的IP地址:

[root@xuegod63 ~]# ping 192.168.1.63

PING 192.168.1.63 (192.168.1.63) 56(84) bytes of data.

64 bytes from 192.168.1.63: icmp_seq=1 ttl=1 time=0.114 ms

64 bytes from 192.168.1.63: icmp_seq=2 ttl=1 time=0.072 ms

ping閘道:

[root@xuegod63 ~]# ping 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.38 ms

64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.65 ms

關閉iptables:

[root@xuegod63 ~]# iptables -L -n

[root@xuegod63 ~]# /etc/init.d/iptables stop

查看DNS:

[root@xuegod63 ~]# cat /etc/resolv.conf

# Generated by NetworkManager

nameserver 202.106.46.151

search localhost

查看冒沖閘道:

[root@xuegod63 ~]# arping 192.168.1.1#有沒有冒沖閘道

ARPING 192.168.1.1 from 192.168.1.63 eth0

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.878ms

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.365ms

查看閘道配置是否正確: 正確

[root@xuegod63 ~]# arping 192.168.1.1

ARPING 192.168.1.1 from 192.168.1.63 eth0

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.878ms

Unicast reply from 192.168.1.1 [14:75:90:15:32:48] 4.365ms

查看防火牆:

[root@xuegod63 ~]# iptables -L -n

Chain INPUT (policy ACCEPT)

target prot opt source destination

Chain FORWARD (policy ACCEPT)

target prot opt source destination

Chain OUTPUT (policy ACCEPT)

target prot opt source destination

[root@xuegod63 ~]#

思路: ping 不通功能變數名稱,ping通閘道。 需要ping公網IP位址

[root@xuegod63 ~]# ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

From 192.168.1.1 icmp_seq=1 Time to live exceeded

From 192.168.1.1 icmp_seq=2 Time to live exceeded

From 192.168.1.1 icmp_seq=3 Time to live exceeded

TTL:資料包的生存週期。

預設linux作業系統值:64.每經過一個路器節點,TTL值減1。TTL值為0時,說明目標位址不可達並返回:Time to live exceeded

作用: 防止資料包,無限制在公網中轉發。

終極大招: 抓包

tcpdump

參數:

-i指定網卡介面

-n: -n 不解析IP地址為功能變數名稱

-v :顯示包中詳細資訊

-t : 指定協定類型

網路不通,抓什麼協定?

ping 使用的是ICMP協定

抓包:

[root@xuegod63 ~]# tcpdump -i eth0 -nv -t icmp

然後另一個終端上執行:[root@xuegod63 ~]#ping -c 1 192.168.1.1

查看抓包的結果:

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>192.168.1.1:ICMP echo request, id 52761, seq 1, length 64

IP (tos 0x0, ttl 64, id 7167, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.1>192.168.1.63:ICMP echo reply, id 52761, seq 1, length 64

注: ping 包特點,一去一回。

[root@xuegod63 ~]# ping-c 1192.168.1.1#ping 一次

如圖:

測試:

[root@xuegod63 ~]# ping -c 1 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

From 192.168.1.1 icmp_seq=1 Time to live exceeded

[root@xuegod63 ~]#tcpdump -i eth0 -nv -t icmp

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>8.8.8.8: ICMPecho request, id 37658, seq 1, length 64

IP (tos 0x0, ttl 64, id 7168, offset 0, flags [DF], proto ICMP (1), length 112)

192.168.1.1>192.168.1.63: ICMPtime exceededin-transit, length 92

IP (tos 0x0, ttl 1, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

修改:TTL值:

[root@xuegod63 ~]# echo 64 > /proc/sys/net/ipv4/ip_default_ttl

再次抓包:

[root@xuegod63 ~]# echo 64 > /proc/sys/net/ipv4/ip_default_ttl

[root@xuegod63 ~]#tcpdump -i eth0 -nv -t icmp

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

IP (tos 0x0,ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)

192.168.1.63>8.8.8.8: ICMPecho request, id 62490, seq 1, length 64

IP (tos 0x90, ttl 30, id 0, offset 0, flags [none], proto ICMP (1), length 84)

8.8.8.8>192.168.1.63: ICMPecho reply,id 62490, seq 1, length 64

[root@xuegod63 ~]# ping g.cn

PING g.cn (203.208.48.148) 56(84) bytes of data.

64 bytes from 203.208.48.148: icmp_seq=1 ttl=43 time=41.7 ms

64 bytes from 203.208.48.148: icmp_seq=2 ttl=43 time=43.5 ms

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示