Guest User

Untitled

a guest
May 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <rewrite>
  2. <rules>
  3. <rule name="Ban user-agent RogueBot" stopProcessing="true">
  4. <match url=".*" />
  5. <conditions>
  6. <add input="{HTTP_USER_AGENT}" pattern="RogueBotName" />
  7. <add input="{MyPrivatePages:{REQUEST_URI}}" pattern="(.+)" />
  8. </conditions>
  9. <action type="AbortRequest" />
  10. </rule>
  11. </rules>
  12. <rewriteMaps>
  13. <rewriteMap name="MyPrivatePages">
  14. <add key="/PrivatePage1.aspx" value="block" />
  15. <add key="/PrivatePage2.aspx" value="block" />
  16. <add key="/PrivatePage3.aspx" value="block" />
  17. </rewriteMap>
  18. </rewriteMaps>
  19. </rewrite>
  20.  
  21. public class UserAgentBasedRedirecter : IHttpModule
  22. {
  23. private static readonly Regex _bannedUserAgentsRegex = null;
  24. private static readonly string _bannedAgentsRedirectUrl = null;
  25.  
  26. static UserAgentBasedRedirecter()
  27. {
  28. _bannedAgentsRedirectUrl = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.RedirectUrl"];
  29. if (String.IsNullOrEmpty(_bannedAgentsRedirectUrl))
  30. _bannedAgentsRedirectUrl = "~/Does/Not/Exist.html";
  31.  
  32. string regex = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.UserAgentsRegex"];
  33. if (!String.IsNullOrEmpty(regex))
  34. _bannedUserAgentsRegex = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  35. }
  36.  
  37. #region Implementation of IHttpModule
  38.  
  39. public void Init(HttpApplication context)
  40. {
  41. context.PreRequestHandlerExecute += RedirectMatchedUserAgents;
  42. }
  43.  
  44. private static void RedirectMatchedUserAgents(object sender, System.EventArgs e)
  45. {
  46. HttpApplication app = sender as HttpApplication;
  47.  
  48. if (_bannedUserAgentsRegex != null &&
  49. app != null && app.Request != null && !String.IsNullOrEmpty(app.Request.UserAgent))
  50. {
  51. if (_bannedUserAgentsRegex.Match(app.Request.UserAgent).Success)
  52. {
  53. app.Response.Redirect(_bannedAgentsRedirectUrl);
  54. }
  55. }
  56. }
  57.  
  58. public void Dispose()
  59. { }
  60.  
  61. #endregion
  62. }
  63.  
  64. <configuration>
  65. <appSettings>
  66. <add key="UserAgentBasedRedirecter.UserAgentsRegex" value="^msnbot/1.1" />
  67. </appSettings>
  68. ...
  69. <system.web>
  70. <httpModules>
  71. <add name="UserAgentBasedRedirecter" type="Andies.Web.Traffic.UserAgentBasedRedirecter, Andies.Web" />
  72. </httpModules>
  73. </system.web>
  74. </configuration>
  75.  
  76. SetEnvIf User-Agent ^$ spammer=yes # block blank user agents
  77.  
  78. Order allow,deny
  79. allow from all
  80. deny from env=spammer
Add Comment
Please, Sign In to add comment