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的安全配置

   降权启动
      Tomcat最大的问题就是,默认Tomcat的服务使用用户是root,也就是说如果黑客控制了Tomcat服务,就拥有了root权限,那么他可以做所有他想做的事情,所以这是很不安全的,所以tomcat的优化最重要的就是降权操作

useradd tomcat
chown -R tomcat:tomcat /tomcat的工作目录
# 启动tomcat的时候使用如下命令
Su -c /tomcat工作目录/bin/startup.sh tomcat

   影藏版本信息
       这个需要修改tomcat的java编译的jar包的参数

# 如果要修改jar包,需要一个解压软件
yum install -y java-1.8.0-openjdk-devel
cd /tomcat工作目录/lib/
jar -xf catalina.jar
# 修改解压的jar包下的配置文件
sed -i '/server.info/cserver.info=自定义一个网站服务名'  org/apache/catalina/util/ServerInfo.properties
sed -i '/server.number/cserver.number=自定义一个网站服务版本号'  org/apache/catalina/util/ServerInfo.properties
# 再改主配置文件/tomcat工作目录/conf/server.xml
#修改未被注释的redirectPort那行,在后面加上
redirectPort="8443" server="自定义过的一个网站服务名"
# 最后删除默认测试页面
rm -rf /tomcat工作目录/webapps/*

打补丁

    数据对比,使用命令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修改一小部分代码后,不需要再把新的代码全部重新下载,简化了升级操作。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值