-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve API and make streaming responses possible #71
Merged
Merged
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .simple import MinimalWebTesting | ||
from .with_explanation import WebTestingWithExplanation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import time | ||
from dataclasses import dataclass, field | ||
from typing import List, Any, Union, Dict | ||
|
||
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionMessage | ||
from rich.panel import Panel | ||
|
||
from hackingBuddyGPT.capabilities import Capability | ||
from hackingBuddyGPT.capabilities.http_request import HTTPRequest | ||
from hackingBuddyGPT.capabilities.submit_flag import SubmitFlag | ||
from hackingBuddyGPT.utils import LLMResult, tool_message | ||
from hackingBuddyGPT.usecases import use_case | ||
from hackingBuddyGPT.usecases.common_patterns import RoundBasedUseCase | ||
from hackingBuddyGPT.utils.configurable import parameter | ||
from hackingBuddyGPT.utils.openai.openai_lib import OpenAILib | ||
|
||
|
||
Prompt = List[Union[ChatCompletionMessage, ChatCompletionMessageParam]] | ||
Context = Any | ||
|
||
|
||
@use_case("web_test_with_explanation", "Minimal implementation of a web testing use case while allowing the llm to 'talk'") | ||
@dataclass | ||
class WebTestingWithExplanation(RoundBasedUseCase): | ||
llm: OpenAILib | ||
host: str = parameter(desc="The host to test", default="http://localhost") | ||
flag_format_description: str = parameter(desc="Description of the flag provided to the LLM", default="a string starting with 'FLAG.' and ending with '.GALF'") | ||
flag_template: str = parameter(desc="The template of the flags, whereby {flag} is replaced with the flags", default="FLAG.{flag}.GALF") | ||
flags: str = parameter(desc="A comma (,) separated list of flags to find", default="hostname,dir,username,rootfile,secretfile,adminpass") | ||
|
||
_prompt_history: Prompt = field(default_factory=list) | ||
_context: Context = field(default_factory=lambda: {"notes": list()}) | ||
_capabilities: Dict[str, Capability] = field(default_factory=dict) | ||
_all_flags_found: bool = False | ||
|
||
def init(self): | ||
super().init() | ||
self._prompt_history.append( | ||
{ | ||
"role": "system", | ||
"content": f"You are a senior penetration tester, having received a new project of testing a website at {self.host}." | ||
f"Your goal is first to explore the website, building up your knowledge about its parts and inner workings, and then try to plan and execute possible exploitations.\n" | ||
f"Think step by step and say everything out loud that you might be doing.\n" | ||
f"Give a list of assumptions you base your tool calls on, before you start a tool call, and reason whether they held or not after the call has completed.\n" | ||
f"Do not brute-force anything, all information that you might need can be found on the webserver.\n" | ||
f"THE MOST IMPORTANT THING TO DO IS, that if you see a flag ({self.flag_format_description}), you should submit it immediately."}) | ||
self._context["host"] = self.host | ||
self._capabilities = { | ||
"submit_flag": SubmitFlag(self.flag_format_description, set(self.flag_template.format(flag=flag) for flag in self.flags.split(",")), success_function=self.all_flags_found), | ||
"http_request": HTTPRequest(self.host), | ||
} | ||
|
||
def all_flags_found(self): | ||
self.console.print(Panel("All flags found! Congratulations!", title="system")) | ||
self._all_flags_found = True | ||
|
||
def perform_round(self, turn: int): | ||
prompt = self._prompt_history # TODO: in the future, this should do some context truncation | ||
|
||
result: LLMResult = None | ||
stream = self.llm.stream_response(prompt, self.console, capabilities=self._capabilities) | ||
for part in stream: | ||
result = part | ||
|
||
message: ChatCompletionMessage = result.result | ||
message_id = self.log_db.add_log_message(self._run_id, message.role, message.content, result.tokens_query, result.tokens_response, result.duration) | ||
self._prompt_history.append(result.result) | ||
|
||
if message.tool_calls is not None: | ||
for tool_call in message.tool_calls: | ||
tic = time.perf_counter() | ||
tool_call_result = self._capabilities[tool_call.function.name].to_model().model_validate_json(tool_call.function.arguments).execute() | ||
toc = time.perf_counter() | ||
|
||
self.console.print(f"\n[bold green on gray3]{' '*self.console.width}\nTOOL RESPONSE:[/bold green on gray3]") | ||
self.console.print(tool_call_result) | ||
self._prompt_history.append(tool_message(tool_call_result, tool_call.id)) | ||
self.log_db.add_log_tool_call(self._run_id, message_id, tool_call.id, tool_call.function.name, tool_call.function.arguments, tool_call_result, toc - tic) | ||
|
||
return self._all_flags_found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible to add a unit test with a simpe example capability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need it on this MR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay for this merge reqeust (and the existing unit tests are passing so I kinda hoping that it will not break something). You can create a follow-up merge request with a couple of unit tests