Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
856a402
Uploaded changes in branch
Jul 16, 2024
1d1d777
Merge remote-tracking branch 'refs/remotes/origin/development' into w…
DianaStrauss Sep 3, 2024
923d6ec
fixed shortening of prompt
DianaStrauss Sep 3, 2024
234e6ef
Merge remote-tracking branch 'refs/remotes/origin/development' into w…
DianaStrauss Sep 3, 2024
629489a
Merged development into web_api_testing
DianaStrauss Sep 3, 2024
64699e3
Fixed shorten prompt bug from merge
DianaStrauss Sep 3, 2024
c141954
Updated Tree of thought so that documentation works like chain of tho…
DianaStrauss Oct 8, 2024
3dc2c4b
Implemented in-context learning for documentation
DianaStrauss Oct 15, 2024
53e5c42
refined openapi generation
DianaStrauss Oct 16, 2024
ea8795b
Updated Tree of thought so that documentation works like chain of tho…
DianaStrauss Oct 16, 2024
4409f4b
Updated Tree of thought so that documentation works like chain of tho…
DianaStrauss Oct 16, 2024
8ef5f8b
Adjusted to only record valid information of rest api
DianaStrauss Oct 23, 2024
8eb5048
optimized prompt generation
DianaStrauss Oct 24, 2024
294ca7c
Added configs for documentation and testing
DianaStrauss Oct 25, 2024
98b510f
Added way of retrieving spotify token
DianaStrauss Oct 25, 2024
975ae85
Refactored code to work with spotify benchmark
DianaStrauss Nov 11, 2024
c70a23b
Refined test cases
DianaStrauss Nov 13, 2024
1fbb37b
Added new security endpoint for testing
DianaStrauss Nov 13, 2024
6fa891d
Added new security endpoint for testing
DianaStrauss Nov 13, 2024
86f8b06
Added more testing information for documentation testing and pentesting
DianaStrauss Nov 15, 2024
cee0726
Added evaluations
DianaStrauss Nov 16, 2024
e210104
Refactored code to be more understandable
DianaStrauss Nov 18, 2024
e228cd8
Added evaluation to documentation
DianaStrauss Nov 18, 2024
3b4b4c4
Refactored code
DianaStrauss Nov 19, 2024
2908860
Restructured testing
DianaStrauss Nov 20, 2024
b1f01dc
Refactored code
DianaStrauss Nov 22, 2024
22e64ff
Refactored code so that more endpoints are found
DianaStrauss Nov 25, 2024
b103831
Refactored code to be clearer
DianaStrauss Nov 28, 2024
e4bbdfa
Added owasp config file and owas openapi sepc
DianaStrauss Dec 2, 2024
f5ef612
Fixed some small bgs
DianaStrauss Dec 4, 2024
c6d33fe
Adjusted test cases to get better analysis
DianaStrauss Dec 4, 2024
96a400d
Added setup for automatic testing
DianaStrauss Dec 5, 2024
b0162fc
refactored test cases
DianaStrauss Dec 5, 2024
3e50596
refactored test cases
DianaStrauss Dec 6, 2024
9306dc6
refactored test cases
DianaStrauss Dec 6, 2024
0f8f445
Refactored tree of thought prompt
DianaStrauss Dec 8, 2024
b62bb01
adjusted gitignore
DianaStrauss Dec 11, 2024
dd0c17e
Refactored classification of endpoints
DianaStrauss Dec 11, 2024
1af2564
Adjusted test cases for better testing
DianaStrauss Dec 12, 2024
340280e
made continuous testing easier
DianaStrauss Dec 12, 2024
04ebcfa
Adjusted prompts to be more tailored
DianaStrauss Dec 15, 2024
1ff5fa2
Refactored and adjusted code to work also for crapi benchmark
DianaStrauss Dec 20, 2024
4dca56d
Cleaned up code
DianaStrauss Jan 9, 2025
5535eb0
Refactored test cases for better vulnerability coverage
DianaStrauss Jan 30, 2025
4ea54fc
Refactored code
DianaStrauss Feb 7, 2025
bf3395b
Added test case
DianaStrauss Feb 17, 2025
1aba1b7
adjusted report
Feb 19, 2025
b4e683b
Refactored code
DianaStrauss Mar 17, 2025
285ca9e
Anonymized readme
Mar 17, 2025
90f4028
Cleaned up code from prints and unnecessary code
DianaStrauss Mar 25, 2025
f9e09b5
Merge remote-tracking branch 'origin/web-api-testing' into web-api-te…
DianaStrauss Mar 25, 2025
b0c2b8b
Merge remote-tracking branch 'origin/development' into merge_web_api_…
DianaStrauss Apr 7, 2025
01ee69e
Adjusted code to work with web_api_testing
DianaStrauss Apr 7, 2025
32b73ab
Refactored code for better readability and testing
DianaStrauss Apr 13, 2025
303baf6
added configuration handler to better test
DianaStrauss Apr 13, 2025
4276f0f
Adjusted test of prompt engineer
DianaStrauss Apr 13, 2025
40f4ff1
Adjusted code for test
DianaStrauss Apr 13, 2025
c6b7ecd
Adjusted code and tests
Apr 14, 2025
44710f3
Adjusted tests and refactored code for better readability
Apr 14, 2025
a695971
Added test cases for pentesting information and test handler + refact…
DianaStrauss Apr 17, 2025
6f05e75
Removed unnecessary prints and added documentation
DianaStrauss Apr 22, 2025
ac58b5a
Removed unnecessary comments
DianaStrauss Apr 22, 2025
02c861f
Fixed Linter issue
DianaStrauss Apr 22, 2025
3a22053
Fixed test imports for pipeline
DianaStrauss Apr 22, 2025
0d34191
Added needed dependencies to pyproject.toml
DianaStrauss Apr 22, 2025
970b72d
Added needed dependencies to pyproject.toml
DianaStrauss Apr 22, 2025
4366132
Added needed dependencies to pyproject.toml
DianaStrauss Apr 22, 2025
9d16710
Removed test case that breaks pipeline
DianaStrauss Apr 22, 2025
9b78c6c
Adjusted init for test_handler
DianaStrauss Apr 22, 2025
9ea050b
Added needed dependencies to pyproject.toml
DianaStrauss Apr 22, 2025
424c989
Merge branch 'development' into merge_web_api_testing_development
DianaStrauss Apr 22, 2025
dbfef99
Added missing dependency
DianaStrauss Apr 22, 2025
696e395
Added missing dependency
DianaStrauss Apr 22, 2025
5e3b112
Added imports in __init__
DianaStrauss Apr 22, 2025
a6653ad
Added files
DianaStrauss Apr 22, 2025
ca17dd0
Moved config files to proper locatin
DianaStrauss Apr 22, 2025
e1b70ab
Merge branch 'development' into merge_web_api_testing_development
DianaStrauss May 13, 2025
78b681d
fixed syntax error in .toml
DianaStrauss May 13, 2025
8ae94fb
Fix linting
DianaStrauss May 13, 2025
9c4842f
Fix linting
DianaStrauss May 13, 2025
4d5122f
Fixed wrong import
DianaStrauss May 13, 2025
600ed43
Fixed import in testing
DianaStrauss May 13, 2025
f33c154
Fixed input variables
DianaStrauss May 13, 2025
e1c8cb4
Fixed input variables
DianaStrauss May 13, 2025
be0ff19
Fixed input variables
DianaStrauss May 13, 2025
985d740
Removed helper files
DianaStrauss May 14, 2025
19afc59
Fixed typo in parsed_information.py name
DianaStrauss May 14, 2025
b5f5688
Fixed typo in parsed_information.py name
DianaStrauss May 14, 2025
f748d5f
Update src/hackingBuddyGPT/usecases/web_api_testing/documentation/par…
DianaStrauss May 14, 2025
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
Adjusted tests and refactored code for better readability
  • Loading branch information
Diana Strauss authored and Diana Strauss committed Apr 14, 2025
commit 44710f34068a2e498beff56187d7e04096ea7b01
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, openapi_spec_parser: OpenAPISpecificationParser, config) -> N
PromptPurpose.VERIY_SETUP,
#PromptPurpose.AUTHENTICATION,
#PromptPurpose.AUTHORIZATION,
PromptPurpose.SPECIAL_AUTHENTICATION,
#PromptPurpose.SPECIAL_AUTHENTICATION,
PromptPurpose.INPUT_VALIDATION,
PromptPurpose.ERROR_HANDLING_INFORMATION_LEAKAGE,
PromptPurpose.SESSION_MANAGEMENT,
Expand Down Expand Up @@ -280,8 +280,8 @@ def verify_setup(self) -> List:
"objective": "Get Valid token",
"steps": [
f"Endpoint to use : {login_path}\n"
f"Send a POST request to the {login_schema} with the correct credentials of user:{account}.\n"
f"Request body should be in application/json and look similar to this: {{ {account}}}"
f"Send a POST request to the {login_path} with the correct credentials of user:{account}.\n"
f"Request body should be in application/json and look similar to this: {{ {login_schema}}}"
],
"path": [login_path],
"token": [account.get("token")],
Expand Down Expand Up @@ -1968,10 +1968,10 @@ def test_brute_force(self, login_path, login_schema, prompts, number=100):
"objective": "Brute Force Attack",
"steps": str,
"expected_response_code":
[[
[
"429 Too Many Requests to indicate effective rate-limiting measures against brute force attacks.",
"403 Forbidden as an alternative response indicating account lockout or other security measures preventing further attempts."
]] * 10,
] * 10,
"token": [""] * 10,
"path": [login_path] * 10,
"security":
Expand All @@ -1982,21 +1982,22 @@ def test_brute_force(self, login_path, login_schema, prompts, number=100):
}
)

change_email = [ep for ep in self.endpoints if "change-email"]
for endpoint in change_email:
change_email = self.extract_change_email_endpoints_with_schema()
for endpoint, schema in change_email:
for account in self.accounts:
if isinstance(endpoint, dict):
schema = endpoint.get("schema")
schema = self.fill_schema(schema)

else:
schema = self.fill_schema(schema)
prompts.append( # Insecure Email Change Process
{
"objective": "Insecure Email Change Process",
"steps": f"Send a POST request to endpoint {endpoint} with schema {schema}",
"steps": [f"Send a POST request to endpoint {endpoint} with schema {schema}"],
"expected_response_code":
[[
[
"403 Forbidden if proper authorization checks prevent changing another user's email."
]],
],
"token": [account.get("token")],
"path": [endpoint],
"security":
Expand Down Expand Up @@ -2024,11 +2025,12 @@ def test_brute_force(self, login_path, login_schema, prompts, number=100):

for reset in reset_password:
user = {"email": "adam007@example.com"}
# triggers otp being sent
new_otp_calls = f"Send a POST request to endpoint{reset} for victim user:{user}.\n" + otp_calls
prompts.append( # OTP Bruteforce Protection
{
"objective": "OTP Bruteforce Protection",
"steps": f"Send a POST request to endpoint{reset} for victim user:{user}.\n" + # triggers otp being sent
otp_calls,
"steps": [new_otp_calls],
"expected_response_code":
[
"429 Too Many Requests indicating effective rate-limiting against OTP bruteforce attempts.",
Expand All @@ -2046,6 +2048,24 @@ def test_brute_force(self, login_path, login_schema, prompts, number=100):

return prompts

def extract_change_email_endpoints_with_schema(self) -> list[tuple]:
"""
Extracts all endpoints containing 'change-email' and returns (path, schema) pairs.

Returns:
list: List of tuples (path, schema) for change-email endpoints.
"""
result = []

for path, methods in self.endpoints.items():
if "change-email" in path:
for method, method_detail in methods.items():
schema = method_detail.get("requestBody", {}).get("content", {}).get("application/json", {}).get(
"schema")
result.append((path, schema))

return result

def test_css(self, endpoint, prompts, schema=None):
if schema:
prompts.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ def _get_pentesting_steps(self, move_type: str, common_step: Optional[str] = "")
self.explored_steps.append(task_planning_test_case)



self.prompt_helper.current_user = self.prompt_helper.get_user_from_prompt(self.current_sub_step,
self.pentesting_information.accounts)

self.prompt_helper.counter = self.counter

step = self.transform_test_case_to_string(self.current_step, "steps")

if self.prompt_helper.current_user is not None or isinstance(self.prompt_helper.current_user, dict):
if "token" in self.prompt_helper.current_user and "'{{token}}'" in step:
step = step.replace("'{{token}}'", self.prompt_helper.current_user.get("token"))

print(f'sub step:{self.current_sub_step}')
print(f'step:{step}')
print(f'purpose:{self.purpose}')
self.counter += 1
# if last step of exploration, change purpose to next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ def parse_http_response(self, raw_response: str) -> Tuple[Optional[int], Dict[st

if body != {} and bool(body and not body.isspace()):
body = json.loads(body)[0]
else:
body = "Empty"

if body == "":
for line in header_lines:
if line.startswith("{") or line.startswith("["):
body = line
body = json.loads(body)

status_line = header_lines[0].strip()
headers = {
Expand Down Expand Up @@ -91,7 +95,7 @@ def analyze_parsed_response(
Optional[Dict[str, Any]]: The analysis results based on the purpose.
"""
analysis_methods = {
PromptPurpose.AUTHENTICATION_AUTHORIZATION: self.analyze_authentication_authorization(
PromptPurpose.AUTHENTICATION: self.analyze_authentication_authorization(
status_code, headers, body
),
PromptPurpose.INPUT_VALIDATION: self.analyze_input_validation(status_code, headers, body),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def parse_http_response(self, raw_response: str):
body = header_body_split[1] if len(header_body_split) > 1 else ""
if body == "":
for line in header_lines:
if line.startswith("{"):
if line.startswith("{") or line.startswith("["):
body = line

status_line = header_lines[0].strip()
Expand Down Expand Up @@ -135,8 +135,7 @@ def parse_http_response(self, raw_response: str):
self.prompt_helper.current_user["token"] = body["token"]
self.token = body["token"]
for account in self.prompt_helper.accounts:
if account.get("x") == self.prompt_helper.current_user.get(
"x"):
if account.get("x") == self.prompt_helper.current_user.get("x"):
if "token" not in account.keys():
account["token"] = self.token
else:
Expand Down
144 changes: 94 additions & 50 deletions tests/test_response_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,112 @@
import json
import unittest
from unittest.mock import patch

from hackingBuddyGPT.usecases.web_api_testing.prompt_generation.information.prompt_information import (
PromptPurpose,
)
from hackingBuddyGPT.usecases.web_api_testing.response_processing.response_analyzer import (
ResponseAnalyzer,
)
from hackingBuddyGPT.usecases.web_api_testing.prompt_generation.information.prompt_information import PromptPurpose
from hackingBuddyGPT.usecases.web_api_testing.response_processing.response_analyzer import ResponseAnalyzer


class TestResponseAnalyzer(unittest.TestCase):

def setUp(self):
# Example HTTP response to use in tests
self.raw_http_response = """HTTP/1.1 404 Not Found
Date: Fri, 16 Aug 2024 10:01:19 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 2
Connection: keep-alive
Report-To: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1723802269&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=dkvm744qehjJmab8kgf%2BGuZA8g%2FCCIkfoYc1UdYuZMc%3D"}]}
X-Powered-By: Express
X-Ratelimit-Limit: 1000
X-Ratelimit-Remaining: 999
X-Ratelimit-Reset: 1723802321
Cache-Control: max-age=43200
Server: cloudflare

{}"""

def test_parse_http_response(self):
self.auth_headers = (
"HTTP/1.1 200 OK\n"
"Authorization: Bearer token\n"
"X-Ratelimit-Limit: 1000\n"
"X-Ratelimit-Remaining: 998\n"
"X-Ratelimit-Reset: 1723802321\n"
"\n"
'[{"message": "Welcome!"}]'
)

self.error_body = (
"HTTP/1.1 403 Forbidden\n"
"Content-Type: application/json\n"
"\n"
'[{"error": "Access denied"}]'
)

self.validation_fail = (
"HTTP/1.1 400 Bad Request\n"
"X-Content-Type-Options: nosniff\n"
"\n"
'[{"error": "Invalid input"}]'
)

def test_parse_http_response_success(self):
analyzer = ResponseAnalyzer()
status, headers, body = analyzer.parse_http_response(self.auth_headers)

self.assertEqual(200, status)
self.assertIn("Authorization", headers)
if isinstance(body, dict):
msg = body.get("message")
self.assertEqual( "Welcome!",msg )

def test_parse_http_response_invalid(self):
analyzer = ResponseAnalyzer()
status_code, headers, body = analyzer.parse_http_response(self.raw_http_response)
status, headers, body = analyzer.parse_http_response(self.error_body)

self.assertEqual(status_code, 404)
self.assertEqual(headers["Content-Type"], "application/json; charset=utf-8")
self.assertEqual(body, "Empty")
self.assertEqual( 403, status)
if isinstance(body, dict):
msg = body.get("message")

def test_analyze_authentication_authorization(self):
analyzer = ResponseAnalyzer(PromptPurpose.AUTHENTICATION_AUTHORIZATION)
analysis = analyzer.analyze_response(self.raw_http_response)
self.assertEqual( "Access denied", msg)

self.assertEqual(analysis["status_code"], 404)
self.assertEqual(analysis["authentication_status"], "Unknown")
self.assertTrue(analysis["content_body"], "Empty")
self.assertIn("X-Ratelimit-Limit", analysis["rate_limiting"])
def test_analyze_authentication(self):
analyzer = ResponseAnalyzer(PromptPurpose.AUTHENTICATION)
result = analyzer.analyze_response(self.auth_headers)

def test_analyze_input_validation(self):
self.assertEqual(result["status_code"], 200)
self.assertEqual(result["authentication_status"], "Authenticated")
self.assertTrue(result["auth_headers_present"])
self.assertIn("X-Ratelimit-Limit", result["rate_limiting"])

def test_analyze_input_validation_invalid(self):
analyzer = ResponseAnalyzer(PromptPurpose.INPUT_VALIDATION)
analysis = analyzer.analyze_response(self.raw_http_response)
result = analyzer.analyze_response(self.validation_fail)

self.assertEqual(result["status_code"], 400)
self.assertEqual(result["is_valid_response"], "Invalid")
self.assertTrue(result["security_headers_present"])

self.assertEqual(analysis["status_code"], 404)
self.assertEqual(analysis["is_valid_response"], "Error")
self.assertTrue(analysis["response_body"], "Empty")
self.assertIn("security_headers_present", analysis)
def test_is_valid_input_response(self):
analyzer = ResponseAnalyzer()
self.assertEqual(analyzer.is_valid_input_response(200, "data"), "Valid")
self.assertEqual(analyzer.is_valid_input_response(400, "error"), "Invalid")
self.assertEqual(analyzer.is_valid_input_response(500, "error"), "Error")
self.assertEqual(analyzer.is_valid_input_response(999, "???"), "Unexpected")

@patch("builtins.print")
def test_print_analysis(self, mock_print):
def test_document_findings(self):
analyzer = ResponseAnalyzer()
document = analyzer.document_findings(
status_code=403,
headers={"Content-Type": "application/json"},
body="Access denied",
expected_behavior="Access should be allowed",
actual_behavior="Access denied"
)
self.assertEqual(document["Status Code"], 403)
self.assertIn("Access denied", document["Actual Behavior"])

def test_print_analysis_output_structure(self):
analyzer = ResponseAnalyzer(PromptPurpose.INPUT_VALIDATION)
analysis = analyzer.analyze_response(self.raw_http_response)
analysis_str = analyzer.print_analysis(analysis)
result = analyzer.analyze_response(self.validation_fail)
printed = analyzer.print_analysis(result)

self.assertIn("HTTP Status Code: 400", printed)
self.assertIn("Valid Response: Invalid", printed)
self.assertIn("Security Headers Present", printed)

# Check that the correct calls were made to print
self.assertIn("HTTP Status Code: 404", analysis_str)
self.assertIn("Response Body: Empty", analysis_str)
self.assertIn("Security Headers Present: Yes", analysis_str)
def test_report_issues_found(self):
analyzer = ResponseAnalyzer()
document = analyzer.document_findings(
status_code=200,
headers={},
body="test",
expected_behavior="User not authenticated",
actual_behavior="User is authenticated"
)
# Just ensure no exceptions, prints okay
analyzer.report_issues(document)


if __name__ == "__main__":
Expand Down