Files
wahrwelt-a-d-setup/docs/vulnerabilities/php.md
T
2026-10-10 07:29:48 +03:00

30 KiB

PHP: find, fix and verify

English | Русский

Catalog · Scanner · Patch verification

23 sections. Each follows Search → Vulnerable example → Fix → Verification. Run rg from a local service checkout; searches only read files. Derive the actor/owner from a verified session, not request data. Adapt examples to the actual API and contract.

Contents

PHP-SQL. Dynamic PHP SQL

Search: scanner rule PHP-SQL. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-SQL for details. Regex: query/exec/prepare with concatenation, double-quoted interpolation and mysqli_query/pg_query with an ordinary connection argument. Escaping, other interpolation and SQL variables are not fully parsed.

Vulnerable example:

$db->query("SELECT id FROM notes WHERE name=" . $name);

Fix: Use driver-supported prepared statements and bound values; allowlist identifiers.

$stmt = $db->prepare("SELECT id FROM notes WHERE name = ?");
$stmt->execute([$name]);

Verification: Quotes stay data and ownership conditions remain.

Expanded example and limitations
// Vulnerable: client input is interpolated into SQL.
$row = $pdo->query("SELECT body FROM notes WHERE id = '" . $_GET['id'] . "'")->fetch();

PDO patch for a private note:

function readPrivateNote(PDO $pdo, ?int $userId, int $noteId): ?string {
    if ($userId === null) {
        throw new RuntimeException('authentication required');
    }
    $stmt = $pdo->prepare('SELECT body FROM notes WHERE id = :id AND owner_id = :owner');
    $stmt->bindValue(':id', $noteId, PDO::PARAM_INT);
    $stmt->bindValue(':owner', $userId, PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    return $row === false ? null : $row['body'];
}

Validate the numeric ID first: (int) $_GET['id'] silently converts some invalid strings. Configure PDO error handling and do not return SQL exceptions to clients. Placeholders represent values, not table/column names. Native prepares may be appropriate for supported drivers, but do not change application-wide settings blindly; driver behavior differs.

Check: Owner, other user, no session, missing record, invalid ID and another searchable text field containing an apostrophe. Preserve intended sharing.

PHP-OBJECT. Untrusted PHP unserialize

Search: scanner rule PHP-OBJECT. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-OBJECT for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

$value = unserialize($data);

Fix: Prefer schema-validated JSON; migrate trusted legacy records separately. allowed_classes alone is not a complete input policy.

$value = json_decode($data, true, 32, JSON_THROW_ON_ERROR); // validate schema

Verification: Unexpected object types fail without construction; valid records stay readable.

Expanded example and limitations
// Vulnerable with untrusted input: object restoration has side effects.
$profile = unserialize($_COOKIE['profile']);

Patch for a simple profile when the protocol supports JSON:

function decodeProfile(string $raw): array {
    $value = json_decode($raw, false, 32, JSON_THROW_ON_ERROR);
    if (!($value instanceof stdClass)) {
        throw new InvalidArgumentException('object required');
    }
    $fields = get_object_vars($value);
    if (array_keys($fields) !== ['display_name'] || !is_string($fields['display_name'])) {
        throw new InvalidArgumentException('unexpected profile fields');
    }
    return ['display_name' => $fields['display_name']];
}

Depth 32 is an example bound, not a competition requirement; bound body size before parsing according to contract. Role and owner come from the server. PHP warns against untrusted unserialize even with allowed_classes: that flag alone is not a complete fix.

Check: Valid profile, unknown field, array instead of object, wrong type and malformed JSON. Format migration must support old clients/data; never retain unserialize fallback for client strings.

PHP-SHELL. PHP code or shell execution

Search: scanner rule PHP-SHELL. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-SHELL for details. Regex: eval/shell_exec/system/passthru/popen/exec and string/variable proc_open. Literal argv arrays in proc_open's first argument (including PHP 8 command:) are excluded unless explicitly launching shell; arrays require PHP 7.4+. Review executable/options, reordered named arguments, backticks, variable functions, wrappers and runtime version. Custom same-named functions/comments may match.

Vulnerable example:

shell_exec($command);

Fix: Prefer a library operation; otherwise use a fixed executable and validated argv without a shell.

$size = filesize($checkedPath);

Verification: Metacharacters and dangerous options cannot change the operation.

proc_open accepts argv arrays from PHP 7.4; the named command argument requires PHP 8.0. Arrays pass arguments without an implicit shell. A genuinely required utility can run without shell, for example on PHP 8+:

$process = proc_open(
    command: ['/usr/bin/file', '--', $checkedName],
    descriptor_spec: $descriptors,
    pipes: $pipes
);

The executable is fixed; $checkedName still needs authorization and path checks. -- is valid only for utilities supporting that separator. command: array('/usr/bin/file', '--', $checkedName) also avoids PHP-SHELL. The exclusion covers only a literal-array first argument; reordered named arguments and variable arrays remain review signals. proc_open arrays and Windows limitations, PHP 8 named arguments, PHP 8.0 proc_open signature.

Explicit shell execution remains risky and still signals even with an array:

$process = proc_open(command: ['sh', '-c', $command], descriptor_spec: $descriptors, pipes: $pipes);

Do not convert a pipeline to argv by simply splitting text: move operations to a library or connect processes explicitly. Review executable, options, PATH, cwd and environment. On Windows array escaping assumes VC-runtime-compatible command-line parsing; other parsers may differ. Close pipes/processes and check errors, runtime/output limits and the old result format.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'proc_open|proc_close|proc_terminate|shell_exec|descriptor_spec|bypass_shell' .

After a match trace actual argv and every argument's origin. In an authorized test environment check spaces, quotes, leading dashes, normal files and process failure. Absence of PHP-SHELL does not prove argument safety. This catalog review checked source and scanner behavior; it did not run PHP or the discovered commands.

PHP-INCLUDE. Client-selected include path

Search: scanner rule PHP-INCLUDE. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-INCLUDE for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

include $_GET['page'];

Fix: Select server-owned include paths from a closed allowlist, never client paths/URLs.

$pages = ['home' => __DIR__ . '/pages/home.php'];
if (!isset($pages[$page])) { throw new Exception('bad page'); }
include $pages[$page];

Verification: Only approved pages load; traversal and remote schemes fail.

Expanded example and limitations
// Vulnerable: the client controls the executable PHP file path.
include $_GET['page'];

Patch when the contract selects one of several known pages:

$pages = [
    'help' => __DIR__ . '/pages/help.php',
    'profile' => __DIR__ . '/pages/profile.php',
];
$page = $_GET['page'] ?? 'help';
if (!is_string($page) || !array_key_exists($page, $pages)) {
    http_response_code(404);
    exit;
}
require $pages[$page];

The server defines paths. The profile page still requires authorization before private data is returned. basename() neither restricts selection to the intended pages nor checks access. If the feature displays user-uploaded files, include must not implement that display.

Check: Both normal pages, unknown names and an array in place of page; anonymous callers cannot open private pages; only permitted selection changes.

PHP-PREFIX-MAC. Hash concatenation instead of MAC

Search: scanner rule PHP-PREFIX-MAC. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-PREFIX-MAC for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

$tag = sha1($key . $payload);

Fix: Use HMAC rather than a hash of secret concatenated with data, compare with hash_equals and migrate/version old formats.

$tag = hash_hmac('sha256', $payload, $key);

Verification: Changed/appended data fails; tags bind purpose and all authentication fields.

A/D variant: Secret-prefix hash mislabeled HMAC. ECSC / Diese, author and source. Signatures used SHA1(secret || data). Patch: Real HMAC, token versioning and safe migration of existing signatures. Test: Changed/added fields fail validation. Search: PHP-PREFIX-MAC; a function named hmac guarantees nothing.

source/write-up 1; source/write-up 2; source/write-up 3.

PHP-PARSE-STR. parse_str key normalization

Search: scanner rule PHP-PARSE-STR. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-PARSE-STR for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

parse_str($token_data, $params);

Fix: Use identical schemas before signing/after parsing; account for dots/spaces becoming underscores, arrays and duplicate keys.

$params = parse_and_validate_canonical_share_dto($token_data); // one schema, duplicate policy, server-owned fields

Verification: Alternate spellings cannot change owner/document; signer and verifier agree.

A/D variant: Parameter-key normalization after signing. ECSC / Diese, author and source. parse_str merges certain distinct names and changes field meaning. Patch: An allowlist and one canonical signer/verifier DTO schema; reject ambiguous duplicates. Test: Dots, spaces, underscores and arrays. Search: PHP-PARSE-STR; the call itself need not be vulnerable.

source/write-up 1; source/write-up 2; source/write-up 3.

PHP-LOOSE-COMPARE. Loose comparison of token or signature

Search: scanner rule PHP-LOOSE-COMPARE. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-LOOSE-COMPARE for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

$ok = $token == $provided;

Fix: Validate types; use hash_equals for tags, === for ordinary values and password_verify for passwords. Numeric-string semantics depend on version/operands.

$ok = is_string($provided) && hash_equals($token, $provided);

Verification: Numbers, arrays and numeric strings cannot grant access; valid values work.

Reference: primary source.

PHP-UPLOAD. Moving an uploaded file

Search: scanner rule PHP-UPLOAD. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-UPLOAD for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

move_uploaded_file($tmp, $webroot . $name);

Fix: Check upload error/size/type; use non-overwriting server-generated names outside executable web roots and authorize downloads.

move_uploaded_file($validatedTmp, $serverChosenPrivatePath);

Verification: Test upload/download, collisions, wrong owner/type and full disk. The call remains a review signal.

Reference: primary source.

PHP-DISPLAY-ERRORS. PHP displays errors to clients

Search: scanner rule PHP-DISPLAY-ERRORS. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-DISPLAY-ERRORS for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

display_errors = On

Fix: Disable client error output and retain redacted local logs; check effective php.ini/FPM and runtime ini_set.

display_errors = Off

Verification: Parse/database/type errors yield expected public responses while private diagnostics remain.

Reference: primary source.

PHP-XSS. Raw data reaches HTML, attributes or JavaScript

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'echo|print|htmlspecialchars|json_encode|<script' .

Identify the output context; htmlspecialchars for HTML text does not make a string safe JavaScript.

Vulnerable example:

echo $title inside HTML lacks encoding; JSON enters script without an appropriate serializer.

Fix: Template autoescape or htmlspecialchars with ENT_QUOTES/ENT_SUBSTITUTE and UTF-8 for HTML; separate safe JavaScript/URL handling.

Verification: Quotes, tags and Unicode render as data; legitimate formatting remains intact.

Expanded example and limitations
// Vulnerable with untrusted title: the browser interprets it as markup.
echo '<p>' . $title . '</p>';

Patch for HTML text content:

echo '<p>' . htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</p>';

Validate $title as a string first. Store original text and encode on output to avoid cumulative double escaping during edits. This is not JavaScript/CSS encoding or URL validation; those contexts need different mechanisms. Intentionally supported HTML requires sanitizing the permitted subset rather than turning all markup into text.

Check: <, >, &, quotes and Unicode render as text; existing records and save/open/edit cycles preserve original content.

PHP-SESSION. Session fixation, strict mode and stale state

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'session_start|session_regenerate_id|session.use_strict_mode|session_id' .

Trace externally supplied session IDs, login-time regeneration and role storage.

Vulnerable example:

An unknown external ID is accepted and remains unchanged after login.

Fix: session.use_strict_mode, supported regeneration with concurrent requests considered, server-derived roles and consistent Secure/HttpOnly/SameSite settings.

Verification: The old ID cannot authorize; normal parallel requests retain state.

Reference: primary source.

PHP-WRAPPERS. Filesystem APIs accept URLs or wrappers

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'file_get_contents|fopen|copy\(|file_exists|is_file|phar|stream_context' .

Determine whether each API accepts a path or stream URL; check installed PHP version and required wrappers.

Vulnerable example:

Users select a scheme/local path instead of an allowed object; open_basedir is not a complete sandbox.

Fix: Server-side paths derived from authorized IDs; a separate policy-constrained HTTP client for URLs; no client-selected wrapper.

Verification: Valid files load; external URLs, foreign files and unsupported schemes never reach the operation.

PHP-EXTRACT. extract overwrites local variables

Search: scanner rule PHP-EXTRACT. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-EXTRACT for details. Regex: any extract(...); trusted template contexts need manual classification.

Additional manual search for forms outside automatic coverage:

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'extract\(|parse_str|\$\$|GLOBALS|register_globals' .

Compare imported keys with authorization/config variable names; two-argument parse_str has a different risk from its historical behavior.

Vulnerable example:

extract($_REQUEST) overwrites locals, or $$key selects a sensitive variable.

Fix: Read explicit allowed fields. EXTR_SKIP reduces overwrites but does not replace schema/type and new-name controls.

Verification: Input is_admin/owner/config cannot alter access; approved values retain types.

Reference: primary source.

Scanner example before:

extract($_POST);
if ($is_admin) { show_private_data(); }

Fix template:

$display_name = validate_display_name($_POST['display_name'] ?? '');

Primary documentation: reference 1.

PHP-SERIALIZE-MAGIC. Magic methods and trust in stored data

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e '__wakeup|__unserialize|__destruct|__toString|unserialize' .

Trace PHP-OBJECT sinks to reachable classes and side effects; a database is not trusted merely because client input was stored there.

Vulnerable example:

A deserialized object performs file/network operations before its result is checked.

Fix: DTOs without type selection or side effects; separate compatible migration, not fallback to an unsafe parser.

Verification: Unknown types have no effects; legitimate legacy formats follow the migration plan.

PHP-CSRF. Cookies authorize mutations without CSRF protection

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'csrf|POST|PUT|DELETE|REQUEST_METHOD|SameSite' .

Review forms and cookie-authenticated JSON endpoints, middleware exceptions and Origin/Referer policies.

Vulnerable example:

The browser automatically attaches a cookie to state-changing requests from another context.

Fix: Framework CSRF tokens and architecture-appropriate origin checks. SameSite supplements, not replaces, server-side checks. Bearer APIs have a different threat context.

Verification: Wrong/missing tokens fail; normal forms and supported API clients work.

PHP-REGEX-ERROR. preg_match errors become permission

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'preg_match|preg_last_error|preg_replace' .

Distinguish false, 0 and 1; find negated results and regex limits.

Vulnerable example:

An engine error on long input enters the 'prohibited content not found' branch.

Fix: Compare results strictly against 1/0/false according to meaning; fail closed on errors and bound length/complexity.

Verification: Match, no-match and engine errors produce distinct expected responses; normal Unicode remains supported.

Reference: primary source.

PHP-XML-FLAGS. XML entity substitution or DTD enabled

Search: scanner rule PHP-XML-FLAGS. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-XML-FLAGS for details. Regex: LIBXML_NOENT/DTDLOAD/DTDATTR/DTDVALID and substituteEntities/resolveExternals=true. Numeric ini/database masks are not decoded; inspect bits against runtime constants.

Vulnerable example: NOENT substitutes entities; NONET alone does not prevent local-file access.

$doc->loadXML($xml, LIBXML_NOENT | LIBXML_DTDLOAD | LIBXML_NONET);

Fix: Disable entity substitution/external DTD loading, bound input and check libxml version. Use LIBXML_NO_XXE where supported; NONET alone is insufficient.

$doc->resolveExternals = false;
$doc->substituteEntities = false;
$doc->loadXML($boundedXml, LIBXML_NONET);

Verification: Ordinary XML works; entities/DTD read no files or network and nesting/size are limited.

Primary documentation: reference 1, reference 2.

PHP-TEMPLATE. Template built from dynamic text

Search: scanner rule PHP-TEMPLATE. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-TEMPLATE for details. Regex: createTemplate($...) and Smarty display/fetch with eval: or string:. Inspect variable loaders, custom filters and database templates.

Vulnerable example: When user data becomes template source, users gain the template engine's capabilities.

$template = $twig->createTemplate($profileText);
echo $template->render($vars);

Fix: Separate trusted template source from values; user templates need explicit sandbox/loader/function/resource policy.

echo $twig->render('profile.html.twig', ['about' => $profileText]);

Verification: Profile template expressions remain data; private templates/files are unavailable.

Primary documentation: reference 1.

PHP-CURL-TLS. cURL certificate or hostname checks disabled

Search: scanner rule PHP-CURL-TLS. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-CURL-TLS for details. Regex: CURLOPT_SSL_VERIFYPEER=false/0 and CURLOPT_SSL_VERIFYHOST=false/0/1 in arguments/setopt_array. Variable values are not evaluated.

Vulnerable example: Disabling server validation permits substitution of outbound HTTPS results.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Fix: Verify peer and hostname with the trusted CA; use VERIFYHOST=2, not historical ambiguous 1.

curl_setopt_array($ch, [CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_CAINFO => $trustedCA]);

Verification: Correct CA/host works; wrong CA/host and expiry fail without fallback.

Primary documentation: reference 1.

PHP-URL-INCLUDE. Remote resource inclusion enabled

Search: scanner rule PHP-URL-INCLUDE. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-URL-INCLUDE for details. Regex: allow_url_include=On/1/true in ini/config/assignment. Review runtime overrides and effective include paths separately.

Vulnerable example: URL include increases the consequences of controlling an include/require path.

allow_url_include = On

Fix: Disable allow_url_include in the effective SAPI config; select include paths from a closed server-side map.

allow_url_include = Off

Verification: Normal modules load; client URLs cannot become executable code.

Primary documentation: reference 1.

PHP-SESSION-CONFIG. Weakened PHP session configuration

Search: scanner rule PHP-SESSION-CONFIG. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain PHP-SESSION-CONFIG for details. Regex: explicit unsafe use_strict_mode/use_only_cookies/use_trans_sid values. Missing settings and ini_set argument forms need manual review.

Vulnerable example: Attacker-selected session IDs and IDs in URLs enable fixation or leakage.

session.use_strict_mode = 0
session.use_only_cookies = 0
session.use_trans_sid = 1

Fix: Enable strict mode and cookie-only transport, disable URL rewriting and regenerate IDs at login; review custom handlers/cookies.

session.use_strict_mode = 1
session.use_only_cookies = 1
session.use_trans_sid = 0

Verification: Unknown fixed IDs fail, login changes the ID, URLs/logs contain no IDs and sessions work.

Primary documentation: reference 1.

PHP-MASS-ASSIGN. Array keys choose columns and privileges

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e '\$_(POST|GET|REQUEST)|array_keys|array_merge|profileData|UPDATE|SET|fill\(|update\(' .

Trace keys as well as values from requests into UPDATE/ORM and owner/admin/association assignment. Review nested fields and numeric indices.

Vulnerable example: SQL values are parameterized but column names come from request keys; role fields or relationships to foreign objects are updated wholesale.

Fix: Explicitly map allowed input fields to fixed columns and types. Check privileges, owners and relationships separately; placeholders do not escape SQL identifiers.

Verification: Unknown columns, administrative fields and foreign relationships fail without mutation; allowed fields persist.

Reference: OWASP Mass Assignment. The explanation and adaptation recipe were written for this catalog.

A/D variant: Oldschool's mass update could change an administrative relationship; column names also entered SQL without restriction. Author description.

PHP-LDAP-FILTER. LDAP filter concatenates a PHP variable

Search: scanner rule PHP-LDAP-FILTER. Use --explain PHP-LDAP-FILTER for the exact detector contract. Regex with lexical exclusions: ldap_search/list/read with simple first two arguments and a string concatenated directly with a variable as the third argument. Variables may already be escaped; prebuilt filters and interpolation need manual review.

Vulnerable example:

ldap_search($conn, $base, '(uid=' . $name . ')');

Fix: Escape values for LDAP filter context, not DN context; keep attribute names and search scope trusted.

$filter = '(uid=' . ldap_escape($name, '', LDAP_ESCAPE_FILTER) . ')';
ldap_search($conn, $base, $filter);

Verification: LDAP metacharacters in a value do not widen results; authorized lookups work.

Primary reference 1