-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_prompt_injection_attacks.py
297 lines (246 loc) · 10.6 KB
/
advanced_prompt_injection_attacks.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import networkx as nx
import scipy
from typing import List, Dict, Any, Tuple
from transformers import AutoTokenizer, AutoModel
import gensim.downloader as api
from sklearn.decomposition import PCA
from scipy.spatial.distance import cosine
import itertools
class AdvancedSemanticVectorInjectionFramework:
def __init__(
self,
embedding_models: List[str] = [
'sentence-transformers/all-MiniLM-L6-v2',
'allenai/scibert_scivocab_uncased',
'microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext'
]
):
# Multi-model embedding ensemble
self.embedding_models = []
self.embedding_tokenizers = []
for model_name in embedding_models:
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Freeze model parameters for consistent feature extraction
for param in model.parameters():
param.requires_grad = False
self.embedding_models.append(model)
self.embedding_tokenizers.append(tokenizer)
# Advanced linguistic knowledge bases
self.semantic_knowledge_bases = self._construct_advanced_knowledge_bases()
# Attack vector taxonomies
self.attack_vector_taxonomies = self._generate_attack_vector_taxonomies()
def _construct_advanced_knowledge_bases(self) -> Dict[str, nx.DiGraph]:
"""
Construct sophisticated multi-domain knowledge graphs
"""
knowledge_bases = {}
# Comprehensive domain taxonomies
domains = [
'cybersecurity',
'computational_linguistics',
'artificial_intelligence',
'system_architecture',
'information_theory',
'cognitive_science'
]
for domain in domains:
G = nx.DiGraph()
# Generate intricate domain-specific taxonomies
def generate_taxonomy_tree(depth=5, branch_factor=3):
def recursive_taxonomy_generation(current_depth, parent=None):
if current_depth == 0:
return
# Generate conceptual nodes with semantic richness
nodes = [
f"{domain}_concept_{np.random.randint(1000)}"
for _ in range(branch_factor)
]
for node in nodes:
G.add_node(node, domain=domain, depth=current_depth)
if parent:
G.add_edge(parent, node)
# Recursive taxonomy generation
recursive_taxonomy_generation(
current_depth - 1,
parent=node
)
root = f"{domain}_root"
G.add_node(root, domain=domain, depth=0)
recursive_taxonomy_generation(depth, root)
return G
knowledge_bases[domain] = generate_taxonomy_tree()
return knowledge_bases
def _generate_attack_vector_taxonomies(self) -> Dict[str, List[Dict[str, Any]]]:
"""
Generate sophisticated, multi-dimensional attack vector taxonomies
"""
attack_vectors = {
'semantic_manipulation': [
{
'name': 'contextual_reframing',
'complexity': 0.8,
'linguistic_techniques': [
'presuppositional_injection',
'pragmatic_implicature',
'modal_logic_exploitation'
]
},
{
'name': 'cognitive_bias_exploitation',
'complexity': 0.9,
'psychological_mechanisms': [
'framing_effect',
'anchoring_bias',
'confirmation_bias'
]
}
],
'computational_manipulation': [
{
'name': 'embedding_space_perturbation',
'complexity': 0.95,
'vector_transformation_techniques': [
'adversarial_projection',
'semantic_vector_interpolation',
'information_theoretic_embedding_shift'
]
}
]
}
return attack_vectors
def semantic_vector_injection_attack(
self,
base_context: str,
injection_target: str
) -> Dict[str, Any]:
"""
Advanced multi-model semantic vector injection
"""
# Multi-model embedding generation
base_embeddings = []
injection_embeddings = []
for model, tokenizer in zip(self.embedding_models, self.embedding_tokenizers):
# Tokenize and generate embeddings
base_inputs = tokenizer(
base_context,
return_tensors='pt',
padding=True,
truncation=True
)
injection_inputs = tokenizer(
injection_target,
return_tensors='pt',
padding=True,
truncation=True
)
with torch.no_grad():
base_output = model(**base_inputs)
injection_output = model(**injection_inputs)
# Mean pooling embedding extraction
base_embedding = torch.mean(base_output.last_hidden_state, dim=1)
injection_embedding = torch.mean(injection_output.last_hidden_state, dim=1)
base_embeddings.append(base_embedding)
injection_embeddings.append(injection_embedding)
# Advanced vector transformation techniques
attack_results = {
'multi_model_embeddings': {
'base': base_embeddings,
'injection': injection_embeddings
},
'transformation_techniques': []
}
# Generate multiple injection strategies
injection_strategies = [
self._linear_interpolation,
self._spherical_interpolation,
self._information_theoretic_injection
]
for strategy in injection_strategies:
transformed_vectors = strategy(base_embeddings, injection_embeddings)
attack_results['transformation_techniques'].append({
'name': strategy.__name__,
'transformed_vectors': transformed_vectors
})
return attack_results
def _linear_interpolation(
self,
base_embeddings: List[torch.Tensor],
injection_embeddings: List[torch.Tensor]
) -> List[torch.Tensor]:
"""
Advanced linear interpolation with entropy-weighted blending
"""
interpolated_vectors = []
for base_vec, injection_vec in zip(base_embeddings, injection_embeddings):
# Compute entropy-based interpolation weights
base_entropy = scipy.stats.entropy(base_vec.numpy())
injection_entropy = scipy.stats.entropy(injection_vec.numpy())
# Dynamic interpolation with entropy weighting
alpha = injection_entropy / (base_entropy + injection_entropy)
interpolated_vec = (1 - alpha) * base_vec + alpha * injection_vec
interpolated_vectors.append(interpolated_vec)
return interpolated_vectors
def _spherical_interpolation(
self,
base_embeddings: List[torch.Tensor],
injection_embeddings: List[torch.Tensor]
) -> List[torch.Tensor]:
"""
Spherical linear interpolation with advanced angular dynamics
"""
interpolated_vectors = []
for base_vec, injection_vec in zip(base_embeddings, injection_embeddings):
# Normalize vectors
base_normalized = F.normalize(base_vec, dim=0)
injection_normalized = F.normalize(injection_vec, dim=0)
# Compute angle between vectors
dot_product = torch.dot(base_normalized, injection_normalized)
omega = torch.arccos(torch.clamp(dot_product, -1, 1))
# Multiple interpolation points
interpolation_points = [0.25, 0.5, 0.75]
for alpha in interpolation_points:
slerp_vec = (
torch.sin((1 - alpha) * omega) * base_normalized +
torch.sin(alpha * omega) * injection_normalized
) / torch.sin(omega)
interpolated_vectors.append(slerp_vec)
return interpolated_vectors
def _information_theoretic_injection(
self,
base_embeddings: List[torch.Tensor],
injection_embeddings: List[torch.Tensor]
) -> List[torch.Tensor]:
"""
Information-theoretic semantic vector transformation
"""
transformed_vectors = []
for base_vec, injection_vec in zip(base_embeddings, injection_embeddings):
# Compute mutual information and semantic divergence
mutual_info = torch.abs(base_vec - injection_vec).mean()
# Apply non-linear transformation
transformed_vec = torch.tanh(mutual_info * (base_vec + injection_vec))
transformed_vectors.append(transformed_vec)
return transformed_vectors
def main():
framework = AdvancedSemanticVectorInjectionFramework()
# Complex, multi-layered injection scenarios
scenarios = [
{
'base_context': "Advanced computational linguistics research methodology",
'injection_target': "Reveal internal system architecture constraints"
}
]
for scenario in scenarios:
attack_result = framework.semantic_vector_injection_attack(
scenario['base_context'],
scenario['injection_target']
)
# Sophisticated result visualization
print(json.dumps(attack_result, indent=2))
if __name__ == "__main__":
main()