File size: 14,598 Bytes
23804b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
"""
Cyber-LLM Command Line Interface
Provides command-line access to Cyber-LLM agents and capabilities.
"""

import click
import json
import logging
import sys
from pathlib import Path
from typing import Dict, Any, Optional

# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

try:
    from agents.recon_agent import ReconAgent, ReconTarget
    from agents.safety_agent import SafetyAgent
    from orchestration.orchestrator import CyberOrchestrator
except ImportError as e:
    print(f"Import error: {e}")
    print("Make sure you're running from the project root directory")
    sys.exit(1)

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

@click.group()
@click.version_option(version='0.4.0')
@click.option('--config', default='configs/model_config.json', help='Configuration file path')
@click.option('--verbose', '-v', is_flag=True, help='Enable verbose logging')
@click.pass_context
def cli(ctx, config, verbose):
    """Cyber-LLM: Advanced Cybersecurity AI Assistant."""
    if verbose:
        logging.getLogger().setLevel(logging.DEBUG)
    
    ctx.ensure_object(dict)
    ctx.obj['config_path'] = Path(config)
    ctx.obj['verbose'] = verbose

@cli.group()
@click.pass_context
def agent(ctx):
    """Run individual agents."""
    pass

@agent.command()
@click.option('--target', required=True, help='Target IP, domain, or network')
@click.option('--type', 'target_type', default='auto', 
              type=click.Choice(['auto', 'ip', 'domain', 'network', 'organization']))
@click.option('--opsec', default='medium',
              type=click.Choice(['low', 'medium', 'high', 'maximum']))
@click.option('--output', '-o', help='Output file for results')
@click.option('--dry-run', is_flag=True, help='Show plan without execution')
@click.pass_context
def recon(ctx, target, target_type, opsec, output, dry_run):
    """Run reconnaissance operations."""
    try:
        # Initialize ReconAgent
        agent = ReconAgent()
        
        # Auto-detect target type if needed
        if target_type == 'auto':
            if target.count('.') == 3 and all(p.isdigit() for p in target.split('.')):
                target_type = 'ip'
            elif '.' in target:
                target_type = 'domain'
            else:
                target_type = 'organization'
        
        # Create target info
        target_info = ReconTarget(
            target=target,
            target_type=target_type,
            constraints={'dry_run': dry_run},
            opsec_level=opsec
        )
        
        # Execute reconnaissance
        result = agent.execute_reconnaissance(target_info)
        
        # Display results
        click.echo(f"Reconnaissance Results for {target}")
        click.echo("=" * 50)
        click.echo(f"Target Type: {target_type}")
        click.echo(f"OPSEC Level: {opsec}")
        click.echo(f"Status: {result['execution_status']}")
        
        if dry_run:
            click.echo("\n[DRY RUN MODE - No actual commands executed]")
        
        # Show planned commands
        plan = result['plan']
        
        click.echo("\nPlanned Commands:")
        for category, commands in plan['commands'].items():
            if commands:
                click.echo(f"\n{category.upper()}:")
                for cmd in commands:
                    click.echo(f"  {cmd}")
        
        # Show OPSEC notes
        if plan['opsec_notes']:
            click.echo("\nOPSEC Considerations:")
            for note in plan['opsec_notes']:
                click.echo(f"  β€’ {note}")
        
        # Show risk assessment
        click.echo(f"\nRisk Assessment: {plan['risk_assessment']}")
        
        # Save to file if requested
        if output:
            output_path = Path(output)
            with open(output_path, 'w') as f:
                json.dump(result, f, indent=2)
            click.echo(f"\nResults saved to: {output_path}")
            
    except Exception as e:
        click.echo(f"Error during reconnaissance: {str(e)}", err=True)
        if ctx.obj['verbose']:
            import traceback
            traceback.print_exc()

@agent.command()
@click.option('--commands-file', required=True, help='JSON file containing commands to validate')
@click.option('--opsec', default='medium',
              type=click.Choice(['low', 'medium', 'high', 'maximum']))
@click.option('--output', '-o', help='Output file for assessment results')
@click.pass_context
def safety(ctx, commands_file, opsec, output):
    """Validate commands for safety and OPSEC compliance."""
    try:
        # Load commands from file
        commands_path = Path(commands_file)
        if not commands_path.exists():
            click.echo(f"Commands file not found: {commands_file}", err=True)
            return
        
        with open(commands_path, 'r') as f:
            commands = json.load(f)
        
        # Initialize SafetyAgent
        agent = SafetyAgent()
        
        # Validate commands
        assessment = agent.validate_commands(commands, opsec_level=opsec)
        
        # Display results
        click.echo("Safety Assessment Results")
        click.echo("=" * 30)
        click.echo(f"Overall Risk: {assessment.overall_risk.value.upper()}")
        click.echo(f"Approved: {'βœ“' if assessment.approved else 'βœ—'}")
        
        # Show individual checks
        click.echo("\nDetailed Checks:")
        for check in assessment.checks:
            status = "βœ“" if check.risk_level.value == 'low' else "⚠" if check.risk_level.value == 'medium' else "βœ—"
            click.echo(f"  {status} {check.check_name}: {check.risk_level.value.upper()}")
            
            if check.violations:
                for violation in check.violations:
                    click.echo(f"    β€’ {violation}")
        
        # Show recommendations
        if any(check.recommendations for check in assessment.checks):
            click.echo("\nRecommendations:")
            for check in assessment.checks:
                for rec in check.recommendations:
                    click.echo(f"  β€’ {rec}")
        
        # Show safe alternatives if not approved
        if not assessment.approved and assessment.safe_alternatives:
            click.echo("\nSafe Alternatives:")
            for alt in assessment.safe_alternatives:
                click.echo(f"  β€’ {alt}")
        
        # Save to file if requested
        if output:
            output_path = Path(output)
            assessment_dict = {
                'overall_risk': assessment.overall_risk.value,
                'approved': assessment.approved,
                'summary': assessment.summary,
                'checks': [
                    {
                        'name': check.check_name,
                        'risk_level': check.risk_level.value,
                        'description': check.description,
                        'violations': check.violations,
                        'recommendations': check.recommendations
                    }
                    for check in assessment.checks
                ],
                'safe_alternatives': assessment.safe_alternatives
            }
            
            with open(output_path, 'w') as f:
                json.dump(assessment_dict, f, indent=2)
            click.echo(f"\nAssessment saved to: {output_path}")
            
    except Exception as e:
        click.echo(f"Error during safety assessment: {str(e)}", err=True)
        if ctx.obj['verbose']:
            import traceback
            traceback.print_exc()

@cli.command()
@click.option('--scenario', required=True, help='Scenario name or file path')
@click.option('--target', help='Target for the scenario')
@click.option('--opsec', default='medium',
              type=click.Choice(['low', 'medium', 'high', 'maximum']))
@click.option('--dry-run', is_flag=True, help='Simulation mode only')
@click.option('--output', '-o', help='Output directory for results')
@click.pass_context
def orchestrate(ctx, scenario, target, opsec, dry_run, output):
    """Run orchestrated multi-agent scenarios."""
    try:
        click.echo(f"Orchestrating scenario: {scenario}")
        click.echo(f"Target: {target}")
        click.echo(f"OPSEC Level: {opsec}")
        
        if dry_run:
            click.echo("\n[SIMULATION MODE]")
        
        # Initialize orchestrator
        # orchestrator = CyberOrchestrator()
        
        # For now, show what would be orchestrated
        click.echo("\nPlanned Orchestration Flow:")
        click.echo("1. ReconAgent - Initial target analysis")
        click.echo("2. SafetyAgent - OPSEC compliance validation")
        click.echo("3. ReconAgent - Execute approved reconnaissance")
        click.echo("4. ExplainabilityAgent - Generate rationale")
        click.echo("5. Generate final report")
        
        click.echo("\n[ORCHESTRATION FEATURE IN DEVELOPMENT]")
        click.echo("This feature will be available in the next release.")
        
    except Exception as e:
        click.echo(f"Error during orchestration: {str(e)}", err=True)
        if ctx.obj['verbose']:
            import traceback
            traceback.print_exc()

@cli.command()
@click.option('--input-dir', required=True, help='Input directory with raw data')
@click.option('--output-dir', required=True, help='Output directory for processed data')
@click.option('--stage', default='all', 
              type=click.Choice(['convert', 'embed', 'preprocess', 'all']))
@click.pass_context
def data(ctx, input_dir, output_dir, stage):
    """Data processing pipeline."""
    try:
        input_path = Path(input_dir)
        output_path = Path(output_dir)
        
        if not input_path.exists():
            click.echo(f"Input directory not found: {input_dir}", err=True)
            return
        
        output_path.mkdir(parents=True, exist_ok=True)
        
        if stage in ['convert', 'all']:
            click.echo("Converting PDF files to text...")
            # Run PDF conversion
            import subprocess
            result = subprocess.run([
                'python', 'scripts/convert_pdf_to_txt.py',
                '--input', str(input_path),
                '--output', str(output_path / 'converted')
            ], capture_output=True, text=True)
            
            if result.returncode != 0:
                click.echo(f"PDF conversion failed: {result.stderr}", err=True)
            else:
                click.echo("βœ“ PDF conversion completed")
        
        if stage in ['embed', 'all']:
            click.echo("Generating embeddings...")
            # Run embedding generation
            import subprocess
            result = subprocess.run([
                'python', 'scripts/generate_embeddings.py',
                '--input', str(output_path / 'converted'),
                '--output', str(output_path / 'embeddings')
            ], capture_output=True, text=True)
            
            if result.returncode != 0:
                click.echo(f"Embedding generation failed: {result.stderr}", err=True)
            else:
                click.echo("βœ“ Embedding generation completed")
        
        if stage in ['preprocess', 'all']:
            click.echo("Preprocessing training data...")
            # Run preprocessing
            import subprocess
            result = subprocess.run([
                'python', 'src/training/preprocess.py',
                '--input', str(output_path / 'converted'),
                '--output', str(output_path / 'processed')
            ], capture_output=True, text=True)
            
            if result.returncode != 0:
                click.echo(f"Preprocessing failed: {result.stderr}", err=True)
            else:
                click.echo("βœ“ Preprocessing completed")
        
        click.echo(f"\nData processing completed. Results in: {output_path}")
        
    except Exception as e:
        click.echo(f"Error during data processing: {str(e)}", err=True)
        if ctx.obj['verbose']:
            import traceback
            traceback.print_exc()

@cli.command()
@click.option('--module', required=True, 
              type=click.Choice(['recon', 'c2', 'postexploit', 'explainability', 'safety', 'all']))
@click.option('--config', help='Training configuration file')
@click.option('--data-dir', default='data/processed', help='Processed data directory')
@click.option('--output-dir', default='models/adapters', help='Output directory for trained adapters')
@click.pass_context
def train(ctx, module, config, data_dir, output_dir):
    """Train LoRA adapters."""
    try:
        click.echo(f"Training {module} adapter...")
        click.echo(f"Data directory: {data_dir}")
        click.echo(f"Output directory: {output_dir}")
        
        # This would call the actual training script
        click.echo("\n[TRAINING FEATURE IN DEVELOPMENT]")
        click.echo("Training pipeline will be available in the next release.")
        click.echo("Configure your training in configs/model_config.py")
        
    except Exception as e:
        click.echo(f"Error during training: {str(e)}", err=True)
        if ctx.obj['verbose']:
            import traceback
            traceback.print_exc()

@cli.command()
def status():
    """Show system status and health check."""
    click.echo("Cyber-LLM System Status")
    click.echo("=" * 25)
    
    # Check components
    components = {
        'ReconAgent': True,
        'SafetyAgent': True,
        'ExplainabilityAgent': False,  # Not implemented yet
        'C2Agent': False,  # Not implemented yet
        'PostExploitAgent': False,  # Not implemented yet
        'Orchestrator': False,  # Not implemented yet
        'Training Pipeline': False,  # Not implemented yet
    }
    
    for component, status in components.items():
        status_icon = "βœ“" if status else "βœ—"
        status_text = "Available" if status else "In Development"
        click.echo(f"  {status_icon} {component}: {status_text}")
    
    # Check directories
    click.echo("\nDirectory Structure:")
    important_dirs = [
        'src/agents',
        'src/training',
        'src/evaluation',
        'configs',
        'scripts',
        'data'
    ]
    
    for dir_path in important_dirs:
        path = Path(dir_path)
        status_icon = "βœ“" if path.exists() else "βœ—"
        click.echo(f"  {status_icon} {dir_path}")

if __name__ == '__main__':
    cli()