2013-04-24 65 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