安装包:

   nginx:tengine-1.4.0.tar.gz

   # wget http://tengine.taobao.org/download/tengine-1.4.0.tar.gz

   naxsi:naxsi-0.48.tgz

   # svn checkout http://naxsi.googlecode.com/svn/tags/0.48 naxsi-0.48

   Python-2.7.5.tgz

   zope.interface-3.8.0.tar.gz

   Twisted-11.0.0.tar

   MySQL-python-1.2.3.tar.gz

   setuptools-0.6c11.tar.gz

编译安装nginx

   ./configure --with-pcre=/home/soft/test/pcre-8.30

               --with-openssl=/home/soft/test/openssl-1.0.0

               --prefix=/home/app/nginx

               --add-module=/home/soft/test/ngx_cache_purge-1.5

               --with-http_concat_module

               --add-module=/home/soft/test/naxsi-0.48/naxsi_src/

naxsi配置文件说明:

   naxsi的规则配置文件

   规则集部分配置文件:包含naxsi要检测***特征的规则,基本规则的格式如下:

   [@beike_41_134 workspace]# vi /etc/nginx/naxsi_core.rules 【naxsi的规则配置文件路径】

   MainRule "str:|" "msg:mysql keyword (|)" "mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie"

   "s:$SQL:8" id:1005;

   规则说明:

   MainRule:主配置规则,规则有三类(加BasicRule-本地配置规则,CheckRule-结果裁定规则),合并形成最终规则。

   str:| : 规则要检测的字符“|”

   msg:mysql keyword (|) : 规则的标签信息。

   mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie : 规则要检查的协议字段,Body还是Url等。

   s:$SQL:8 : 如果匹配上该条规则则设置变量SQL的值为8,当该变量超过引擎配置中设置的阈值时naxsi就采取劢作。

   id:1005 : 规则的id号,根据该id号可以识别日志中产生的信息是由哪一条规则触发的。

   备注:规则文件可通过http://security.sohu-inc.com/conf_rule/naxsi/获取。


   naxsi的引擎配置文件

   引擎配置文件:包含naxsi引擎工作方式的控制,自学习模式、引擎开关、阈值、劢作等参数设置。

   [@beike_41_134 workspace]# vi /etc/nginx/nbs.rules 【naxsi的引擎配置文件路径】

   1 #learningMode;

   2 SecRulesEnabled;

   3 #SecRulesDisabled;

   4 DeniedUrl "/RequestDenied";

   5 #include "/tmp/naxsi_rules.tmp";

   6

   7 ## check rules

   8 CheckRule "$SQL >= 8" BLOCK;

   9 CheckRule "$RFI >= 8" BLOCK;

   10 CheckRule "$TRAVERSAL >= 4" BLOCK;

   11 CheckRule "$EVADE >= 4" BLOCK;

   12 CheckRule "$XSS >= 8" BLOCK;

   备注:

   第1行:开吭自学习模式。目前为关闭状态,需要相关内置的配置脚本参不手劢执行才能完成;

   第2行:开吭该模块的检测功能;

   第3行:关闭该模块的检测功能;

   第4行:拒绝请求时的应答内容位置-/RequestDenied目录下的默认文件;

   第5行:设置自学习生成的规则文件;

   第8-12行:相关内置变量的阈值设置,即达到该阈值即阻断请求(BLOCK)。naxsi根据核心规则    集检测数据包,发现匹配上规则就增加相关特征变量的值,达到阈值即认为是***,最后采取劢     作。修改红色字体部分的规则-使能的设置即可吭劢或者关闭该模块。

nginx 配置文件

http {

include /etc/nginx/naxsi_core.rules;    

##naxsi_core.rules文件在naxsi-0.48/naxsi_config/naxsi_core.rules

……

server {

listen 80;

server_name localhost;

access_log /tmp/nginx_access.log;

error_log /tmp/nginx_error.log;

location / {

include /etc/nginx/nbs.rules;

#nbs.rules 文件是naxsi-0.48/naxsi_config/default_location_config.example

root html;

index index.html index.htm;

}

location /RequestDenied {

proxy_pass http://127.0.0.1:8082;

}

}

启动nginx

/usr/local/nginx/sbin/nginx

naxsi[[http://code.google.com/p/naxsi/wiki/LearningMode0_46][学习模式(0.46版)]]

naxsi制定一批规则(见naxsi_core.rules),对于网络***进行过滤。这样的规则有时会无差别的过滤一些正常的页面,因此需要制定白名单,对其放行。naxsi学习模式就是用来收集白名单。naxsi一般的定制流程为:

1. 安装nginx+naxsi

2. 配置学习模式

3. 运行nginx、nx_intercept.py、nx_extract.py三个程序

4. 下载网站镜像

5. 收集白名单

6. 修改配置,取消学习模式

7. 杀死nx_intercept.py、nx_extract.py,重启nginx


** 各文件说明

| 文件名            | 说明                                                      |

|-------------------+-----------------------------------------------------------|

| nx_intercept.py   | 监听端口8080,获取naxsi产生的异常信息,并将其保存到数据库 |

| nx_extract.py     | 监听端口8081,从数据库读取异常信息,以web形式显示         |

| nx_parser.py      | 业务处理,主要是数据分析以及读写数据库                    |

| MySQLConnector.py | 数据库句柄                                                |

| naxsi-ui.conf     | 配置文件        

** 安装python类库

Twisted-11.0.0.tar

MySQL-python-1.2.3.tar.gz

安装过程中会提示安装其他包,需要什么安装什么

*** nbs.rules

: root@ubuntu:~# touch /tmp/naxsi_rules.tmp

: root@ubuntu:~# cat /usr/local/nginx/conf/nbs.rules

: LearningMode;

: SecRulesEnabled;

: #SecRulesDisabled;

: DeniedUrl "/RequestDenied";

:

: include "/tmp/naxsi_rules.tmp";

: BasicRule wl:1315 "mz:$HEADERS_VAR:cookie";

:

: ## check rules

: CheckRule "$SQL >= 8" BLOCK;

: CheckRule "$RFI >= 8" BLOCK;

: CheckRule "$TRAVERSAL >= 4" BLOCK;

: CheckRule "$EVADE >= 4" BLOCK;

: CheckRule "$XSS >= 8" BLOCK;

安装mysql

yum install mysql -y

创建mysql库

mysql -uroot -p123456 -e 'create database naxsi_sig;

** 运行python脚本,收集白名单

启用nx_intercept.py和nx_extract.py两进程,请注意mysql登录用户名和密码要:

: root@ubuntu:~/software# cd naxsi-0.46-1/contrib/naxsi-ui

: root@ubuntu:~/software/naxsi-0.46-1/contrib/naxsi-ui# cat naxsi-ui.conf

[sql]

dbtype = mysql

username=root

password=

hostname=127.0.0.1

dbname=naxsi_sig


[nx_extract]

port = 8083

rules_path=/usr/local/nginx/conf/core.rules

username = naxsi_web

password =


[nx_intercept]

port = 8082

# python nx_intercept.py -c naxsi-ui.conf

# python nx_extract.py naxsi-ui.conf

** 页面403

自己编译的nginx可能会出现这个问题,主要原因是nginx对网站目录没有权限。解决方法:一,给文件夹加访问权限;二,修改nginx.conf。第一行改成user  root root


** _mysql_exceptions.OperationalError: (1049, "Unknown database 'naxsi_sig'")

一般因为数据库没创建才有这个问题:

: root@ubuntu:~/software# mysql -uroot -p123456 -e 'create database naxsi_sig;'


<type 'exceptions.IOError'>: [Errno 5] Input/output error

[root@WEB_YQ_64 naxsi-ui]# cat naxsi-ui.conf

[sql]

dbtype = mysql

username=root

password=

hostname=127.0.0.1

dbname=naxsi_sig


[nx_extract]

port = 8083

rules_path=/usr/local/nginx/conf/naxsi_core.rules

username = naxsi_web

password =


[nx_intercept]

port = 8082

path_data=/tmp/naxsi-ui-data/   添加此行解决问题


** _mysql_exceptions.ProgrammingError: (1146, "Table 'naxsi_sig.exception' doesn't exist")

一般是因为数据库里没有数据,线查看nginx的server配置是不是这样的,再确定是不是启动了nx_intercept.py

:     location /RequestDenied {

:         proxy_pass http://127.0.0.1:8080;

: #        proxy_pass http://192.168.198.171:8080;

: #        return 405;

:     }


通过访问页面获取到白名单规则,加入到nginx  location中