processes:list_all_non-kernel_processes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
processes:list_all_non-kernel_processes [2016/10/17 13:57] – peter | processes:list_all_non-kernel_processes [2019/12/01 22:37] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Processes - List all non-kernel processes ====== | ||
- | Usually kernel processes are safe and clean. | ||
- | |||
- | <code bash> | ||
- | ps --ppid 2 -p 2 -p 1 \ | ||
- | | ||
- | </ | ||
- | |||
- | Returns: | ||
- | |||
- | < | ||
- | UID | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | ... ... | ||
- | ... ... | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | ... ... | ||
- | ... ... | ||
- | </ | ||
- | |||
- | **NOTE**: | ||
- | |||
- | * **rss** (resident set size): real RAM usage. | ||
- | * **-deselect**: | ||
- | |||
- | | ||
- | ===== Rule out trusted procsses ===== | ||
- | |||
- | We may have many processes running, which are expected and trusted. e.g apache2, tomcat7, mysqld, etc. To avoid distraction, | ||
- | |||
- | ===== Sort processes by memory and cpu ===== | ||
- | |||
- | We’re more concerned about suspicious processes using noticeable resource. | ||
- | |||
- | <code bash> | ||
- | # Sort by memory first, then cpu | ||
- | ps --ppid 2 -p 2 -p 1 --deselect \ | ||
- | -o uid, | ||
- | --sort -rss,-cpu | ||
- | </ | ||
- | |||
- | |||
- | ===== Automate Detection Process and Get Alerts ===== | ||
- | |||
- | We hide all the complexities and white list configuration in a python script (detect_suspicious_process.py). | ||
- | |||
- | If the number is not 0 or it changes, send alerts. | ||
- | |||
- | <code bash> | ||
- | wget -O / | ||
- | https:// | ||
- | DennyZhang/ | ||
- | detect_suspicious_process/ | ||
- | detect_suspicious_process.py | ||
- | |||
- | # Detect suspicious process | ||
- | python / | ||
- | |||
- | # Detect by customized whitelist | ||
- | python / | ||
- | | ||
- | </ | ||
- | |||
- | |||
- | ===== detect_suspicious_process.py ===== | ||
- | |||
- | <code python> | ||
- | # -*- coding: utf-8 -*- | ||
- | # | ||
- | ## | ||
- | ## @copyright 2015 DennyZhang.com | ||
- | ## File : detect_suspicious_process.py | ||
- | ## Author : DennyZhang.com < | ||
- | ## Description : http:// | ||
- | ## python ./ | ||
- | ## python ./ | ||
- | ## -- | ||
- | ## Created : < | ||
- | ## Updated: Time-stamp: < | ||
- | ## | ||
- | import argparse | ||
- | import subprocess | ||
- | import os, sys | ||
- | |||
- | ################################################################################ | ||
- | # TODO: move to common library | ||
- | def string_in_regex_list(string, | ||
- | import re | ||
- | for regex in regex_list: | ||
- | regex = regex.strip() | ||
- | if regex == "": | ||
- | continue | ||
- | if re.search(regex, | ||
- | # print " | ||
- | return True | ||
- | return False | ||
- | |||
- | ################################################################################ | ||
- | DEFAULT_WHITE_LIST = ''' | ||
- | /sbin/getty -.* | ||
- | dbus-daemon .* | ||
- | acpid -c / | ||
- | atd$ | ||
- | cron$ | ||
- | / | ||
- | / | ||
- | | ||
- | / | ||
- | | ||
- | / | ||
- | / | ||
- | ''' | ||
- | |||
- | COMMAND_GET_NONKERNEL = ''' | ||
- | sudo ps --ppid 2 -p 2 -p 1 --deselect \ | ||
- | -o uid, | ||
- | --sort -rss,-cpu | ||
- | ''' | ||
- | |||
- | def get_nonkernel_process(): | ||
- | process_list = subprocess.check_output(COMMAND_GET_NONKERNEL, | ||
- | return process_list | ||
- | |||
- | def load_whitelist(fname): | ||
- | white_list = "" | ||
- | if fname is None: | ||
- | print "No white list file is given. Use default value." | ||
- | white_list = DEFAULT_WHITE_LIST | ||
- | else: | ||
- | print "load white list from %s" % (fname) | ||
- | with open(fname) as f: | ||
- | white_list = f.readlines() | ||
- | return white_list | ||
- | |||
- | def list_process(process_list, | ||
- | import re | ||
- | l = [] | ||
- | for line in process_list.split(" | ||
- | line = line.strip() | ||
- | if line == "": | ||
- | continue | ||
- | if not string_in_regex_list(line, | ||
- | l.append(line) | ||
- | return l | ||
- | |||
- | ################################################################################ | ||
- | if __name__==' | ||
- | parser = argparse.ArgumentParser() | ||
- | parser.add_argument(' | ||
- | help=" | ||
- | args = parser.parse_args() | ||
- | white_list = load_whitelist(args.whitelist_file) | ||
- | nonkernel_process_list = get_nonkernel_process() | ||
- | process_list = list_process(nonkernel_process_list, | ||
- | |||
- | # Remove header | ||
- | print " | ||
- | print " | ||
- | ## File : detect_suspicious_process.py ends | ||
- | </ |
processes/list_all_non-kernel_processes.1476712654.txt.gz · Last modified: 2020/07/15 09:30 (external edit)