wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz 或者svn co svn://vcs.exim.org/pcre/code/trunk pcre
tar zxvf nginx-1.7.8.tar.gztar zxvf pcre-8.36.tar.gzcd nginx-1.7.8/./configure --add-module=/root/naxsi_waf_source/naxsi/naxsi_src --prefix=/opt/naxsi_waf --with-pcre=/root/naxsi_waf_source/pcre-8.36/ --with-pcre-jit --with-debug --with-http_ssl_module --without-http_fastcgi_module --without-http_uwsgi_module --without-http_scgi_module
makemake install
cp ~/naxsi/naxsi_config/naxsi_core.rules /opt/naxsi_waf/conf/vim /opt/naxsi_waf/conf/my_naxsi.rules
#LearningMode; #Enables learning mode 若开启该模式,不会拦截任何请求
SecRulesEnabled;#SecRulesDisabled;DeniedUrl "/RequestDenied";## check rulesCheckRule "$SQL >= 8" BLOCK;CheckRule "$RFI >= 8" BLOCK;CheckRule "$TRAVERSAL >= 4" BLOCK;CheckRule "$EVADE >= 4" BLOCK;CheckRule "$XSS >= 8" BLOCK;
vim /opt/naxsi_waf/conf/nginx.conf
http {include /opt/naxsi_waf/conf/naxsi_core.rules;#naxsi 核心规则地址log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_body" "$http_content_type"';server {listen 80;server_name tanjiti;proxy_set_header Proxy-Connection "";access_log logs/nginx_access.log main;error_log logs/nginx_error.log;#dynamic modifiers for naxsi waf 见naxsi配置[高级]
#set $naxsi_flag_enable 0; #override SecRulesEnabled which 0: disable 1:enable
#set $naxsi_flag_learning 1;#override learning flag
#set $naxsi_flag_post_action 0;# 0:disable post_act ion in learning mode
#set $naxsi_extensive_log 1;#log the CONTENT of variable matching ruleslocation / {include /opt/naxsi_waf/conf/my_naxsi.rules;proxy_pass http://127.0.0.1:8080;proxy_set_header Host www.tanjiti.com;}location /RequestDenied {return 418;//拦截响应码}}}
sbin/nginx -c conf/nginx.conf
http "localhost:80/xss.php?name=<script>alert(1);</script>" Host:www.tanjiti.com -v
编辑 /opt/naxsi_waf/conf/my_naxsi.rules开启LearningMode;

编辑/opt/naxsi_waf/conf/my_naxsi.rules注释掉 LearningMode;

#dynamic modifiers for naxsi waf
#set $naxsi_flag_enable 0; #override SecRulesEnabled which 0: disable 1:enable
#set $naxsi_flag_learning 1;#override learning flag
#set $naxsi_flag_post_action 0;# 0:disable post_act ion in learning mode
#set $naxsi_extensive_log 1;#log the CONTENT of variable matching rules
set $naxsi_flag_enable 0;set $naxsi_extensive_log 1;
set $naxsi_flag_enable 1;set $naxsi_extensive_log 1;
MainRule "rx:select|union|update|delete|insert|table|from|ascii|hex|unhex|drop" "msg:sql keywords" "mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie" "s:$SQL:4" id:1000;
MainRule "str:\"" "msg:double quote" "mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie" "s:$SQL:8,$XSS:8" id:1001;
MainRule negative "rx:multipart/form-da
ta|application/x-www-form-urlencoded" "msg:Content is neither mulipart/x-www-form.." "mz:$HEADERS_VAR:Content-type" "s:$EVADE:4" id:1402;
ARGS$ARGS_VAR (字符串匹配)BasicRule wl:1100 "mz:$ARGS_VAR:redirect_to";$ARGS_VAR_X (正则匹配)
BODY$BODY_VARBasicRule wl:1000 "mz:$BODY_VAR:save";$BODY_VAR_X
HEADERSHEADERS_VARBasicRule wl:1402 "mz:$HEADERS_VAR:content-type";HEADERS_VAR_X
URL (uri "?"前面的部分)BasicRule wl:1000 "mz:URL|$URL:/wp-admin/update.php";$URL_X
FILE_EXT 上传文件名
http "localhost:80/xss.php?name=<script>alert(1);</script>" Host:www.tanjiti.com -v
2014/12/17 19:20:54 [error] 26370#0: *40842 NAXSI_FMT: ip=127.0.0.1&server=www.tanjiti.com&uri=/xss
.php&learning=1&vers=0.53-1&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=12&cscore
1=$XSS&score1=56&zone0=ARGS&id0=1008&var_name0=name&zone1=ARGS&id1=1010&var_name1=name&zone2=ARGS&id2=1011&var_name2=name&zone3=ARGS&id3=1302&var_name3=name&zone4=ARGS&id4=1303&var_name4=name, client: 127.0.0.1, server: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "www.tanjiti.com"
2014/12/17 19:26:06 [error] 396#0: *40844 NAXSI_FMT: ip=127.0.0.1&server=www.tanjiti.com&uri=/xss.p
hp&learning=0&vers=0.53-1&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=4&cscore1=$
XSS&score1=8&zone0=ARGS&id0=1008&var_name0=name, client: 127.0.0.1, server: tanjiti_naxsi, request:
"GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "www.tanjiti.com"
const char *fmt_base = "ip=%.*s&server=%.*s&uri=%.*s&learning=%d&vers=%.*s&total_processed=%zu&total_blocked=%zu&block=%d";
const char *fmt_score = "&cscore%d=%.*s&score%d=%zu";const char *fmt_rm = "&zone%d=%s&id%d=%d&var_name%d=%.*s";
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1008&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1010&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1011&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1302&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1303&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:38:31 [error] 16164#0: *40845 NAXSI_FMT: ip=127.0.0.1&server=www.tanjiti.com&uri=/xss
.php&learning=1&vers=0.53-1&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=12&cscore
1=$XSS&score1=56&zone0=ARGS&id0=1008&var_name0=name&zone1=ARGS&id1=1010&var_name1=name&zone2=ARGS&id2=1011&var_name2=name&zone3=ARGS&id3=1302&var_name3=name&zone4=ARGS&id4=1303&var_name4=name, client: 127.0.0.1, server: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E
HTTP/1.1", host: "www.tanjiti.com"
2014/12/17 19:43:07 [error] 22252#0: *40847 NAXSI_EXLOG: ip=127.0.0.1&server=www.tanjiti.com&uri=/x
ss.php&id=1008&zone=ARGS&var_name=name&content=<script>alert(1)B</script>, client: 127.0.0.1, serve
r: tanjiti_naxsi, request: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "
www.tanjiti.com"
2014/12/17 19:43:07 [error] 22252#0: *40847 NAXSI_FMT: ip=127.0.0.1&server=www.tanjiti.com&uri=/xss
.php&learning=0&vers=0.53-1&total_processed=1&total_blocked=1&block=1&cscore0=$SQL&score0=4&cscore1
=$XSS&score1=8&zone0=ARGS&id0=1008&var_name0=name, client: 127.0.0.1, server: tanjiti_naxsi, reques
t: "GET /xss.php?name=%3Cscript%3Ealert(1);%3C/script%3E HTTP/1.1", host: "www.tanjiti.com"
"NAXSI_EXLOG: ip=%V&server=%V&uri=%V&id=%d&zone=%s&var_name=%V&content=%V"
an intensive auto-learning phase that will automatically generate whitelisting rules regarding website's behaviour
Why do you keep radically changing learning tools ?Because it's a not-that-easy problem, and we didn't found a satisfying solution yet ;)
以ubuntu上的安装为例apt-get install openjdk-7-jdk
vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 替换成实际的java安装目录source ~/.bashrc
安装成功后,默认的关键路径如下wget -qO - https://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
vim /etc/apt/sources.list添加deb http://packages.elasticsearch.org/elasticsearch/1.4/debian stable mainapt-get updateapt-get install elasticsearchupdate-rc.d elasticsearch defaults 95 10 #加入到系统启动文件
配置(强迫症般的喜欢修改默认端口)二进制文件 /usr/share/elasticsearch/bin
库文件 /usr/share/elasticsearch/lib/配置文件 /etc/elasticsearch/elasticsearch.yml日志文件 /var/log/elasticsearch/工作目录 /tmp/elasticsearch/
启动vim /etc/elasticsearch/elasticsearch.yml
修改端口http.port: 8108
安装管理插件marvel ,然后可以使用可视化界面进行管理/etc/init.d/elasticsearch start
(2)创建索引/usr/share/elasticsearch/bin/plugin -i elasticsearch/marvel/latest
/etc/init.d/elasticsearch restart
(3)修改nxapi全局配置curl -XPUT 'http://localhost:8108/nxapi?pretty'
(4) 安装nxtool.py白名单生成工具vim nxapi.json
修改如下
"elastic" : {
"host" : "127.0.0.1:8108",
"index" : "nxapi",
"doctype" : "events",
"default_ttl" : "7200",
"max_size" : "1000"
},"naxsi" : {
"rules_path" : "/home/work/naxsi_waf/conf/naxsi_core.rules", #naxsi waf的配置路径
"template_path" : [ "tpl/"],
"geoipdb_path" : "nx_datas/country2coords.txt"
},
pip install elasticsearch
安装后重要的文件如下python setup.py build
python setup.py install
/usr/local/nxapi/ #模板tpl文件与geo data文件
/usr/local/bin/nxtool.py #可执行文件
nxtool.py -h
Usage: nxtool.py [options]
Options:
-h, --help show this help message and exit
Configuration options:
-c CFG_PATH, --config=CFG_PATH nxapi配置文件
Path to nxapi.json (config).
--colors Disable output colorz.
Input options (log acquisition):
--files=FILES_IN Path to log files to parse.
--fifo=FIFO_IN Path to a FIFO to be created & read from. [infinite]
--stdin Read from stdin.
--no-timeout Disable timeout on read operations (stdin/fifo).
--syslog Listen on tcp port for syslog logging.
Filtering options (for whitelist generation):
-s SERVER, --server=SERVER
FQDN to which we should restrict operations.
--filter=FILTER This option specify a filter for each type of filter,
filter are merge with existing templates/filters.
(--filter 'uri /foobar')关键选项,在生成白名单的时候使用能提高白名单准确度,支持‘server', 'uri', 'zone', 'var_name', 'ip', 'id', 'content', 'country', 'date', '?server', '?uri', '?var_name', '?content' 关键字
Tagging options (tag existing events in database):
-w WL_FILE, --whitelist-path=WL_FILE
A path to whitelist file, will find matching events in
DB.
-i IPS, --ip-path=IPS
A path to IP list file, will find matching events in
DB.
--tag Actually tag matching items in DB.
Whitelist Generation:
-f, --full-auto Attempt fully automatic whitelist generation process.
-t TEMPLATE, --template=TEMPLATE
Path to template to apply.
--slack Enables less strict mode.
--type Generate whitelists based on param type,参数类型包括空,布尔类型(0,1),颜色(#[0-9a-f]),十六进制[0-9a-f],数字字符[0-9a-z],url,word,数组,文本,
Statistics Generation:
-x, --stats Generate statistics about current's db content.
nxtool.py -c ./nxapi.json -x --colors
# Whitelist(ing) ratio :
# false 79.96 % (total:196902/246244)
# true 20.04 % (total:49342/246244)
# Top servers :
# www.x1.fr 21.93 % (total:43181/196915)
# www.x2.fr 15.21 % (total:29945/196915)
...
# Top URI(s) :
# /foo/bar/test 8.55 % (total:16831/196915)
# /user/register 5.62 % (total:11060/196915)
# /index.php/ 4.26 % (total:8385/196915)
...
# Top Zone(s) :
# BODY 41.29 % (total:81309/196924)
# HEADERS 23.2 % (total:45677/196924)
# BODY|NAME 16.88 % (total:33243/196924)
# ARGS 12.47 % (total:24566/196924)
# URL 5.56 % (total:10947/196924)
# ARGS|NAME 0.4 % (total:787/196924)
# FILE_EXT 0.2 % (total:395/196924)
# Top Peer(s) :
# ...
nxtool.py -c nxapi.json -s www.x1.fr -f --filter 'uri /foo/bar/test' --slack
...
#msg: A generic whitelist, true for the whole uri
#Rule (1303) html close tag
#total hits 126
#content : lyiuqhfnp,+<a+href="http://preemptivelove.org/">Cialis+forum</a>,+KKSXJyE,+[url=http://preemptivelove.org/]Viagra+or+cialis[/url],+XGRgnjn,+http
#content : 4ThLQ6++<a+href="http://aoeymqcqbdby.com/">aoeymqcqbdby</a>,+[url=http://ndtofuvzhpgq.com/]ndtofuvzhpgq[/url],+[link..
#peers : x.y.z.w
...
#uri : /faq/
#var_name : numcommande
#var_name : comment
...
# success : global_rule_ip_ratio is 58.82
# warnings : rule_ip is 10
BasicRule wl:1303 "mz:$URL:/foo/bar/test|BODY";
生成白名单的过程:{ "zone" : "HEADERS", "var_name" : "cookie", "id" : "?"}
{ "whitelisted" : "false" }
第三步:解析tpl模版文件各个字段,合并到es filter中,{ "whitelisted" : "false", "server" : "www.x1.fr" }
第四步:字段取值为?进行扩展{ "whitelisted" : "false", "server" : "www.x1.fr", "zone" : "HEADERS", "var_name" : "cookie" }
{ "whitelisted" : "false", "server" : "www.x1.fr", "zone" : "HEADERS", "var_name" : "cookie", "id" : "1000"} { "whitelisted" : "false", "server" : "www.x1.fr", "zone" : "HEADERS", "var_name" : "cookie", "id" : "1001"}
BasicRule wl:X "mz:$HEADERS_VAR:cookie"; ..