Lesson 7 of 9 · 26m
Auditing a Legacy PHP Codebase Without Getting Lost
A practical audit method: map entry points, trace sources to sinks, rank exploitability by reachability, and turn findings into a fix plan instead of a fear document.
The first time you open a real legacy PHP application, it rarely looks like a project. It looks like sediment: three frameworks, one custom router, backup files on the web root, includes inside includes, and a table named users2_final. The mistake is trying to read everything. Audit like an investigator — establish the map first, then trace attacker-controlled movement through it.
Build the entry-point inventory
Start with what the internet or an internal user can actually reach. Enumerate web roots, route files, cron scripts, admin panels, upload handlers, API endpoints, and stray PHP files:
find public html www -type f -name '*.php' 2>/dev/null | sort > php-entrypoints.txt
rg -n "\$_GET|\$_POST|\$_REQUEST|\$_COOKIE|php://input" public html wwwA vulnerability in a file nobody can invoke is a maintenance problem. A mediocre vulnerability in an unauthenticated endpoint is an incident waiting for scanning bots.
Sources, sinks, and the trust graph
Sources are places input enters: superglobals, uploaded files, headers, JSON bodies, database rows influenced by users, remote API responses. Sinks are places input becomes power: SQL, HTML, shell, filesystem paths, eval, unserialize, redirects, mail headers, XML parsers, template includes.
rg -n "eval|system|exec|shell_exec|passthru|popen|proc_open" .
rg -n "include|require|unserialize|extract|parse_str" .
rg -n "header\(['"]Location|mysqli_query|mysql_query|->query\(" .The goal is not to grep every dangerous word and declare victory. It is to ask, for each sink, what data reaches it and under which validation or encoding rules.
Reachability beats theoretical severity
A perfect SQL injection in a dead file behind missing authentication code may matter less than a less dramatic IDOR in the customer invoice endpoint. Rank findings by exploitability path: unauthenticated or authenticated, user role required, ease of discovery, data sensitivity, automation potential, and whether existing logs would show exploitation.
Reproduce safely, not theatrically
Use a local copy, seeded data, and a database snapshot. Prove the smallest exploit that establishes impact: read one extra row, bypass one ownership check, execute one harmless command that writes a timestamp to a test location. Do not exfiltrate production data to prove a point. The purpose of proof is prioritization, not adrenaline.
Common legacy landmines
- Backup files such as
config.php.bakordb.sql.gzexposed under the web root. extract($_POST)silently overwriting authorization variables.unserialize($_COOKIE['state'])turning object injection into a class-loading puzzle.- Dynamic includes built from
page,template, orlangparameters. - SQL concatenation hidden inside helper functions with reassuring names like
q(). - Custom encryption using XOR, base64, or a fixed key committed beside the code.
- Error pages revealing filesystem paths, SQL fragments, and stack traces.
Turn the audit into engineering work
A useful finding has location, reachability, impact, evidence, and a proposed class of fix. Group findings by remediation pattern rather than page: "introduce PDO wrapper," "centralize HTML escaping," "remove dynamic includes," "add authorization middleware." That is how you avoid 300 tickets that all require touching the same unsafe helper.
Field notes
- Search for
$_REQUESTseparately; it obscures which request channel the developer believed they were using. - Inspect
.htaccess, nginx configs, and deployment scripts; the runtime path often differs from the repository layout. - Composer dependencies may be absent from legacy apps entirely or installed by copying vendor folders into git. Treat both as inventory problems.
- Preserve line numbers and commit hashes in findings. Legacy files change slowly, but audits age quickly.
Was this lesson helpful?