PHP Filesystem Operation Audit (php-filesystem-audit)
Analyze critical filesystem operations in PHP projects, paying particular attention to how these operations interact with:
- Path validation / normalization failures (directory traversal, missing basePath)
- Write / read / inclusion chains to the drop location
- Post-write triggers (executability, includability)
- Race conditions (TOCTOU)
When they occur together, thereby increasing the likelihood of real exploitation.
Coverage Scope (Mandatory)
Must cover and output the "evidence point + risk explanation + exploitability assessment" for:
1) Creation / directory operations
mkdir($path, $mode, $recursive)
rmdir($path)
touch($path)
tmpfile() / tempnam()
- Framework-wrapped directory creation (e.g. the underlying wrapper of
Storage::put/ensureDirectory, if identifiable)
2) Permission / ownership changes (commonly used in privilege escalation chains or execution policy bypasses)
3) Links and redirection drop locations (common symlink/hardlink bypasses)
symlink($target, $link)
link($target, $link)
readlink($path)
4) Deletion and cleanup (combined with TOCTOU or overwrite chains)
unlink($path)
unlink/rename variant deletions
rename($src, $dest) (requires special attention to TOCTOU when the drop location changes)
copy/move variants (when they appear as a "drop location preparation step")
5) Path races and TOCTOU (must be audited separately)
Give a verdict on "check-then-use" code patterns:
- Whether there is an
if (realpath/canonicalize + prefix validation) followed by a repeated join/open/write in some branch or exception handler (the path may change)
- Whether there is a writability check followed by a write, or a check followed by the file operation
- Whether there is inconsistent symlink resolution (the resolved path at check time differs from the path actually opened)
Key Output Requirements (Mandatory)
Every finding must include:
Location evidence: file path and function / call site (include the line number where possible; mark it as pending location if the line number cannot be found)
Operation type: creation / permission / link / deletion / rename / copy, etc.
Path control source: whether the path parameter comes from GET/POST/SESSION/configuration or is hardcoded
Relationship to the chain (at least one must be explained):
- Related to the executability or includability of a
WRITE/UPLOAD drop location
- Related to
FILE (read/inclusion) or ARCHIVE (extraction drop location)
- Related to authorization / resource ownership (e.g. only admin directories are writable but the check is bypassed)
- Related to a TOCTOU race (the path may be swapped between the check point and the use point)
Exploitability assessment:
- ✅ Confirmed: the key precondition of the exploit chain can be proven to be provided by this filesystem operation (or the evidence forms a closed loop with WRITE/UPLOAD/FILE)
- ⚠️ Unverified: a risk exists, but a key precondition or trace / resolution evidence is missing
- ❌ Not Exploitable: strong constraints apply (the baseDir allowlist is in effect, resolved path consistency protection exists, permissions are fixed and cannot be changed, the race window is eliminated)
- 🔍 Environment-Dependent: it depends on a specific file system, permissions, concurrency timing, or server-side directory structure
Triggerability constraints (mandatory):
- Only generate a vulnerability entry for a filesystem operation that can be triggered by a concrete HTTP route / entry point, and you must be able to provide the real route with complete parameters for the PoC.
- If the concrete route entry point cannot be located (e.g. a pure background job / scheduled task / CLI tool with an untraceable entry point), that FS risk may only be noted in the report as "entry point not located, entry generation skipped (trigger path to be completed manually)", and must not be output as a vulnerability entry with a non-executable PoC.
Report Output
Output to:
{output_path}/vuln_audit/fs_{timestamp}.md
Vulnerability ID Convention (recommended: FS)
- See
shared/SEVERITY_RATING.md for details
- Vulnerability ID:
{C/H/M/L}-FS-{sequence}
Vulnerability Entry Template (Mandatory)
Every finding must follow the structure below (no omissions):
### [{severity-prefix}-FS-{sequence}] Filesystem Operation Chain Risk (permission / link / deletion / TOCTOU / path control)
| Item | Information |
|------|------|
| Severity Level | {🔴/🟠/🟡/🔵} (CVSS {score}) |
| Reachability (R) | {0-3} - {rationale} |
| Impact Scope (I) | {0-3} - {rationale} |
| Exploitation Complexity (C) | {0-3} - {rationale} |
| Exploitability | ✅ Confirmed / ⚠️ Unverified / ❌ Not Exploitable / 🔍 Environment-Dependent |
| Location | {file}:{line} ({Function/Class}) |
#### Data Flow Chain (Source → Path Control → FS Operation → Chain Influence)
(write line by line: how user/context input enters the path control variable, whether it passes through realpath/canonicalize and prefix validation, how it finally reaches the parameters of filesystem operations such as mkdir/chown/symlink/unlink/rename, and how that filesystem operation affects the capability of subsequent chains: WRITE/UPLOAD/FILE/ARCHIVE/CMD/TPL/EXPR/DESER, etc.)
#### Exploitability Preconditions
- Authentication Requirement: {none / login required / specific privilege required}
- Input Controllability: {fully controllable / conditionally controllable / not controllable}
- Trigger conditions: {branch / exception path / specific file system permissions required / race window required / specific directory structure required}
- Chain continuation: {how it provides "exploitability-enhancing" explanation for other sink categories (write drop location / includability / privilege escalation / execution policy bypass / race trigger)}
#### Verification PoC (mandatory: observable verification framework)
```http
{HTTP Method} {real route with full parameters} HTTP/1.1
Host: {host}
{required Header/Session/JWT/Cookie}
{Payload}
PoC output / observation points (must be spelled out):
- Whether the file system state change caused by this FS operation is observable (e.g. file created under the target path / permission change / link target change / deletion result / final drop location existence).
- If it is TOCTOU: demonstrate the observable phenomenon of "the resolved path at the check point differs from the resolved path at the use point / the window can be exploited" (e.g. concurrent triggering causes the final drop location to change).
Evidence References (mandatory: from source snippets)
Must include at least three evidence snippets matching the location (key lines may be restated directly in the text):
- FS operation call site (mkdir/chmod/chown/symlink/unlink/rename, etc.) and parameter positions (file:line)
- Path validation / normalization logic (realpath/canonicalize/baseDir prefix validation) and its relationship to the FS operation (within the same function or across functions)
- Path control source (GET/POST/SESSION/hardcoded configuration, etc.) and its propagation chain
Recommended Fix
- Path validation and actual use must be bound to the same resolved target: after
realpath/canonicalize, use the same value for open/rename/write (avoid TOCTOU)
- Splitting "validate first, use later" is forbidden for paths that an attacker can influence; if the split is unavoidable, complete it within the same system call / same file descriptor context
- For symlink/hardlink: enable a no-link policy at the target drop location (disable symlink following when the platform supports it, or verify consistency based on inode / file descriptor)
- Permission changes must be minimized and must not be influenceable by the user; avoid paths where an attacker can control
chmod/chown
Code Search Queries (mandatory)
rg -n "(mkdir\\(|rmdir\\(|touch\\(|tmpfile\\(|tempnam\\(|chmod\\(|chown\\(|chgrp\\(|symlink\\(|readlink\\(|link\\(|unlink\\(|rename\\(|copy\\(|symlink\\()" .
rg -n "(realpath\\(|canonicalize\\(|dirname\\(|basename\\(|baseDir|dest(path)?|target(path)?)" .
Remediation Recommendations (Mandatory Points)
- Path validation and actual use must be bound to the same resolved target: after
realpath/canonicalize, use the same value for open/rename/write (avoid TOCTOU)
- Splitting "validate first, use later" is forbidden for paths that an attacker can influence; if the split is unavoidable, complete it within the same system call / same file descriptor context
- For symlink/hardlink: enable a no-link policy at the target drop location (disable symlink following when the platform supports it, or verify consistency based on inode / file descriptor)
- Permission changes must be minimized and must not be influenceable by the user; avoid paths where an attacker can control
chmod/chown
1---2name: php-filesystem-audit3description: "PHP filesystem operation audit tool. Focuses on the security risks of operations such as mkdir/chmod/chown/unlink/rmdir/link/symlink/readlink/touch, permissions, and TOCTOU, providing exploitability-enhancing evidence for path validation bypass and write-chain exploitation (does not replace FILE/UPLOAD/WRITE sink sub-audits)."4zh-signals: "PHP 文件系统操作安全审计。信号:PHP,文件系统,TOCTOU,权限,符号链接,路径绕过"5when-to-use: "Use when auditing PHP filesystem operations such as mkdir, chmod, symlink, or unlink for path validation bypass and TOCTOU write-chain risks."6metadata:7 source: https://skillmd.com/skills/dangeroussong/php-filesystem-audit8---910# PHP Filesystem Operation Audit (php-filesystem-audit)11- 攻击大类:C09 Web应用漏洞利用1213Analyze critical filesystem operations in PHP projects, paying particular attention to how these operations interact with:1415- Path validation / normalization failures (directory traversal, missing basePath)16- Write / read / inclusion chains to the drop location17- Post-write triggers (executability, includability)18- Race conditions (TOCTOU)1920When they occur together, thereby increasing the likelihood of real exploitation.2122## Coverage Scope (Mandatory)23Must cover and output the "evidence point + risk explanation + exploitability assessment" for:2425### 1) Creation / directory operations26- `mkdir($path, $mode, $recursive)`27- `rmdir($path)`28- `touch($path)`29- `tmpfile()` / `tempnam()`30- Framework-wrapped directory creation (e.g. the underlying wrapper of `Storage::put`/`ensureDirectory`, if identifiable)3132### 2) Permission / ownership changes (commonly used in privilege escalation chains or execution policy bypasses)33- `chmod/chown/chgrp`34- `umask`3536### 3) Links and redirection drop locations (common symlink/hardlink bypasses)37- `symlink($target, $link)`38- `link($target, $link)`39- `readlink($path)`4041### 4) Deletion and cleanup (combined with TOCTOU or overwrite chains)42- `unlink($path)`43- `unlink/rename` variant deletions44- `rename($src, $dest)` (requires special attention to TOCTOU when the drop location changes)45- `copy/move` variants (when they appear as a "drop location preparation step")4647### 5) Path races and TOCTOU (must be audited separately)48Give a verdict on "check-then-use" code patterns:49- Whether there is an `if (realpath/canonicalize + prefix validation)` followed by a repeated join/open/write in some branch or exception handler (the path may change)50- Whether there is a writability check followed by a write, or a check followed by the file operation51- Whether there is inconsistent symlink resolution (the resolved path at check time differs from the path actually opened)5253## Key Output Requirements (Mandatory)54Every finding must include:55561. Location evidence: file path and function / call site (include the line number where possible; mark it as pending location if the line number cannot be found)572. Operation type: creation / permission / link / deletion / rename / copy, etc.583. Path control source: whether the path parameter comes from GET/POST/SESSION/configuration or is hardcoded594. Relationship to the chain (at least one must be explained):60 - Related to the executability or includability of a `WRITE`/`UPLOAD` drop location61 - Related to `FILE` (read/inclusion) or `ARCHIVE` (extraction drop location)62 - Related to authorization / resource ownership (e.g. only admin directories are writable but the check is bypassed)63 - Related to a TOCTOU race (the path may be swapped between the check point and the use point)645. Exploitability assessment:65 - ✅ Confirmed: the key precondition of the exploit chain can be proven to be provided by this filesystem operation (or the evidence forms a closed loop with WRITE/UPLOAD/FILE)66 - ⚠️ Unverified: a risk exists, but a key precondition or trace / resolution evidence is missing67 - ❌ Not Exploitable: strong constraints apply (the baseDir allowlist is in effect, resolved path consistency protection exists, permissions are fixed and cannot be changed, the race window is eliminated)68 - 🔍 Environment-Dependent: it depends on a specific file system, permissions, concurrency timing, or server-side directory structure69706. Triggerability constraints (mandatory):71 - Only generate a vulnerability entry for a filesystem operation that can be triggered by a concrete HTTP route / entry point, and you must be able to provide the real route with complete parameters for the PoC.72 - If the concrete route entry point cannot be located (e.g. a pure background job / scheduled task / CLI tool with an untraceable entry point), that FS risk may only be noted in the report as "entry point not located, entry generation skipped (trigger path to be completed manually)", and must not be output as a vulnerability entry with a non-executable PoC.7374## Report Output75Output to:76```77{output_path}/vuln_audit/fs_{timestamp}.md78```7980## Vulnerability ID Convention (recommended: FS)81- See `shared/SEVERITY_RATING.md` for details82- Vulnerability ID: `{C/H/M/L}-FS-{sequence}`8384## Vulnerability Entry Template (Mandatory)85Every finding must follow the structure below (no omissions):8687```markdown88### [{severity-prefix}-FS-{sequence}] Filesystem Operation Chain Risk (permission / link / deletion / TOCTOU / path control)8990| Item | Information |91|------|------|92| Severity Level | {🔴/🟠/🟡/🔵} (CVSS {score}) |93| Reachability (R) | {0-3} - {rationale} |94| Impact Scope (I) | {0-3} - {rationale} |95| Exploitation Complexity (C) | {0-3} - {rationale} |96| Exploitability | ✅ Confirmed / ⚠️ Unverified / ❌ Not Exploitable / 🔍 Environment-Dependent |97| Location | {file}:{line} ({Function/Class}) |9899#### Data Flow Chain (Source → Path Control → FS Operation → Chain Influence)100(write line by line: how user/context input enters the path control variable, whether it passes through realpath/canonicalize and prefix validation, how it finally reaches the parameters of filesystem operations such as mkdir/chown/symlink/unlink/rename, and how that filesystem operation affects the capability of subsequent chains: WRITE/UPLOAD/FILE/ARCHIVE/CMD/TPL/EXPR/DESER, etc.)101102#### Exploitability Preconditions103- Authentication Requirement: {none / login required / specific privilege required}104- Input Controllability: {fully controllable / conditionally controllable / not controllable}105- Trigger conditions: {branch / exception path / specific file system permissions required / race window required / specific directory structure required}106- Chain continuation: {how it provides "exploitability-enhancing" explanation for other sink categories (write drop location / includability / privilege escalation / execution policy bypass / race trigger)}107108#### Verification PoC (mandatory: observable verification framework)109```http110{HTTP Method} {real route with full parameters} HTTP/1.1111Host: {host}112{required Header/Session/JWT/Cookie}113114{Payload}115```116117PoC output / observation points (must be spelled out):1181. Whether the file system state change caused by this FS operation is observable (e.g. file created under the target path / permission change / link target change / deletion result / final drop location existence).1192. If it is TOCTOU: demonstrate the observable phenomenon of "the resolved path at the check point differs from the resolved path at the use point / the window can be exploited" (e.g. concurrent triggering causes the final drop location to change).120121#### Evidence References (mandatory: from source snippets)122Must include at least three evidence snippets matching the location (key lines may be restated directly in the text):1231. FS operation call site (mkdir/chmod/chown/symlink/unlink/rename, etc.) and parameter positions (file:line)1242. Path validation / normalization logic (realpath/canonicalize/baseDir prefix validation) and its relationship to the FS operation (within the same function or across functions)1253. Path control source (GET/POST/SESSION/hardcoded configuration, etc.) and its propagation chain126127#### Recommended Fix128- Path validation and actual use must be bound to the same resolved target: after `realpath`/canonicalize, use the same value for open/rename/write (avoid TOCTOU)129- Splitting "validate first, use later" is forbidden for paths that an attacker can influence; if the split is unavoidable, complete it within the same system call / same file descriptor context130- For symlink/hardlink: enable a no-link policy at the target drop location (disable symlink following when the platform supports it, or verify consistency based on inode / file descriptor)131- Permission changes must be minimized and must not be influenceable by the user; avoid paths where an attacker can control `chmod/chown`132133#### Code Search Queries (mandatory)134```text135rg -n "(mkdir\\(|rmdir\\(|touch\\(|tmpfile\\(|tempnam\\(|chmod\\(|chown\\(|chgrp\\(|symlink\\(|readlink\\(|link\\(|unlink\\(|rename\\(|copy\\(|symlink\\()" .136rg -n "(realpath\\(|canonicalize\\(|dirname\\(|basename\\(|baseDir|dest(path)?|target(path)?)" .137```138139## Remediation Recommendations (Mandatory Points)140- Path validation and actual use must be bound to the same resolved target: after `realpath`/canonicalize, use the same value for open/rename/write (avoid TOCTOU)141- Splitting "validate first, use later" is forbidden for paths that an attacker can influence; if the split is unavoidable, complete it within the same system call / same file descriptor context142- For symlink/hardlink: enable a no-link policy at the target drop location (disable symlink following when the platform supports it, or verify consistency based on inode / file descriptor)143- Permission changes must be minimized and must not be influenceable by the user; avoid paths where an attacker can control `chmod/chown`