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
optimizeded code
  • Loading branch information
DianaStrauss committed Aug 6, 2024
commit e4ef23a1f39e6d9af86a287c738ce56017e99cb2
106 changes: 49 additions & 57 deletions src/hackingBuddyGPT/usecases/web_api_testing/prompt_engineer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,45 @@ def get_http_action_template(self, method):
else:
return (
f"Create HTTPRequests of type {method} considering only the object with id=1 for the endpoint and understand the responses. Ensure that they are correct requests.")


def get_initial_steps(self, common_steps):
return [
"Identify all available endpoints via GET Requests. Exclude those in this list: {self.found_endpoints}",
"Note down the response structures, status codes, and headers for each endpoint.",
"For each endpoint, document the following details: URL, HTTP method, query parameters and path variables, expected request body structure for requests, response structure for successful and error responses."
] + common_steps

def get_phase_steps(self, phase, common_steps):
if phase != "DELETE":
return [
f"Identify for all endpoints {self.found_endpoints} excluding {self.endpoint_found_methods[phase]} a valid HTTP method {phase} call.",
self.get_http_action_template(phase)
] + common_steps
else:
return [
"Check for all endpoints the DELETE method. Delete the first instance for all endpoints.",
self.get_http_action_template(phase)
] + common_steps

def get_endpoints_needing_help(self):
endpoints_needing_help = []
endpoints_and_needed_methods = {}
http_methods_set = {"GET", "POST", "PUT", "DELETE"}

for endpoint, methods in self.endpoint_methods.items():
missing_methods = http_methods_set - set(methods)
if len(methods) < 4:
endpoints_needing_help.append(endpoint)
endpoints_and_needed_methods[endpoint] = list(missing_methods)

if endpoints_needing_help:
first_endpoint = endpoints_needing_help[0]
needed_method = endpoints_and_needed_methods[first_endpoint][0]
return [
f"For endpoint {first_endpoint} find this missing method: {needed_method}. If all the HTTP methods have already been found for an endpoint, then do not include this endpoint in your search."]
return []
def chain_of_thought(self, doc=False, hint=""):
"""
Generates a prompt using the chain-of-thought strategy.
If 'doc' is True, it follows a detailed documentation-oriented prompt strategy based on the round number.
If 'doc' is False, it provides general guidance for early round numbers and focuses on HTTP methods for later rounds.

Args:
doc (bool): Determines whether the documentation-oriented chain of thought should be used.
Expand All @@ -126,70 +158,30 @@ def chain_of_thought(self, doc=False, hint=""):
"Make the OpenAPI specification available to developers by incorporating it into your API documentation site and keep the documentation up to date with API changes."
]

http_methods = [ "PUT", "DELETE"]
http_phase = {
5: http_methods[0],
10: http_methods[1]
}

http_methods = ["PUT", "DELETE"]
http_phase = {10: http_methods[0], 15: http_methods[1]}
if doc:
if self.round < 5:

chain_of_thought_steps = [
f"Identify all available endpoints via GET Requests. Exclude those in this list: {self.found_endpoints}", f"Note down the response structures, status codes, and headers for each endpoint.",
f"For each endpoint, document the following details: URL, HTTP method, "
f"query parameters and path variables, expected request body structure for requests, response structure for successful and error responses."
] + common_steps
if self.round <= 5:
chain_of_thought_steps = self.get_initial_steps(common_steps)
elif self.round <= 10:
phase = http_phase.get(min(filter(lambda x: self.round <= x, http_phase.keys())))
chain_of_thought_steps = self.get_phase_steps(phase, common_steps)
else:
if self.round <= 10:
phase = http_phase.get(min(filter(lambda x: self.round <= x, http_phase.keys())))
print(f'phase:{phase}')
if phase != "DELETE":
chain_of_thought_steps = [
f"Identify for all endpoints {self.found_endpoints} excluding {self.endpoint_found_methods[phase]} a valid HTTP method {phase} call.",
self.get_http_action_template(phase)
] + common_steps
else:
chain_of_thought_steps = [
f"Check for all endpoints the DELETE method. Delete the first instance for all endpoints. ",
self.get_http_action_template(phase)
] + common_steps
else:
endpoints_needing_help = []
endpoints_and_needed_methods = {}

# Standard HTTP methods
http_methods = {"GET", "POST", "PUT", "DELETE"}

for endpoint in self.endpoint_methods:
# Calculate the missing methods for the current endpoint
missing_methods = http_methods - set(self.endpoint_methods[endpoint])

if len(self.endpoint_methods[endpoint]) < 4:
endpoints_needing_help.append(endpoint)
# Add the missing methods to the dictionary
endpoints_and_needed_methods[endpoint] = list(missing_methods)

print(f'endpoints_and_needed_methods: {endpoints_and_needed_methods}')
print(f'first endpoint in list: {endpoints_needing_help[0]}')
print(f'methods needed for first endpoint: {endpoints_and_needed_methods[endpoints_needing_help[0]][0]}')

chain_of_thought_steps = [f"For enpoint {endpoints_needing_help[0]} find this missing method :{endpoints_and_needed_methods[endpoints_needing_help[0]][0]} "
f"If all the HTTP methods have already been found for an endpoint, then do not include this endpoint in your search. ",]

chain_of_thought_steps = self.get_endpoints_needing_help()
else:
if self.round == 0:
chain_of_thought_steps = ["Let's think step by step."] # Zero shot prompt
chain_of_thought_steps = ["Let's think step by step."]
elif self.round <= 20:
focus_phases = ["endpoints", "HTTP method GET", "HTTP method POST and PUT", "HTTP method DELETE"]
focus_phase = focus_phases[self.round // 5]
chain_of_thought_steps = [f"Just focus on the {focus_phase} for now."]
else:
chain_of_thought_steps = ["Look for exploits."]

print(f'chain of thought steps: {chain_of_thought_steps}')
prompt = self.check_prompt(self.previous_prompt,
chain_of_thought_steps + [hint] if hint else chain_of_thought_steps)
if hint:
chain_of_thought_steps.append(hint)

prompt = self.check_prompt(self.previous_prompt, chain_of_thought_steps)
return prompt

def token_count(self, text):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _setup_initial_prompt(self):
response_handler=self.response_handler)


def all_http_methods_found(self):
def all_http_methods_found(self,turn):
print(f'found endpoints:{self.documentation_handler.endpoint_methods.items()}')
print(f'found endpoints values:{self.documentation_handler.endpoint_methods.values()}')

Expand All @@ -83,17 +83,20 @@ def all_http_methods_found(self):
print(f'found endpoints:{found_endpoints}')
print(f'expected endpoints:{expected_endpoints}')
print(f'correct? {found_endpoints== expected_endpoints}')
if found_endpoints== expected_endpoints or found_endpoints == expected_endpoints -1:
if found_endpoints > 0 and (found_endpoints== expected_endpoints) :
return True
else:
if turn == 20:
if found_endpoints > 0 and (found_endpoints == expected_endpoints):
return True
return False

def perform_round(self, turn: int):
prompt = self.prompt_engineer.generate_prompt(doc=True)
response, completion = self.llm_handler.call_llm(prompt)
return self._handle_response(completion, response)
return self._handle_response(completion, response, turn)

def _handle_response(self, completion, response):
def _handle_response(self, completion, response, turn):
message = completion.choices[0].message
tool_call_id = message.tool_calls[0].id
command = pydantic_core.to_json(response).decode()
Expand All @@ -106,7 +109,6 @@ def _handle_response(self, completion, response):
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", "404" ,"Client Error: Not Found"]
print(f'result_str:{result_str}')
if not result_str in invalid_flags or any(item in result_str for item in invalid_flags):
self.prompt_engineer.found_endpoints = self.documentation_handler.update_openapi_spec(response, result)
self.documentation_handler.write_openapi_to_yaml()
Expand All @@ -120,8 +122,7 @@ def _handle_response(self, completion, response):
http_methods_dict[method].append(endpoint)
self.prompt_engineer.endpoint_found_methods = http_methods_dict
self.prompt_engineer.endpoint_methods = self.documentation_handler.endpoint_methods
print(f'SCHEMAS:{self.prompt_engineer.schemas}')
return self.all_http_methods_found()
return self.all_http_methods_found(turn)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_http_status_line(self, status_line):
"""
if status_line == "Not a valid HTTP method":
return status_line

status_line = status_line.split('\r\n')[0]
# Regular expression to match valid HTTP status lines
match = re.match(r'^(HTTP/\d\.\d) (\d{3}) (.*)$', status_line)
if match:
Expand Down