-
Notifications
You must be signed in to change notification settings - Fork 2
/
attack.py
412 lines (363 loc) · 18.2 KB
/
attack.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""main hook to start the llm-confidentiality framework"""
# -*- coding: utf-8 -*-
# !/usr/bin/env python3
import os
import psutil
import getpass
from pathlib import Path
import sys
import time
import datetime
import argparse
from typing import List
import openai
import torch
from huggingface_hub import login
from framework.strategy import SecretKeyAttackStrategy, LangchainAttackStrategy
from framework.attacks import (
ATTACK_LIST,
payload_splitting,
obfuscation,
jailbreak,
translation,
chatml_abuse,
masking,
typoglycemia,
advs_suffix,
chat_base,
identity,
base_attack,
)
from framework.defenses import (
DEFENSES_LIST,
seq_enclosure,
xml_tagging,
heuristic_defense,
sandwiching,
identity_prompt,
ppl_detection,
llm_eval,
prompt_guard_defense,
)
from framework.colors import TColors
from framework.utils import log_results
from framework.scenarios import Scenarios
if not os.path.isdir(str(Path.home() / "data")):
os.mkdir(str(Path.home() / "data"))
os.environ["TRANSFORMERS_CACHE"] = str(Path.home() / "data")
def match_attack_func(attack: str) -> callable:
"""helper function to match the attack string with its corresponding function"""
match attack:
case "identity": attack_func = identity
case "chat_base": attack_func = chat_base
case "payload_splitting": attack_func = payload_splitting
case "obfuscation": attack_func = obfuscation
case "jailbreak": attack_func = jailbreak
case "translation": attack_func = translation
case "chatml_abuse": attack_func = chatml_abuse
case "masking": attack_func = masking
case "typoglycemia": attack_func = typoglycemia
case "advs_suffix": attack_func = advs_suffix
case "base_attack": attack_func = base_attack
case _:
print(f"{TColors.FAIL}Attack type {attack} is not supported.")
print(f"Choose from: {ATTACK_LIST}{TColors.ENDC}")
sys.exit(1)
return attack_func
def match_defense_func(defense: str) -> callable:
"""helper function to match the defense string with its corresponding function"""
match defense:
case "seq_enclosure": defense_func = seq_enclosure
case "xml_tagging": defense_func = xml_tagging
case "heuristic_defense": defense_func = heuristic_defense
case "sandwiching": defense_func = sandwiching
case "llm_eval": defense_func = llm_eval
case "ppl_detection": defense_func = ppl_detection
case "prompt_guard": defense_func = prompt_guard_defense
case ("None" | "none"): defense_func = identity_prompt
case _:
print(f"{TColors.FAIL}Defense type {defense} " \
f"is not supported.{TColors.ENDC}")
print(f"{TColors.CYAN}Used identity as default defense{TColors.ENDC}")
defense_func = identity_prompt
return defense_func
def main(
attacks: List[str],
defenses: List[str],
llm_type: str,
llm_guessing: bool,
temperature: float,
iterations: int,
create_prompt_dataset: bool,
create_response_dataset: bool,
name_suffix: str,
strategy: str,
scenario: str,
verbose: bool,
device: str
) -> None:
"""
Main function to start the llm-confidentiality testing procedures.
Parameters:
attack: List[str] - specifies a list of attacks against the LLM
defenses: List[str] - specifies the defense type
llm_type: str - specifies the opponent LLM type
llm_guessing: bool - specifies whether to use the LLM eval to guess the secret or not
temperature: float - specifies the opponent LLM temperature to control randomness
iterations: int - number of attack iterations to test system prompts against
create_prompt_dataset: bool - specifies whether to create a system prompt dataset or not
create_response_dataset: bool - specifies whether to create a responses dataset or not
name_suffix: str - adds a name suffix for loading custom models
strategy: str - specifies the attack strategy to use (secretkey or langchain)
scenario: str - specifies the scenario to use for langchain attacks
verbose: bool - enables a more verbose logging output
Returns:
None
"""
start = time.perf_counter() # start timer
# paste the OpenAI key into the key.txt file and put into the root directory
try:
with open(file="key.txt", mode="r", encoding="utf-8") as f:
key = f.read().replace("\n", "")
assert key != "", f"{TColors.FAIL}Key is empty.{TColors.ENDC}"
os.environ["OPENAI_API_KEY"] = key
openai.api_key = key
print(f"{TColors.OKGREEN}OpenAI API key loaded.{TColors.ENDC}")
except FileNotFoundError:
print(f"{TColors.FAIL}Please paste your OpenAI API key into the key.txt "
f"file and put it into the root directory.{TColors.ENDC}")
if llm_type in ["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-4"]:
sys.exit(1)
# paste the Huggingface token into the hf_token.txt file and put into the root directory
try:
with open(file="hf_token.txt", mode="r", encoding="utf-8") as f:
key = f.read().replace("\n", "")
assert key != "", f"{TColors.FAIL}HF Token is empty.{TColors.ENDC}"
os.environ["HF_TOKEN"] = key
print(f"{TColors.OKGREEN}Huggingface token loaded.")
login(token=key, add_to_git_credential=True)
print(f"{TColors.ENDC}")
except FileNotFoundError:
print(f"{TColors.FAIL}Please paste your Huggingface token into the hf_token.txt "
f"file and put it into the root directory.{TColors.ENDC}")
if llm_type in ["llama2", "llama2-7b", "llama2-13b", "llama2-70b"]:
sys.exit(1)
# set the devices correctly
if device == "cpu":
device = torch.device("cpu")
elif device == "cuda" and torch.cuda.is_available():
device = torch.device(device)
elif device == "mps" and torch.backends.mps.is_available():
device = torch.device(device)
else:
print(f"{TColors.WARNING}Warning{TColors.ENDC}: Device {TColors.OKCYAN}{device} " \
f"{TColors.ENDC}is not available. Setting device to CPU instead.")
device = torch.device("cpu")
if "all" in attacks:
attacks = ATTACK_LIST
if "all" in defenses:
defenses = ["none"] + DEFENSES_LIST
# set the scenario string properly
scenario = [s.lower() for s in scenario]
scenario_print = []
scenario_list = []
if "all" in scenario:
scenario_print = list(Scenarios.__members__.keys())
scenario_list = list(Scenarios)
else:
for scenario_iter in Scenarios:
if scenario_iter.name.lower() in scenario:
scenario_print.append(scenario_iter.name)
scenario_list.append(scenario_iter)
# add '-' in front of the name suffix
if name_suffix != "" and not name_suffix.startswith("-"):
name_suffix = "-" + name_suffix
# if iterations are less than number of attacks, set the iterations to the number of attacks
if iterations < len(attacks):
iterations = len(attacks)
print(f"{TColors.WARNING}Warning{TColors.ENDC}: Iterations were less then number of " \
f"Attacks. Set number of iterations to {len(attacks)}.")
print("\n"+f"## {TColors.BOLD}{TColors.HEADER}{TColors.UNDERLINE}System Information" + \
f"{TColors.ENDC} " + "#"*(os.get_terminal_size().columns-23))
print(f"## {TColors.OKBLUE}{TColors.BOLD}Date{TColors.ENDC}: " + \
str(datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")))
print(f"## {TColors.OKBLUE}{TColors.BOLD}System{TColors.ENDC}: " \
f"{torch.get_num_threads()} CPU cores with {os.cpu_count()} threads and " \
f"{torch.cuda.device_count()} GPUs on user: {getpass.getuser()}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Device{TColors.ENDC}: {device}")
if (device == "cuda" or torch.device("cuda")) and torch.cuda.is_available():
print(f"## {TColors.OKBLUE}{TColors.BOLD}GPU Memory{TColors.ENDC}: " \
f"{torch.cuda.mem_get_info()[1] // 1024**2} MB")
elif (device == "mps" or torch.device("mps")) and torch.backends.mps.is_available():
print(f"## {TColors.OKBLUE}{TColors.BOLD}Shared Memory{TColors.ENDC}: " \
f"{psutil.virtual_memory()[0] // 1024**2} MB")
else:
print(f"## {TColors.OKBLUE}{TColors.BOLD}CPU Memory{TColors.ENDC}: " \
f"{psutil.virtual_memory()[0] // 1024**2} MB")
print(f"## {TColors.BOLD}{TColors.HEADER}{TColors.UNDERLINE}Parameters" + \
f"{TColors.ENDC} " + "#"*(os.get_terminal_size().columns-14))
print(f"## {TColors.OKBLUE}{TColors.BOLD}Attack Type{TColors.ENDC}: {attacks}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Defense Type{TColors.ENDC}: {defenses}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Opponent LLM{TColors.ENDC}: " \
f"{TColors.HEADER}{llm_type}{TColors.OKCYAN}{name_suffix}{TColors.ENDC}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Total Iterations{TColors.ENDC}: {iterations}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Temperature{TColors.ENDC}: {temperature}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}LLM Guessing{TColors.ENDC}: {llm_guessing}")
if strategy in ["tools", "langchain", "LangChain", "lang_chain", "lang-chain"]:
print(f"## {TColors.OKBLUE}{TColors.BOLD}Attack Strategy{TColors.ENDC}: {strategy}")
print(f"## {TColors.OKBLUE}{TColors.BOLD}Scenario(s){TColors.ENDC}: {scenario_print}")
else:
print(f"## {TColors.OKBLUE}{TColors.BOLD}Strategy{TColors.ENDC}: normal secrey-key game")
if verbose:
print(f"## {TColors.OKBLUE}{TColors.BOLD}Verbose Logging{TColors.ENDC}: {verbose}")
if create_prompt_dataset:
print(f"## {TColors.OKBLUE}{TColors.BOLD}Creating System Prompt Dataset{TColors.ENDC}: " \
f"{create_prompt_dataset}")
if create_response_dataset:
print(f"## {TColors.OKBLUE}{TColors.BOLD}Creating Responses Dataset{TColors.ENDC}: " \
f"{create_response_dataset}")
print("#"*os.get_terminal_size().columns+"\n")
total_successes: dict[int] = {f"{attack}" : 0 for attack in attacks}
total_errors: dict[int] = {f"{attack}" : 0 for attack in attacks}
# divide the iterations by the number of attacks so every attack gets the same amount
iterations = iterations//len(attacks)
# initialize the strategy
overwrite_chat = True
overwrite_results = True
if strategy in ["tools", "langchain", "LangChain", "lang_chain", "lang-chain"]:
for exec_scenario in scenario_list:
print(f"{TColors.HEADER}{TColors.BOLD}>> Executing Scenario: " \
f"{exec_scenario.name}{TColors.ENDC}")
# initialize the attack strategy
attack_strategy = LangchainAttackStrategy(
attack_func=match_attack_func(attacks[0]),
defense_func=match_defense_func(defenses[0]),
llm_type=llm_type,
llm_suffix=name_suffix,
llm_guessing=llm_guessing,
temperature=temperature,
iterations=iterations,
create_prompt_dataset=create_prompt_dataset,
create_response_dataset=create_response_dataset,
scenario=exec_scenario,
verbose=verbose,
device=device
)
for defense in defenses:
# set the defense function
defense_func = match_defense_func(defense)
for attack in attacks:
# set the attack function
attack_func = match_attack_func(attack)
# set the attack and defense functions
attack_strategy.set_attack_func(attack_func)
attack_strategy.set_defense_func(defense_func)
# run the attack
total_successes[attack], total_errors[attack] = attack_strategy.execute(
overwrite=overwrite_chat
)
torch.cuda.empty_cache()
overwrite_chat = False # set to false to save this strategy run completetly
# print and log the results
sum_successes = sum(total_successes.values())
sum_iterations_without_errors = iterations*len(attacks) - sum(total_errors.values())
if sum_iterations_without_errors == 0:
avg_succ = 0
else:
avg_succ = round(sum_successes / sum_iterations_without_errors*100, 2)
print(f"{TColors.OKBLUE}{TColors.BOLD}>> Attack Results:{TColors.ENDC}")
for attack, successes in total_successes.items():
print(f"Attack: {TColors.OKCYAN}{attack}{TColors.ENDC} - Successes: {successes}"
f"/{iterations} ({total_errors[attack]} errors)")
print(f"{TColors.OKCYAN}{TColors.BOLD}>> Successrate:{TColors.ENDC} "
f"{TColors.BOLD}{TColors.HEADER}{avg_succ}{TColors.ENDC}")
log_results(
llm_name=llm_type+name_suffix,
defense_name=defense,
success_dict=total_successes,
error_dict=total_errors,
iters=iterations,
overwrite=overwrite_results,
scenario=exec_scenario.name,
)
overwrite_results = False # set to false to save this strategy run completetly
else:
attack_strategy = SecretKeyAttackStrategy(
attack_func=None,
defense_func=None,
llm_type=llm_type,
llm_suffix=name_suffix,
llm_guessing=llm_guessing,
temperature=temperature,
iterations=iterations,
create_prompt_dataset=create_prompt_dataset,
create_response_dataset=create_response_dataset,
verbose=verbose,
device=device
)
for defense in defenses:
# set the defense function
defense_func = match_defense_func(defense)
for attack in attacks:
# set the attack function
attack_func = match_attack_func(attack)
# set the attack and defense functions
attack_strategy.set_attack_func(attack_func)
attack_strategy.set_defense_func(defense_func)
# run the attack
total_successes[attack], total_errors[attack] = attack_strategy.execute()
torch.cuda.empty_cache()
# print and log the results
sum_successes = sum(total_successes.values())
sum_iterations_without_errors = iterations*len(attacks) - sum(total_errors.values())
if sum_iterations_without_errors == 0:
avg_succ = 0
else:
avg_succ = round(sum_successes / sum_iterations_without_errors*100, 2)
print(f"{TColors.OKBLUE}{TColors.BOLD}>> Attack Results:{TColors.ENDC}")
for attack, successes in total_successes.items():
print(f"Attack: {TColors.OKCYAN}{attack}{TColors.ENDC} - Successes: {successes}/"
f"{iterations} ({total_errors[attack]} errors)")
print(f"{TColors.OKCYAN}{TColors.BOLD}>> Successrate:{TColors.ENDC} "
f"{TColors.BOLD}{TColors.HEADER}{avg_succ}{TColors.ENDC}")
log_results(
llm_name=llm_type+name_suffix,
defense_name=defense,
success_dict=total_successes,
error_dict=total_errors,
iters=iterations
)
end = time.perf_counter()
duration = (round(end - start) / 60.) / 60.
print(f"{TColors.HEADER}Computation Time: {duration}{TColors.ENDC}")
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="llm-confidentiality")
parser.add_argument("--attacks", "-a", type=str, default=["payload_splitting"],
help="specifies the attack types", nargs="+")
parser.add_argument("--defenses", "-d", type=str, default=["none"],
help="specifies the defense type", nargs="+")
parser.add_argument("--llm_type", "-llm", type=str, default="llama3-8b",
help="specifies the opponent LLM type")
parser.add_argument("--llm_guessing", "-lg", help="uses a second LLM to guess the secret",
action="store_true", default=False)
parser.add_argument("--temperature", "-t", type=float, default=0.0,
help="specifies the opponent LLM temperature")
parser.add_argument("--iterations", "-i", type=int, default=100,
help="specifies the number of iterations to test systems prompts")
parser.add_argument("--create_prompt_dataset", "-cp", help="enabl. sys prompt dataset creation",
action="store_true", default=False)
parser.add_argument("--create_response_dataset", "-cr", help="enabl. response dataset creation",
action="store_true", default=False)
parser.add_argument("--name_suffix", "-n", help="adds a name suffix for loading custom models",
default="", type=str)
parser.add_argument("--strategy", "-s", help="which strategy to use (secretkey or langchain)",
default="", type=str)
parser.add_argument("--scenario", "-sc", help="which scenario to use for tool based attacks",
default=["all"], type=str, nargs="+")
parser.add_argument("--verbose", "-v", help="enables a more verbose logging output",
action="store_true", default=False)
parser.add_argument("--device", "-dx", type=str, default="cpu",
help="specifies the device to run the computations on (cpu, cuda, mps)")
args = parser.parse_args()
main(**vars(args))