15、Linux日志审计

Linux日志审计

1、日志文件的功能和分类

日志的功能

用于记录系统、程序运行中发生的各种事件。

通过阅读日志,有助于诊断和解决系统故障日志。

文件的分类

1、内核及系统日志:由系统 syslog 统一进行管理,日志格式基本相似。

2、用户日志/服务日志:记录系统用户登录及退出系统的相关信息。(启动时必须要有的服务信息-ssh/dhcp)

3、程序:由各种应用程序独立管理的日志文件,记录格式不统一。(额外下载的程序)

日志属于内核的一部分。 
内核:在启动的过程当中,文件系统是后加载。但没加载文件系统的话则数据无法进行处理,无法将文件系统加载之前的数据进行关联。
内核日志:调用内核时产生的日志消息,会单独存放在内核日志当中。
数据的管理方式通过文件系统实现。
syslog(回调函数):是日志记录的统一标准。
syslogd/dmesg进程一般是可以被人为终止、调用的,但守护进程不接受任何终端控制(随着系统开启而开启,随着系统关闭而关闭)。

2、日志文件保存位置和文件介绍

Linux操作系统本身和大部分服务器程序的日志文件都默认放在目录/var/log/下。

一部分程序共用一个日志文件,一部分程序使用单个日志文件,而有些大型服务器程序由于日志文件不止一个,所以会在/var/log/目录中建立相应的子目录来存放日志文件,这样既保证了日志文件目录的结构清晰,又可以快速定位日志文件。

有相当一部分日志文件只有root用户才有权限读取,这保证了相关日志信息的安全性。

Linux日志文件常用的保存方式-->回滚切割:将日志分割为一部分一部分来进行存储,这样子的好处是单个日志文件量不会很大,加载速度不会很慢,也方便查看。

日志文件保存位置

/var/log/目录下的日志文件(日志保存位置默认位于/var/log目录下):(知道、了解)
/var/log/boot.log ->包含系统启动时的日志/该文件记录了系统在引导过程中发生的事件
/var/log/utmp ->包含登录退出信息
/var/log/secure ->包含验证和授权信息/Linux系统安全日志,记录用户和工作组的情况、用户登陆认证情况。		(重要)
/var/log/dpkg.log ->包含安装或dpkg命令清除软件包的日志
/var/log/kern.log ->包含内核产生的日志,有助于在定制内核时解决问题
/var/log/Xorg.x.log ->来自X的日志信息
/var/log/alternatives.log ->更新替代信息都记录在这个文件中
/var/log/cups ->涉及所有打印信息的日志
/var/log/anaconda.log ->在安装Linux时,所有安装信息都储存在这个文件中
/var/log/yum.log ->包含使用yum安装的软件包信息
/var/log/cron ->每当cron进程进行一个工作,就会将相关信息记录在此文件中/计划任务	(重要)

文件介绍

主要日志文件介绍:
日志文件					描述
/var/log/cron 		记录了系统定时任务相关的日志
/var/log/cups		记录打印信息的日志
/var/log/dmesg		记录了系统在开机时内核自检的信息,也可以使用dmesg命令直接查看内核自检信息
/var/log/mailog		记录邮件信息
/var/log/message	记录系统重要信息的日志。这个日志文件中会记录Linux系统的绝大多数重要信息,如果系统出现问题时,首先要检查的就应该是这个日志文件
/var/log/btmp		记录错误登录日志,这个文件是二进制文件
/var/log/lastlog	记录系统中所有用户最后一次登录时间的日志,这个文件是二进制文件
/var/log/wtmp		永久记录所有用户的登录、注销信息,同时记录系统的启动、重启、关机事件。同样这个文件也是一个二进制文件
/var/log/utmp 		记录当前已经登录的用户信息,这个文件会随着用户的登录和注销不断变化,只记录当前登录用户的信息。同样这个文件不能直接vi,而要使用w,who,users等命令来查询
/var/log/secure		记录验证和授权方面的信息,只要涉及账号和密码的程序都会记录,比如SSH登录,su切换用户,sudo授权,甚至添加用户和修改用户密码都会记录在这个日志文件中



内核及公共消息日志(大部分程序的运行日志-一些错误信息)/var/log/messages
[root@localhost log]# cd /var/log
[root@localhost log]# cat ./messages 
[root@localhost log]# tail -f /var/log/messages 	//tail -f表示显示最后/最近的十条消息(用于报错时的检查)
守护进程相关的日志(计算机启动相关的内核日志):
[root@localhost log]# cat ./dmesg

3、管理日志服务的配置文件

//定义日志的存放过程以及日志的存放位置,在下面的文件目录下:
[root@localhost ~]# cat /etc/rsyslog.conf		//查看日志存放的规定文件(管理日志的配置文件)
# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console
例:与kern.* 服务相关的均放入/dev/console文件目录下。
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log


# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###

4、内核及系统日志

这种日志数据由系统服务 rsyslog 统一管理,根据其主配置文件/etc/rsyslog.conf中的设置决定将内核消息及各种系统程序消息记录到什么位置。
系统中有相当一部分程序会把自己的日志文件交由 rsyslog 管理,因而这些程序使用的日志记录也具有相似的格式。

5、日志消息的级别

日志消息的级别(事件处理的轻重缓急):
0  EMERG(紧急):会导致主机系统不可用的情况
1  ALERT(警告):必须马上采取措施解决问题
2  CRIT(严重):比较严重的情况
3  ERR(错误):运行出现错误(一般能处理的日志等级)(三级及三级以下就越来越严重了)
4  WARNING (提醒):可能会影响系统功能的事件
5  INFO(信息):一般信息
6  DEBUG(调试):程序或系统调试信息等


日志消息等级3:
[root@localhost ~]# systemctl restart NetworkManager	//当网卡配置出现问题时可以选择将该网络管理关闭,再重启网卡

6、日志记录的一般格式

看消息日志:(tail -f表示显示最后的十条消息)
基本解读:①时间 ②主机名 ③服务/进程 ④消息
systemd----表示内核进程
[root@localhost ~]# tail -f /var/log/messages
Oct 21 19:26:24 localhost pulseaudio: Most likely this is a bug in the ALSA driver 'snd_ens1371'. Please report this issue to the ALSA developers.
Oct 21 19:26:24 localhost pulseaudio: We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail.
Oct 21 19:30:01 localhost systemd: Started Session 4 of user root.
Oct 21 19:40:01 localhost systemd: Started Session 5 of user root.
Oct 21 19:42:14 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
Oct 21 19:42:14 localhost systemd: [/usr/lib/systemd/system/mdcheck_start.timer:12] Failed to parse calendar specification, ignoring: Sun *-*-1..7 1:00:00
Oct 21 19:42:14 localhost systemd: mdcheck_start.timer lacks value setting. Refusing.
Oct 21 19:42:28 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
Oct 21 19:42:28 localhost systemd: [/usr/lib/systemd/system/mdcheck_start.timer:12] Failed to parse calendar specification, ignoring: Sun *-*-1..7 1:00:00
Oct 21 19:42:28 localhost systemd: mdcheck_start.timer lacks value setting. Refusing.

7、用户日志分析

暂无

8、程序日志分析

暂无

9、日志服务器搭建

Linux日志服务器搭建

10、补充

[root@localhost ~]# systemctl restart network       //重启下网络    
[root@localhost ~]# tail -n 10 /var/log/messages | grep network    
//读取最近10行消息发现重启网卡记录已经在日志文件中当中存在(可用于网络服务的排错) 
Oct 21 20:10:29 localhost systemd: Started LSB: Bring up/down networking.


检索日志方法:
tail (默认查看后10行)	【tail -f [路径]】
	-n x:读取最近x行消息
	-Ax:关键词所在行与其下x行消息
	-Bx:关键词所在行与其上x行消息
tail -f file.log 	//持续输出日志,可添加 | grep “关键字”
tail -n 100 file.log | grep “关键字”		//输出文件的后100行中包含的关键字的行(-n 100 即 –lines=100)
tail -n 100 file.log | grep “关键字” -A10		//输出文件的后100行中包含关键字的行和该行的后10行
tail -n 100 file.log | grep “关键字” -B10		//输出文件的后100行中包含关键字的行和该行的前10行
tail -n 100 file.log | grep “关键字” –B10-A10		//输出文件的后100行中包含关键字的行和该行的前后10行


[root@localhost ~]# systemctl list-unit-files | grep enabled	//显示所有服务的列表,筛选出所有可用的服务


lastlog命令解读:
[root@localhost ~]# lastlog -t 1000
Username         Port     From             Latest
root             :0                        Wed Sep 28 20:28:04 +0800 2022
gdm              :0                        Wed Sep 28 20:27:53 +0800 2022
xzm              :0                        Wed Sep 28 20:09:29 +0800 2022
reboot   system boot  3.10.0-1160.el7. Wed Sep 28 20:20 - 20:12 (1+23:51) 
①用户名②终端③来源④开始时间⑤结束时间⑥持续时间
3.10.0-1160.el7.:内核版本
:0:本地登录
pts:是pty的实现方法/控制终端
tty:终端;虚拟终端pty(pseudo-tty)
lastb:登录失败日志
last:登录成功日志 , 还可以查看用户在线状态


[root@localhost ~]# lastlog -t 2	//最近两天用户登录消息 
[root@localhost ~]# w		//显示当前登录用户及这些用户正在做什么的命令。它同时也能显示系统运行时长,当前系统时间和系统平均负载情况。
[root@localhost ~]# lastb	//last与lastb命令用来列出目前与过去登录系统的用户相关信息。
[root@localhost ~]# ss      //ss是Socket Statistics的缩写。顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容。ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。
[root@localhost ~]# stat bbb  	//bbb为文件名(stat:时间命令)
Access: 2022-10-21 20:38:00.289546112 +0800	//最近一次访问
Modify: 2022-10-21 20:38:02.676545904 +0800	//最近一次修改
Change: 2022-10-21 20:38:02.676545904 +0800	//最近一次改变(chmod、chown)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值