天天看點

第一個shell腳本-監測惡意登入遠端伺服器

最近學習到linux系統日志和計劃任務,下班回家的地鐵上有了靈感,嘗試編寫了自己的第一個腳本,監測如果有惡意登入伺服器的話,發郵件通知管理者。暫時還沒學習到如何發郵件給管理者,目前隻是指令行的提醒和日志記錄;腳本的内容也比較簡單,都是學習過的基本知識,活學活用。

1、首先編寫一個腳本:

定義一個變量LT,變量的值為lastb指令列出的行數(即無效登入的次數,如有惡意登入的話行數會變多);

執行一個if判斷語句,如果定義的值大于15次的話,判斷為惡意登入,通知管理者。

腳本内容如下:

1

2

3

4

5

6

7

8

9

10

<code>[root@localhost ~]</code><code># cat lt.sh </code>

<code>#! /bin/bash</code>

<code>#定義變量LT,記錄無效登入的次數;</code>

<code>LT=`lastb |</code><code>wc</code> <code>-l |</code><code>cut</code> <code>-d </code><code>' '</code> <code>-f 1`</code>

<code>if</code> <code>[ $LT -gt </code><code>"15"</code> <code>]</code>

<code>        </code><code>#判斷無效登入的次數如果大于15的話,執行下面的操作;</code>

<code>        </code><code>then</code> <code>echo</code> <code>"somebody try to login please check log"</code>

<code>        </code><code>#列印有人嘗試登入系統請檢查日志</code>

<code>fi</code>

2、編寫一個計劃任務

每隔一分鐘自動執行上面的腳本

<code>[root@localhost ~]</code><code># crontab -l</code>

<code>*</code><code>/1</code> <code>* * * * </code><code>/bin/sh</code> <code>/root/lt</code><code>.sh</code>

3、檢視效果

超過15次登入在目前指令行模式會提示,有一封新郵件在/var/spool/mail/root下;

<code>[root@localhost ~]</code><code># </code>

<code>You have new mail </code><code>in</code> <code>/var/spool/mail/root</code>

檢視新郵件,會發現腳本裡面的内容,證明有人在嘗試登入主機;

<code>[root@localhost ~]</code><code># tail -2 /var/spool/mail/root </code>

<code>somebody try to login please check log</code>

執行lastb指令檢視發現很多登入失敗的記錄

11

<code>[root@localhost ~]</code><code># lastb |head</code>

<code>user1    </code><code>ssh</code><code>:notty    192.168.22.1     Tue Apr 21 22:04 - 22:04  (00:00)    </code>

<code>user1    </code><code>ssh</code><code>:notty    192.168.22.1     Tue Apr 21 22:03 - 22:03  (00:00)    </code>

<code>user1    </code><code>ssh</code><code>:notty    192.168.22.1     Tue Apr 21 21:29 - 21:29  (00:00)    </code>

<code>user1    </code><code>ssh</code><code>:notty    192.168.22.1     Tue Apr 21 21:29 - 21:29  (00:00)</code>

檢視/var/log/secure 日志也會發現有多次登入失敗的記錄

<code>Apr 21 22:03:35 localhost unix_chkpwd[1501]: password check failed </code><code>for</code> <code>user (user1) </code>

<code>Apr 21 22:03:35 localhost sshd[1499]: pam_unix(sshd:auth): authentication failure; </code><code>logname</code><code>= uid=0 euid=0 </code><code>tty</code><code>=</code><code>ssh</code> <code>ruser= rhost=192.168.22.1  user=user1</code>

<code>Apr 21 22:03:36 localhost sshd[1499]: Failed password </code><code>for</code> <code>user1 from 192.168.22.1 port 50591 ssh2</code>

<code>Apr 21 22:03:39 localhost unix_chkpwd[1502]: password check failed </code><code>for</code> <code>user (user1)</code>

<code>Apr 21 22:03:41 localhost sshd[1499]: Failed password </code><code>for</code> <code>user1 from 192.168.22.1 port 50591 ssh2</code>

<code>Apr 21 22:03:44 localhost unix_chkpwd[1503]: password check failed </code><code>for</code> <code>user (user1)</code>

<code>Apr 21 22:03:46 localhost sshd[1499]: Failed password </code><code>for</code> <code>user1 from 192.168.22.1 port 50591 ssh2</code>

<code>Apr 21 22:03:49 localhost unix_chkpwd[1504]: password check failed </code><code>for</code> <code>user (user1)</code>

<code>Apr 21 22:03:51 localhost sshd[1499]: Failed password </code><code>for</code> <code>user1 from 192.168.22.1 port 50591 ssh2</code>

<code>Apr 21 22:03:52 localhost sshd[1499]: Failed password </code><code>for</code> <code>user1 from 192.168.22.1 port 50591 ssh2</code>

<code>Apr 21 22:03:54 localhost sshd[1500]: Received disconnect from 192.168.22.1: 0:</code>

根據通路日志的來源IP,我們可以對來源設定iptables規則,禁止通路伺服器的22端口,或者封閉ip位址;

暫時隻有這麼多,小小的驕傲一下,給自己增加點自信心,相信之後的學習中會更加深入了解linux;

和大家分享一下,共勉之。

本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1636847,如需轉載請自行聯系原作者

繼續閱讀