skater's avatar

Best way to handle tons of "hacking attempts" that generates 404

Hi:

I have tons and tons of requests as hacking attempts ... for example, taking the last block today I have requests to:

/filemanager/dialog.php
/asset/filemanager/dialog.php
/assets/filemanager/dialog.php
/server/php/
/fileupload/server/php/
/admin/server/php/
/upload/server/php/
/wp-includes/wlwmanifest.xml
/feed/
/wp-content/plugins/wpdiscuz/themes/default/style-rtl.css
/wp-content/plugins/fancy-product-designer/inc/custom-image-handler.php
/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php
/wp-content/langar.php
/wp-includes/small.php
/wp-includes/lfx.php
/wp-content/mu-plugins/db-safe-mode.php
/wp-content/wp-old-index.php?action=login&pass=-1&submit=
/wp-content/plugins/wpconfig.bak.php?act=sf
/wp-includes/wpconfig.bak.php?act=sf
/wp-content/plugins/ubh/up.php
/wp-content/plugins/config.bak.php
/wp-content/themes/config.bak.php
/wp-includes/config.bak.php
/wp-content/config.bak.php
/wp-admin/config.bak.php
/wp-includes/css/css.php
/wp-includes/fonts/css.php
/wp-content/wp-1ogin_bak.php
/wp-content/plugins/ioptimization/IOptimize.php?rchk=
/wp-content/db_cache.php
/wp-content/plugins/backup_index.php

The problem is that I have a component to get notifies each 404 appears, so I can be aware of these issues to handle and solve them (in case a broken link, a mistake coding, etc ...)

But I want to get rid of these notifications since they are false positives.

What is the best method to handle this ?

  1. create Routes to null pages ?

  2. redirect all of them in .htaccess to Wikipedia page :-D ?

  3. any module (with updates) that handle these kind of annoying requests ?

  4. other ?

0 likes
18 replies
Sinnbeck's avatar

Maybe try getting the user agent. Perhaps you can filter them by that

skater's avatar

@Sinnbeck Hackers are more clever than that ...

Last browsers used:

BLEXBot 1.0
CFNetwork
Chrome 106.0.0
Chrome 34.0.1866
Chrome 39.0.2171
Chrome 78.0.3904
Chrome 81.0.4044
Chrome 86.0.4240
Chrome 88.0.4240
Chrome 89.0.4389
Chrome 90.0.4430
Chrome 95.0.4638
IE 11.0
Other
Python Requests 2.27
curl 7.3.2
Sinnbeck's avatar

@skater yeah it's tricky to get zero false positives. It was to filter at least some. Another idea is to embed Javascript into the page that triggers the logger endpoint. This should filter those not reading Javascript

Or worst case add a button "report broken link"

skater's avatar

@Sinnbeck I think you didn't understand the question. My problem is not detect a broken link (no need to add a button).

My problem is to handle all false positives 404 that are really hacking attempts. And it's a problem because I have a component that alerts me any 404 happening in the page (since it's very important to know where and when it's a 404 to solve it asap)

Sinnbeck's avatar

@skater is it because you want a different handler for 404 caused by urls that doesn't match anything? If that's the case, add this at the bottom of the web.php

Route::fallback(function () {
    return 'piss off hacker';
});
skater's avatar

@Sinnbeck with that fallback, if my content manager fails in some content and wrote an incorrect link, I will never notice it

Sinnbeck's avatar

@skater yeah if it's not just due to a wrong ID. In that case trying to filter false positives is the best idea. For that I suggested 2 ideas.

kokoshneta's avatar

Do you have WordPress installed on the server? If not, you can make a route for anything beginning with /wp- to get rid of at least a good part of it:

Route::get('/wp-{dir}/{any?}', function ($dir, $any = NULL) {
    return 'piss off hacker';
})->where(['any' => '.*']);
skater's avatar

@kokoshneta Thank you !

Wordpress is about 20% of all requests ... I have a list of about 650 different requests of hacking attempts ... My question is about what is the BEST way to handle this ... is creating a 650 rules set the best ?

Snapey's avatar

@skater Perhaps you can whitelist your own URLs that should be notified. Assuming that they all start with some recognisable pattern.

The other option is to use cloudflare. Let them filter the known test urls.

skater's avatar

@Snapey Again ... with a whitelisting, if my content manager writes /kontact-us insted of /contact-us, I will never notice of that.

That's the reason of my notification system of all 404 ... because I REALLY NEED to know about them

But in this case, I have tons of hacking attempts, merged with the 404 I want to get notified.

My question is if it's optimal to create 650 laravel routes ... I think it's not, that's why I'm asking other proposals ;-)

Sinnbeck's avatar

@skater no its not. But do they start with 650 different things? Or are they grouped like wp-?

Route::get('/vendor/{any?}', function ($any = NULL) {
    return 'piss off hacker';
})->where(['any' => '.*']); 
skater's avatar

@Sinnbeck Thay are completely different ... maybe I can group some of them ... but literally have houndreds of different strings...

kokoshneta's avatar
Level 27

@skater So the only ones you really want to actually know about are ones where someone clicks on a page on your site that takes them to a 404 page?

Say if a content manager in a published post writes <a href="/kontact-us">Contact us</a> with a typo, and some user then clicks on the link in that post, then you want to be notified – but if someone opens their browser and types in www.yoursite.com/rbadudosbds, you don’t want to be notified?

If so, you can take advantage of the fact that most normal user agents set the HTTP_REFERER header when you click on a link, sending the URL of the page the user came from along with the request. You can then set a fallback route and only return a 404 if the HTTP referer is your own app:

Route::fallback(function(Request $request) {
	$referrer = parse_url($request->headers->get('referer'));
	$referrer = $referrer['scheme'] . '://' . $referrer['host'];

	if ($referrer == config('app.url')) {
		abort(404);
	} else {
		return 'piss off hacker';
	}
}

Even better, if you can manually trigger your notification plugin directly, you could disable automatic notification on all 404s and instead do this:

Route::fallback(function(Request $request) {
	$referrer = parse_url($request->headers->get('referer'));
	$referrer = $referrer['scheme'] . '://' . $referrer['host'];

	if ($referrer == config('app.url')) {
		notify404($request->path());
	}

	abort(404);
}

That way, everyone gets the correct 404 page, but you only get notified if the request has a referrer that matches your app URL.

1 like
Sinnbeck's avatar

@kokoshneta as I understand it, the content manager types urls manually

my content manager writes /kontact-us insted of /contact-us, I will never notice of that.

I don't know why this needs to be logged, but it does as I understand it

kokoshneta's avatar

@Sinnbeck I think they mean that if a content manager types a URL that points to a nonexistent location (in the same app) as the href parameter to a link on a public-facing page in the app, then they want to be notified when someone clicks on that link and gets a 404 page. Such a notification will then alert the developer to a potential problem so they can check what the endpoint is and where the manually typed link to it is, so that they can get it fixed as quickly as possible.

Though it may be that you’re right, and they’re actually looking for a way to log a content manager typing the incorrect URL into the address bar in their browser and getting a 404. If so, I doubt there’s any way to filter out human-typed URLs from bot-/spammer-followed URLs.

Snapey's avatar

@skater You could have an ignore list in your 404 handler. This could be a plain text file that you maintain with known attack urls. Before reporting the 404, check if the URL is in that file. If so, just drop the 404.

However, it does seem like whack-a-mole.

@kokoshneta has a good idea with the referrer.

1 like

Please or to participate in this conversation.