Linux安全基础——day4——审计、服务安全

系统审计
    简介: 基于事先配置的规则生成日志,记录可能发生在系统上的事件,但是审计不会为系统提供额外的保护,致使会发现并记录违反安全策略的人以及对应的行为。
    日志内容: 日期与事件、事件结果、触发事件的用户、所有认证机制的使用、对关键数据的修改行为

部署audit
    装包: audit(默认系统已经安装好了)

/etc/audit/auditd.conf 配置文件
/var/log/audit/audit.log 日志信息
/etc/audit/rules.d/audit.rules 规则配置文件
 
auditctl -s 查看状态
auditctl -l 查看规则
auditctl -D 删除所有规则
auditctl -w 审计的目录 -p 权限 -k 设置标示信息 定义临时规则
  权限有: r(读)、w(写)、x(执行)、a(属性修改)
echo '-w 审计的目录 -p 权限 -k 设置标示信息' >> /etc/audit/rules.d/audit.rules 永久修改规则

  例子:

# 当读写passwd文件,就会被记录,标示为change_passwd这个名字
auditctl -w /etc/passwd -p rwa -k change_passwd

# 当执行了rm 命令,就记录,标示为remove_comond这个名字
auditctl -w /usr/bin/rm -p rx -k remove_comond


审计日志:

type 指定类型
msg 时间,从1970年1月1日开始到现在多少秒
success 显示事件是否执行成功
auid uid euid 记录执行操作的用户id,以及登陆该用户使用的用户,以及su 转换用户前的用户信息,用以防止别人控制其他人的设备做违规的事情
a0-a3 程序调用时的4个参数
ppid pid 父进程和运行的进程的ID号
tty 指定哪个终端执行的
comm 用户执行的命令是什么
exe 实际程序的路径
key 管理员定义策略时候用的关键字
type=CWD 该行后面记录的当前工作目录


查阅日志信息:ausearch -k 关键字 -i
默认搜索/var/log/audit/audit.log日志文件中的内容


nginx安全

   1. 编译安装前的安全操作
     1.1 隐藏软件名 修改src/http/ngx_http_header_filter_module.c文件的48行左右的内容,将三个变量参数修改了

ngx_http_server_string[]
ngx_http_server_full_string[]
ngx_http_server_build_string[]

     1.2 安装模块的时候,不要配置自己不需要的模块,例如--without-http_autoindex_module 、 --without-http_ssi_module

   2. 安装后修改nginx的配置文件
     2.1 屏蔽非法访问
    nginx配置文件中的server里面写:
    if($request_method !~ ^(GET|POST)$){
        return 444;
    }

# 可以用命令非交互完成
sed -i '/#charset/a\        }' /etc/nginx/nginx.conf
sed -i '/#charset/a\            return 444;' /etc/nginx/nginx.conf
sed -i '/#charset/a\        if ($request_method !~ ^(GET|POST)$){' /etc/nginx/nginx.conf

     2.2 限制并发,解决DOS攻击,其做法是:限制一个IP并发访问,1秒钟处理一个IP一个请求,多余的放到内存中,不过内存中只能存一定个数,然后慢慢处理,超出的就不处理。

sed -i '/default_type/a\    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;' /etc/nginx/nginx.conf
sed -i '/#charset/a\        limit_req zone=one burst=5;' /etc/nginx/nginx.conf

# 上面的代码执行后,重启nginx服务,那么一个IP如果并发上百个访问,那么只处理6个

数据库安全优化

     软件mariadb有一个初始化安全脚本 mysql_secure_installation ,执行后进行交互式处理

[root@test ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 这里输入当前数据库的root密码,没有密码就直接回车即可
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y  如果重新设置密码回答Y,不重设密码回答N
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y   建议删除匿名用户,如果删除回答Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n    如果允许root远程登陆属于Y,如果不允许root远程登陆用N
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y    建议删除自带的测试数据库,因为这个数据库是系统自带,不管什么用户都对其拥有所有权限,删除输入Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y     如果上面设置没有问题,就Y保存
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

系统命令安全
    历史命令最好不要被查看到,如果做了一些操作比较重要,就删除历史命令,命令如下:
history -c ; history -w
# 选项-c 是清空当前的历史命令 , -w 是把当前历史命令覆盖记录到 ~/.bash_history 下
    其配置文件是:/etc/profile 修改里面的 HISTSIZE 来控制历史命令的个数


Tomcat的安全配置


打补丁

    数据对比,使用命令diff

-u 输出比较的内容以及统一的内容
-r 第归对比目录下的所有资源
-a 所有文件视为文本,这样就可以比较二进制的程序
-N 文件不存在的时候视为空,也会参与比较

    一般用于补丁的时候,所有的参数都会用上。


    1. 安装补丁软件包: patch

    2. 生成补丁:

diff -Nura 旧版本文件(目录) 新版本文件(目录) > 补丁文件

    3. 还原补丁

# 前往要被打补丁的文件目录位置,下面写的是基于当前目录下的补丁文件的相对路径
patch -p数字  相对路径/补丁文件

    例子:
      1. 如果补丁文件test.patch和被打补丁的文件test.txt在一个目录dir下那就是

[root@test ~]# cd dir ; ls 
test.patch  test.txt
[root@test dir]# patch -p0 test.patch

      2. 如果补丁文件test.patch和被打补丁的文件test.txt在不一个目录下,补丁文件在的目录是/dir被打补丁文件在/dir/test/file目录下

[root@test ~]# ls /dir 
test.patch  test
[root@test ~]# ls /dir/test/file ; ls
test.txt
[root@test file]# patch -p2 ../../test.patch

    作用: 补丁的作用主要就是,当一个软件因为一些bug修改一小部分代码后,不需要再把新的代码全部重新下载,简化了升级操作。

猜你喜欢

转载自blog.csdn.net/Yu1543376365/article/details/83869936
今日推荐