-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvulnhound.py
executable file
·223 lines (184 loc) · 7.18 KB
/
vulnhound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
"""
Vulnhound - Security Vulnerability Scanner for Code Repositories
A command-line tool that combines AI-powered and pattern-based approaches
to detect security vulnerabilities in code repositories across multiple languages.
"""
import os
import sys
import argparse
import json
import time
from pathlib import Path
import colorama
from core.scanner import Scanner, ScannerError
from core.environment import check_environment, EnvironmentError
from utils.logger import setup_logger
from output.report_generator import generate_report
def parse_arguments():
"""
Parse command line arguments with detailed help information
Returns:
argparse.Namespace: Parsed arguments
"""
parser = argparse.ArgumentParser(
description="Vulnhound - Security Vulnerability Scanner for Code Repositories",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"repository",
help="Path to the code repository to scan (absolute or relative path)"
)
parser.add_argument(
"--extensions",
help="Comma-separated list of file extensions to scan (e.g., py,js,java)",
default="py,js,java,go,php,rb,c,cpp,cs"
)
parser.add_argument(
"--exclude-dirs",
help="Comma-separated list of directories to exclude (e.g., tests,docs)",
default=".git,node_modules,venv,__pycache__,.venv,dist,build"
)
parser.add_argument(
"--output-format",
choices=["json", "html", "console"],
default="console",
help="Output format for the vulnerability report"
)
parser.add_argument(
"--output-file",
help="Path to save the output report (not needed for console output)"
)
parser.add_argument(
"--max-workers",
type=int,
help="Maximum number of worker threads for parallel scanning"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Enable verbose logging"
)
parser.add_argument(
"--skip-environment-check",
action="store_true",
help="Skip environment verification"
)
parser.add_argument(
"--no-ai",
action="store_true",
help="Disable AI-based detection (faster but less accurate)"
)
parser.add_argument(
"--no-banner",
action="store_true",
help="Disable ASCII art banner"
)
parser.add_argument(
"--version",
action="version",
version="Vulnhound 0.1.0",
help="Show version information and exit"
)
return parser.parse_args()
def main():
"""
Main entry point for Vulnhound
This function:
1. Parses command line arguments
2. Sets up logging
3. Validates the environment
4. Initializes and runs the scanner
5. Generates the vulnerability report
Returns:
int: Exit code (0 for success, 1 for scan with vulnerabilities, 2 for error)
"""
start_time = time.time()
try:
# Parse command line arguments
args = parse_arguments()
# Banner display
if not args.no_banner:
banner = r"""
__ __ _ _ _
\ \ / / | | | | | |
\ \ / / _| |_ __ | |__ ___ _ _ _ __ | |
\ \/ / | | | | '_ \| '_ \ / _ \| | | | '_ \ | |
\ /| |_| | | | | | | | | (_) | |_| | | | | |_|
\/ \__,_|_|_| |_|_| |_|\___/ \__,_|_| |_| (_)
AI-enhanced Security Vulnerability Scanner
"""
print(colorama.Fore.CYAN + banner + colorama.Style.RESET_ALL)
# Setup logging
logger = setup_logger(verbose=args.verbose)
logger.info("Starting Vulnhound security scan")
# Check environment unless skipped
if not args.skip_environment_check:
try:
if not check_environment(skip_ai=args.no_ai):
logger.error("Environment check failed. Please install required dependencies.")
return 2
except EnvironmentError as e:
logger.error(f"Environment error: {str(e)}")
return 2
# Validate repository path
repo_path = Path(args.repository).resolve()
if not repo_path.exists():
logger.error(f"Repository path does not exist: {repo_path}")
return 2
if not repo_path.is_dir():
logger.error(f"Repository path is not a directory: {repo_path}")
return 2
# Parse extensions and excluded directories
extensions = [ext.strip() for ext in args.extensions.split(",")]
exclude_dirs = set(dir.strip() for dir in args.exclude_dirs.split(","))
# Initialize scanner
try:
scanner = Scanner(
repo_path,
extensions,
logger,
exclude_dirs=exclude_dirs,
max_workers=args.max_workers
)
# Run the scan
vulnerabilities = scanner.scan()
# Generate and output report
if vulnerabilities:
logger.warning(f"Found {len(vulnerabilities)} potential security vulnerabilities")
if args.output_format == "console":
generate_report(vulnerabilities, "console", None, logger)
else:
if not args.output_file:
logger.error(f"{args.output_format} output format requires --output-file parameter")
return 2
output_path = Path(args.output_file)
# Create parent directories if they don't exist
output_path.parent.mkdir(parents=True, exist_ok=True)
generate_report(vulnerabilities, args.output_format, str(output_path), logger)
logger.info(f"Report saved to {output_path}")
# Exit with status code 1 if vulnerabilities were found (for CI/CD pipelines)
return 1
else:
logger.info("No security vulnerabilities detected")
return 0
except ScannerError as e:
logger.error(f"Scanning error: {str(e)}")
return 2
except KeyboardInterrupt:
logger.warning("Scan interrupted by user")
return 2
except Exception as e:
logger = setup_logger(verbose=True) # Ensure we have a logger even if it failed earlier
logger.error(f"Unexpected error: {str(e)}")
if "--verbose" in sys.argv or "-v" in sys.argv:
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
return 2
finally:
elapsed_time = time.time() - start_time
logger = setup_logger(verbose=False) # Ensure we have a logger even if it failed earlier
logger.info(f"Total execution time: {elapsed_time:.2f} seconds")
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)