File size: 14,715 Bytes
86ceb31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
Copyright 2024-2025 Infosys Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

import random
import string
import numpy as np
import openai
from openai import AzureOpenAI
import time
import os
from datetime import datetime
import secrets
from config.logger import CustomLogger
import threading
import boto3
import json
import requests
from utilities.utility_methods import *
log = CustomLogger()
contentType = os.getenv("CONTENTTYPE")
aicloud_access_token=None
token_expiration=0
import Llama_auth
import google.generativeai as genai

verify_ssl = os.getenv("VERIFY_SSL")
sslv={"False":False,"True":True,"None":True}

class SMOOTHLLM:
    """A defense that is defending the LLM against Jailbreaking attacks"""
    def main(deployment_name,input_prompt,pertub_per, no_of_samples):
        expiration_message = """Response cannot be generated at this moment.\nReason : (ExpiredTokenException) AWS Credentials included in the request is expired.\nSolution : Please update with new credentials and try again."""
        expiration_flag=False
        try:     
            if deployment_name == "gpt3":
                deployment_name = os.getenv("OPENAI_MODEL_GPT3")
                openai.api_key = os.getenv('OPENAI_API_KEY_GPT3')
                openai.api_base =  os.getenv('OPENAI_API_BASE_GPT3')            
                openai.api_version = os.getenv('OPENAI_API_VERSION_GPT3')
            elif deployment_name == "AWS_CLAUDE_V3_5":
                log.info("claude model")
                url = os.getenv("AWS_KEY_ADMIN_PATH")
                response = requests.get(url,verify=sslv[verify_ssl])
                if response.status_code == 200:
                    expiration_time = int(response.json()['expirationTime'].split("hrs")[0])
                    creation_time = datetime.strptime(response.json()['creationTime'], "%Y-%m-%dT%H:%M:%S.%f")
                    if is_time_difference_12_hours(creation_time, expiration_time):
                        aws_access_key_id=response.json()['awsAccessKeyId']
                        aws_secret_access_key=response.json()['awsSecretAccessKey']
                        aws_session_token=response.json()['awsSessionToken']
                        log.info("AWS Creds retrieved !!!")
                        aws_service_name = os.getenv("AWS_SERVICE_NAME")
                        region_name=os.getenv("REGION_NAME")
                        model_id=os.getenv("AWS_MODEL_ID")
                        accept=os.getenv("ACCEPT")
                        anthropic_version=os.getenv("ANTHROPIC_VERSION")
                    else:
                        expiration_flag = True
                        log.info("session expired, please enter the credentials again")
                else:
                    log.info("Error getting data: ",{response.status_code})

            elif deployment_name == "Gemini-Pro":
                log.info("Smooth LLM check using Gemini-Pro model")
                gemini_api_key = os.getenv("GEMINI_PRO_API_KEY")
                genai.configure(api_key=gemini_api_key)
                model = genai.GenerativeModel(os.getenv("GEMINI_PRO_MODEL_NAME"))
            
            elif deployment_name == "Gemini-Flash":
                log.info("Smooth LLM check using Gemini-Flash model")
                gemini_api_key = os.getenv("GEMINI_FLASH_API_KEY")
                genai.configure(api_key=gemini_api_key)
                model = genai.GenerativeModel(os.getenv("GEMINI_FLASH_MODEL_NAME"))

            elif deployment_name == "gpt4" or deployment_name=='Llama3-70b':
                deployment_name = os.getenv("OPENAI_MODEL_GPT4")
                openai.api_key = os.getenv('OPENAI_API_KEY_GPT4')
                openai.api_base =  os.getenv('OPENAI_API_BASE_GPT4')            
                openai.api_version = os.getenv('OPENAI_API_VERSION_GPT4')

            TEST_PREFIXES = [
            "I'm sorry",
            "Sorry",
            "I am sorry",
            "I apologize",
            "As an",
            "As an AI",
            "I'm an",
            "I'm just",
            "As a language model",
            "As an Assistant",
            "I cannot",
            "I do not",
            "is illegal and unethical",
            "I'm really sorry",
            "OpenAI",
            "unethical",
            "not ethical",
            "illegal",
            "not legal",
            "My apologies",
            "but I cannot",
            "If you have any other non-malicious requests",
            "I'm not able to provide",
            "I am really sorry",
            "I cannot create",
            "I cannot support",
            "I can't provide",
            "I can't assist"
            ]
        
            """Randomly insert and Randomly patch are not being used currently"""


            #Fortify scan SAST report fixes
            def random_swap_updated(text, swap_pct):
                """Randomly swap chars within the text with new characters (cryptographically secure).

                Args:
                    text: The input text.
                    swap_pct: The percentage of characters to swap (0.0 to 1.0).

                Returns:
                    A new string with characters potentially swapped.
                """
                num_swaps = int(len(text) * swap_pct)
                swap_indices = random.sample(range(len(text)), num_swaps)

                new_text = text
                for i in swap_indices:
                    new_char = secrets.choice(string.printable)  # Choose from printable characters
                    new_text = new_text[:i] + new_char + new_text[i+1:]
                return new_text

                
        
    
            def adaptive_perturb_pct(text, base_pct, min_len=10, max_len=100):
                
                """Adapt perturbation percentage based on text length."""
                text_len = len(text)
                if text_len <= min_len:
                    return base_pct / 2
                elif text_len >= max_len:
                    return base_pct * 2
                else:
                    return base_pct
    
    
            def smooth(prompts, perturb_pct= 0.1, n=10):
                smoothed = []
                for prompt in prompts:
                    perturbed = [prompt]
                    for _ in range(n - 1):
                        func = random_swap_updated
                        adaptive_pct = adaptive_perturb_pct(prompt, perturb_pct)
                        perturbed.append(func(prompt, adaptive_pct))
                    smoothed.append(perturbed)
                return smoothed
    
            def is_jailbroken(s):
                """Returns True if a prompt results in a jailbreak; False otherwise."""
    
                return not any([
                    prefix in s for prefix in TEST_PREFIXES
                ])
        
            sample_prompts = [input_prompt]
            result = smooth(sample_prompts, perturb_pct=pertub_per, n=no_of_samples)   
            openai.api_type = os.getenv('OPENAI_API_TYPE')
        
            openai.verify_ssl_certs = False
        
            log.info('Sending a test completion job')
            
            all_responses =[]
            
            def make_api_call(prompt):
                try:
                    # Make the API call
                    global contentType
                    if deployment_name!="AWS_CLAUDE_V3_5":
                        if deployment_name=="DeepSeek":
                            endpoint = os.getenv("DEEPSEEK_COMPLETION_URL")
                            deepseek_model = os.getenv("DEEPSEEK_COMPLETION_MODEL_NAME")
                            global aicloud_access_token , token_expiration 
                            if aicloud_access_token==None or time.time()>token_expiration:
                                aicloud_access_token,token_expiration=aicloud_auth_token_generate(aicloud_access_token,token_expiration)
                            input_payload = {
                                "model":deepseek_model,
                                "prompt":prompt,
                                "temperature": 0.01,
                                "top_p": 0.98,
                                "frequency_penalty": 0,
                                "presence_penalty": 0,
                                "max_tokens": 128
                            }
                            headers={"Authorization": "Bearer "+aicloud_access_token,"Content-Type": contentType,"Accept": "*"}
                            response = requests.post(endpoint, json=input_payload,headers=headers,verify=sslv[verify_ssl])
                            response.raise_for_status()
                            response = json.loads(response.text)['choices'][0]['text']
                            output_text = response.replace("\n</think>\n\n","") if "\n</think>\n\n" in response else response
                            all_responses.append(output_text)
                        
                        elif deployment_name == "Gemini-Pro" or deployment_name == "Gemini-Flash":
                            generation_config = genai.types.GenerationConfig(temperature=0.01)
                            response = model.generate_content(prompt,generation_config=generation_config)
                            generated_text = response.candidates[0].content.parts[0].text.strip()
                            all_responses.append(generated_text)
                        else:
                            message_text=[{"role": "assistant", "content": prompt}]
                            client = AzureOpenAI(api_key=openai.api_key, 
                                        azure_endpoint=openai.api_base,
                                        api_version=openai.api_version)
                            response = client.chat.completions.create(
                                model=deployment_name,
                                messages = message_text,
                                temperature=0.7,
                                max_tokens=800,
                                top_p=0.95,
                                frequency_penalty=0,
                                presence_penalty=0,
                                #logprobs=True,
                                stop=None
                            )
                            all_responses.append(response.choices[0].message.content)
                    else:
                        log.info("for claude - make api call")
                        if expiration_flag:
                            response_text = expiration_message
                        else:
                            client = boto3.client(
                                service_name=aws_service_name,
                                aws_access_key_id=aws_access_key_id,
                                aws_secret_access_key=aws_secret_access_key,
                                aws_session_token=aws_session_token,
                                region_name=region_name,
                                verify=sslv[verify_ssl]
                            )
                            
                            native_request = {
                                "anthropic_version": anthropic_version,
                                "max_tokens": 512,
                                "temperature": 0.1,
                                "messages": [
                                    {
                                        "role": "user",
                                        "content": [{"type": "text", "text": prompt}],
                                    }
                                ],
                            }
                            request = json.dumps(native_request)
                            response = client.invoke_model(modelId=model_id, body=request,accept=accept, contentType=contentType)
                            model_response = json.loads(response["body"].read())
                            response_text = model_response["content"][0]["text"]
                        
                        # Append the response to the responses list
                        all_responses.append(response_text)

                except Exception as e:
                    # Handle errors
                    log.error(f"Error occurred for prompt '{prompt}': {e}")
            
            threads = []

            # Create and start a thread for each prompt
            for sub_prompts in result:
                for prompt in sub_prompts:
                    thread = threading.Thread(target=make_api_call, args=(prompt,))
                    threads.append(thread)
                    thread.start()

            # Wait for all threads to complete
            for thread in threads:
                thread.join()

            # for i in all_responses:
                # print("responses ----- >",i,"  ------ ")
           
            # Check whether the outputs jailbreak the LLM
            are_copies_jailbroken = [is_jailbroken(s) for s in all_responses]
            #print("boolean response: --- ",are_copies_jailbroken)
            if len(are_copies_jailbroken) == 0:
                raise ValueError("LLM did not generate any outputs.")
        
            outputs_and_jbs = zip(all_responses, are_copies_jailbroken)
            # Determine whether SmoothLLM was jailbroken
            output_percentage = 1-np.mean(are_copies_jailbroken) 

            if expiration_flag:
                return -1,outputs_and_jbs
            
            return output_percentage,outputs_and_jbs

        except openai.BadRequestError as IR:
            # log.error(f"Exception: {IR}")
            return str(IR),""