Описание
Null byte termination in hostnames
Summary
fsockopen()
doesn't regard hostname
as well, hostname
is terminated at the null byte. This can cause Server Side Request Forgery in general case.
Details
During fsockopen
is being called hostname
is passed directly to the low-level C function calls.
When fsockopen()
is called, it retrieves hostname from first parameter, into host
and host_len
. host
can contain null bytes in the middle of the string, but host_len
can be used to prevent unexpected null termination. These two host
and host_len
is passed to php_stream_xport_create()
/main/streams/transports.c:_php_stream_xporet_create()
After TCP factory is selected, php_network_getaddresses()
is called, but still null bytes or any control characters are not processed. This will lead Server Side Request Forgery.
While other url-related functions like parse_url has processing logic using zend_string
type and iscntrl()
check. This difference can be used to trigger SSRF in general case.
For example, one developer can write following reasonable code.
When $connect_host
is given as localhost\0.safedomain.com
, parse_url
will return localhost_.safedomain.com
as its host, which can pass the security check, but fsockopen()
will connect to localhost
and occurs server side request forgery.
PoC
This code will connect to localhost:4000
.
Impact
Server Side Request Forgery
Classification
PHP does not usually classify \0
as a security issue because users are expected to sanitize the input. Nevertheless this was considered as a low impact security issue as a precaution for users that do not do that. It also take into account that the patch is simple.
Пакеты
< 8.1.33
8.1.33
< 8.2.29
8.2.29
< 8.3.23
8.3.23
< 8.4.10
8.4.10
Связанные уязвимости
In PHP versions:8.1.* before 8.1.33, 8.2.* before 8.2.29, 8.3.* before 8.3.23, 8.4.* before 8.4.10 some functions like fsockopen() lack validation that the hostname supplied does not contain null characters. This may lead to other functions like parse_url() treat the hostname in different way, thus opening way to security problems if the user code implements access checks before access using such functions.
In PHP versions:8.1.* before 8.1.33, 8.2.* before 8.2.29, 8.3.* before 8.3.23, 8.4.* before 8.4.10 some functions like fsockopen() lack validation that the hostname supplied does not contain null characters. This may lead to other functions like parse_url() treat the hostname in different way, thus opening way to security problems if the user code implements access checks before access using such functions.
In PHP versions:8.1.* before 8.1.33, 8.2.* before 8.2.29, 8.3.* before 8.3.23, 8.4.* before 8.4.10 some functions like fsockopen() lack validation that the hostname supplied does not contain null characters. This may lead to other functions like parse_url() treat the hostname in different way, thus opening way to security problems if the user code implements access checks before access using such functions.
In PHP versions:8.1.* before 8.1.33, 8.2.* before 8.2.29, 8.3.* before ...