天天看點

記錄一次阿裡雲ecs被挖礦的經曆

作者:fana

早上起來,看到釘釘上出現大量告警,CPU,記憶體都被占到99%了,馬上登入機器檢查。

使用top檢視占用資源情況,大P,按cpu占用排序,仔細看下有個.system,這個就是僞裝的挖坑程式。

記錄一次阿裡雲ecs被挖礦的經曆

ps -ef|grep system 檢視服務程序資訊,pwdx 3343檢視程式的目錄

記錄一次阿裡雲ecs被挖礦的經曆

檢視遠端連接配接的IP位址是荷蘭的

記錄一次阿裡雲ecs被挖礦的經曆

kill -9 後會重新啟動,先把名字改掉,再kill。

https://www.virustotal.com/gui/home/upload 免費的線上病毒清除網站,可以把腳本上傳檢測

記錄一次阿裡雲ecs被挖礦的經曆

後續系統檢查

參考:https://www.freebuf.com/column/162604.html
 
 #一般linux系統不會有/etc/ld.so.preload這個問題,先把這個檔案修改
 mv /etc/ld.so.preload /etc/ld.so.preloadbak
 
 cat /etc/ld.so.preloadbak 
 /usr/lib/libsystemd-shared-165.so
 
 #果斷删除這個so檔案           
記錄一次阿裡雲ecs被挖礦的經曆
#1.檢查定時任務,
 crontab -l
 
 cd /var/spool/cron
 
 ll /etc/cron* #進到每個檔案夾都檢查下
 
 cat /etc/bashrc #檢查是否有特殊指令
 
 #2.檢查環境變量
 
 #3.檢視開啟自動項
 systemctl list-unit-files
 cat /etc/rc.local 或者/etc/rc.d/rc.local            

安全加強

#1.限制使用者從tty登入
 vim /etc/pam.d/login
 #在第一行(最前面)添加如下内容:
 auth required pam_tally2.so deny=3 lock_time=300 even_deny_root root_unlock_time=10
 ##deny設定普通使用者和root使用者連續錯誤登陸的最大次數,超過最大次數,則鎖定該使用者 
 ##unlock_time設定普通使用者鎖定後,多少時間後解鎖,機關是秒; 
 ##even_deny_root也限制root使用者; 
 ##root_unlock_time設定root使用者鎖定後,多少時間後解鎖,機關是秒;
 
 #2.限制遠端(SSH)登入
 vim /etc/pam.d/sshd
 #在第一行(最前面)添加如下内容:
 auth required pam_tally2.so deny=3 unlock_time=300 even_deny_root root_unlock_time=10
 
 
 #3.配置ssh的config檔案
 使用者級(user-specific file):~/.ssh/config,通常不存在,如果需要自己建立
 系統級(system-wide file):/etc/ssh/ssh_config,内置有一些預設配置
 
 vim /etc/ssh/sshd_config
 #禁用root賬戶登入,非必要,但為了安全性,可以選擇配置
 PermitRootLogin no
 
 #是否讓sshd去檢查使用者家目錄或相關檔案的權限資料,這是為了擔心使用者将某些重要檔案的權限設錯,可能會導緻一些問題所緻。例如使用者的~/.ssh/權限設錯時,某些特殊情況下會不許使用者登入
 StrictModes no
 
 #是否允許使用者自行使用成對的密鑰系統進行登入行為,僅針對version2。至于自制的公鑰資料就放置于使用者家目錄下的.ssh/authorized_keys内
 RSAAuthentication yes
 PubkeyAuthentication yes
 AuthorizedKeysFile %h/.ssh/authorized_keys
 
 #有了證書登入了,就禁用密碼登入吧,安全要緊
 PasswordAuthentication no
 
 #在ssh配置檔案中查找MaxAuthTries修改為:
 MaxAuthTries 3 #錯誤3次即斷開連接配接
            

fail2ban

一款安全保護工具,觸發限制後會建立防火牆規則封鎖IP,諸如對ssh暴力破解、ftp/http密碼窮舉等場景提供強有力的保護。

#1.安裝
 yum install epel-release -y
 yum update -y
 yum install fail2ban-firewalld -y
 
 #2.配置
 cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local #将預設的配置拷貝一份
 vim jail.local
 [DEFAULT]
 ignoreip = 127.0.0.1/8 ::1 #永遠不會被禁止的IP位址白名單
 bantime  = 10m #禁止IP位址的時長,m代表分鐘、h表示時,不寫就是秒,-1将永久禁止
 findtime  = 10m #失敗的時間
 maxretry = 5 #失敗的次數
 backend = auto #指定用于擷取檔案修改的後端
 banaction = custom-firewalld #找到banaction,修改為這個
 
 vim /etc/fail2ban/jail.d/sshd.local
 [sshd]
 enabled = true
 
 
 vim /etc/fail2ban/action.d/custom-firewalld.conf
 [INCLUDES]
 before  =
 
 [Definition]
 actionstart =
 actionstop =
 actioncheck =
 
 actionflush = sed -i '/<source address=/d' /etc/firewalld/zones/drop.xml
 actionban = firewall-cmd --change-source=<ip> --zone=drop && firewall-cmd --change-source=<ip> --zone=drop --permanent
 actionunban = firewall-cmd --remove-source=<ip> --zone=drop && firewall-cmd --remove-source=<ip> --zone=drop --permanent || echo 0
 
 [Init]
 
 #3.啟動
 systemctl enable --now fail2ban
 systemctl restart firewalld
 
 #禁用ip例:fail2ban-client set sshd banip 1.1.1.1
 fail2ban-client set <jail name here> banip <ip here>
 #解禁ip
 fail2ban-client set <jail name here> unbanip <ip here>
 
 #4.驗證,客戶段發送暴力破解
 hydra -l root -P 00.txt -vV -o ssh.log -e ns ssh://192.168.10.12:22
 
 #5.過一會 檢視日志提示
 WARNING [sshd] 192.168.10.11 already banned
 
 #然後檢視防火牆,就會被禁止了
 firewall-cmd --list-all --zone=drop
 drop (active)
   target: DROP
   icmp-block-inversion: no
   interfaces: 
   sources: 192.168.10.11
   services: 
   ports: 
   protocols: 
   forward: yes
   masquerade: no
   forward-ports: 
   source-ports: 
   icmp-blocks: 
   rich rules: