-
-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for 418 for dubious queries #1875
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
/** | ||
* Exception thrown when attacker hits the HoneyPot. | ||
*/ | ||
class HttpHoneyPotException extends HttpException | ||
{ | ||
/** | ||
* Basic constructor. | ||
* | ||
* @param string $path used by the attacker | ||
* @param \Throwable|null $previous exception | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(string $path, \Throwable $previous = null) | ||
{ | ||
parent::__construct(Response::HTTP_I_AM_A_TEAPOT, sprintf('The route %s could not be found.', $path), $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Exceptions\HttpHoneyPotException; | ||
use Illuminate\Routing\Controller; | ||
use function Safe\preg_match; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* This is a HoneyPot. We use this to allow Fail2Ban to stop scanning. | ||
* The goal is pretty simple, if you are hitting this controller, and touch the honey, | ||
* then this means that you have no interest in our pictures. | ||
*/ | ||
class HoneyPotController extends Controller | ||
{ | ||
public function __invoke(string $path = ''): void | ||
{ | ||
// Check if Honey is available | ||
if (config('honeypot.enabled', true) !== true) { | ||
$this->throwNotFound($path); | ||
} | ||
|
||
/** @var array<int,string> $honeypot_paths_array */ | ||
$honeypot_paths_array = config('honeypot.paths', []); | ||
|
||
/** @var array<int,string> $honeypot_xpaths_array_prefix */ | ||
$honeypot_xpaths_array_prefix = config('honeypot.xpaths.prefix', []); | ||
|
||
/** @var array<int,string> $honeypot_xpaths_array_suffix */ | ||
$honeypot_xpaths_array_suffix = config('honeypot.xpaths.suffix', []); | ||
|
||
foreach ($honeypot_xpaths_array_prefix as $prefix) { | ||
foreach ($honeypot_xpaths_array_suffix as $suffix) { | ||
$honeypot_paths_array[] = $prefix . '.' . $suffix; | ||
} | ||
} | ||
|
||
// Turn the path array into a regex pattern. | ||
// We escape . and / to avoid confusions with other regex characters | ||
$honeypot_paths = '/^(' . str_replace(['.', '/'], ['\.', '\/'], implode('|', $honeypot_paths_array)) . ')/i'; | ||
|
||
// If the user tries to access a honeypot path, fail with the teapot code. | ||
if (preg_match($honeypot_paths, $path) !== 0) { | ||
$this->throwTeaPot($path); | ||
} | ||
|
||
// Otherwise just display our regular 404 page. | ||
$this->throwNotFound($path); | ||
} | ||
|
||
/** | ||
* using abort(404) does not give the info which path was called. | ||
* This could be very useful when debugging. | ||
* By throwing a proper exception we preserve this info. | ||
* This will generate a log line of type ERROR. | ||
* | ||
* @param string $path called | ||
* | ||
* @return never | ||
* | ||
* @throws NotFoundHttpException | ||
*/ | ||
public function throwNotFound(string $path) | ||
{ | ||
throw new NotFoundHttpException(sprintf('The route %s could not be found.', $path)); | ||
} | ||
|
||
/** | ||
* Similar to abort(404), abort(418) does not give info. | ||
* It is more interesting to raise a proper exception. | ||
* By having a proper exception we are also able to decrease the severity to NOTICE. | ||
* | ||
* @param string $path called | ||
* | ||
* @return never | ||
* | ||
* @throws HttpHoneyPotException | ||
*/ | ||
public function throwTeaPot(string $path) | ||
{ | ||
throw new HttpHoneyPotException($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* Enable HoneyPot to return 418 when hitting honey. | ||
*/ | ||
'enabled' => true, | ||
|
||
/** | ||
* Honey. | ||
* | ||
* Set of possible path. | ||
* Those will be concatenated into a regex. | ||
*/ | ||
'paths' => [ | ||
'.env', | ||
'.git/config', | ||
'.git/HEAD', | ||
'.well-known/security.txt', | ||
'.well-known/traffic-advice', | ||
|
||
'readme.txt', | ||
'pools', | ||
'pools/default/buckets', | ||
'__Additional', | ||
|
||
'wp-login.php', | ||
'Portal/Portal.mwsl', | ||
'Portal0000.htm', | ||
|
||
'aQQY', | ||
'nmaplowercheck1686252089', | ||
'sdk', | ||
], | ||
|
||
/** | ||
* Because of all the combinations, it is more interesting to do a cross product. | ||
*/ | ||
'xpaths' => [ | ||
'prefix' => [ | ||
'admin', | ||
'base', | ||
'default', | ||
'home', | ||
'indice', | ||
'inicio', | ||
'localstart', | ||
'main', | ||
'menu', | ||
'start', | ||
], | ||
|
||
'suffix' => [ | ||
'asp', | ||
'aspx', | ||
'cgi', | ||
'html', | ||
'jhtml', | ||
'php', | ||
'pl', | ||
'shtml', | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* We don't care for unhandled exceptions in tests. | ||
* It is the nature of a test to throw an exception. | ||
* Without this suppression we had 100+ Linter warning in this file which | ||
* don't help anything. | ||
* | ||
* @noinspection PhpDocMissingThrowsInspection | ||
* @noinspection PhpUnhandledExceptionInspection | ||
*/ | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Http\Response; | ||
use Tests\AbstractTestCase; | ||
|
||
class HoneyPotTest extends AbstractTestCase | ||
{ | ||
public function testRoutesWithHoney(): void | ||
{ | ||
foreach (config('honeypot.paths') as $path) { | ||
$response = $this->get($path); | ||
$this->assertStatus($response, Response::HTTP_I_AM_A_TEAPOT); | ||
$response = $this->post($path); | ||
$this->assertStatus($response, Response::HTTP_I_AM_A_TEAPOT); | ||
} | ||
|
||
// We check one of the version from the xpaths cross product | ||
$response = $this->get('admin.asp'); | ||
$this->assertStatus($response, Response::HTTP_I_AM_A_TEAPOT); | ||
} | ||
|
||
public function testRoutesWithoutHoney(): void | ||
{ | ||
$response = $this->get('/something'); | ||
$this->assertStatus($response, Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
public function testDisabled(): void | ||
{ | ||
config(['honeypot.enabled' => false]); | ||
foreach (config('honeypot.paths') as $path) { | ||
$response = $this->get($path); | ||
$this->assertStatus($response, Response::HTTP_NOT_FOUND); | ||
$response = $this->post($path); | ||
$this->assertStatus($response, Response::HTTP_NOT_FOUND); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really related: We could actually add a security.txt to Lychee to easily allow researchers to report vulns to us.