告訴 nginx 和/或 php *不*解釋每個目錄的指令?

告訴 nginx 和/或 php *不*解釋每個目錄的指令?

我正在測試我的新網站,並使用 weevely 產生 php 有效負載。我直接手動將其放入圖像中,用戶可以將圖像上傳到我的網站。我能夠與 中的 weevely 有效負載建立反向連線/images。是否可以告訴 nginx 和/或 php不是解釋來自 /images 目錄的命令?除了使用檔案驗證正確[1]編碼安全輸入機制之外,我們還能做些什麼來防止有效負載在特定(/images)目錄中執行?

[1]https://php.earth/doc/security/uploading

user www-data;
worker_processes  auto;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    charset utf-8;
    server_tokens  off;

    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Frame-Options DENY;
    add_header Referrer-Policy "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Pragma public;
    add_header Cache-Control "public";

    include /etc/nginx/conf.d/*.conf;

    gzip on;
    gzip_comp_level 2;
    gzip_min_length 1000;

    server {
    listen 127.0.0.1:80;
    server_name website.com;
    root /var/www/website/;
    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
    }

    location ~* .(png|ico|gif|jpg|jpeg|css|html|txt|php)$ {
        expires 2d;
            add_header Pragma public;
        add_header Cache-Control "public";
    }

    if ($request_method !~ ^(GET|POST)$) {
         return 444;
    }
    }
}

答案1

問題不清楚。 「解釋命令」是什麼意思? Nginx 為其他伺服器或服務(例如 PHP)提供檔案和代理請求。 PHP 運行腳本。

我認為您希望 images 目錄只提供文件,而不是運行 PHP 腳本。

我已經添加了快取標頭。 Pragma 已經很舊了,你不需要使用它。

location \images
   root \whatever;
   add_header Cache-Control "public, max-age=691200, s-maxage=691200";
   more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires";
}

或者,您可以使用類似答案的內容這個問題:

location ~ /images/(.+)\.php$ {
  deny all;
}
location ~ \.php$ {
  // as above
}

相關內容