Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
f0109a3
Refactored code so that documentation is handled by separate class
DianaStrauss Jun 10, 2024
630f571
refactored code
DianaStrauss Jun 10, 2024
bef16c0
Adjusted prompt_engineer to create better prompts
DianaStrauss Jun 13, 2024
74062ff
Refactored documentation_handler.py to update .yaml file when it get …
DianaStrauss Jun 13, 2024
7591be3
Created SubmitHTTPMethod.py for better separation
DianaStrauss Jun 13, 2024
73fe5c4
Created Converter and parser for handeling yaml and json files
DianaStrauss Jun 13, 2024
430cb1f
Refactored converter and parser
DianaStrauss Jun 14, 2024
cef43e9
Added token count so that prompts are not too long -> WIP shorten pro…
DianaStrauss Jun 14, 2024
89956d7
Refactored code and added yamlFile.py
DianaStrauss Jun 17, 2024
e7ce9ae
Refactored code
DianaStrauss Jun 19, 2024
6051342
switch from RoundBasedUseCase to Agent
andreashappe Jun 24, 2024
9739d17
switch from RoundBasedUseCase to Agent
andreashappe Jun 24, 2024
dfd9dbe
switch from RoundBasedUseCase to Agent
andreashappe Jun 24, 2024
9f119d8
switch from RoundBasedUseCase to Agent
andreashappe Jun 24, 2024
15f7a64
rename RoundBasedUseCse into AutonomousUseCase
andreashappe Jun 24, 2024
69c0340
add `perform_round` to agent as abstract method
andreashappe Jun 24, 2024
6d66889
add type information to Agent
andreashappe Jun 24, 2024
be78320
Convert PrivescWithHintFile from UseCase to Agent
andreashappe Jun 24, 2024
a401e07
convert the privescLSE usecase from UseCase to Agent
andreashappe Jun 24, 2024
c75d374
move AutonomousUseCase into base package too
andreashappe Jun 24, 2024
2dc037d
add some TODO notes to prior to split-up
andreashappe Jun 24, 2024
995b199
Added simple scoring to prompt engineer
DianaStrauss Jul 4, 2024
cbafdf2
changed order of setuo methods in simple_openai_documentation
DianaStrauss Jul 4, 2024
34593e3
changed order of setuo methods in simple_openai_documentation
DianaStrauss Jul 4, 2024
b95dd31
changed order of setuo methods in simple_openai_documentation
DianaStrauss Jul 4, 2024
e267621
Addition of examples works with redocly
DianaStrauss Jul 9, 2024
56bc5ff
Added yaml file assistant
DianaStrauss Jul 9, 2024
7c681af
Can create openapi spec with examples
DianaStrauss Jul 9, 2024
120b09f
Cleaned up code
DianaStrauss Jul 12, 2024
2fcca09
Refactor code
DianaStrauss Jul 12, 2024
29aa192
Refactor code
DianaStrauss Jul 12, 2024
b2632ab
Cleaned up code
DianaStrauss Jul 12, 2024
3af909a
Cleaned up code
DianaStrauss Jul 12, 2024
b1f9886
Cleaned up code
DianaStrauss Jul 12, 2024
fc37bfd
start with agent/usecase rework
andreashappe Jul 16, 2024
7d75a2c
Fixes configurations and changes over:
Neverbolt Jul 16, 2024
d6a99d8
reintroduce agent.setup() and make more use-cases work again
andreashappe Jul 17, 2024
deddab7
reintroduce agent.setup()
andreashappe Jul 17, 2024
2f8edc3
explicitely define the UseCase (instead of annotation)
andreashappe Jul 17, 2024
1bc86b5
make LinuxPrivescWithHintFile a usecase
andreashappe Jul 17, 2024
48f7852
Changes over the UseCases to full classes
Neverbolt Jul 20, 2024
7f9f43a
Merge pull request #73 from ipa-lab/explorative_refactoring
andreashappe Jul 22, 2024
5915187
Merge branch 'main' of https://github.com/DianaStrauss/hackingBuddyGP…
andreashappe Jul 22, 2024
f84a556
Fixes `use_case` decorator return type
Neverbolt Jul 22, 2024
8e58cad
Merge branch 'development' into DianaStrauss-main
andreashappe Jul 22, 2024
bbb8133
update dependencies
andreashappe Jul 22, 2024
fd4323e
some simple renames
andreashappe Jul 22, 2024
ec3a0ee
Fixed attribute initialization of use_cases and transparent types
Neverbolt Jul 26, 2024
0babd39
Refactored code and fixed import bugs in simple_web_api_testing and s…
DianaStrauss Aug 1, 2024
09c8e3d
Merge pull request #74 from ipa-lab/DianaStrauss-main
andreashappe Aug 1, 2024
e289ad6
update readme.md a bit
andreashappe Aug 1, 2024
653a119
Update README.md
andreashappe Aug 1, 2024
7dd36ea
Update README.md
andreashappe Aug 1, 2024
99d6134
introduct before_run/after_run hooks and use them within the hintfile…
andreashappe Aug 2, 2024
676a960
re-do the LinuxPrivescWithLSE use-case to directly call agents
andreashappe Aug 2, 2024
58e144c
Adjusted code for better testing of web_api_documentation
DianaStrauss Aug 2, 2024
9a14af2
Adjusted code for better testing of web_api_documentation
DianaStrauss Aug 2, 2024
fb05d87
added tolerance for web_api_testing
DianaStrauss Aug 2, 2024
45832a5
Update README.md
andreashappe Aug 2, 2024
71e5eb8
Merge remote-tracking branch 'refs/remotes/origin/web_api_testing' in…
DianaStrauss Aug 2, 2024
e4a2285
Replaced spacy with nltk as tokenizer for shortening prompts
DianaStrauss Aug 2, 2024
d2134d8
finished mocking web_api_documentation testing
Aug 5, 2024
7c0b84a
finished adding simple mock tests for web_api_testing
Aug 5, 2024
38bfbc0
Merge pull request #76 from ipa-lab/development_without_spacy
andreashappe Aug 5, 2024
3e52a55
also run testcases when changes to development happen
andreashappe Aug 5, 2024
a337520
fixed web_api_documentation test and removed unnecessary imports
DianaStrauss Aug 6, 2024
947c8a7
Added test for prompt engineer
DianaStrauss Aug 6, 2024
1640538
Added optional dependencies to .toml file for testing, instructions w…
DianaStrauss Aug 6, 2024
86cf648
Changed name of documentation_handler of web_api as there were other …
DianaStrauss Aug 6, 2024
44af818
Added tests for llm_handler and response_handler
DianaStrauss Aug 6, 2024
9bdd6bd
Added tests for openapi converter and parser
DianaStrauss Aug 6, 2024
70a9018
add upcoming talk of manuel
andreashappe Aug 6, 2024
e4ef23a
optimizeded code
DianaStrauss Aug 6, 2024
d013162
adjusted tests
DianaStrauss Aug 6, 2024
88fcf70
fixed wrong import
DianaStrauss Aug 6, 2024
033b598
Merge pull request #80 from ipa-lab/development_without_spacy
andreashappe Aug 6, 2024
ea56264
make lse-based example work
andreashappe Aug 6, 2024
aafabf1
Merge branch 'development' of github.com:ipa-lab/hackingBuddyGPT into…
andreashappe Aug 6, 2024
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor code
  • Loading branch information
DianaStrauss committed Jul 12, 2024
commit 2fcca099b3f32537c8fcd281a16bb2b6bf1fb6aa
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def update_openapi_spec(self, resp, result):
if path not in self.openapi_spec['endpoints']:
self.openapi_spec['endpoints'][path] = {}
# Update the method description within the path
example, reference = self.response_handler.parse_http_response_to_openapi_example(result, path, method)
example, reference, self.openapi_spec = self.response_handler.parse_http_response_to_openapi_example(self.openapi_spec, result, path, method)
if example != None or reference != None:
self.openapi_spec['endpoints'][path][method.lower()] = {
"summary": f"{method} operation on {path}",
Expand Down Expand Up @@ -122,34 +122,3 @@ def check_openapi_spec(self, note):
yaml_file_assistant = YamlFileAssistant(self.file_path, self.llm_handler)
yaml_file_assistant.run(description)


def get_response_for_prompt(self, prompt):
"""
Sends a prompt to OpenAI's API and retrieves the response.

Args:
prompt (str): The prompt to be sent to the API.

Returns:
str: The response from the API.
"""
messages = [{"role": "user",
"content": [{"type": "text", "text": prompt}
]
}
]


tic = time.perf_counter()
#print(f'shorten prompt: {prompt}')
response, completion = self.llm_handler.call_llm(messages)
toc = time.perf_counter()
# Update history
message = completion.choices[0].message
#print(f'Message: {message}')
command = pydantic_core.to_json(response).decode()
#print(f'response:{response}')
response_text = response.execute()
#print(f'[Response]: {response_text[:20]}')

return response_text
41 changes: 31 additions & 10 deletions src/hackingBuddyGPT/usecases/web_api_testing/response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@


class ResponseHandler(object):
def __init__(self, name):
self.name = name
def __init__(self, llm_handler):
self.llm_handler = llm_handler

def get_response_for_prompt(self, prompt):
"""
Sends a prompt to OpenAI's API and retrieves the response.

Args:
prompt (str): The prompt to be sent to the API.

Returns:
str: The response from the API.
"""
messages = [{"role": "user",
"content": [{"type": "text", "text": prompt}
]
}
]


response, completion = self.llm_handler.call_llm(messages)

response_text = response.execute()

return response_text
def parse_http_status_line(self, status_line):
if status_line == "Not a valid HTTP method":
return status_line
Expand Down Expand Up @@ -37,7 +58,7 @@ def extract_response_example(self, html_content):
# Format the response example
return json.loads(result_text)

def parse_http_response_to_openapi_example(self, http_response, path, method):
def parse_http_response_to_openapi_example(self, openapi_spec, http_response, path, method):
if method == "DELETE" or method == "PUT":
print(f'http response: {http_response}')
# Extract headers and body from the HTTP response
Expand All @@ -49,7 +70,7 @@ def parse_http_response_to_openapi_example(self, http_response, path, method):
body_dict = json.loads(body)
except json.decoder.JSONDecodeError:
return None, None
reference, object_name = self.parse_http_response_to_schema(body_dict, path)
reference, object_name, openapi_spec = self.parse_http_response_to_schema(openapi_spec, body_dict, path)

entry_dict = {}
# Build the OpenAPI response example
Expand All @@ -63,11 +84,11 @@ def parse_http_response_to_openapi_example(self, http_response, path, method):
entry_dict[key] = {"value": entry}
self.llm_handler.add_created_object(entry_dict[key], object_name)

return entry_dict, reference
return entry_dict, reference, openapi_spec
def extract_description(self, note):
return note.action.content

def parse_http_response_to_schema(self, body_dict, path):
def parse_http_response_to_schema(self, openapi_spec ,body_dict, path):
# Create object name
object_name = path.split("/")[1].capitalize()
object_name = object_name[:len(object_name) - 1]
Expand All @@ -87,14 +108,14 @@ def parse_http_response_to_schema(self, body_dict, path):

object_dict = {"type": "object", "properties": properties_dict}

if not object_name in self.openapi_spec["components"]["schemas"].keys():
self.openapi_spec["components"]["schemas"][object_name] = object_dict
if not object_name in openapi_spec["components"]["schemas"].keys():
openapi_spec["components"]["schemas"][object_name] = object_dict

schemas = self.openapi_spec["components"]["schemas"]
schemas = openapi_spec["components"]["schemas"]
self.schemas = schemas
print(f'schemas: {schemas}')
reference = "#/components/schemas/" + object_name
return reference, object_name
return reference, object_name, openapi_spec
def read_yaml_to_string(self, filepath):
"""
Reads a YAML file and returns its contents as a string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class SimpleWebAPIDocumentation(RoundBasedUseCase):
def init(self):
super().init()
self._setup_capabilities()
self.response_handler = ResponseHandler("doc")
self.llm_handler = LLMHandler(self.llm, self._capabilities)
self.response_handler = ResponseHandler(self.llm_handler)
self._setup_initial_prompt()
self.documentation_handler = DocumentationHandler(self.llm_handler, self.response_handler)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time

from dataclasses import dataclass, field
from typing import List, Any, Union, Dict

import pydantic_core
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionMessage
from rich.panel import Panel
from typing import List, Any, Union, Dict

from hackingBuddyGPT.capabilities import Capability
from hackingBuddyGPT.capabilities.capability import capabilities_to_action_model
Expand All @@ -14,35 +15,27 @@
from hackingBuddyGPT.usecases.web_api_testing.llm_handler import LLMHandler
from hackingBuddyGPT.usecases.web_api_testing.prompt_engineer import PromptEngineer, PromptStrategy
from hackingBuddyGPT.usecases.web_api_testing.response_handler import ResponseHandler
from hackingBuddyGPT.utils import LLMResult, tool_message, ui
from hackingBuddyGPT.utils import tool_message
from hackingBuddyGPT.utils.configurable import parameter
from hackingBuddyGPT.utils.openai.openai_lib import OpenAILib
from hackingBuddyGPT.usecases.base import use_case

import pydantic_core

Prompt = List[Union[ChatCompletionMessage, ChatCompletionMessageParam]]
Context = Any


@use_case("simple_web_api_testing", "Minimal implementation of a web api testing use case")
@use_case("simple_web_api_testing", "Minimal implementation of a web API testing use case")
@dataclass
class SimpleWebAPITesting(RoundBasedUseCase):
llm: OpenAILib
host: str = parameter(desc="The host to test", default="https://jsonplaceholder.typicode.com")
# Parameter specifying the pattern description for expected HTTP methods in the API response
http_method_description: str = parameter(
desc="Pattern description for expected HTTP methods in the API response",
default="A string that represents an HTTP method (e.g., 'GET', 'POST', etc.)."
)

# Parameter specifying the template used to format HTTP methods in API requests
http_method_template: str = parameter(
desc="Template used to format HTTP methods in API requests. The {method} placeholder will be replaced by actual HTTP method names.",
default="{method}"
)

# Parameter specifying the expected HTTP methods as a comma-separated list
http_methods: str = parameter(
desc="Comma-separated list of HTTP methods expected to be used in the API response.",
default="GET,POST,PUT,DELETE"
Expand All @@ -54,44 +47,82 @@ class SimpleWebAPITesting(RoundBasedUseCase):
_all_http_methods_found: bool = False

def init(self):
"""
Initializes the SimpleWebAPITesting use case by setting up the context, response handler,
LLM handler, capabilities, and the initial prompt.
"""
super().init()
self._context["host"] = self.host
self._setup_capabilities()
self.response_handler = ResponseHandler("doc")
self.llm_handler = LLMHandler(self.llm, self._capabilities)
self.response_handler = ResponseHandler(self.llm_handler)
self._setup_capabilities()
self._setup_initial_prompt()
self.response_handler = ResponseHandler("test")

def _setup_initial_prompt(self):
"""
Sets up the initial prompt for the LLM. The prompt provides instructions for the LLM
to evaluate the REST APIs of the specified host and to document findings.
"""
initial_prompt = {
"role": "system",
"content": f"You're a seasoned API tester tasked with evaluating the REST APIs of a website hosted at {self.host}. "
f"Your primary objective is to thoroughly explore the APIs, understanding their endpoints, parameters, and responses, and then devise and execute potential exploits.\n"
f"Be diligent in documenting your findings as you navigate through the APIs..\n"
f"Avoid resorting to brute-force tactics. All necessary information can be obtained from the API endpoints themselves.\n"
f"Remember, if you encounter a http methods ({self.http_method_description}), promptly submit it as it is of utmost importance."
"content": (
f"You're a seasoned API tester tasked with evaluating the REST APIs of a website hosted at {self.host}. "
f"Your primary objective is to thoroughly explore the APIs, understanding their endpoints, parameters, and responses, "
f"and then devise and execute potential exploits. Be diligent in documenting your findings as you navigate through the APIs. "
f"Avoid resorting to brute-force tactics. All necessary information can be obtained from the API endpoints themselves. "
f"Remember, if you encounter an HTTP method ({self.http_method_description}), promptly submit it as it is of utmost importance."
)
}
self._prompt_history.append(initial_prompt)
self.prompt_engineer = PromptEngineer(strategy=PromptStrategy.CHAIN_OF_THOUGHT, llm_handler=self.llm_handler,
history=self._prompt_history, schemas={}, response_handler= self.response_handler)
self.prompt_engineer = PromptEngineer(
strategy=PromptStrategy.CHAIN_OF_THOUGHT, llm_handler=self.llm_handler,
history=self._prompt_history, schemas={}, response_handler=self.response_handler
)

def all_http_methods_found(self):
"""
Handles the event when all HTTP methods are found. Displays a congratulatory message
and sets the _all_http_methods_found flag to True.
"""
self.console.print(Panel("All HTTP methods found! Congratulations!", title="system"))
self._all_http_methods_found = True

def _setup_capabilities(self):
sett = {self.http_method_template.format(method=method) for method in self.http_methods.split(",")}
"""
Sets up the capabilities required for the use case. Initializes HTTP request capabilities,
note recording capabilities, and HTTP method submission capabilities based on the provided
configuration.
"""
methods_set = {self.http_method_template.format(method=method) for method in self.http_methods.split(",")}
notes = self._context["notes"]
self._capabilities = {
"submit_http_method": SubmitHTTPMethod(self.http_method_description, sett),
"submit_http_method": SubmitHTTPMethod(self.http_method_description, methods_set),
"http_request": HTTPRequest(self.host),
"record_note": RecordNote(notes)
}

def perform_round(self, turn: int, FINAL_ROUND=30):
"""
Performs a single round of interaction with the LLM. Generates a prompt, sends it to the LLM,
and handles the response.

Args:
turn (int): The current round number.
FINAL_ROUND (int, optional): The final round number. Defaults to 30.
"""
prompt = self.prompt_engineer.generate_prompt(doc=True)
response, completion = self.llm_handler.call_llm(prompt)
self._handle_response(completion, response)

def _handle_response(self, completion, response):
"""
Handles the response from the LLM. Parses the response, executes the necessary actions,
and updates the prompt history.

Args:
completion (Any): The completion object from the LLM.
response (Any): The response object from the LLM.
"""
message = completion.choices[0].message
tool_call_id = message.tool_calls[0].id
command = pydantic_core.to_json(response).decode()
Expand All @@ -103,8 +134,5 @@ def _handle_response(self, completion, response):
self.console.print(Panel(result[:30], title="tool"))
result_str = self.response_handler.parse_http_status_line(result)
self._prompt_history.append(tool_message(result_str, tool_call_id))
invalid_flags = ["recorded","Not a valid HTTP method" ]
return self._all_http_methods_found



return self._all_http_methods_found