Skip to content

๐Ÿš€ Firewall Rule Optimizer is an AI-powered tool designed to simplify ๐Ÿ”ง and optimize โš™๏ธ your firewall rule management. ๐Ÿ“Š Large-scale configurations often get messy, redundant ๐Ÿ”, and tough to handle.๐Ÿ’ก This tool brings clarity, speed, and intelligence to your firewall strategy ๐Ÿ”

License

Notifications You must be signed in to change notification settings

Dev-axay18/firewall-rule-optimizer

Repository files navigation

AI-Powered Firewall Rule Optimizer Banner

Live Demo

๐Ÿ”ฅ AI-Powered Firewall Rule Optimizer and Visualizer

A comprehensive Python-based tool that analyzes Linux iptables firewall rules, detects redundant or conflicting rules, suggests optimization strategies, and provides interactive visualizations with beautiful CLI graphics for better security management.

๐ŸŽฏ Features

๐Ÿ” Analysis Capabilities

  • Redundant Rule Detection: Identifies duplicate rules that can be removed
  • Conflict Resolution: Finds rules that contradict each other
  • Performance Optimization: Suggests rule reordering for better efficiency
  • Security Analysis: Detects potential security vulnerabilities
  • Unreachable Rule Detection: Finds rules that will never be executed

๐Ÿ’ก Intelligent Recommendations

  • Priority-based Suggestions: Categorizes recommendations by importance (๐Ÿ”ฅCritical, ๐ŸšจHigh, โš ๏ธMedium, ๐Ÿ’กLow)
  • Risk Assessment: Evaluates the risk level of each recommendation
  • Impact Analysis: Estimates performance and security improvements
  • Implementation Guidance: Provides step-by-step instructions

๐ŸŽจ Beautiful CLI Graphics

  • Colorful Progress Bars: Visual score representation for security and efficiency
  • ASCII Bar Charts: Issue distribution visualization
  • Severity Pie Charts: Visual breakdown of issue severity levels
  • Priority Charts: Recommendation priorities with icons and colors
  • Impact Gauges: Visual optimization benefits display
  • Professional Styling: VS Code-like syntax highlighting with emojis

๐Ÿ“Š Advanced Visualizations

  • Interactive Rule Flow Diagrams: Visualize packet flow through rules
  • Dependency Graphs: Show relationships between chains and rules
  • Security Dashboards: Real-time security and efficiency scores
  • Rule Coverage Heatmaps: Analyze protocol and port coverage
  • Optimization Impact Charts: Visualize potential improvements

๐Ÿ–ฅ๏ธ Multiple Interfaces

  • Enhanced CLI: Beautiful terminal interface with charts and colors
  • Web Interface: User-friendly Streamlit-based GUI with interactive features
  • Python API: Integrate into your own applications

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • Linux system with iptables (for live analysis)
  • Administrator privileges (for applying changes)

Installation

  1. Clone the repository:
git clone https://github.com/your-username/firewall-optimizer.git cd firewall-optimizer
  1. Install dependencies:
pip install -r requirements.txt

๐ŸŽฏ Instant Usage (No Configuration Required!)

๐Ÿ” Analyze sample firewall rules with beautiful CLI graphics

python main.py analyze

โšก Generate optimization recommendations with visual charts

python main.py optimize

๐Ÿ“Š Create interactive visualizations

python main.py visualize

๐ŸŒ Launch web interface

python main.py webapp

That's it! The tool includes sample data, so you can start exploring immediately!

๐ŸŽจ CLI Interface Features

Visual Elements

Our enhanced CLI provides a beautiful terminal experience with:

  • ๐Ÿ”ฅ Colorful Startup Banner with ASCII art
  • ๐Ÿ“Š Progress Bars for security and efficiency scores
  • ๐Ÿ“ˆ Bar Charts showing issue distribution
  • ๐Ÿฅง Pie Charts for severity level breakdown
  • ๐ŸŽฏ Priority Visualization for recommendations
  • โšก Impact Gauges showing optimization benefits
  • ๐ŸŒˆ Color-coded Output for easy reading

Sample CLI Output

 โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•— โ•‘ โ•‘ โ•‘ ๐Ÿ”ฅ AI-Powered Firewall Rule Optimizer ๐Ÿ”ฅ โ•‘ โ•‘ โ•‘ โ•‘ Analyze โ€ข Optimize โ€ข Visualize โ€ข Secure โ•‘ โ•‘ โ•‘ โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๐Ÿ“Š Score Overview โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Security Score.......... โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 54.0% Efficiency Score........ โ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 5.0% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๐Ÿ“Š Issues Distribution โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Conflicting............. โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ 15 Unreachable............. โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ 13 Inefficient Order....... โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ 11 Security Risk........... โ–ˆโ–ˆโ–ˆโ–ˆ 2 

Console Output


๐Ÿง  Console Output

๐ŸŒ Web Dashboard Preview

๐Ÿ“Š Run in your browser: python main.py webapp

๐Ÿ“ท Screenshot Samples


๐Ÿง  AI-Powered Analysis Dashboard


๐Ÿ“ˆ Real-time Visualizations of Issues and Priorities


๐ŸŒ Rule Flow Diagrams & Dependency Mapping


๐ŸŒ Visualization


๐Ÿ“‹ Complete Command Reference

๐Ÿ” Analysis Commands

Basic analysis with beautiful graphics

python main.py analyze

Analyze specific file

python main.py analyze --input /path/to/iptables-rules.txt

Analyze current system rules (Linux)

python main.py analyze --input system

Save results to file

python main.py analyze --output report.json --format json

โšก Optimization Commands

# Generate recommendations with visual priority charts python main.py optimize # Optimize specific configuration python main.py optimize --input /path/to/rules.txt # Save optimized rules python main.py optimize --input rules.txt --output optimized.txt # Apply to system with backup (requires root) sudo python main.py optimize --input system --apply --backup

๐Ÿ“Š Visualization Commands

# Create all interactive visualizations python main.py visualize # Specific visualization types python main.py visualize --type flow # Rule flow diagram python main.py visualize --type issues # Issues dashboard  python main.py visualize --type graph # Network topology

๐ŸŒ Web Interface

# Launch web app (default: localhost:8501) python main.py webapp # Custom port and host python main.py webapp --port 8080 --host 0.0.0.0

๐Ÿ’พ Backup & Restore

Create backup

python main.py backup --input system --description "Before optimization"

Restore from backup

python main.py restore --backup /path/to/backup.json

๐Ÿ“– Usage Guide

๐ŸŒ Web Interface

Launch the interactive web interface:

python main.py webapp

Then open your browser to http://localhost:8501

Features:

  • ๐Ÿ“ Upload firewall configuration files or use sample data
  • ๐Ÿ” Real-time analysis with colorful charts and metrics
  • ๐Ÿ“Š Interactive visualizations and dashboards
  • ๐Ÿ’พ Backup and restore functionality
  • โš™๏ธ Configurable analysis settings
  • ๐Ÿ“ค Export reports in multiple formats

๐Ÿ’ป Enhanced Command Line Interface

Our CLI features beautiful terminal graphics with:

  • ๐ŸŽจ VS Code-like syntax highlighting with colors
  • ๐Ÿ“Š ASCII progress bars and charts
  • ๐Ÿ”ฅ Emoji-rich output for better readability
  • ๐Ÿ“ˆ Visual data representation right in your terminal

For complete command documentation, see COMMANDS.md - a comprehensive guide with:

  • ๐Ÿ“‹ All available commands and options
  • ๐Ÿ’ก Usage examples and best practices
  • ๐ŸŽจ CLI features and visual elements
  • ๐Ÿ”ง Advanced configuration options
  • ๐Ÿšจ Safety features and troubleshooting

๐Ÿ’ป Command Line Interface

Analyze Firewall Rules

# Analyze a configuration file python main.py analyze --input /etc/iptables/rules.v4 # Analyze current system rules python main.py analyze --input system # Save analysis to file python main.py analyze --input rules.txt --output analysis.json --format json

Generate Optimization Recommendations

# Generate recommendations python main.py optimize --input rules.txt # Generate and save optimized rules python main.py optimize --input rules.txt --output optimized_rules.txt # Apply optimizations (with backup) python main.py optimize --input system --apply --backup

Create Visualizations

Create comprehensive visualization report

python main.py visualize --input rules.txt --output ./reports

Create specific visualization types

python main.py visualize --input rules.txt --output ./reports --type flow

Backup and Restore

Create backup

python main.py backup --input system --description "Before optimization"

Restore from backup

python main.py restore --backup backup_20250126_143022.json --apply

๐Ÿ Python API

fromoptimizerimport ( IptablesParser, FirewallAnalyzer, FirewallRecommender, FirewallVisualizer ) # Initialize componentsparser=IptablesParser() analyzer=FirewallAnalyzer() recommender=FirewallRecommender() visualizer=FirewallVisualizer() # Load and parse configurationwithopen('rules.txt', 'r') asf: rules_content=f.read() config=parser.parse_iptables_save(rules_content) # Analyze configurationanalysis=analyzer.analyze_configuration(config) print(f"Security Score: {analysis.security_score:.1f}/100") print(f"Issues Found: {len(analysis.issues)}") # Generate recommendationsplan=recommender.generate_recommendations(config, analysis) print(f"Recommendations: {len(plan.recommendations)}") # Create visualizationsflow_chart=visualizer.create_rule_flow_diagram(config) dashboard=visualizer.create_issue_dashboard(analysis) # Show interactive plotsflow_chart.show() dashboard.show()

๏ฟฝ Documentation & Help

๐Ÿ“– Complete Command Reference

  • COMMANDS.md - Comprehensive command documentation with examples
  • Use python main.py --help for general help
  • Use python main.py <command> --help for command-specific help

๐ŸŽจ What Makes Our CLI Special

  1. Beautiful Visual Output: ASCII progress bars, charts, and colored text
  2. Instant Feedback: No configuration needed - works with sample data
  3. Professional Styling: VS Code-like syntax highlighting with emojis
  4. Smart Graphics: Charts adjust to terminal width automatically
  5. Cross-Platform: Works on Windows, macOS, and Linux
  6. Comprehensive: Analysis, optimization, visualization, and web interface

๐Ÿ”ฅ Key Features Highlights

๐ŸŽฏ Smart Analysis

  • Detects 47+ different types of firewall issues
  • Provides actionable recommendations for each issue
  • Calculates security and efficiency scores
  • Identifies unreachable and redundant rules

โšก Visual Performance

  • Beautiful terminal graphics without external dependencies
  • Real-time progress indicators during analysis
  • Color-coded severity levels (๐Ÿ”ดCritical, ๐ŸŸกMedium, ๐ŸŸขLow)
  • Interactive web charts with Plotly integration

๐Ÿ›ก๏ธ Enterprise-Ready

  • Automatic backup creation before any changes
  • Dry-run mode by default (no accidental modifications)
  • Comprehensive logging and audit trails
  • Linux system integration with iptables

๏ฟฝ๐Ÿ“‚ Project Structure

firewall-optimizer/ โ”‚ โ”œโ”€โ”€ data/ # Sample data and test files โ”‚ โ””โ”€โ”€ sample_rules.txt # Sample iptables rules โ”‚ โ”œโ”€โ”€ optimizer/ # Core optimizer package โ”‚ โ”œโ”€โ”€ __init__.py # Package initialization โ”‚ โ”œโ”€โ”€ parser.py # Iptables rule parser โ”‚ โ”œโ”€โ”€ analyzer.py # Rule analysis engine โ”‚ โ”œโ”€โ”€ recommender.py # Optimization recommender โ”‚ โ”œโ”€โ”€ visualizer.py # Visualization components โ”‚ โ””โ”€โ”€ utils.py # Utility functions โ”‚ โ”œโ”€โ”€ web_ui/ # Streamlit web interface โ”‚ โ””โ”€โ”€ app.py # Main web application โ”‚ โ”œโ”€โ”€ main.py # Command line interface โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ README.md # This file โ””โ”€โ”€ .github/ โ””โ”€โ”€ copilot-instructions.md # Copilot customization 

๐Ÿ”ง Configuration

The optimizer can be configured through YAML configuration files:

# optimizer_config.yamlbackup: enabled: truedirectory: './backups'max_backups: 10auto_backup_before_changes: trueanalysis: check_redundant_rules: truecheck_conflicting_rules: truecheck_rule_ordering: truecheck_unreachable_rules: truecheck_security_issues: trueconfidence_threshold: 0.8security: allow_system_modifications: falserequire_sudo_confirmation: truevalidate_rules_before_apply: truevisualization: default_theme: 'plotly_white'save_format: 'html'include_interactive: true

๐Ÿ“Š Example Analysis Output

FIREWALL ANALYSIS RESULTS ============================================================ Overall Scores: Security Score: 78.5/100 Efficiency Score: 85.2/100 Statistics: Total Rules: 23 Total Chains: 6 Total Tables: 3 Accept Rules: 8 Drop Rules: 12 Reject Rules: 1 Issues Found (4): Redundant (2 issues): โ€ข Redundant rule found: duplicate of rule at line 15 โ†’ Remove the duplicate rule to improve performance โ€ข Redundant rule found: duplicate of rule at line 22 โ†’ Remove the duplicate rule to improve performance Security Risk (1 issues): โ€ข Administrative port 22 open to all sources โ†’ Restrict access to administrative ports to specific source IPs Inefficient Order (1 issues): โ€ข Specific rule at line 18 comes after general rule at line 12 โ†’ Move more specific rules before general ones for better performance 

๐Ÿ›ก๏ธ Security Considerations

โš ๏ธImportant Safety Features

  • Dry Run Mode: All operations default to dry run mode
  • Automatic Backups: Creates backups before any modifications
  • Rule Validation: Validates rules before applying changes
  • Confirmation Prompts: Requires explicit confirmation for system changes
  • Rollback Capability: Can restore from backups if needed

๐Ÿ”’ Best Practices

  1. Always test in dry run mode first
  2. Create backups before making changes
  3. Validate optimized rules in a test environment
  4. Review all recommendations before applying
  5. Keep the original configuration as a backup

๐Ÿ”ฌ Example Optimizations

Issue TypeBeforeAfterImpact
Redundancy-A INPUT -p tcp --dport 22 -j ACCEPT (duplicated)Single occurrenceReduced rule count
ConflictAllow port 80, then deny port 80Resolved based on policyPredictable behavior
InefficiencyBroad ACCEPT before specific DROPReordered for securityBetter performance
SecuritySSH open to 0.0.0.0/0Restricted to specific IPsReduced attack surface

๐Ÿงช Testing

Run the example analysis with sample data:

# Test with sample rules python main.py analyze --input data/sample_rules.txt # Test web interface python main.py webapp # Test visualization generation python main.py visualize --input data/sample_rules.txt --output ./test_reports

๐Ÿ“‹ Dependencies

Core Dependencies

  • streamlit - Web interface framework
  • pandas - Data manipulation and analysis
  • numpy - Numerical computing
  • matplotlib - Static plotting
  • plotly - Interactive plotting
  • networkx - Graph analysis
  • seaborn - Statistical visualization
  • pyparsing - Text parsing
  • pyyaml - YAML configuration
  • click - Command line interface

Optional Dependencies

  • scikit-learn - Machine learning (for advanced analysis)
  • xgboost - Gradient boosting (for ML features)
  • dash - Alternative web framework
  • flask - Lightweight web framework

๐Ÿค Contributing

We welcome contributions! Please see our contributing guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Clone the repository git clone https://github.com/your-username/firewall-optimizer.git cd firewall-optimizer # Create virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate# Install development dependencies pip install -r requirements.txt pip install -e .# Run tests python -m pytest tests/ # Run linting black optimizer/ flake8 optimizer/ mypy optimizer/

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • iptables - The Linux firewall utility that makes this project possible
  • Streamlit - For providing an excellent web framework for data applications
  • Plotly - For interactive visualization capabilities
  • NetworkX - For graph analysis and visualization
  • The Python Community - For the amazing ecosystem of tools and libraries

๐Ÿ“ž Support


โญ Star this repository if you find it useful!

Made with โค๏ธ by the Akshay Kale

About

๐Ÿš€ Firewall Rule Optimizer is an AI-powered tool designed to simplify ๐Ÿ”ง and optimize โš™๏ธ your firewall rule management. ๐Ÿ“Š Large-scale configurations often get messy, redundant ๐Ÿ”, and tough to handle.๐Ÿ’ก This tool brings clarity, speed, and intelligence to your firewall strategy ๐Ÿ”

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published