2013-04-24 67 views
3

我正在使用.htaccess阻止对我的网站的一些不良请求。一切工作我的本地服务器(XAMPP)在如预期,但是当我上传到主机(共享)我得到这个错误:RewriteCond: bad flag delimiters.htaccess - RewriteCond:坏标记分隔符

下面是在我的.htaccess代码

ServerSignature Off 
Options -Indexes 

RewriteEngine On 

RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK|DEBUG) [NC] 
RewriteRule ^(.*)$ - [F,L] 

RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clsh ttp|loader) [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} (;|<|>|'|"|\)|\(|%0A|%0D|%22|%27|%28|%3C|%3E|%00). *(libwww-perl|wget|python|nikto|curl|scan|java|winhttp|HTTr ack|clshttp|archiver|loader|email|harvest|extract| grab|miner) [NC,OR] 

RewriteCond %{THE_REQUEST} \/\*\ HTTP/ [NC,OR] 
RewriteCond %{THE_REQUEST} etc/passwd [NC,OR] 
RewriteCond %{THE_REQUEST} cgi-bin [NC,OR] 
RewriteCond %{THE_REQUEST} (%0A|%0D) [NC,OR] 
RewriteCond %{THE_REQUEST} \?\ HTTP/ [NC,OR] 

RewriteCond %{QUERY_STRING} ^(.*);(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)\"|\(|\)(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)$ [NC,OR] 
RewriteCond %{QUERY_STRING} \=PHP[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} [NC,OR] 
RewriteCond %{QUERY_STRING} (\.\./|\.\.) [OR] 
RewriteCond %{QUERY_STRING} ftp\: [NC,OR] 
RewriteCond %{QUERY_STRING} http\: [NC,OR] 
RewriteCond %{QUERY_STRING} https\: [NC,OR] 
RewriteCond %{QUERY_STRING} \=\|w\| [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)/self/(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)cPath=http://(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (\<|%3C).*iframe.*(\>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|%3C)([^i]*i)+frame.*(>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR] 
RewriteCond %{QUERY_STRING} base64_(en|de)code[^(]*\([^)]*\) [NC,OR] 
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR] 
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR] 
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR] 
RewriteCond %{QUERY_STRING} (NULL|OUTFILE|LOAD_FILE) [OR] 
RewriteCond %{QUERY_STRING} (\./|\../|\.../)+(motd|etc|bin) [NC,OR] 
RewriteCond %{QUERY_STRING} (localhost|loopback|127\.0\.0\.1) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR] 
RewriteCond %{QUERY_STRING} concat[^\(]*\([NC,OR] 
RewriteCond %{QUERY_STRING} union([^s]*s)+elect [NC,OR] 
RewriteCond %{QUERY_STRING} union([^a]*a)+ll([^s]*s)+elect [NC,OR] 
RewriteCond %{QUERY_STRING} (;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|drop|delete|update|cast|cre ate|char|convert|alter|declare|order|script|set|md 5|benchmark|encode) [NC,OR] 
RewriteCond %{QUERY_STRING} (sp_executesql) [NC] 
RewriteRule ^(.*)$ - [F,L] 

<FilesMatch "^(install\.php|setup\.php|wp-config\.php|config\.php|configuration\.php|php\.in i|php5\.ini|bb-config\.php|db.php)"> 
Order allow,deny 
Deny from all 
</FilesMatch> 

Options -ExecCGI 
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi 
AddType text/plain .pl .cgi .php 
<FilesMatch "\.(php|pl|py|jsp|asp|htm|shtml|sh|cgi)$"> 
ForceType text/plain 
</FilesMatch> 

我已经删除了尾部空格和评论。仍然得到这个错误。

回答

2

有一些(可能)在正则表达式不需要的空间,这是为我工作:

ServerSignature Off 
Options -Indexes 

RewriteEngine On 

RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK|DEBUG) [NC] 
RewriteRule ^(.*)$ - [F,L] 

RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} (;|<|>|'|"|\)|\(|%0A|%0D|%22|%27|%28|%3C|%3E|%00).*(libwww-perl|wget|python|nikto|curl|scan|java|winhttp|HTTrack|clshttp|archiver|loader|email|harvest|extract|grab|miner) [NC,OR] 

RewriteCond %{THE_REQUEST} \/\*\ HTTP/ [NC,OR] 
RewriteCond %{THE_REQUEST} etc/passwd [NC,OR] 
RewriteCond %{THE_REQUEST} cgi-bin [NC,OR] 
RewriteCond %{THE_REQUEST} (%0A|%0D) [NC,OR] 
RewriteCond %{THE_REQUEST} \?\ HTTP/ [NC,OR] 

RewriteCond %{QUERY_STRING} ^(.*);(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)\"|\(|\)(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR] 
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)$ [NC,OR] 
RewriteCond %{QUERY_STRING} \=PHP[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} [NC,OR] 
RewriteCond %{QUERY_STRING} (\.\./|\.\.) [OR] 
RewriteCond %{QUERY_STRING} ftp\: [NC,OR] 
RewriteCond %{QUERY_STRING} http\: [NC,OR] 
RewriteCond %{QUERY_STRING} https\: [NC,OR] 
RewriteCond %{QUERY_STRING} \=\|w\| [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)/self/(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} ^(.*)cPath=http://(.*)$ [NC,OR] 
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (\<|%3C).*iframe.*(\>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|%3C)([^i]*i)+frame.*(>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR] 
RewriteCond %{QUERY_STRING} base64_(en|de)code[^(]*\([^)]*\) [NC,OR] 
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR] 
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR] 
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR] 
RewriteCond %{QUERY_STRING} (NULL|OUTFILE|LOAD_FILE) [OR] 
RewriteCond %{QUERY_STRING} (\./|\../|\.../)+(motd|etc|bin) [NC,OR] 
RewriteCond %{QUERY_STRING} (localhost|loopback|127\.0\.0\.1) [NC,OR] 
RewriteCond %{QUERY_STRING} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR] 
RewriteCond %{QUERY_STRING} concat[^\(]*\([NC,OR] 
RewriteCond %{QUERY_STRING} union([^s]*s)+elect [NC,OR] 
RewriteCond %{QUERY_STRING} union([^a]*a)+ll([^s]*s)+elect [NC,OR] 
RewriteCond %{QUERY_STRING} (;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|drop|delete|update|cast|create|char|convert|alter|declare|order|script|set|md5|benchmark|encode) [NC,OR] 
RewriteCond %{QUERY_STRING} (sp_executesql) [NC] 
RewriteRule ^(.*)$ - [F,L] 

<FilesMatch "^(install\.php|setup\.php|wp-config\.php|config\.php|configuration\.php|php\.ini|php5\.ini|bb-config\.php|db.php)"> 
Order allow,deny 
Deny from all 
</FilesMatch> 

Options -ExecCGI 
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi 
AddType text/plain .pl .cgi .php 
<FilesMatch "\.(php|pl|py|jsp|asp|htm|shtml|sh|cgi)$"> 
ForceType text/plain 
</FilesMatch> 
+0

谢谢! 你们都是对的,但奥拉夫Dietsche更快。 – user1410644 2013-04-30 08:51:23

+1

@ user1410644其实,maxgalbu是第一个,在答案的日期见。 – 2013-04-30 08:58:04

+0

谢谢!诚实的鞠躬! 我修好了! – user1410644 2013-04-30 21:33:22

12

你既然知道这个问题是一个RewriteCond,你可以相当迅速消除一半定位问题的条件,然后测试剩下的。

您可以从删除第一条规则开始。错误依然存在。接下来的步骤是将剩余的RewriteCond s(约20)的前半部分删除并再次测试。继续进行,直到您遇到少数几个RewriteCond s并逐个将其移除。

该错误随着倒数第二个规则而消失。重新插入此规则以验证错误是否再次出现。现在看看

RewriteCond %{QUERY_STRING} (;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|drop|delete|update|cast|cre ate|char|convert|alter|declare|order|script|set|md 5|benchmark|encode) [NC,OR] 

这种情况有什么问题?你看到cre atemd 5的空格吗?

只要您删除这些空格或用反斜杠\转义它们,错误就消失了。

+1

这个答案更详细和有帮助。它实际上帮助我调试我自己的配置,而不是像上面的答案那样修改配置而没有解释。谢谢@OlafDietsche! – splodingsocks 2014-08-28 00:05:38