859 字
4 分钟
Linux记录终端用户行为的一种审计方法
Linux 下用户行为审计的一种方法
Linux 提供了一种可以对用户的行为进行记录和重放的方法。这种方法对于比较重要的环境下的审计以及问题的处理,有比较广泛的使用。
启用行为记录
下面是一个启用该功能的 shell 脚本:
#!/bin/bashmkdir -p /var/log/session# Define the block to be added to /etc/profileblock='if [ "x$SESSION_RECORD" = "x" ]thentimestamp=$(date +%d-%m-%Y-%T)session_log=/var/log/session/session.$USER.$timestamp.logSESSION_RECORD=startedexport SESSION_RECORDscript -t -f -q 2>${session_log}.timing $session_logexitfi'
# Add the block to /etc/profileecho "$block" | sudo tee -a /etc/profile > /dev/null
# Source the profile to apply the changessource /etc/profile上面脚本的目的如下:
- 以管理员账户运行该脚本,创建/var/log/session 目录。
- 如果用户 session 记录没有启用,那么开始进行参数定义
- 设定 timestamp 格式,这个格式可以自己进行修改,不影响功能
- 设定 session 日志的文件位置和命名格式
- 启动 session 记录
- 生成带有时间的脚本
- 将上面的这些指令放到/etc/profile 中,并进行更新。
- 从此以后,所有终端登录到这台 Linux 机器的人,他们的所有的输入和输出都会被记录到对应的文件中,并且可以进行回放。
发送记录
发送到审计人的邮箱或者使用 scp 进行传输到其他地方这个是可选的。只是发送到邮箱更简单一些,方便其他人更好的进行外部审计。
由于 shell 发送邮件在不同的操作系统中命令的格式略有不同,所以下面以 python 代码的形式实现,方便统一。
下面是使用 python 代码发送用户行为日志的脚本:
import osimport smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email import encodersimport zipfile
def send_email(sender, password, recipients, subject, body, attachment_folder, smtp_server, smtp_port): # Create a multipart message message = MIMEMultipart() message['From'] = sender message['To'] = ', '.join(recipients) message['Subject'] = subject
# Add body to the email message.attach(MIMEText(body, 'plain'))
# Create a zip file zip_filename = 'attachments.zip' with zipfile.ZipFile(zip_filename, 'w') as zip_file: # Iterate through files in the attachment folder for filename in os.listdir(attachment_folder): attachment_path = os.path.join(attachment_folder, filename) if os.path.isfile(attachment_path): # Add the file to the zip archive zip_file.write(attachment_path, filename)
# Attach the zip file to the message with open(zip_filename, 'rb') as attachment: part = MIMEBase('application', 'zip') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', f'attachment; filename= {zip_filename}') message.attach(part)
# Connect to the SMTP server server = smtplib.SMTP(smtp_server, smtp_port) # server.starttls() server.login(sender, password)
# Send the email server.send_message(message)
# Disconnect from the server server.quit()
# Delete the zip file os.remove(zip_filename)
# usagesender = ''user_activity@mytemp.com”password = “password”recipients = ['mark@mark.com', 'abc@cde.com']subject = 'user activity log'body = 'Please find the attachments in this email.'attachment_folder = '/var/log/session'smtp_server = 'smtp.mytemp.com'smtp_port = 25
send_email(sender, password, recipients, subject, body, attachment_folder, smtp_server, smtp_port)需要说明的是,enterprise 的邮箱有可能会过滤掉 zip 文件,不过经过几次测试一般都是进入垃圾邮箱的。
重放(可选)
拿到日志的审计人员,可以在自己的环境进行重放,当然直接读 log 也够了。
重放的指令如下:
scriptreplay session.root.xxx.timing session.root.xxx.log注意事项
这种审计模式由于包含所有的输入输出,而且在/etc/profile 下会对所有的终端用户启用,所以需要特别注意,是否只应对个别用户启用,而且记录的 log 由于可能包含有个人的数据信息,所以也需要审计人员特别的注意。还有就是如果用户的输入输出操作比较频繁,生成的 log 可能会非常大,要定期对旧文件进行备份和删除的操作,避免存储空间的大量占用。
Linux记录终端用户行为的一种审计方法
https://dididudu998.github.io/posts/记录终端用户行为的方法/