Skip to content
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

PHP filters support #109

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/php-extension/HandlePathAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

/* Helper for handle pre file path access */
void helper_handle_pre_file_path_access(char *filename, EVENT_ID &eventId) {
if (strncmp(filename, "php://", 6) == 0) {
// Whitelist php:// streams as they are often used by PHP frameworks a lot
if (strncmp(filename, "php://", 6) == 0 &&
tudor-timcu marked this conversation as resolved.
Show resolved Hide resolved
strncmp(filename, "php://filter", 12) != 0) {
// Whitelist all php:// streams apart from php://filter, for performance reasons (some PHP frameworks do 1000+ calls / request with these streams as param)
// php://filter can be used to open arbitrary files, so we still monitor this
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
]
},
"method": "POST",
"body": "{\"folder\": \"../../../..\"}",
"body": "{\"file\": \"../../../../file\"}",
"route": "/testDetection"
},
"attack": {
"kind": "path_traversal",
"operation": "fopen",
"blocked": true,
"source": "body",
"path": ".folder",
"payload": "../../../..",
"path": ".file",
"payload": "../../../../file",
"metadata": {
"filename": "../../../../file"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "detected_attack",
"request": {
"headers": {
"content_type": [
"application/json"
]
},
"method": "POST",
"body": "{\"file\": \"php://filter/convert.base64-encode/resource=../../../../file\"}",
"route": "/testDetection"
},
"attack": {
"kind": "path_traversal",
"operation": "fopen",
"blocked": true,
"source": "body",
"path": ".file",
"payload": "php://filter/convert.base64-encode/resource=../../../../file",
"metadata": {
"filename": "php://filter/convert.base64-encode/resource=../../../../file"
}
},
"agent": {
"dryMode": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
]
},
"method": "POST",
"body": "{\"folder\": \"../../../..\"}",
"body": "{\"file\": \"../../../../file\"}",
"route": "/testDetection"
},
"attack": {
"kind": "path_traversal",
"operation": "fopen",
"blocked": false,
"source": "body",
"path": ".folder",
"payload": "../../../..",
"path": ".file",
"payload": "../../../../file",
"metadata": {
"filename": "../../../../file"
}
Expand Down
5 changes: 2 additions & 3 deletions tests/server/test_path_traversal/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
$data = json_decode($requestBody, true);

// Check if 'folder' exists and get its value
if (isset($data['folder'])) {
$f = $data['folder'] . '/file';
fopen($f, 'r');
if (isset($data['file'])) {
fopen($data['file'], 'r');
echo "File opened!";
} else {
echo "Field 'folder' is not present in the JSON data.";
Expand Down
13 changes: 8 additions & 5 deletions tests/server/test_path_traversal/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
3. Checks that the detection event was submitted and is valid.
'''

def check_path_traversal(response_code, response_body, event_id, expected_json):
response = php_server_post("/testDetection", {"folder": "../../../.."})
def check_path_traversal(exploit_path, response_code, response_body, event_id, expected_json):
response = php_server_post("/testDetection", {"file": exploit_path})
assert_response_code_is(response, response_code)
assert_response_body_contains(response, response_body)

Expand All @@ -22,13 +22,16 @@ def check_path_traversal(response_code, response_body, event_id, expected_json):
assert_event_contains_subset_file(events[event_id], expected_json)

def run_test():
check_path_traversal(500, "", 1, "expect_detection_blocked.json")
exploit_path = "../../../../file"

check_path_traversal(exploit_path, 500, "", 1, "expect_detection_blocked.json")
check_path_traversal(f"php://filter/convert.base64-encode/resource={exploit_path}", 500, "", 2, "expect_detection_blocked_php_filter.json")

apply_config("change_config_disable_blocking.json")
check_path_traversal(200, "File opened!", 2, "expect_detection_not_blocked.json")
check_path_traversal(exploit_path, 200, "File opened!", 3, "expect_detection_not_blocked.json")

apply_config("start_config.json")
check_path_traversal(500, "", 3, "expect_detection_blocked.json")
check_path_traversal(exploit_path, 500, "", 4, "expect_detection_blocked.json")

if __name__ == "__main__":
load_test_args()
Expand Down
Loading