Linux异常进程排查 命令记录

写下一些有用的命令,并在需要时检查它们。这篇文章没有太多营养,所以把它当词典用吧。

[En]

Write down some useful commands and check them later when you need them. This article doesn’t have much nutrition, so use it as a dictionary.

目标就是通过技术手段去分析出Linux系统内异常的进程,但是想要达成这个目标,我感觉需要分三个阶段:

  • 初级阶段
  • 主要通过系统命令定位
  • 中级阶段
  • 学习攻击者如何消除入侵痕迹、隐藏进程、做持久Shell
  • 高级阶段
  • 主要是攻防双方的对抗了

查看登录用户数、IP、运行命令

top

最常用的方法

查看所有进程 ps

两个命令功能一样的,写法不一样而已;

ps aux
BSD操作系统格式

参数:

  • a
  • u
  • x
ps -le
linux标准命令格式

参数:

如果top和ps都找不到异常进程

可以试试unhide命令,这个需要安装一下,有时候网络问题会安装失败,需要更换源或者代理试试。

Unhide 通过下述三项技术来发现隐藏的进程。

$ sudo unhide -h
Unhide 20130526
Copyright © 2013 Yago Jesus & Patrick Gouin
License GPLv3+ : GNU GPL version 3 or later
http://www.unhide-forensics.info

NOTE : This version of unhide is for systems using Linux >= 2.6

Usage: unhide [options] test_list

Option :
   -V          Show version and exit
   -v          verbose
   -h          display this help
   -m          more checks (available only with procfs, checkopendir & checkchdir commands)
   -r          use alternate sysinfo test in meta-test
   -f          log result into unhide-linux.log file
   -o          same as '-f'
   -d          do a double check in brute test
Test_list :
   Test_list is one or more of the following
   Standard tests :
      brute
      proc
      procall
      procfs
      quick
      reverse
      sys
   Elementary tests :
      checkbrute
      checkchdir
      checkgetaffinity
      checkgetparam
      checkgetpgid
      checkgetprio
      checkRRgetinterval
      checkgetsched
      checkgetsid
      checkkill
      checknoprocps
      checkopendir
      checkproc
      checkquick
      checkreaddir
      checkreverse
      checksysinfo
      checksysinfo2
      checksysinfo3

查看进程树 pstree

查看进程的父子关系

pstree

参数:

查看是否有别人连接
查看sshd远程连接进程是否有别人连接

pstree | grep sshd

系统进程管理器 top

linux的系统进程管理器

top

默认刷新是每3秒刷新一次,
木马在发送大量数据包时肯定会消耗资源。通过整理,我们可以发现,在顶层流程中有很多嫌疑人。

[En]

Trojans will definitely consume resources when sending a large number of packets. Through sorting, we can find that there are a lot of suspects in the top process.

和ps 的区别是,top能做命令交互。

-d
-b
-n

?/h
P
M
N
q

查看后台进程 jobs

放入后台的进程都是长时间运行的进程,且不需要在和用户交互。

jobs

/+ 刚进入后台的进程
/- 倒数第二个进入后台的进程
Stopped 表示该进程暂停了
Running 表示该进程在运行
Done 表示该进程执行结束

把进程放入后台的方法

方法1:

  • python a.py &
  • 运行起来之后按下 ctrl+z,放在后台暂停。
    这种关闭了当前的shell窗口,后台的任务也会结束。

方法2:crontab -e
写定时任务

方法3:nohup 命令

netstat
lsof

检查系统计划任务中是否有可疑进程。计划任务的命令都需要确认它们是否已安装并具有权限(访问控制文件)。

[En]

Check the system scheduled tasks for suspicious processes. Commands for scheduled tasks all need to confirm whether they are installed and have permissions (access control files).

一次性定时任务

atq
查看一次性计划任务的当前列表

[En]

View the current list of one-time scheduled tasks

at -c 【任务编号】
at任务的具体执行内容

atrm [工作号]
删除指定的at任务

如果atq是由root执行,则将列出系统中所有没有执行的Unix at命令。然后可以使用atrm命令根据atq输出Job号来删除at作业。

crontab

循环预定任务、游戏服务器,需要每月定期重启,使服务器恢复良好状态。

[En]

Cycle scheduled tasks, game server, need to restart regularly every month to get the server back to good condition.

身份绑定

crontab是需要绑定当前用户的身份。
root能编辑root的,hack能编辑hack的,不能串 低权限用户也不能执行高权限命令。

手工编辑定时任务

crontab -e
编辑定时任务

root权限
cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {}

系统配置文件编辑定时任务

root可以编辑/etc/crontab 这个配置文件,

或者使用系统定义的时间、日、周和月配置文件来添加计划任务

[En]

Or use the time, day, week and month profiles defined by the system to add scheduled tasks

/etc/cron.hourly
/etc/cron.daily
/etc/corn.weekly
/etc/cron.monthly

/var/spool/anacron 是新版的 centOS6.0后启用,添加的功能是,如果一些故障(关机了),anacron的主机在恢复正常后,会立即恢复工作
这4个文件的定时任务就能在规定时间点重新执行起来 补回机制不知道:
/etc/cron.hourly
/etc/cron.daily
/etc/corn.weekly
/etc/cron.monthly

crontab -l
查看当前用户的定时任务

crontab -r
删除当前用户的全部定时任务,想删除单个用 -e编辑,一行一行删。

cd /etc/init.d
这个目录存放的是一些脚本,一般是linux以rpm包安装时设定的一些服务的启动脚本

vim /etc/rc.local
这个是个配置文件,作为一个shell脚本来编辑,将自己的启动命令编辑到脚本中,无论如何都会被加载
/etc/init.d/samba start

因为Linux可以按照不同的场景来选择是否加载,甚至是加载不同的启动脚本,这时就要用rc(0-6)按场景来分:

  • rc0.d
  • rc1.d
  • rc2.d
  • rc3.d
  • rc4.d
  • rc5.d
  • rc6.d

以上也是紧急情况下需要检查的启动项目。

[En]

The above are also the startup items that need to be checked in case of emergency.

另外一个添加启动项的地方在/etc/profile里面,还有 /etc/profile.d/目录下以sh结尾的文件

Original: https://www.cnblogs.com/mysticbinary/p/12631895.html
Author: Mysticbinary
Title: Linux异常进程排查 命令记录

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/522819/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球