IaC Security Scanner by Gbolahan AwobajoIaC Security Scanner by Gbolahan Awobajo

IaC Security Scanner

Portfolio project
Gbolahan Awobajo

IaC Security Scanner

A Python-based security scanner for Infrastructure as Code (Terraform and CloudFormation) that detects common security misconfigurations and provides detailed remediation guidance.

Features

Scans Terraform (.tf) and CloudFormation (.yaml, .yml, .json) files
Detects 11+ common security misconfigurations
Severity-based prioritization (CRITICAL, HIGH, MEDIUM, LOW)
Detailed remediation instructions with code examples
Multiple output formats (Console, HTML, JSON, Markdown)
CIS AWS and NIST 800-53 framework references
Beautiful HTML reports with statistics
CI/CD integration ready

Security Checks

CRITICAL Severity

S3 buckets with public read access
Security groups allowing all traffic from anywhere
RDS instances publicly accessible

HIGH Severity

S3 buckets without encryption
Security groups with SSH (port 22) open to 0.0.0.0/0
Security groups with RDP (port 3389) open to 0.0.0.0/0
Unencrypted RDS instances
IAM policies with wildcard (*) permissions
No CloudTrail configuration

MEDIUM Severity

Unencrypted EBS volumes
VPCs without flow logs

Installation

Requirements

Python 3.6+
No external dependencies required for basic functionality
Optional: colorama for colored console output

Setup

Clone or download the scanner cd /path/to/your/project # Optional: Install colorama for colored output pip install colorama # Make the script executable (Linux/Mac) chmod +x iac_security_scanner.py

Usage

Basic Scan

Scan a directory and output to console + HTML:

Scan a specific directory python iac_security_scanner.py /path/to/terraform/files # Scan current directory (where you are now) python iac_security_scanner.py .

Note: The scanner requires a directory path as an argument. You'll get an error if you run it without specifying where to scan.

Specify Output Formats

Console and JSON output python iac_security_scanner.py ./infra --format console,json # All formats python iac_security_scanner.py ./infra --format console,html,json,markdown # HTML only python iac_security_scanner.py ./infra --format html

Custom Output File Name

python iac_security_scanner.py ./infra --output my_scan_results # Creates: my_scan_results.html, my_scan_results.json, etc.

Test with Examples

First, create the test directory and move main.tf into it mkdir example_terraform mv main.tf example_terraform/ # Now test the scanner python iac_security_scanner.py example_terraform/ # OR scan the current directory if your .tf files are here python iac_security_scanner.py . # OR scan a specific file directly python iac_security_scanner.py main.tf

Output Formats

1. Console Output

Color-coded severity levels
Quick summary of findings
Ideal for terminal/CI pipelines

2. HTML Report

Professional, interactive report
Statistics dashboard
Expandable finding details
Easy to share with team

3. JSON Report

Machine-readable format
Perfect for automation
Integration with other tools

4. Markdown Report

Documentation-friendly format
Can be committed to Git
Renders nicely on GitHub

CI/CD Integration

GitHub Actions Example

name: IaC Security Scan on: [push, pull_request] jobs: security-scan: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.9' - name: Run Security Scanner run: | python iac_security_scanner.py ./terraform --format json - name: Upload Report uses: actions/upload-artifact@v2 with: name: security-report path: security_report.json - name: Fail on Critical Issues run: | # Exit code 2 = critical issues found exit_code=$? if [ $exit_code -eq 2 ]; then echo "Critical security issues found!" exit 1 fi

GitLab CI Example

iac-security-scan: stage: test script: - python iac_security_scanner.py ./terraform --format html,json artifacts: paths: - security_report.html - security_report.json expire_in: 1 week only: - merge_requests - main

Pre-commit Hook

Create .git/hooks/pre-commit:
#!/bin/bash python iac_security_scanner.py . --format console exit_code=$? if [ $exit_code -eq 2 ]; then echo "❌ Critical security issues found! Commit blocked." exit 1 fi exit 0

Exit Codes

0: No issues found
1: Issues found (HIGH, MEDIUM, or LOW)
2: Critical issues found

Extending the Scanner

Adding New Rules

Edit the SECURITY_RULES dictionary in the script:
SECURITY_RULES = { 'YOUR_RULE_ID': { 'severity': 'HIGH', 'title': 'Your Rule Title', 'description': 'What the issue is', 'remediation': ''' Step-by-step remediation instructions ''', 'references': ['CIS AWS X.Y', 'NIST 800-53 XX-Y'] } }
Then add detection logic in the appropriate scan method:
def scan_terraform_file(self, file_path: str): # ... existing code ... # Your new check if 'your_resource_type' in content: if 'bad_pattern' in content: self.add_finding('YOUR_RULE_ID', file_path, line_number, resource_name)

Sample Output

============================================================ Infrastructure Security Scan Results ============================================================ Total Findings: 8 Critical: 2 High: 4 Medium: 2 Low: 0 [CRITICAL] S3 Bucket Allows Public Read Access File: main.tf:5 Resource: data_bucket S3 bucket has public read permissions which could expose sensitive data [HIGH] Security Group Allows SSH from Anywhere File: main.tf:22 Resource: web_sg Security group allows SSH (port 22) access from 0.0.0.0/0

Best Practices

Run Regularly: Integrate into your CI/CD pipeline
Fix Critical First: Prioritize CRITICAL and HIGH severity issues
Review Context: Some findings may be intentional (e.g., public website S3 buckets)
Version Control: Commit reports to track security improvements over time
Team Collaboration: Share HTML reports with your team for review
Automate Remediation: Use findings to create automated fix scripts

Roadmap

Future enhancements:
Support for Azure Resource Manager templates
Support for Google Cloud Deployment Manager
Auto-remediation mode
Custom rule configuration files
Integration with AWS Config Rules
Severity threshold configuration
Ignore/suppress findings feature
Diff mode (show new findings vs. previous scan)

Troubleshooting

"No module named 'colorama'"

This is optional. Either:
pip install colorama
Or use without colors (works fine!)

False Positives

The scanner uses pattern matching and may occasionally flag secure configurations. Always review findings in context.

Large Codebases

For very large Terraform projects (100+ files), consider:
Running scans on specific directories
Using JSON output for programmatic filtering
Implementing parallel processing (future enhancement)

Contributing

Suggestions for new security rules? Found a bug? Contributions welcome!

License

MIT License - feel free to use and modify for your projects.

Author

Created by a cloud security engineer for cloud security engineers 🔒

Acknowledgments

CIS AWS Foundations Benchmark
NIST 800-53 Security Controls
AWS Security Best Practices
Terraform Security Best Practices

0 likes · 0 views · Nov 29, 2026

Built a Python scanner that detects common Terraform and CloudFormation security misconfigurations and provides severity-ranked findings with remediation guidan

Tags