If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Blocking Malicious URLs in htaccess

Started by MashaMarkoma, Aug 16, 2024, 01:38 AM

Previous topic - Next topic

MashaMarkomaTopic starter

I need some help; many users commonly attempting to discover weaknesses in the system by inputting commands like this:

https://site.ком/enter/../../../../script.14.13425.5255225.5/. ./.goal.php and so on.

Can we block that access (403 error) for these type of requests using htaccess?

For instance, if there are unnecessary slashes or sequences like /../ in the request.

What would be the best way to set this up?
  •  


sonyrobin

To block those kinds of requests using `.htaccess`, you can use some mod_rewrite rules. This will allow you to intercept any URL patterns that attempt directory traversal or contain unnecessary slashes.

Here's a way you can set this up:

1. First, you need to make sure that mod_rewrite is enabled on your server. You can usually check this in your server's configuration files, or contact your hosting provider.

2. Open or create the `.htaccess` file in the root directory of your website.

3. Add the following rules to the `.htaccess` file:

RewriteEngine On

# Block requests with multiple consecutive slashes
RewriteCond %{REQUEST_URI} ^//+ [OR]
RewriteCond %{REQUEST_URI} /../
RewriteRule ^ - [F,L]

# Block specific patterns like script or goal
RewriteCond %{REQUEST_URI} (script|goal) [NC]
RewriteRule ^ - [F,L]


Let's break down what's happening here:

- The `RewriteEngine On` line turns on the mod_rewrite engine.
- The first block deals with URLs that have multiple slashes or the sequence `/../`. If the request URI matches these patterns, it will return a 403 Forbidden error `(F)`.
- The second block checks if the request URI contains words like "script" or "goal". If it does, it also returns a 403 Forbidden error.

After you save the changes, test it by trying to access some URLs with those patterns. You should get a 403 error if everything is set up correctly.

It's also smart to keep an eye on your server's logs to see if any unauthorized attempts are being made so you can update the rules accordingly.
  •  

Wielerog

When .htaccess is present, it indicates the use of Apache server.

To protect against malicious requests, there is a specific module called modsecurity which provides WAF (Web Application Firewall) functionality for Apache.

However, it's important to pay close attention to how settings are adjusted!

Subsequently, you must also set up fail2ban to monitor the error.log file, ensuring that any IPs flagged by modsecurity are banned right away, like for a month or so! Implementing these safeguards is critical for enhancing the security of your web applications. Regularly updating and auditing your configurations will also help maintain a robust defense against cyber threats.
  •  

Bexigefep

It's important to make sure that the POST method is limited to specific URIs where it's actually needed. This helps to maintain security and reduces unnecessary risks. Furthermore, with mod_rewrite, we should permit query parameters in URLs only for those instances where they are actually included, following the format like /LINK/?query-parameter.

By setting certain conditions, we can ensure that if there is any query string present, we check the incoming request. If the request isn't a GET or HEAD method for the permitted URI with the allowed query string, then we can block that request.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...