构建安全的web文件共享服务器

这篇不聊算法,主要聊本次自己建立HTTP的文件共享经历。事情是这样子的,由于需要在互联网上共享一些文件,各种网盘由于速度原因并不支持;QQ群空间,SFTP这些也无法满足要求,因为有后台程序请求。
刚好,目前手头有一台2年前买的闲置的阿里云主机可以派上用场。

一 服务器选型和试用

想想tomcat这种复杂的配置,就有点头疼,所以先排除了tomcat。想了下,这种需求场景应该很普遍,看看大家都怎么做的,就随手搜了下。

1.1 python开启web服务共享文件

发现可以用python启动简单的web服务,方法如下:

#python 3.x 默认开启8000端口
python -m http.server -d 要共享的目录路径

#python 2.x 默认开启8000端口
python -m SimpleHTTPServer

默认都是共享程序运行的目录,当然可以通过-d选项指定;
由于担心python的性能问题,放弃了这种方式。

1.2 http-server开启服务共享

又看到一款比较常用的就是http-server,这个是node.js,安装如下:

yum install npm
npm install http-server -g
#默认开启8080 可以通过-p制定
http-server /root/backup/test

也是一款零配置的http服务器,可以指定共享的路径,端口等,其他选项,大家可以自己百度搜索。不过运行了一天,竟然自己停了。

1.3 Nginx 开启文件服务共享

Nginx作为老牌的服务器,而且是C写的,稳定,内存占用低,想想我那1G的小内存,这种还是蛮适合的。
去搜了下关于Nginx的开启文件服务共享的配置:

#安装nginx
yum install nginx -y

#更改配置
vim /etc/nginx/nginx.conf

server {
        #开启的端口
        listen       80;
        server_name  localhost;
        location / {
          #共享的目录
          root         /root/weixin/luyin;
          #有这个选项才可以看到文件
          autoindex on;
          autoindex_exact_size off;
          autoindex_localtime on;
          charset utf-8;
        }
}
#启动程序
systemctl start nginx
#查看运行
systemctl status nginx

本来到这里,已经可以开开心心地玩耍去了,自己看了下日志,发现:

173.249.16.234 - - [23/Nov/2019:16:55:44 +0800] "GET / HTTP/1.0" 200 2286 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"
167.99.40.21 - - [23/Nov/2019:16:58:20 +0800] "GET / HTTP/1.0" 200 2286 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"
167.99.40.21 - - [23/Nov/2019:16:58:24 +0800] "GET / HTTP/1.0" 200 2286 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"
167.99.40.21 - - [23/Nov/2019:16:58:27 +0800] "GET / HTTP/1.0" 200 2286 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"
139.162.88.63 - - [23/Nov/2019:17:07:58 +0800] "GET http://clientapi.ipip.net/echo.php?info=1234567890 HTTP/1.1" 404 169 "-" "Go-http-client/1.1" "-"
217.147.85.78 - - [23/Nov/2019:17:12:00 +0800] "GET / HTTP/1.0" 200 2286 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"

这才多会啊,一堆扫描器就上来了,虽然我这个80端口确实有点招摇,但是也不能这样欺负人啊,咋办,上WAF吧。

二. 开源WAF选型

WAF是Web Application Firewall 简称,直翻译过来就是web应用防火墙,是通过分析web请求,想详细了解WAF可以看下:https://blog.csdn.net/enweitech/article/details/78899312 我就不抄了。
综合各种搜索和比较,最终选择ModSecurity
WAF

ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web 应用程序,所以也被称为Web应用程序防火墙。 它可以作为Apache Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web application的安全性和保护Web application以避免遭受来自已知与未知的攻击。
选择它的原因是:

  1. 牌子比较响,得到nginx官方推荐。
  2. 嵌入式的,潜入到nginx里面。

2.1 安装

本次安装的是嵌入式的ModSecurity,好处是可以解决一些https报文,无法解密的问题。嵌入在Nginx中可以通过反向代理,一个带ModSecurity的Nginx反向代理,后面Nginx服务器可以不加载这个模块了,比较方便,虽然我还用不到。

示意图如下:
ModSecurity

安装ModSecurity的依赖:

yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel git  -y
yum install install autoconf automake libtool

下载nginix源码

#源码要和自己在用的一样,我是直接用最新的
wget  http://nginx.org/download/nginx-1.17.6.tar.gz

下载安装Geoip
安装Geoip库可以方便查询客户端IP的所在的国家城市等信息,当然这不是必须的,不过ModSecurity里面有包含这类规则,不需要可以注释掉也是可以的。

#ip归属库
wget http://download.zhufunin.com/libmaxminddb-1.3.2.tar.gz
#ip归属库的nginx模块
wget http://download.zhufunin.com/ngx_http_geoip2_module.zip
#ip和城市映射数据
wget http://download.zhufunin.com/maxmind-city.mmdb.tar.gz

编译安装:libmaxminddb

cd libmaxminddb;
./bootstrap && ./configure && make && make install

编译安装ModSecurity

git clone --depth 1 -b v3/master --single-branch [https://github.com/SpiderLabs/ModSecurity](https://github.com/SpiderLabs/ModSecurity)
cd ModSecurity && git submodule init && git submodule update && ./build.sh && ./configure  --enable-standalone-module
&& make && make install

一直编译不过,和libmaxminddb 结合不起来,连接不上库,本来是很简单的错误,却改了多次一直不过,
换一种安装方式安装libmaxminddb库。
报错信息如下:

/usr/bin/ld: cannot find -llibmaxminddb
collect2: error: ld returned 1 exit status

安装:

yum -y install libtool gcc gcc-c++ pcre-devel zlib-devel libxml2-devel libxslt-devel gd-devel perl perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data libatomic_ops-devel
yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel git apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev ibpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev -y

重点是:GeoIP-devel和libmaxminddb-devel

ModSecurity和Nginix连接器

git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

编译Nginx

./configure --with-compat --add-dynamic-module=../ModSecurity-nginx --add-dynamic-module=../ngx_http_geoip2_module-master
make && make install

到安装目录上看下已经安装上去了:

[root@eishost nginx]# ./sbin/nginx -V
nginx version: nginx/1.17.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --with-compat --add-dynamic-module=../ModSecurity-nginx --add-dynamic-module=../ngx_http_geoip2_module-master
[root@eishost nginx]# pwd
/usr/local/nginx

规则库的下载

git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
cd owasp-modsecurity-crs
cp crs-setup.conf.example crs-setup.conf

推荐配置下载:

 wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
mv modsecurity.conf-recommended modsecurity.conf

Nginx下配置设置:

mkdir /usr/local/nginx/conf/modsec
cp modsecurity.conf /usr/local/nginx/conf/modsec
cd  /usr/local/nginx/conf/modsec
cp owasp-modsecurity-crs/crs-setup.conf ./
vim main.conf
#添加以下内容:
nclude  /usr/local/nginx/conf/modsec/modsecurity.conf
Include  /root/soft/nginx-waf/owasp-modsecurity-crs/crs-setup.conf
Include /root/soft/nginx-waf/owasp-modsecurity-crs/rules/*.conf
vim modsecurity.conf
#添加以下内容
SecRuleEngine On

拷贝映射文件:

cp /root/soft/nginx-waf/ModSecurity/unicode.mapping  /usr/local/nginx/conf/modsec

编辑nginx.conf文件:

#头部加载模块
load_module modules/ngx_http_modsecurity_module.so;
load_module modules/ngx_http_geoip2_module.so;

#在http内添加:
   geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb {
    $geoip2_data_country_code default=US source=$remote_addr country iso_code;
    $geoip2_data_country_name country names en;
    $geoip2_data_city_name default=London city names en;
    $geoip2_data_province_name subdivisions 0 names en;
    $geoip2_data_province_isocode subdivisions 0 iso_code;
   }

#在server中可以添加测试ip的信息
modsecurity on;
modsecurity_rules_file /usr/local/nginx/conf/modsec/main.conf;

 location /myip {
              default_type text/plain;
            return 200 "$remote_addr $geoip2_data_country_name $geoip2_data_country_code  
           $geoip2_data_city_name";
       }

IP的城市和国家映射数据集合:

wget http://download.zhufunin.com/maxmind-city.mmdb.tar.gz
tar xvf maxmind-city.mmdb.tar.gz
cp maxmind-city.mmdb /usr/local/nginx/geoip/

三. 最终测试

cd /usr/local/nginx/sbin/nginx -t -c ./conf/nginx.conf

重新加载配置:

/usr/local/nginx/sbin/nginx -s reload

访问下/myip 测试下结果。

为了一个文件共享,折腾了一天。

参考
https://www.jianshu.com/p/d22f3914d153

posted @ 2019-11-24 09:10  XGogo  阅读(312)  评论(0)    收藏  举报