1. 程式人生 > 資料庫 >nginx 對於post,get引數訪問做xss,sql注入過濾

nginx 對於post,get引數訪問做xss,sql注入過濾

現在很多基於百度的nginx 防止sql注入都是get方式,如果post就沒有了.

坑點:

1.$query_string  獲取get請求的資料

2.$request_body 獲取post請求的資料,但是這裡如果對$request_body進行校驗,則為空!!!!!!!!!!  所以這個方式不可行.

3.在網上找到,通過另外一種方式來獲取請求資料.openresty.下面就來說一說如何操作.

1.環境:

    1.1 作業系統  windows 10

    1.2  下載可以獲取請求引數基於windows下的nginx(模組自帶nginx)

    

 

2.編寫  nginx.conf 配置檔案(./openresty-1.19.3.1-win64/conf/nginx.conf)

   

 

 下面是原文

server {
    #nginx 監聽埠
    listen 80;
    #nginx 服務名稱
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
#root html;
#index index.html index.htm;

    default_type text/html;
#開啟 lua 允許使用這種方式來獲取請求資料
    lua_need_request_body on;
    access_by_lua_block {
#獲取 post請求資料
        local body = ngx.var.request_body
#獲取 get請求資料
        local query = ngx.var.query_string
#正則表示式
        local regex = "(.*?((select)|(from)|(count)|(delete)|(update)|(drop)|(truncate)).*?){1,}"
#匹配post引數
        local m,err = ngx.re.match(body, regex)
#匹配get引數
        local n,err = ngx.re.match(query, regex)
#做get或者post校驗
        if m then
            #返回資料 (二選一)
            ngx.say('{"code": 999,"msg": "傳參異常","ok": false,"runningTime": "0ms"}')
            #返回頁面
            #ngx.exit(404)
        end
    }
#設定日誌
access_log logs/host.access.log json_log;
#轉換應用服務ip和埠
proxy_pass http://127.0.0.1:8081;
}

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}    

  效果:

 

 

 

 

 

 

  

 希望大家互相學習.