-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_usage.py
More file actions
137 lines (111 loc) · 5.06 KB
/
example_usage.py
File metadata and controls
137 lines (111 loc) · 5.06 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
#!/usr/bin/env python3
"""
Example usage of the AI-Powered Penetration Testing Agent
This script demonstrates how to use the agent for authorized security testing.
Remember to only test systems you own or have explicit permission to test.
"""
import os
import sys
from loguru import logger
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from core.agent import PentestAgent
def main():
"""Main example function"""
logger.info("AI-Powered Penetration Testing Agent - Example Usage")
try:
agent = PentestAgent("config/config.yaml")
logger.info("Agent initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize agent: {e}")
return
logger.info("\n=== Example 1: Basic Reconnaissance ===")
try:
target = agent.set_target("example.com", scope=["web"])
recon_results = agent.recon_engine.run_comprehensive_recon(target)
logger.info(f"Reconnaissance completed:")
logger.info(f" - IP Address: {recon_results.get('ip_address', 'Not found')}")
logger.info(f" - Open Ports: {len(recon_results.get('ports', []))}")
logger.info(f" - Services: {len(recon_results.get('services', {}))}")
logger.info(f" - Web Pages: {len(recon_results.get('web_pages', []))}")
except Exception as e:
logger.error(f"Reconnaissance failed: {e}")
logger.info("\n=== Example 2: Focused Testing ===")
try:
focused_results = agent.recon_engine.run_focused_recon(
target,
focus_areas=["dns", "web"]
)
logger.info(f"Focused reconnaissance completed:")
logger.info(f" - Focus Areas: {focused_results.get('focus_areas', [])}")
logger.info(f" - Success Rate: {focused_results.get('successful_attempts', 0)}/{focused_results.get('total_attempts', 1)}")
except Exception as e:
logger.error(f"Focused testing failed: {e}")
logger.info("\n=== Example 3: AI Agent Statistics ===")
try:
stats = agent.rl_agent.get_performance_stats()
if stats:
logger.info(f"AI Agent Performance:")
logger.info(f" - Total Episodes: {stats.get('total_episodes', 0)}")
logger.info(f" - Success Rate: {stats.get('success_rate', 0):.2%}")
logger.info(f" - Average Reward: {stats.get('average_reward', 0):.2f}")
else:
logger.info("No training data available yet")
except Exception as e:
logger.error(f"Failed to get AI stats: {e}")
logger.info("\n=== Example 4: Safety Features ===")
try:
safety_summary = agent.safety_manager.get_safety_summary()
logger.info(f"Safety Summary:")
logger.info(f" - Emergency Stop: {safety_summary.get('emergency_stop_active', False)}")
logger.info(f" - Rate Limiting: {safety_summary.get('rate_limiting_enabled', False)}")
logger.info(f" - Safe Mode: {safety_summary.get('safe_mode_enabled', False)}")
logger.info(f" - Authorization Required: {safety_summary.get('authorization_required', False)}")
except Exception as e:
logger.error(f"Failed to get safety summary: {e}")
logger.info("\n=== Example 5: Report Generation ===")
try:
mock_results = {
"target": "example.com",
"timestamp": 1234567890,
"reconnaissance": {
"ip_address": "93.184.216.34",
"ports": [80, 443],
"services": {"80": "http", "443": "https"},
"successful_attempts": 3,
"total_attempts": 3
},
"vulnerabilities": {
"found_vulnerabilities": [],
"successful_attempts": 0,
"total_attempts": 1
},
"exploitation": {
"successful_exploits": [],
"successful_attempts": 0,
"total_attempts": 0
},
"privilege_escalation": {
"escalation_successful": False,
"successful_attempts": 0,
"total_attempts": 0
},
"attack_path": [],
"success_rate": 0.75
}
report_path = agent.report_generator.generate_report(
mock_results,
[],
"example_report.html"
)
logger.info(f"Example report generated: {report_path}")
except Exception as e:
logger.error(f"Report generation failed: {e}")
logger.info("\n=== Example Usage Complete ===")
logger.info("Remember to:")
logger.info(" - Only test systems you own or have permission to test")
logger.info(" - Follow responsible disclosure practices")
logger.info(" - Comply with all applicable laws and regulations")
if __name__ == "__main__":
logger.remove()
logger.add(sys.stderr, level="INFO", format="<green>{time:HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>")
main()