A comprehensive static analysis tool for detecting security vulnerabilities in C/C++ source code using both AI-powered analysis and rule-based heuristics.
- Hybrid Analysis: Combines AI-powered analysis (Phi-4 model) with rule-based vulnerability detection
- Offline Operation: Works completely offline after initial setup
- Multiple Output Formats: Text and JSON reports
- Comprehensive Coverage: Detects buffer overflows, memory leaks, format string vulnerabilities, and more
- Smart Chunking: Intelligently splits large files for efficient analysis
- Severity Classification: Categorizes vulnerabilities by severity (Critical/High/Medium/Low)
- Fix Suggestions: Provides actionable remediation advice
- CLI Interface: Easy-to-use command-line interface
The tool follows a modular architecture with clear separation of concerns:
vuln-analyzer/
├── cli.py # CLI entry point and orchestration
├── models/
│ └── phi-4/ # AI model files (after setup)
├── src/
│ ├── analysis/
│ │ ├── chunker.py # AI model integration (Phi-4)
│ │ ├── extractor.py # Code chunking and preprocessing
│ │ ├── heuristics.py # Vulnerability pattern extraction
│ │ └── llm_backend.py # Rule-based analysis engine
│ └── report/
│ └── formatter.py # Report generation and formatting
├── tests/
│ └── data/ # Test files
├── README.md
├── Report.md
├── setup_model.py # Model setup and download script
├── test_offline.py
└── test_phi4_load.py
- VulnerabilityAnalyzer (
cli.py): Main orchestrator that coordinates all components - ONNXLLMBackend (
llm_backend.py): AI-powered analysis using Microsoft's Phi-4 model - CodeChunker (
chunker.py): Intelligent code segmentation for efficient processing - HeuristicAnalyzer (
heuristics.py): Rule-based vulnerability detection - VulnerabilityExtractor (
extractor.py): Pattern matching and vulnerability extraction - ReportFormatter (
formatter.py): Multi-format report generation
This tool operates completely offline after initial setup:
- One-time setup requires internet (run
setup_model.py) - After setup, NO internet connection is needed
- All analysis happens locally on your machine
- No data is ever sent to external servers
- Python 3.8 or higher
- 8GB RAM minimum (16GB recommended for Phi-4)
- 10GB disk space for model files
- Internet connection (only for initial setup)
git clone https://github.com/danielnisanov/vuln-analyzer.git
cd vuln-analyzerpip install -r requirements.txt# This downloads and prepares model files for offline use
python setup_model.py# Test that the tool works without internet
python test_offline.pyAfter setup is complete, the tool operates entirely offline.
Once the setup is complete, the tool requires NO internet connection. All analysis is performed locally using:
- Pre-downloaded Phi-4 ONNX model
- Local tokenizer files
- Rule-based heuristics as fallback
To ensure offline operation:
- Run
setup_model.pyonce with internet - All model files are stored in
./models/phi-4/ - The tool will never attempt to download files during normal operation
# Analyze a single file
python cli.py vulnerable_code.c
# Verbose output with fixes
python cli.py --verbose --fixes vulnerable_code.c
# Generate JSON report
python cli.py --format json --output report.json source.cpp
# Rule-based analysis only (no AI model required)
python cli.py --no-model vulnerable_code.c
# Custom model path
python cli.py --model ./custom_model input.c
# Configuration file
python cli.py --config config.json input.cThe analyzer detects various vulnerability types:
- Buffer Overflows:
gets(), unsafestrcpy(),strcat(),sprintf() - Command Injection:
system(),exec(),popen()with user input
- Use After Free: Memory access after deallocation
- Format String: Unvalidated format strings in
printf()family - Buffer Overflows: Unsafe string operations
- Memory Leaks: Missing
free()calls - Null Pointer Dereference: Unchecked pointer access
- Race Conditions: Unsafe threading operations
- Resource Management: File operations without error checking
- Integer Overflow: Potential arithmetic overflows
- Weak Random:
rand()without proper seeding
=== Vulnerability Analysis Report ===
File: vulnerable_code.c
Analysis Date: 2024-01-15 10:30:45
Line 15: [CRITICAL] Buffer Overflow - gets() function is unsafe and deprecated
Line 23: [HIGH] Use After Free - Variable 'ptr' used after free
Line 35: [MEDIUM] Memory Leak - malloc/calloc without corresponding free
=== Summary ===
Total vulnerabilities: 3
Critical: 1, High: 1, Medium: 1, Low: 0
Risk Score: 21
{
"filename": "vulnerable_code.c",
"vulnerabilities": [
{
"line": 15,
"severity": "CRITICAL",
"type": "Buffer Overflow",
"description": "gets() function is unsafe and deprecated",
"cwe": "CWE-120",
"suggested_fix": "Replace gets() with fgets() and specify buffer size"
}
],
"statistics": {
"total_vulnerabilities": 3,
"risk_score": 21
}
}Create a config.json file for advanced configuration:
{
"analysis": {
"max_chunk_size": 3000,
"enable_llm": true,
"confidence_threshold": 0.7
},
"output": {
"include_context": true,
"max_context_lines": 3
},
"vulnerabilities": {
"ignore_patterns": ["test_", "debug_"],
"severity_weights": {
"CRITICAL": 10,
"HIGH": 7,
"MEDIUM": 4,
"LOW": 1
}
}
}# Check if model files exist
ls -la models/phi-4/
# Test offline capability
python test_offline.py
# Use rule-based analysis only
python cli.py --no-model file.c- Use
--no-modelfor faster analysis on large codebases - Adjust chunk size in configuration for memory optimization
- Use JSON output format for programmatic processing
- "Tokenizer not found": Run
python setup_model.pyfirst - "ONNX model not available": The tool works with rule-based analysis; download ONNX files for AI features
- Out of memory: Reduce chunk size or use
--no-modelflag
The tool returns different exit codes based on findings:
0: No vulnerabilities found1: Low/Medium vulnerabilities found2: High severity vulnerabilities found3: Critical vulnerabilities found4: Analysis error occurred
- Microsoft for the Phi-4 model
- ONNX Runtime team for efficient inference
- Security research community for vulnerability patterns