# ISPProtect BanDaemon 2.0

ISPProtect BanDaemon is a powerful Linux security daemon that monitors log files for brute force attacks and automatically bans offending IP addresses using iptables. Originally designed for ISPConfig integration, this modernized "Next Generation" version supports both standalone and distributed operation across multiple servers.

## Modern Installation (Recommended - systemd)

**Requirements**: systemd-based Linux distributions, PHP 5.6+, MySQL/MariaDB

1. Extract files to temporary directory
2. Run installer as root:
```bash
bash install.sh
```

The new installer provides:
- Full systemd integration with service hardening
- Automatic migration from legacy installations  
- Enhanced security features and resource limits
- Modern PHP and database support
- Comprehensive configuration management

## Legacy Installation (init.d systems)

**For older systems without systemd**:

```bash
bash install_initd.sh
```

Use this method for:
- Older CentOS/RHEL versions (< 7)
- Older Debian/Ubuntu versions (< 15.04)
- Custom systems without systemd

## Post-Installation

### Configuration
Edit `/opt/ispprotect_bandaemon/config.inc.php` to customize:
- Database connection settings
- Iptables configuration (INPUT/PREROUTING, INSERT/APPEND)
- Service monitoring (enable/disable specific services)
- Security features (abuse reporting, evidence collection)
- Email/SMTP settings for notifications

### Service Management

**Modern (systemd)**:
```bash
systemctl start ispprotect_bandaemon    # Start service
systemctl stop ispprotect_bandaemon     # Stop service  
systemctl status ispprotect_bandaemon   # Check status
systemctl enable ispprotect_bandaemon   # Auto-start at boot
journalctl -u ispprotect_bandaemon -f   # View logs
```

**Legacy (init.d)**:
```bash
service ispprotect_bandaemon start      # Start service
service ispprotect_bandaemon stop       # Stop service
service ispprotect_bandaemon status     # Check status
```

### Helper Tools

**systemd_service_helper.sh** - Enhanced service management:
```bash
./systemd_service_helper.sh start|stop|restart|status|logs|health|migrate
```

**ispp_bd_helper.php** - Manual IP management:
```bash
php ispp_bd_helper.php --ban=IP --bantime=3600
php ispp_bd_helper.php --unban=IP
php ispp_bd_helper.php --showbans
php ispp_bd_helper.php --whitelist=IP
```

## Advanced Configuration

### Monitored Services
- **SSH** - Brute force protection
- **Postfix/Dovecot** - Email authentication failures
- **Apache/Nginx** - Web application attacks
- **WordPress/Joomla** - CMS-specific protection
- **DNS (Bind)** - Query flood protection
- **Custom applications** - Configurable regex patterns

### Security Features
- **Evidence Collection** - Automatic log preservation for banned IPs
- **Abuse Reporting** - Integration with whois data for abuse contacts
- **GeoIP Scoring** - Country-based risk weighting
- **IPv6 Support** - Full dual-stack protection
- **Distributed Operation** - Multi-server database sharing

### Database Schema
The daemon automatically creates required tables:
- `auth_fails` - Authentication failure tracking
- `bans` - Active and historical bans
- `whitelist` - Protected IP addresses
- `settings` - Daemon configuration and state
- `logpos` - Log file position tracking

### SMTP Configuration (Optional)
For enhanced email delivery, configure SMTP in `config.inc.php`:
```php
define('EMAIL_SMTP_HOST', 'smtp.example.com');
define('EMAIL_SMTP_PORT', 587);
define('EMAIL_SMTP_USER', 'your_user');
define('EMAIL_SMTP_PASS', 'your_password');
define('EMAIL_SMTP_ENCRYPTION', 'tls');
define('EMAIL_SMTP_AUTH', true);
```

## System Requirements

### Minimum Requirements
- **OS**: Linux with iptables support
- **PHP**: 5.6+ (supports up to PHP 8.x)
- **Database**: MySQL 5.5+ or MariaDB 10.0+
- **Memory**: 64MB RAM minimum
- **Disk**: 100MB free space

### Recommended Requirements  
- **OS**: Modern Linux distribution with systemd
- **PHP**: 7.4+ or 8.x with extensions: PDO, mysqli, pcre
- **Database**: MariaDB 10.3+ or MySQL 8.0+
- **Memory**: 256MB RAM
- **Packages**: php-geoip, geoip-database-contrib

## Network Integration

### Firewall Compatibility
- **iptables** - Native support (IPv4/IPv6)
- **fail2ban** - Can coexist and complement
- **UFW/firewalld** - Works alongside other firewall managers
- **Cloud platforms** - Compatible with AWS, Azure, GCP security groups

### Load Balancer Integration
- Works behind load balancers and reverse proxies
- Supports X-Forwarded-For header analysis
- Can protect multiple backend servers from single installation

## Troubleshooting

### Common Issues
1. **Permission denied**: Ensure script runs as root
2. **Database connection**: Check MySQL credentials and service status
3. **Iptables errors**: Verify iptables is installed and accessible
4. **Log parsing**: Check log file permissions and paths

### Debug Mode
Enable verbose logging in `config.inc.php`:
```php
define('BANDAEMON_VERBOSE', true);
define('BANDAEMON_TESTMODE', true);  // Test without actual bans
```

### Support Resources
- **Log files**: `/var/log/ispprotect_bandaemon.log`
- **Service status**: `systemctl status ispprotect_bandaemon`
- **Configuration**: `/opt/ispprotect_bandaemon/config.inc.php`
- **Database**: Check `ispprotect_bandaemon` database tables

## Migration Guide

### From ISPConfig3 BanDaemon
The installer automatically detects and migrates:
- Configuration files
- Database settings  
- Log positions
- Service definitions

### From Legacy Versions
1. Stop old service: `service ispconfig3_bandaemon stop`
2. Run modern installer: `bash install.sh`
3. Installer handles migration automatically
4. Verify new service: `systemctl status ispprotect_bandaemon`

---

**Copyright**: Originally (c) 2016 pixcept KG, Updated 2025  
**License**: Commercial
**Compatibility**: PHP 5.6+ through PHP 8.x, MySQL/MariaDB, Linux with iptables