{"id":466473,"date":"2025-07-08T15:00:55","date_gmt":"2025-07-08T15:00:55","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=466473"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=466473","title":{"rendered":"<span>\u0421\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430 \u0442\u043e\u0431\u043e\u0439: \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u0443\u0434\u0438\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0432 Linux<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/65a\/c38\/a09\/65ac38a093b03609d3b132d00be30b77.jpg\" width=\"1120\" height=\"1120\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/65a\/c38\/a09\/65ac38a093b03609d3b132d00be30b77.jpg 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/65a\/c38\/a09\/65ac38a093b03609d3b132d00be30b77.jpg 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c auditd &#8212; \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0430\u0443\u0434\u0438\u0442\u0430 \u0432 Linux, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435, \u0430 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442 \u043b\u043e\u0433\u0438 \u0432 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0435 \u043e\u0442\u0447\u0451\u0442\u044b \u0438 \u0430\u043b\u0435\u0440\u0442\u044b.<\/p>\n<p>Linux-\u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0435\u0437 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u2014 \u043a\u0430\u043a \u0434\u043e\u043c \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c\u0438 \u043e\u043a\u043d\u0430\u043c\u0438. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0440\u0435\u0448\u0438\u0442\u044c \u0430\u0443\u0434\u0438\u0442:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0441\u0430\u043d\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f (\u043a\u0442\u043e \u0438 \u043a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>sudo<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (<code>rm -rf<\/code>, \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0430\u0432, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0444\u0430\u0439\u043b\u0430\u043c)<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0446\u0438\u0434\u0435\u043d\u0442\u043e\u0432 (\u043a\u0442\u043e \u0447\u0442\u043e \u043d\u0430\u0442\u044b\u043a\u0430\u043b \u043f\u0435\u0440\u0435\u0434 \u043f\u0430\u0434\u0435\u043d\u0438\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0430)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0418\u0411 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438\u043b\u0438 \u0432\u044b \u043b\u044e\u0431\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c)<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435: <code>auditd<\/code> + Python-\u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438 \u0430\u043b\u0435\u0440\u0442\u043e\u0432.<\/p>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 auditd<\/h3>\n<p><strong>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 auditd<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043b\u0438 <code>auditd<\/code>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo apt install auditd -y  # \u0415\u0441\u043b\u0438 Debian\/Ubuntu sudo yum install audit      # \u0415\u0441\u043b\u0438 CentOS<\/code><\/pre>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0430\u0432\u0442\u043e\u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo systemctl enable --now auditd sudo systemctl status auditd  # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0442\u0430\u0442\u0443\u0441 <\/code><\/pre>\n<p><strong>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b auditctl:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u0442\u0435\u043a\u0443\u0447\u0438\u0445 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b:<\/p>\n<pre><code class=\"bash\">sudo auditctl -l<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430 (\u0438\u0441\u0447\u0435\u0437\u043d\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0440\u0435\u0441\u0442\u0430\u0440\u0442\u0430):<\/p>\n<pre><code class=\"bash\">sudo auditctl -w \/etc\/passwd -p rwa -k sensitive_files<\/code><\/pre>\n<p>\u0413\u0434\u0435:<\/p>\n<ul>\n<li>\n<p><code>-w<\/code> &#8212; \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443\/\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><code>-p<\/code> &#8212; \u043f\u0440\u0430\u0432\u0430 (r \u2014 \u0447\u0442\u0435\u043d\u0438\u0435, w \u2014 \u0437\u0430\u043f\u0438\u0441\u044c, x \u2014 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, a \u2014 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432).<\/p>\n<\/li>\n<li>\n<p><code>-k<\/code> &#8212; \u043a\u043b\u044e\u0447 (\u043c\u0435\u0442\u043a\u0430 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043b\u043e\u0433\u043e\u0432).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043f\u0440\u0430\u0432\u0438\u043b:<\/p>\n<pre><code class=\"bash\">sudo auditctl -D<\/code><\/pre>\n<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>\n<p>\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0430\u0443\u0434\u0438\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 <code>\/etc\/audit\/rules.d\/audit.rules<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0444\u0438\u0433\u0430:<\/p>\n<pre><code class=\"bash\"># \u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \/etc\/passwd \u0438 \/etc\/shadow -w \/etc\/passwd -p wa -k passwd_changes -w \/etc\/shadow -p wa -k shadow_changes  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u0430\u043d\u0434, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 sudo -w \/usr\/bin\/sudo -p x -k sudo_usage  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 -a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -k file_deletion  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 shell-\u043a\u043e\u043c\u0430\u043d\u0434 (bash, zsh, sh) -w \/bin\/bash -p x -k shell_commands -w \/bin\/zsh -p x -k shell_commands -w \/bin\/sh -p x -k shell_commands  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u0430\u0445 -w \/etc\/ -p wa -k etc_changes<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430:<\/p>\n<pre><code class=\"bash\">sudo auditctl -R \/etc\/audit\/rules.d\/audit.rules<\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437 \u043b\u043e\u0433\u043e\u0432<\/h3>\n<ol>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043b\u043e\u0433\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\n<p><code>ausearch<\/code> \u2014 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043b\u043e\u0433\u0430\u043c.<\/p>\n<pre><code class=\"bash\">sudo ausearch -k sudo_usage  # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043a\u043b\u044e\u0447\u0443 sudo ausearch -m EXECVE      # \u0422\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b<\/code><\/pre>\n<\/li>\n<li>\n<p><code>aureport<\/code> \u2014 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u0447\u0435\u0442\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo aureport --summary     # \u041e\u0431\u0449\u0430\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 sudo aureport -f            # \u041e\u0442\u0447\u0435\u0442 \u043f\u043e \u0444\u0430\u0439\u043b\u0430\u043c sudo aureport -u            # \u041e\u0442\u0447\u0435\u0442 \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043b\u043e\u0433\u0430 auditd<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 <code>\/var\/log\/audit\/audit.log<\/code>:<\/p>\n<pre><code>type=SYSCALL msg=audit(1620000000.123:456): arch=c000003e syscall=59 success=yes exit=0 a0=123 a1=456 a2=789 a3=0 items=2 ppid=1234 pid=5678 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 comm=\"sudo\" exe=\"\/usr\/bin\/sudo\" key=\"sudo_usage\"<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u043e\u0440:<\/p>\n<ul>\n<li>\n<p><code>uid=0<\/code> \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043e\u0442 root.<\/p>\n<\/li>\n<li>\n<p><code>comm=\"sudo\"<\/code> \u2014 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>sudo<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>key=\"sudo_usage\"<\/code> \u2014 \u043c\u0435\u0442\u043a\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<h3>Python-\u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438 \u0430\u043b\u0435\u0440\u0442\u043e\u0432<\/h3>\n<p><strong>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u043b\u043e\u0433\u043e\u0432<\/strong><\/p>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c <code>audit_monitor.py<\/code>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">#!\/usr\/bin\/env python3 import subprocess import re from datetime import datetime  # \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f ALERT_RULES = {     \"sudo_usage\": \"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 sudo: {details}\",     \"shadow_changes\": \"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \/etc\/shadow: {details}\",     \"file_deletion\": \"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432: {details}\", }  def parse_audit_log():     alerts = []     for key, message in ALERT_RULES.items():         # \u041f\u043e\u0438\u0441\u043a \u043b\u043e\u0433\u0430 \u043f\u043e \u043a\u043b\u044e\u0447\u0443         cmd = f\"ausearch -k {key} --raw | aureport -i -f\"         logs = subprocess.getoutput(cmd).split('\\n')                  for line in logs:             if not line:                 continue             # \u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0441\u0442\u0440\u043e\u043a\u0438 \u043b\u043e\u0433\u0430             time_match = re.search(r\"msg=audit\\((\\d+\\.\\d+)\", line)             user_match = re.search(r\"auid=(\\d+)\", line)             cmd_match = re.search(r'exe=\"([^\"]+)\"', line)                          if time_match and user_match:                 timestamp = datetime.fromtimestamp(float(time_match.group(1)))                 user = f\"UID {user_match.group(1)}\"                 details = f\"{timestamp} | {user} | {line}\"                 alerts.append(message.format(details=details))          return alerts  if __name__ == \"__main__\":     alerts = parse_audit_log()     if alerts:         print(\"\\n\".join(alerts))         # \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0432 Telegram (\u0440\u0430\u0441\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c)         # import requests         # requests.post(\"https:\/\/api.telegram.org\/botTOKEN\/sendMessage\", json={\"chat_id\": \"ID\", \"text\": \"\\n\".join(alerts)})     else:         print(\"\u041f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043e.\")<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0422\u0435\u0441\u0442\u0438\u043c \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"bash\">sudo cat \/etc\/shadow<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<pre><code class=\"bash\">chmod +x audit_monitor.py .\/audit_monitor.py<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0432\u043e\u0434\u0430:<\/strong><\/p>\n<pre><code>\u0414\u043e\u0441\u0442\u0443\u043f \u043a \/etc\/shadow: 2023-05-01 12:00:00 | UID 1000 | type=SYSCALL ... exe=\"\/usr\/bin\/cat\"<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 cron \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043a\u0430\u0436\u0434\u044b\u0435 5 \u043c\u0438\u043d\u0443\u0442:<\/p>\n<pre><code class=\"bash\">(crontab -l ; echo \"*\/5 * * * * \/path\/to\/audit_monitor.py &gt;&gt; \/var\/log\/audit_monitor.log\") | crontab -<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 Telegram \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0435\u0440\u0442\u043e\u0432<\/h3>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Telegram-\u0431\u043e\u0442\u0430:<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<ul>\n<li>\n<p>\u0420\u0430\u0441\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u043b\u043e\u043a \u0441 <a href=\"http:\/\/requests.post\" rel=\"noopener noreferrer nofollow\"><code>requests.post<\/code><\/a><code>()<\/code> \u0438 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0432\u043e\u0438 <code>TOKEN<\/code> \u0438 <code>chat_id<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0430\u043b\u0435\u0440\u0442\u0430 \u0432 Telegram:<\/p>\n<pre><code>!!! \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 sudo: 2023-05-01 12:05:00 | UID 1000 | exe=\"\/usr\/bin\/rm\"<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u044b\u043b\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 <code>auditd<\/code> \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u043e\u043f\u0438\u0441\u0430\u043d \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043b\u043e\u0433\u043e\u0432 \u0438, \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u043b\u0435\u0440\u0442\u043e\u0432 \u0432 Telegram. \u041a\u0430\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 (\u0438 \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u043d\u043e?) \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0412\u0430\u043c &#8212; \u0440\u0435\u0448\u0438\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u043b\u044f \u0441\u0435\u0431\u044f. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c.<\/p>\n<p><strong>\u041f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 (\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435 24:00 = \u043f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e)<\/p>\n<\/li>\n<li>\n<p>\u0412\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043f\u043e ssh (<code>-w \/etc\/ssh\/sshd_config<\/code>)<\/p>\n<\/li>\n<\/ul>\n<p><em>P.S. \u042f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0441\u0432\u043e\u044e \u0433\u0440\u0443\u043f\u043f\u0443 \u0432<\/em>\u00a0<a href=\"https:\/\/t.me\/automate_today\" rel=\"noopener noreferrer nofollow\"><em>\u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u043c<\/em><\/a><em>, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0441\u0435\u0445, \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0432 \u043c\u0438\u0440\u0435 IT.<\/em>  <\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/925962\/\"> https:\/\/habr.com\/ru\/articles\/925962\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c auditd &#8212; \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0430\u0443\u0434\u0438\u0442\u0430 \u0432 Linux, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435, \u0430 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442 \u043b\u043e\u0433\u0438 \u0432 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0435 \u043e\u0442\u0447\u0451\u0442\u044b \u0438 \u0430\u043b\u0435\u0440\u0442\u044b.<\/p>\n<p>Linux-\u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0435\u0437 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u2014 \u043a\u0430\u043a \u0434\u043e\u043c \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c\u0438 \u043e\u043a\u043d\u0430\u043c\u0438. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0440\u0435\u0448\u0438\u0442\u044c \u0430\u0443\u0434\u0438\u0442:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0441\u0430\u043d\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f (\u043a\u0442\u043e \u0438 \u043a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>sudo<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (<code>rm -rf<\/code>, \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0430\u0432, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0444\u0430\u0439\u043b\u0430\u043c)<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0446\u0438\u0434\u0435\u043d\u0442\u043e\u0432 (\u043a\u0442\u043e \u0447\u0442\u043e \u043d\u0430\u0442\u044b\u043a\u0430\u043b \u043f\u0435\u0440\u0435\u0434 \u043f\u0430\u0434\u0435\u043d\u0438\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0430)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0418\u0411 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438\u043b\u0438 \u0432\u044b \u043b\u044e\u0431\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c)<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435: <code>auditd<\/code> + Python-\u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438 \u0430\u043b\u0435\u0440\u0442\u043e\u0432.<\/p>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 auditd<\/h3>\n<p><strong>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 auditd<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043b\u0438 <code>auditd<\/code>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo apt install auditd -y  # \u0415\u0441\u043b\u0438 Debian\/Ubuntu sudo yum install audit      # \u0415\u0441\u043b\u0438 CentOS<\/code><\/pre>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0430\u0432\u0442\u043e\u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo systemctl enable --now auditd sudo systemctl status auditd  # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0442\u0430\u0442\u0443\u0441 <\/code><\/pre>\n<p><strong>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b auditctl:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u0442\u0435\u043a\u0443\u0447\u0438\u0445 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b:<\/p>\n<pre><code class=\"bash\">sudo auditctl -l<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430 (\u0438\u0441\u0447\u0435\u0437\u043d\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0440\u0435\u0441\u0442\u0430\u0440\u0442\u0430):<\/p>\n<pre><code class=\"bash\">sudo auditctl -w \/etc\/passwd -p rwa -k sensitive_files<\/code><\/pre>\n<p>\u0413\u0434\u0435:<\/p>\n<ul>\n<li>\n<p><code>-w<\/code> &#8212; \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443\/\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><code>-p<\/code> &#8212; \u043f\u0440\u0430\u0432\u0430 (r \u2014 \u0447\u0442\u0435\u043d\u0438\u0435, w \u2014 \u0437\u0430\u043f\u0438\u0441\u044c, x \u2014 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, a \u2014 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432).<\/p>\n<\/li>\n<li>\n<p><code>-k<\/code> &#8212; \u043a\u043b\u044e\u0447 (\u043c\u0435\u0442\u043a\u0430 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043b\u043e\u0433\u043e\u0432).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043f\u0440\u0430\u0432\u0438\u043b:<\/p>\n<pre><code class=\"bash\">sudo auditctl -D<\/code><\/pre>\n<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>\n<p>\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0430\u0443\u0434\u0438\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 <code>\/etc\/audit\/rules.d\/audit.rules<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0444\u0438\u0433\u0430:<\/p>\n<pre><code class=\"bash\"># \u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \/etc\/passwd \u0438 \/etc\/shadow -w \/etc\/passwd -p wa -k passwd_changes -w \/etc\/shadow -p wa -k shadow_changes  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u0430\u043d\u0434, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 sudo -w \/usr\/bin\/sudo -p x -k sudo_usage  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 -a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -k file_deletion  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0445 shell-\u043a\u043e\u043c\u0430\u043d\u0434 (bash, zsh, sh) -w \/bin\/bash -p x -k shell_commands -w \/bin\/zsh -p x -k shell_commands -w \/bin\/sh -p x -k shell_commands  # \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u0430\u0445 -w \/etc\/ -p wa -k etc_changes<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430:<\/p>\n<pre><code class=\"bash\">sudo auditctl -R \/etc\/audit\/rules.d\/audit.rules<\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437 \u043b\u043e\u0433\u043e\u0432<\/h3>\n<ol>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043b\u043e\u0433\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\n<p><code>ausearch<\/code> \u2014 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043b\u043e\u0433\u0430\u043c.<\/p>\n<pre><code class=\"bash\">sudo ausearch -k sudo_usage  # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043a\u043b\u044e\u0447\u0443 sudo ausearch -m EXECVE      # \u0422\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b<\/code><\/pre>\n<\/li>\n<li>\n<p><code>aureport<\/code> \u2014 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u0447\u0435\u0442\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">sudo aureport --summary     # \u041e\u0431\u0449\u0430\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 sudo aureport -f            # \u041e\u0442\u0447\u0435\u0442 \u043f\u043e \u0444\u0430\u0439\u043b\u0430\u043c sudo aureport -u            # \u041e\u0442\u0447\u0435\u0442 \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043b\u043e\u0433\u0430 auditd<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 <code>\/var\/log\/audit\/audit.log<\/code>:<\/p>\n<pre><code>type=SYSCALL msg=audit(1620000000.123:456): arch=c000003e syscall=59 success=yes exit=0 a0=123 a1=456 a2=789 a3=0 items=2 ppid=1234 pid=5678 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 comm=\"sudo\" exe=\"\/usr\/bin\/sudo\" key=\"sudo_usage\"<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u043e\u0440:<\/p>\n<ul>\n<li>\n<p><code>uid=0<\/code> \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043e\u0442 root.<\/p>\n<\/li>\n<li>\n<p><code>comm=\"sudo\"<\/code> \u2014 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>sudo<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>key=\"sudo_usage\"<\/code> \u2014 \u043c\u0435\u0442\u043a\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<h3>Python-\u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438 \u0430\u043b\u0435\u0440\u0442\u043e\u0432<\/h3>\n<p><strong>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u043b\u043e\u0433\u043e\u0432<\/strong><\/p>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c <code>audit_monitor.py<\/code>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">#!\/usr\/bin\/env python3 import subprocess import re from datetime import datetime  # \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f ALERT_RULES = {     \"sudo_usage\": \"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 sudo: {details}\",     \"shadow_changes\": \"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \/etc\/shadow: {details}\",     \"file_deletion\": \"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432: {details}\", }  def parse_audit_log():     alerts = []     for key, message in ALERT_RULES.items():         # \u041f\u043e\u0438\u0441\u043a \u043b\u043e\u0433\u0430 \u043f\u043e \u043a\u043b\u044e\u0447\u0443         cmd = f\"ausearch -k {key} --raw | aureport -i -f\"         logs = subprocess.getoutput(cmd).split('\\n')                  for line in logs:             if not line:                 continue             # \u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u0441\u0442\u0440\u043e\u043a\u0438 \u043b\u043e\u0433\u0430             time_match = re.search(r\"msg=audit\\((\\d+\\.\\d+)\", line)             user_match = re.search(r\"auid=(\\d+)\", line)             cmd_match = re.search(r'exe=\"([^\"]+)\"', line)                          if time_match and user_match:                 timestamp = datetime.fromtimestamp(float(time_match.group(1)))                 user = f\"UID {user_match.group(1)}\"                 details = f\"{timestamp} | {user} | {line}\"                 alerts.append(message.format(details=details))          return alerts  if __name__ == \"__main__\":     alerts = parse_audit_log()     if alerts:         print(\"\\n\".join(alerts))         # \u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0432 Telegram (\u0440\u0430\u0441\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c)         # import requests         # requests.post(\"https:\/\/api.telegram.org\/botTOKEN\/sendMessage\", json={\"chat_id\": \"ID\", \"text\": \"\\n\".join(alerts)})     else:         print(\"\u041f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043e.\")<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0422\u0435\u0441\u0442\u0438\u043c \u0442\u0440\u0438\u0433\u0433\u0435\u0440:<\/p>\n<pre><code class=\"bash\">sudo cat \/etc\/shadow<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<pre><code class=\"bash\">chmod +x audit_monitor.py .\/audit_monitor.py<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0432\u043e\u0434\u0430:<\/strong><\/p>\n<pre><code>\u0414\u043e\u0441\u0442\u0443\u043f \u043a \/etc\/shadow: 2023-05-01 12:00:00 | UID 1000 | type=SYSCALL ... exe=\"\/usr\/bin\/cat\"<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 cron \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043a\u0430\u0436\u0434\u044b\u0435 5 \u043c\u0438\u043d\u0443\u0442:<\/p>\n<pre><code class=\"bash\">(crontab -l ; echo \"*\/5 * * * * \/path\/to\/audit_monitor.py &gt;&gt; \/var\/log\/audit_monitor.log\") | crontab -<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 Telegram \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0435\u0440\u0442\u043e\u0432<\/h3>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Telegram-\u0431\u043e\u0442\u0430:<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<ul>\n<li>\n<p>\u0420\u0430\u0441\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u043b\u043e\u043a \u0441 <a href=\"http:\/\/requests.post\" rel=\"noopener noreferrer nofollow\"><code>requests.post<\/code><\/a><code>()<\/code> \u0438 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0432\u043e\u0438 <code>TOKEN<\/code> \u0438 <code>chat_id<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0430\u043b\u0435\u0440\u0442\u0430 \u0432 Telegram:<\/p>\n<pre><code>!!! \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 sudo: 2023-05-01 12:05:00 | UID 1000 | exe=\"\/usr\/bin\/rm\"<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u044b\u043b\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 <code>auditd<\/code> \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u043e\u043f\u0438\u0441\u0430\u043d \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043b\u043e\u0433\u043e\u0432 \u0438, \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u043b\u0435\u0440\u0442\u043e\u0432 \u0432 Telegram. \u041a\u0430\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 (\u0438 \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u043d\u043e?) \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0412\u0430\u043c &#8212; \u0440\u0435\u0448\u0438\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u043b\u044f \u0441\u0435\u0431\u044f. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430, \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c.<\/p>\n<p><strong>\u041f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 (\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435 24:00 = \u043f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e)<\/p>\n<\/li>\n<li>\n<p>\u0412\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043f\u043e ssh (<code>-w \/etc\/ssh\/sshd_config<\/code>)<\/p>\n<\/li>\n<\/ul>\n<p><em>P.S. \u042f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0441\u0432\u043e\u044e \u0433\u0440\u0443\u043f\u043f\u0443 \u0432<\/em>\u00a0<a href=\"https:\/\/t.me\/automate_today\" rel=\"noopener noreferrer nofollow\"><em>\u0422\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u043c<\/em><\/a><em>, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0441\u0435\u0445, \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0432 \u043c\u0438\u0440\u0435 IT.<\/em>  <\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/925962\/\"> https:\/\/habr.com\/ru\/articles\/925962\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-466473","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/466473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=466473"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/466473\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=466473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=466473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=466473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}