安全运营 -- 监控linux命令history

news/2024/10/28 12:30:37/

0x00 背景

最近,有个IT的同事给我提了一个需求,说想监控/root/.ssh/ 文件夹下的文件变动,于是我灵机一动,这个需求只要对执行过的历史命令做审计就可以了。

0x01 实践

我实现这个功能使用 rsyslog 和 firewalld 两个组件。

我的设计是把命令history 转发到/usr/share/commands.log这个文件,再对这个文件监控。

export PROMPT_COMMAND='history 1 >> /usr/share/commands.log'

这个命令是临时的,只对当前会话可以记录。

如果想永久重定向,执行下面的命令

vi ~/.bashrc; export PROMPT_COMMAND='history 1 >> /usr/share/commands.log'; source ~/.bashrc

 然后开始配置文件通过syslog配置轮转到 /var/log/firewalld.log,按照下面命令修改配置

vi /etc/rsyslog.d/commands.conf

# Load the imfile module
module(load="imfile")

# Monitor /usr/share/commands.log
input(type="imfile"
      File="/usr/share/commands.log"
      Tag="commands-log"
      Severity="info"
      Facility="local7")

# Send logs to /var/log/firewalld.log
local7.* /var/log/firewalld.log

重启 syslog服务 

systemctl restart rsyslog

当然,需要确保 firewall 处于开启状态

提供一份简单的检查firewall 状态代码

systemctl status firewalld
#开启firewalld Block 日志
firewall-cmd --set-log-denied=all                                                                                                
echo "Kern.* /var/log/firewalld.log">> /etc/rsyslog.d/firewalld.conf                                            
sed -i '3a /var/log/firewalld.log' /etc/logrotate.d/syslog       #第三行后面追加    
systemctl restart rsyslog   
#设定本机使用public安全区                                                                                                      
firewall-cmd --set-default-zone=public                                                                                    
#设定堡垒机网段                                                                                                                           
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.xx.x.xx/26" service name="ssh" accept"  
#添加/移除对外开放的端口号                                                                                                 
firewall-cmd --permanent --add-port 443/tcp 
firewall-cmd --add-port 137/udp --permanent
firewall-cmd --add-source 127.0.0.1/8 --permanent
firewall-cmd --add-source 0.0.0.0 --permanent 
firewall-cmd --remove-port 22/tcp --permanent 
#添加服务IP白名单                                                                                                                    
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.x.xx" accept"
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.xx.x.xx" port protocol="tcp" port="8089" accept"  
#reload
firewall-cmd --reload                                                                                                    
firewall-cmd --list-all 
#开机自启动
systemctl enable firewalld

只要监控这个文件即可,不止有firewalld.log 还有command history,可以根据需求自由添加。


http://www.ppmy.cn/news/1542581.html

相关文章

MATLAB|怎么存储Simulink运行过程中的变量呢?m语言persistent变量代替C语言Static变量

做实验时,例如使用ARM或者DSP实现控制,时常会定义全局变量,来存储需要的值,以保存某一状态。 做MATLAB/Simulink仿真时,想要实现上述功能则不容易实现(可以,但不容易)。往往这样的需求只需要通过局部静态变…

【STM32 Blue Pill编程实例】-I2C主从机通信(中断、DMA)

I2C主从机通信(中断、DMA) 文章目录 I2C主从机通信(中断、DMA)1、STM32的I2C介绍2、I2C模式3、STM32 I2C 数据包错误检查4、STM32 I2C 错误情况5、STM32 I2C中断6、STM32 I2C 主发送和接收(Tx 和 RX)6.1 I2C 轮询模式6.2 I2C 中断模式6.3 I2C DMA 模式6.4 STM32 I2C 设备…

GO语言的数据结构

Go语言(Golang)具有丰富的内建数据类型和一些基本的数据结构。以下是一些常见的数据结构: 1、基本数据类型: 整数类型:int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64 浮点数类型&#xff1…

【GO实战课】第七讲:电子商务网站(7):性能优化和测试

1. 简介 本课程将探讨电子商务网站的性能优化和测试,以及使用GO语言实现。在本课程中,我们将介绍如何设计一个高性能、可扩展和可靠的电子商务网站,并演示如何使用GO语言编写相关代码。 本课程的目标是帮助学生理解电子商务网站的性能优化和测试原则,并提供一个实际的项目…

fastboot解锁操作

有时候会遇到这样的场景:设备需要解锁才能remount 操作如下: adb reboot bootloader 进入fastboot fastboot device 查看是否有fastboot设备 fastboot flashing unlock 解锁 fastboot reboot 重启生效 然后就能正常的root和remount&#xff1…

2024年,Rust开发语言,现在怎么样了?

Rust开发语言有着一些其他语言明显的优势,但也充满着争议,难上手、学习陡峭等。 Rust 是由 Mozilla 主导开发的通用、编译型编程语言,2010年首次公开。 在 Stack Overflow 的年度开发者调查报告中,Rust 连续多年被评为“最受喜爱…

First - Word Fall - Through ( FWFT ) Read Operation

When looking at Xilinx 8 Kintex-7 4 FPGAs memory resources 32, you’ll find that its FIFO generators support two modes of read options - standard read operating and FWFT read operation. What is FWFT? FWFT is a acronym for First-Word Fall-Through. It is g…

vue3+ts实时播放视频,视频分屏

使用vue3以及播放视频组件Jessibuca Jessibuca地址 使用循环个数来实现分屏 效果图&#xff0c;四屏 九屏 dom代码 <div class"icon"><div class"icon-box"><span class"text">分屏&#xff1a;</span><el-icon …