溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux服務(wù)器被黑以后如何處理

發(fā)布時間:2021-06-11 13:01:58 來源:億速云 閱讀:218 作者:小新 欄目:服務(wù)器

小編給大家分享一下Linux服務(wù)器被黑以后如何處理,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

隨著開源產(chǎn)品的越來越盛行,作為一個Linux運維工程師,能夠清晰地鑒別異常機器是否已經(jīng)被入侵了顯得至關(guān)重要,個人結(jié)合自己的工作經(jīng)歷,整理了幾種常見的機器被黑情況供參考

背景信息:以下情況是在CentOS 6.9的系統(tǒng)中查看的,其它Linux發(fā)行版類似

1.入侵者可能會刪除機器的日志信息,可以查看日志信息是否還存在或者是否被清空,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="98eaf7f7ecd8f0f4f5fbfdf6aea1f6ab">[email protected]</a> ~]# ll -h /var/log/*
-rw-------. 1 root root 2.6K Jul 7 18:31 /var/log/anaconda.ifcfg.log
-rw-------. 1 root root 23K Jul 7 18:31 /var/log/anaconda.log
-rw-------. 1 root root 26K Jul 7 18:31 /var/log/anaconda.program.log
-rw-------. 1 root root 63K Jul 7 18:31 /var/log/anaconda.storage.log
[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="493b26263d092125242a2c277f70277a">[email protected]</a> ~]# du -sh /var/log/*
8.0K /var/log/anaconda
4.0K /var/log/anaconda.ifcfg.log
24K /var/log/anaconda.log
28K /var/log/anaconda.program.log
64K /var/log/anaconda.storage.log

2.入侵者可能創(chuàng)建一個新的存放用戶名及密碼文件,可以查看/etc/passwd及/etc/shadow文件,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="80f2efeff4c0e8ecede3e5eeb6b9eeb3">[email protected]</a> ~]# ll /etc/pass*
-rw-r--r--. 1 root root 1373 Sep 15 11:36 /etc/passwd
-rw-r--r--. 1 root root 1373 Sep 15 11:36 /etc/passwd-
[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="c8baa7a7bc88a0a4a5abada6fef1a6fb">[email protected]</a> ~]# ll /etc/sha*
----------. 1 root root 816 Sep 15 11:36 /etc/shadow
----------. 1 root root 718 Sep 15 11:36 /etc/shadow-

3.入侵者可能修改用戶名及密碼文件,可以查看/etc/passwd及/etc/shadow文件內(nèi)容進(jìn)行鑒別,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="9ceef3f3e8dcf4f0f1fff9f2aaa5f2af">[email protected]</a> ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="20524f4f5460484c4d43454e16194e13">[email protected]</a> ~]# more /etc/shadow
root:*LOCK*:14600::::::
bin:*:17246:0:99999:7:::
daemon:*:17246:0:99999:7:::

4.查看機器最近成功登陸的事件和最后一次不成功的登陸事件,對應(yīng)日志“/var/log/lastlog”,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="4e3c21213a0e2622232d2b207877207d">[email protected]</a> ~]# lastlog
Username     Port   From       Latest
root                    **Never logged in**
bin                    **Never logged in**
daemon                   **Never logged in**

5.查看機器當(dāng)前登錄的全部用戶,對應(yīng)日志文件“/var/run/utmp”,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="c3b1acacb783abafaea0a6adf5faadf0">[email protected]</a> ~]# who
stone  pts/0    2017-09-20 16:17 (X.X.X.X)
test01  pts/2    2017-09-20 16:47 (X.X.X.X)

6.查看機器創(chuàng)建以來登陸過的用戶,對應(yīng)日志文件“/var/log/wtmp”,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="ec9e838398ac8480818f8982dad582df">[email protected]</a> ~]# last
test01  pts/1    X.X.X.X  Wed Sep 20 16:50  still logged in 
test01  pts/2    X.X.X.X  Wed Sep 20 16:47 - 16:49 (00:02)  
stone  pts/1    X.X.X.X  Wed Sep 20 16:46 - 16:47 (00:01)  
stone  pts/0    X.X.X.X  Wed Sep 20 16:17  still logged in

7.查看機器所有用戶的連接時間(小時),對應(yīng)日志文件“/var/log/wtmp”,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="03716c6c77436b6f6e60666d353a6d30">[email protected]</a> ~]# ac -dp
     stone                11.98
Sep 15   total    11.98
     stone                67.06
Sep 18   total    67.06
     stone                1.27
     test01                0.24
Today    total    1.50

8.如果發(fā)現(xiàn)機器產(chǎn)生了異常流量,可以使用命令“tcpdump”抓取網(wǎng)絡(luò)包查看流量情況或者使用工具”iperf”查看流量情況

9.可以查看/var/log/secure日志文件,嘗試發(fā)現(xiàn)入侵者的信息,相關(guān)命令示例:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="82f0ededf6c2eaeeefe1e7ecb4bbecb1">[email protected]</a> ~]# cat /var/log/secure | grep -i "accepted password"
Sep 20 12:47:20 hlmcen69n3 sshd[37193]: Accepted password for stone from X.X.X.X port 15898 ssh3
Sep 20 16:17:47 hlmcen69n3 sshd[38206]: Accepted password for stone from X.X.X.X port 9140 ssh3
Sep 20 16:46:00 hlmcen69n3 sshd[38511]: Accepted password for stone from X.X.X.X port 2540 ssh3
Sep 20 16:47:16 hlmcen69n3 sshd[38605]: Accepted password for test01 from X.X.X.X port 10790 ssh3
Sep 20 16:50:04 hlmcen69n3 sshd[38652]: Accepted password for test01 from X.X.X.X port 28956 ssh3

10.查詢異常進(jìn)程所對應(yīng)的執(zhí)行腳本文件

a.top命令查看異常進(jìn)程對應(yīng)的PID

b.在虛擬文件系統(tǒng)目錄查找該進(jìn)程的可執(zhí)行文件

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="285a47475c684044454b4d461e11461b">[email protected]</a> ~]# ll /proc/1850/ | grep -i exe
lrwxrwxrwx. 1 root root 0 Sep 15 12:31 exe -> /usr/bin/python
[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="1d6f7272695d7571707e78732b24732e">[email protected]</a> ~]# ll /usr/bin/python
-rwxr-xr-x. 2 root root 9032 Aug 18 2016 /usr/bin/python

11.如果確認(rèn)機器已經(jīng)被入侵,重要文件已經(jīng)被刪除,可以嘗試找回被刪除的文件

Note:

1>當(dāng)進(jìn)程打開了某個文件時,只要該進(jìn)程保持打開該文件,即使將其刪除,它依然存在于磁盤中。這意味著,進(jìn)程并不知道文件已經(jīng)被刪除,它仍然可以向打開該文件時提供給它的文件描述符進(jìn)行讀取和寫入。除了該進(jìn)程之外,這個文件是不可見的,因為已經(jīng)刪除了其相應(yīng)的目錄索引節(jié)點。

2>在/proc 目錄下,其中包含了反映內(nèi)核和進(jìn)程樹的各種文件。/proc目錄掛載的是在內(nèi)存中所映射的一塊區(qū)域,所以這些文件和目錄并不存在于磁盤中,因此當(dāng)我們對這些文件進(jìn)行讀取和寫入時,實際上是在從內(nèi)存中獲取相關(guān)信息。大多數(shù)與 lsof 相關(guān)的信息都存儲于以進(jìn)程的 PID 命名的目錄中,即 /proc/1234 中包含的是 PID 為 1234 的進(jìn)程的信息。每個進(jìn)程目錄中存在著各種文件,它們可以使得應(yīng)用程序簡單地了解進(jìn)程的內(nèi)存空間、文件描述符列表、指向磁盤上的文件的符號鏈接和其他系統(tǒng)信息。lsof 程序使用該信息和其他關(guān)于內(nèi)核內(nèi)部狀態(tài)的信息來產(chǎn)生其輸出。所以lsof 可以顯示進(jìn)程的文件描述符和相關(guān)的文件名等信息。也就是我們通過訪問進(jìn)程的文件描述符可以找到該文件的相關(guān)信息。

3>當(dāng)系統(tǒng)中的某個文件被意外地刪除了,只要這個時候系統(tǒng)中還有進(jìn)程正在訪問該文件,那么我們就可以通過lsof從/proc目錄下恢復(fù)該文件的內(nèi)容。

假設(shè)入侵者將/var/log/secure文件刪除掉了,嘗試將/var/log/secure文件恢復(fù)的方法可以參考如下:

a.查看/var/log/secure文件,發(fā)現(xiàn)已經(jīng)沒有該文件

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="20524f4f5460484c4d43454e16194e13">[email protected]</a> ~]# ll /var/log/secure
ls: cannot access /var/log/secure: No such file or directory

b.使用lsof命令查看當(dāng)前是否有進(jìn)程打開/var/log/secure,

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="deacb1b1aa9eb6b2b3bdbbb0e8e7b0ed">[email protected]</a> ~]# lsof | grep /var/log/secure
rsyslogd  1264   root  4w   REG        8,1 3173904   263917 /var/log/secure (deleted)

c.從上面的信息可以看到 PID 1264(rsyslogd)打開文件的文件描述符為4。同時還可以看到/var/log/ secure已經(jīng)標(biāo)記為被刪除了。因此我們可以在/proc/1264/fd/4(fd下的每個以數(shù)字命名的文件表示進(jìn)程對應(yīng)的文件描述符)中查看相應(yīng)的信息,如下:

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="9deff2f2e9ddf5f1f0fef8f3aba4f3ae">[email protected]</a> ~]# tail /proc/1264/fd/4
Sep 20 16:47:21 hlmcen69n3 sshd[38511]: pam_unix(sshd:session): session closed for user stone
Sep 20 16:47:21 hlmcen69n3 su: pam_unix(su-l:session): session closed for user root
Sep 20 16:49:30 hlmcen69n3 sshd[38605]: pam_unix(sshd:session): session closed for user test01
Sep 20 16:50:04 hlmcen69n3 sshd[38652]: reverse mapping checking getaddrinfo for 190.78.120.106.static.bjtelecom.net [106.120.78.190] failed - POSSIBLE BREAK-IN ATTEMPT!
Sep 20 16:50:04 hlmcen69n3 sshd[38652]: Accepted password for test01 from 106.120.78.190 port 28956 ssh3
Sep 20 16:50:05 hlmcen69n3 sshd[38652]: pam_unix(sshd:session): session opened for user test01 by (uid=0)
Sep 20 17:18:51 hlmcen69n3 unix_chkpwd[38793]: password check failed for user (root)
Sep 20 17:18:51 hlmcen69n3 sshd[38789]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=51.15.81.90 user=root
Sep 20 17:18:52 hlmcen69n3 sshd[38789]: Failed password for root from 51.15.81.90 port 47014 ssh3
Sep 20 17:18:52 hlmcen69n3 sshd[38790]: Connection closed by 51.15.81.90

d.從上面的信息可以看出,查看/proc/1264/fd/4就可以得到所要恢復(fù)的數(shù)據(jù)。如果可以通過文件描述符查看相應(yīng)的數(shù)據(jù),那么就可以使用I/O重定向?qū)⑵渲囟ㄏ虻轿募校?

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="8bf9e4e4ffcbe3e7e6e8eee5bdb2e5b8">[email protected]</a> ~]# cat /proc/1264/fd/4 > /var/log/secure

e.再次查看/var/log/secure,發(fā)現(xiàn)該文件已經(jīng)存在。對于許多應(yīng)用程序,尤其是日志文件和數(shù)據(jù)庫,這種恢復(fù)刪除文件的方法非常有用。

[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="63110c0c17230b0f0e00060d555a0d50">[email protected]</a> ~]# ll /var/log/secure
-rw-r--r--. 1 root root 3173904 Sep 20 17:24 /var/log/secure
[<a href="/cdn-cgi/l/email-protection" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-cfemail="ec9e838398ac8480818f8982dad582df">[email protected]</a> ~]# head /var/log/secure
Sep 17 03:28:15 hlmcen69n3 sshd[13288]: reverse mapping checking getaddrinfo for 137-64-15-51.rev.cloud.scaleway.com [51.15.64.137] failed - POSSIBLE BREAK-IN ATTEMPT!
Sep 17 03:28:15 hlmcen69n3 unix_chkpwd[13290]: password check failed for user (root)
Sep 17 03:28:15 hlmcen69n3 sshd[13288]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=51.15.64.137 user=root
Sep 17 03:28:17 hlmcen69n3 sshd[13288]: Failed password for root from 51.15.64.137 port 59498 ssh3
Sep 17 03:28:18 hlmcen69n3 sshd[13289]: Received disconnect from 51.15.64.137: 11: Bye Bye
Sep 17 03:28:22 hlmcen69n3 sshd[13291]: reverse mapping checking getaddrinfo for 137-64-15-51.rev.cloud.scaleway.com [51.15.64.137] failed - POSSIBLE BREAK-IN ATTEMPT!
Sep 17 03:28:22 hlmcen69n3 unix_chkpwd[13293]: password check failed for user (root)
Sep 17 03:28:22 hlmcen69n3 sshd[13291]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=51.15.64.137 user=root
Sep 17 03:28:24 hlmcen69n3 sshd[13291]: Failed password for root from 51.15.64.137 port 37722 ssh3
Sep 17 03:28:25 hlmcen69n3 sshd[13292]: Received disconnect from 51.15.64.137: 11: Bye Bye

以上是“Linux服務(wù)器被黑以后如何處理”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI