CentOS 7 下 Nginx 安全加固配置规范

安全 nginx yii2 运维 · ez · 于 4年前 发布 · 5306 次阅读

目标

安全,安全,安全

WEB服务器搭建

在做yii开发的时候,离不开nginx+php-fpm组合。所以我们是在CentOS 7 中使用Nginx作为web服务

一. Nginx 安装

安装nginx,采用nginx官方源安装,详细参考Nginx官方手册

1. 准备工作

开始安装前,需要一些前置工具的安装

sudo yum install yum-utils

2. 官方源设置

创建源配置文件/etc/yum.repos.d/nginx.repo,并在文件中写入以下信息

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

3. 安装

安装nginx

sudo yum install nginx

设置nignx开机启动并启动

systemctl enable nginx
systemctl start nginx

二. 配置Yii网站

创建nginx对应的yii配置文件

vim /etc/nginx/conf.d/yii.conf

加入下列参数,参考:推荐使用的 Nginx 配置

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.bestyii.com;
    root        /path/to/basic/web;
    index       index.php;

    access_log  /path/to/basic/log/access.log;
    error_log   /path/to/basic/log/error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}

使用该配置时,你还应该在 php.ini 文件中设置 cgi.fix_pathinfo=0 , 能避免掉很多不必要的 stat() 系统调用。

>还要注意当运行一个 HTTPS 服务器时,需要添加 fastcgi_param HTTPS on; 一行, 这样 Yii 才能正确地判断连接是否安全。

测试以下配置文件是否正确

nginx -t

确认无误后,重启Nignx 使之生效。

systemctl restart nginx

安全加固

nginx的多年发展,自身的安全漏洞比较少,一般利用软件包管理器(yum)升级一下就好了。

现在侧重讲述如何利用nginx来加固web应用,干一些应用防火墙(WAF)干的活。

默认参数重置

软件本身会有一些默认参数,往往这些参数会暴露更多的信息,或者留有安全隐患。所以我们要合理的调整系统参数。

防止Host头攻击

访问网站时如果访问路径中缺少/,大多数中间件都会自动将路径补全,返回302或301跳转如下图,Location位置的域名会使用Host头的值。

这种情况实际上风险较低,难以构成Host头攻击。但是由于大多漏洞扫描器会将这种情况检测为Host头攻击,为了通过上级检查或各种审核,大多数甲方单位会要求修复漏洞,彻底解决问题。

添加一个默认server,当host头被修改匹配不到server时会跳到该默认server,该默认server直接返回403错误。

server {

       listen 80 default;

       server_name _;

       location / {

            return 403;

       }

}

禁用autoindex

确保nginx.conf配置文件上禁用autoindex,即autoindex off或者没有配置autoindex。

关闭服务器标记

server_tokens off;建议加载全局配置中。如:

http{
    include       naxsi_core.rules;
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server_tokens off;#关闭服务器标记
    ... ...

自定义缓存

设置自定义缓存以限制缓冲区溢出攻击。nginx.conf配置如下:

http{
    ... ...
    server{
        ... ...
        client_body_buffer_size  16K;
       client_header_buffer_size  1k;
        client_max_body_size  1m;
       large_client_header_buffers  4  8k;
        ... ...

注:上述的参数不是最优参数,仅供参考。

设置timeout设置timeout设低来防御DOS攻击,nginx.conf配置如下:

http {
    ... ...
       client_body_timeout   10;
       client_header_timeout  30;
       keepalive_timeout     30  30;
       send_timeout          10;

限制访问的方法

在目前的应用系统中值使用到POST和GET方法,所以除了它们之外,其他方式的请求均可拒绝。Nginx.conf配置如下:

server{
       ... ...
       if($request_method !~ ^(GET|HEAD|POST)$) {        
                     return404;
              }
       ... ...

封杀各种user-agent

user-agent 也即浏览器标识,每个正常的web请求都包含用户的浏览器信息,除非经过伪装,恶意扫描工具一般都会在user-agent里留下某些特征字眼,比如scan,nmap等。我们可以用正则匹配这些字眼,从而达到过滤的目的,请根据需要调整。

if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {
    return 403;
}

这里分析得不够细致,具体的非法user-agent还得慢慢从日志中逐个提取。

封杀特定的url

特定的文件扩展名,比如.bak

location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {
    rewrite ^/(.*)$  $host  permanent;
}

知名程序,比如phpmyadmin

location /(admin|phpadmin|status)  {
    deny all;
}

强制网站使用域名访问

可以逃过IP扫描,比如

if ( $host !~* 'abc.com' ) {
    return 403;
}

url 参数过滤敏感字

if ($query_string ~* "union.*select.*\(") {
    rewrite ^/(.*)$  $host  permanent;
}

if ($query_string ~* "concat.*\(") {
    rewrite ^/(.*)$  $host  permanent;
}

在头信息中定义安全参数

 # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
   add_header Content-Security-Policy "default-src 'self';  style-src  'unsafe-inline'  'self';  script-src  'unsafe-inline' 'self' *.bestyii.com analysis.bestyii.com *.baidu.com *.google-analytics.com;  img-src 'self' data: oss.bestyii.com *.baidu.com *.google-analytics.com;  connect-src  'self'  *.baidu.com ;"; #按需配置

本文由 ez 创作,采用 知识共享署名 3.0 中国大陆许可协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册