laravel-security maintained by ozankurt
Web Application Firewall (WAF) package for Laravel
This package intends to protect your Laravel app from different type of attacks such as XSS, SQLi, RFI, LFI, User Agent, and a lot more. It will also block repeated attacks and send notification via email and/or slack when attack is detected. Furthermore, it will log failed logins and block the IP after a number of attempts.
Note: Some middleware classes (i.e. Xss) are empty as the Middleware abstract class that they extend does all of the job, dynamically. In short, they all works ;)
Getting Started
1. Install
Run the following command:
composer require ozankurt/laravel-security
2. Publish
Publish configuration, language, and migrations
php artisan vendor:publish --tag=security
3. Database
Create db tables
php artisan migrate
4. Configure
You can change the security settings of your app from config/security.php file
Usage
Middlewares are already defined so should just add them to routes. The firewall.all middleware applies all the middlewares available in the all_middleware array of config file.
Route::group(['middleware' => 'firewall.all'], function () {
Route::get('/', 'HomeController@index');
});
You can apply each middleware per route. For example, you can allow only whitelisted IPs to access admin:
Route::group(['middleware' => 'firewall.whitelist'], function () {
Route::get('/admin', 'AdminController@index');
});
Or you can get notified when anyone NOT in whitelist access admin, by adding it to the inspections config:
Route::group(['middleware' => 'firewall.url'], function () {
Route::get('/admin', 'AdminController@index');
});
Available middlewares applicable to routes:
firewall.all
firewall.agent
firewall.bot
firewall.geo
firewall.ip
firewall.lfi
firewall.php
firewall.referrer
firewall.rfi
firewall.session
firewall.sqli
firewall.swear
firewall.url
firewall.whitelist
firewall.xss
firewall.keyword
You may also define routes for each middleware in config/security.php and apply that middleware or firewall.all at the top of all routes.
Notifications
Firewall will send a notification as soon as an attack has been detected. Emails entered in notifications.email.to config must be valid Laravel users in order to send notifications. Check out the Notifications documentation of Laravel for further information.
Dashboard
In order to view the dashboard, you must enable it in your AppServiceProvider:
use App\Models\User;
use Illuminate\Support\Facades\Gate;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Gate::define('viewSecurityDashboard', function (?User $user) {
return $user?->id === 1;
});
// ...
}
.env Variables
FIREWALL_ENABLED=true
FIREWALL_WHITELIST="127.0.0.0/24"
FIREWALL_DASHBOARD_ENABLED=true
FIREWALL_DB_CONNECTION="${DB_CONNECTION}"
FIREWALL_DB_PREFIX=security_
FIREWALL_CRON_ENABLED=false
FIREWALL_CRON_EXPRESSION="* * * * *"
FIREWALL_NOTIFICATIONS_ATTACK_DETECTED_ENABLED=false
FIREWALL_NOTIFICATIONS_SECURITY_REPORT_ENABLED=false
FIREWALL_NOTIFICATIONS_SUCCESSFUL_LOGIN_ENABLED=false
FIREWALL_NOTIFICATION_CHANNELS_EMAIL_ENABLED=false
FIREWALL_NOTIFICATION_CHANNELS_EMAIL_NAME="${MAIL_FROM_NAME}"
FIREWALL_NOTIFICATION_CHANNELS_EMAIL_FROM="${MAIL_FROM_ADDRESS}"
FIREWALL_NOTIFICATION_CHANNELS_EMAIL_TO="webmaster@example.com"
FIREWALL_NOTIFICATION_CHANNELS_EMAIL_QUEUE=default
FIREWALL_NOTIFICATION_CHANNELS_SLACK_ENABLED=false
FIREWALL_NOTIFICATION_CHANNELS_SLACK_EMOJI=":fire:"
FIREWALL_NOTIFICATION_CHANNELS_SLACK_FROM="Laravel Security"
FIREWALL_NOTIFICATION_CHANNELS_SLACK_TO= # webhook url
FIREWALL_NOTIFICATION_CHANNELS_SLACK_CHANNEL=null
FIREWALL_NOTIFICATION_CHANNELS_SLACK_QUEUE=default
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_ENABLED=false
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_WEBHOOK_URL=
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_QUEUE=default
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_FROM="Laravel Security"
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_FROM_IMG=https://ozankurt.com/laravel-security.png
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_ROUTE=
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_TITLE="Attack Detected"
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_FOOTER="Laravel Security"
FIREWALL_NOTIFICATION_CHANNELS_DISCORD_FOOTER_IMG=https://ozankurt.com/laravel-security.png
FIREWALL_MIDDLEWARE_IP_ENABLED=true
FIREWALL_MIDDLEWARE_AGENT_ENABLED=true
FIREWALL_MIDDLEWARE_BOT_ENABLED=true
FIREWALL_MIDDLEWARE_GEO_ENABLED=true
FIREWALL_MIDDLEWARE_LFI_ENABLED=true
FIREWALL_MIDDLEWARE_LOGIN_ENABLED=true
FIREWALL_MIDDLEWARE_PHP_ENABLED=true
FIREWALL_MIDDLEWARE_REFERRER_ENABLED=true
FIREWALL_MIDDLEWARE_RFI_ENABLED=true
FIREWALL_MIDDLEWARE_SESSION_ENABLED=true
FIREWALL_MIDDLEWARE_SQLI_ENABLED=true
FIREWALL_MIDDLEWARE_SWEAR_ENABLED=true
FIREWALL_MIDDLEWARE_URL_ENABLED=true
FIREWALL_MIDDLEWARE_WHITELIST_ENABLED=true
FIREWALL_MIDDLEWARE_XSS_ENABLED=true
FIREWALL_MIDDLEWARE_KEYWORD_ENABLED=true
Changelog
Please see Releases for more information on what has changed recently.
Contributing
Pull requests are more than welcome. You must follow the PSR coding standards.
Security
Please review our security policy on how to report security vulnerabilities.
Credits
Todo
- logs/ips datatable
- ip by country breakdown -> datatable + chart
- type of attack breakdown -> datatable + chart
License
The MIT License (MIT). Please see LICENSE for more information.