带分隔符的可解析 NGINX 访问日志文件

Kas*_*bbe 7 nginx logging format parsing

默认的 NGINX 格式是这样的:

log_format combined '$remote_addr - $remote_user [$time_local]  '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';
Run Code Online (Sandbox Code Playgroud)

这有点难以解析。我担心人们会注入"请求、引荐来源或用户代理。

我考虑过使用分隔符,并使用我自己的格式,|P-,|用作分隔符:

log_format parsable '$status |P-,| $time_iso8601 |P-,| $http_host 
|P-,| $bytes_sent |P-,| $http_user_agent |P-,| $http_referer 
|P-,| $request_time |P-,| $request';
Run Code Online (Sandbox Code Playgroud)

但是,没有什么可以阻止用户注入|P-,|他们的请求、引荐来源或用户代理。

我读了这篇关于 ASCII 分隔文本的文章:https : //ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii-delimited-text-not-csv-or-tab-delimited-text/

我认为这可以用来解决这个问题,但用户也可以将 ASCII 分隔符注入他们的数据中。

是否有解决此问题的最佳实践方法?

Ale*_*Ten 14

没有问题。

我担心人们会注入"请求、引荐来源或用户代理。

" 表示为 \x22

要求:

$ curl 'localhost/"?"="' --header 'User-Agent: "'
Run Code Online (Sandbox Code Playgroud)

日志中的行:

[27/Mar/2014:16:14:42 +0400] localhost 127.0.0.1 "GET /\x22?\x22=\x22 HTTP/1.1" 200 "-" "\x22" "-" "/index.html"
Run Code Online (Sandbox Code Playgroud)

更新

来自 nginx 更新日志

nginx 1.1.6 的变化 2011 年 10 月 17 日

*) Change: now the 0x7F-0x1F characters are escaped as \xXX in an
   access_log.
Run Code Online (Sandbox Code Playgroud)

nginx 0.7.0 的变化 2008 年 5 月 19 日

*) Change: now the 0x00-0x1F, '"' and '\' characters are escaped as \xXX
   in an access_log.
   Thanks to Maxim Dounin.
Run Code Online (Sandbox Code Playgroud)