-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·235 lines (177 loc) · 6.61 KB
/
Copy pathcli.py
File metadata and controls
executable file
·235 lines (177 loc) · 6.61 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
"""
CLI Interface for AI Security Assistant
Run security scans from command line
"""
import argparse
import sys
import os
# Add src directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from scanners.log_scanner import LogScanner
from scanners.network_analyzer import NetworkAnalyzer
from scanners.file_scanner import FileScanner
from scanners.registry_scanner import RegistryScanner
from ai.ollama_client import OllamaClient, SecurityAI
from utils.config_manager import ConfigManager
def scan_logs(args):
"""Scan log files"""
scanner = LogScanner()
if args.file:
results = scanner.scan_file(args.file)
report = scanner.generate_report(results)
elif args.directory:
results = scanner.scan_directory(args.directory)
report = scanner.generate_report(results)
else:
# Use configured paths
config = ConfigManager()
paths = config.get_log_paths()
if not paths:
print("No log paths configured. Use --file or --directory option.")
return 1
results = {}
for path in paths:
if os.path.isfile(path):
findings = scanner.scan_file(path)
if findings:
results[path] = findings
elif os.path.isdir(path):
dir_results = scanner.scan_directory(path)
results.update(dir_results)
report = scanner.generate_report(results)
print(report)
if args.ai_analysis:
analyze_with_ai("log", report, args)
return 0
def scan_network(args):
"""Scan network connections"""
analyzer = NetworkAnalyzer()
report = analyzer.generate_report()
print(report)
if args.ai_analysis:
analyze_with_ai("network", report, args)
return 0
def scan_file(args):
"""Scan a file"""
scanner = FileScanner()
if not args.file:
print("Error: --file argument required")
return 1
results = scanner.scan_file_content(args.file)
report = scanner.generate_report(results)
print(report)
if args.ai_analysis:
analyze_with_ai("file", report, args)
return 0
def scan_registry(args):
"""Scan Windows registry"""
scanner = RegistryScanner()
if not scanner.available:
print("Registry scanning is only available on Windows.")
return 1
report = scanner.generate_report()
print(report)
if args.ai_analysis:
analyze_with_ai("registry", report, args)
return 0
def analyze_with_ai(scan_type, results, args):
"""Analyze results with AI"""
config = ConfigManager()
print("\n" + "=" * 60)
print("AI ANALYSIS")
print("=" * 60)
client = OllamaClient(config.get_ollama_url())
if not client.is_available():
print("Error: Ollama is not available. Please start Ollama first.")
return
ai = SecurityAI(client, config.get_ai_model())
analysis = ai.analyze_scan_results(scan_type, results)
print(analysis)
print("=" * 60)
def interactive_chat(args):
"""Start interactive chat with AI"""
config = ConfigManager()
client = OllamaClient(config.get_ollama_url())
if not client.is_available():
print("Error: Ollama is not available. Please start Ollama first.")
return 1
ai = SecurityAI(client, config.get_ai_model())
print("=" * 60)
print("AI Security Assistant - Interactive Chat")
print("=" * 60)
print("Type 'exit' or 'quit' to end the session")
print()
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ['exit', 'quit']:
print("Goodbye!")
break
if not user_input:
continue
response = ai.chat_conversation(user_input)
print(f"\nAI Assistant: {response}\n")
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {str(e)}")
return 0
def main():
"""Main CLI entry point"""
parser = argparse.ArgumentParser(
description="AI Security Assistant - Command Line Interface",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s scan-logs --file /var/log/auth.log
%(prog)s scan-network --ai-analysis
%(prog)s scan-file --file suspicious.exe
%(prog)s scan-registry
%(prog)s chat
"""
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Scan logs command
logs_parser = subparsers.add_parser('scan-logs', help='Scan log files')
logs_parser.add_argument('--file', help='Scan a specific log file')
logs_parser.add_argument('--directory', help='Scan all logs in a directory')
logs_parser.add_argument('--ai-analysis', action='store_true',
help='Analyze results with AI')
# Scan network command
network_parser = subparsers.add_parser('scan-network', help='Analyze network connections')
network_parser.add_argument('--ai-analysis', action='store_true',
help='Analyze results with AI')
# Scan file command
file_parser = subparsers.add_parser('scan-file', help='Scan a file for threats')
file_parser.add_argument('--file', required=True, help='File to scan')
file_parser.add_argument('--ai-analysis', action='store_true',
help='Analyze results with AI')
# Scan registry command
registry_parser = subparsers.add_parser('scan-registry',
help='Scan Windows registry (Windows only)')
registry_parser.add_argument('--ai-analysis', action='store_true',
help='Analyze results with AI')
# Chat command
chat_parser = subparsers.add_parser('chat', help='Interactive chat with AI assistant')
args = parser.parse_args()
if not args.command:
parser.print_help()
return 1
# Execute command
if args.command == 'scan-logs':
return scan_logs(args)
elif args.command == 'scan-network':
return scan_network(args)
elif args.command == 'scan-file':
return scan_file(args)
elif args.command == 'scan-registry':
return scan_registry(args)
elif args.command == 'chat':
return interactive_chat(args)
else:
parser.print_help()
return 1
if __name__ == "__main__":
sys.exit(main())