-
Notifications
You must be signed in to change notification settings - Fork 13
Add socket function support (socket_connect, fsockopen, stream_socket_client) for SSRF detection #374
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
Open
PopoviciMarian
wants to merge
11
commits into
main
Choose a base branch
from
add-socket-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add socket function support (socket_connect, fsockopen, stream_socket_client) for SSRF detection #374
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
268dc42
Add socket function support (socket_connect, fsockopen, stream_socket…
PopoviciMarian ad0ae36
Enhance parameter handling for socket functions to support PHP 8.0+ c…
PopoviciMarian 5a450a0
fix
PopoviciMarian bab52e0
fix
PopoviciMarian 2d205e0
Refactor socket parameter handling
PopoviciMarian deb437b
Include PHP sockets header in Includes.h
PopoviciMarian 0ea4a20
Update version to v1.0.1 and enable sockets in Docker build configura…
PopoviciMarian 32f00ff
Refactor
PopoviciMarian 1755220
Add socket functions to README
PopoviciMarian 1795a6e
fix tests
PopoviciMarian 632de8f
refactor
PopoviciMarian 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| --TEST-- | ||
| Test socket functions (socket_connect, fsockopen, stream_socket_client) | ||
|
|
||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_VERSION_ID >= 80500) { | ||
| die("skip PHP >= 8.5."); | ||
| } | ||
| if (!extension_loaded('sockets')) { | ||
| die("skip sockets extension not loaded"); | ||
| } | ||
| ?> | ||
|
|
||
| --ENV-- | ||
| AIKIDO_LOG_LEVEL=INFO | ||
|
|
||
| --FILE-- | ||
| <?php | ||
| // Test socket_connect | ||
| $socket1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
| if ($socket1 !== false) { | ||
| @socket_connect($socket1, "example.com", 443); | ||
| socket_close($socket1); | ||
| } | ||
|
|
||
| // Test fsockopen | ||
| $fp1 = @fsockopen("httpbin.org", 443, $errno, $errstr, 1); | ||
| if ($fp1) { | ||
| fclose($fp1); | ||
| } | ||
|
|
||
| // Test stream_socket_client | ||
| $fp2 = @stream_socket_client("tcp://facebook.com:443", $errno, $errstr, 1); | ||
| if ($fp2) { | ||
| fclose($fp2); | ||
| } | ||
|
|
||
| // Test socket_connect with explicit port | ||
| $socket2 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
| if ($socket2 !== false) { | ||
| @socket_connect($socket2, "www.aikido.dev", 80); | ||
| socket_close($socket2); | ||
| } | ||
|
|
||
| // Test fsockopen with explicit port | ||
| $fp3 = @fsockopen("some-invalid-domain.com", 4113, $errno, $errstr, 1); | ||
| if ($fp3) { | ||
| fclose($fp3); | ||
| } | ||
|
|
||
| // Test stream_socket_client with http:// scheme | ||
| $fp4 = @stream_socket_client("http://www.aikido.dev:80", $errno, $errstr, 1); | ||
| if ($fp4) { | ||
| fclose($fp4); | ||
| } | ||
|
|
||
| // Test stream_socket_client with https:// scheme | ||
| $fp5 = @stream_socket_client("https://example.com:443", $errno, $errstr, 1); | ||
| if ($fp5) { | ||
| fclose($fp5); | ||
| } | ||
|
|
||
| ?> | ||
|
|
||
| --EXPECT-- | ||
| [AIKIDO][INFO] [BEFORE] Got domain: example.com | ||
| [AIKIDO][INFO] [AFTER] Got domain: example.com port: 443 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: httpbin.org | ||
| [AIKIDO][INFO] [AFTER] Got domain: httpbin.org port: 443 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: facebook.com | ||
| [AIKIDO][INFO] [AFTER] Got domain: facebook.com port: 443 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: www.aikido.dev | ||
| [AIKIDO][INFO] [AFTER] Got domain: www.aikido.dev port: 80 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: some-invalid-domain.com | ||
| [AIKIDO][INFO] [AFTER] Got domain: some-invalid-domain.com port: 4113 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: www.aikido.dev | ||
| [AIKIDO][INFO] [AFTER] Got domain: www.aikido.dev port: 80 | ||
| [AIKIDO][INFO] [BEFORE] Got domain: example.com | ||
| [AIKIDO][INFO] [AFTER] Got domain: example.com port: 443 | ||
|
|
11 changes: 11 additions & 0 deletions
11
tests/server/test_ssrf_socket/change_config_disable_blocking.json
This file contains hidden or 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,11 @@ | ||
| { | ||
| "success": true, | ||
| "serviceId": 1, | ||
| "heartbeatIntervalInMS": 600000, | ||
| "endpoints": [], | ||
| "blockedUserIds": [], | ||
| "allowedIPAddresses": [], | ||
| "receivedAnyStats": true, | ||
| "block": false | ||
| } | ||
|
|
This file contains hidden or 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,6 @@ | ||
| { | ||
| "AIKIDO_BLOCK": "1", | ||
| "AIKIDO_LOCALHOST_ALLOWED_BY_DEFAULT": "0", | ||
| "AIKIDO_FEATURE_COLLECT_API_SCHEMA": "1" | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
36
tests/server/test_ssrf_socket/expect_detection_blocked.json
This file contains hidden or 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,36 @@ | ||
| { | ||
| "type": "detected_attack", | ||
| "request": { | ||
| "headers": { | ||
| "content_type": [ | ||
| "application/json" | ||
| ] | ||
| }, | ||
| "method": "POST", | ||
| "body": "{\"url\": \"http://127.0.0.1:8081\", \"method\": \"socket_connect\"}", | ||
| "source": "php", | ||
| "route": "/testDetection" | ||
| }, | ||
| "attack": { | ||
| "kind": "ssrf", | ||
| "operation": "socket_connect", | ||
| "blocked": true, | ||
| "source": "body", | ||
| "path": ".url", | ||
| "stack": "tests/server/test_ssrf_socket/index.php(20): socket_connect()", | ||
| "payload": "http://127.0.0.1:8081", | ||
| "metadata": { | ||
| "hostname": "127.0.0.1", | ||
| "port": "8081" | ||
| }, | ||
| "user": { | ||
| "id": "12345", | ||
| "name": "Tudor" | ||
| } | ||
| }, | ||
| "agent": { | ||
| "dryMode": false, | ||
| "library": "firewall-php" | ||
| } | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
36
tests/server/test_ssrf_socket/expect_detection_blocked_fsockopen.json
This file contains hidden or 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,36 @@ | ||
| { | ||
| "type": "detected_attack", | ||
| "request": { | ||
| "headers": { | ||
| "content_type": [ | ||
| "application/json" | ||
| ] | ||
| }, | ||
| "method": "POST", | ||
| "body": "{\"url\": \"http://127.0.0.1:8081\", \"method\": \"fsockopen\"}", | ||
| "source": "php", | ||
| "route": "/testDetection" | ||
| }, | ||
| "attack": { | ||
| "kind": "ssrf", | ||
| "operation": "fsockopen", | ||
| "blocked": true, | ||
| "source": "body", | ||
| "path": ".url", | ||
| "stack": "tests/server/test_ssrf_socket/index.php(27): fsockopen()", | ||
| "payload": "http://127.0.0.1:8081", | ||
| "metadata": { | ||
| "hostname": "127.0.0.1", | ||
| "port": "8081" | ||
| }, | ||
| "user": { | ||
| "id": "12345", | ||
| "name": "Tudor" | ||
| } | ||
| }, | ||
| "agent": { | ||
| "dryMode": false, | ||
| "library": "firewall-php" | ||
| } | ||
| } | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.