← All tracks
FoundationsLocal File Inclusion10 labsFoundation → Intermediate → Advanced5h practice

LFI: Inclusion Is a Trust Decision

A page name is navigation to you and an include to the interpreter.

“The page loader included a file. The attacker included a secret.”

CWE-98OWASP Top 10 A03:2021OWASP ASVS 12.3.1
The mental model
Inclusion asks the interpreter to bring another file into the current execution. If the user chooses the file, the user chooses part of the program.

The application builds an include path from a page or template parameter. Traversal escapes the pages directory, and wrappers or logs can turn a local file into readable data.

A page name feels like navigation. The interpreter sees an include. The attacker speaks to the interpreter.

Threat model

What is actually at stake?

Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.

Assets

  • Application source and configuration
  • Log files containing secrets
  • Uploaded files and session data
  • Server-side code execution

Actors

  • User who controls a template, language or page parameter
  • Attacker with an upload feature
  • Malicious content in a log or cache

Trust boundaries

  • Request parameter to include path
  • Include root to filesystem
  • Uploaded content to executed code
  • Log file to code path

Abuse cases

  • Include ../../secret/flag.txt
  • Use php://filter to read source
  • Include a log file containing a crafted payload
  • Include an uploaded image with embedded PHP
  • Escalate disclosure to remote code execution
PrerequisitesHow include and require resolve pathsPHP wrappers or equivalent runtime include behaviorPath traversal basics
Request anatomy

Follow the decision, not the payload

Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.

  1. 01

    Accept

    A route chooses a template, language file, page or module by name.

  2. 02

    Resolve

    The runtime joins the name with a base path and resolves traversal, wrappers or stream filters.

  3. 03

    Include

    The file is read as code or data and executed in the current process.

  4. 04

    Execute

    Any code in the included file runs with application privileges; even a non-PHP file may disclose source.

  5. 05

    Persist

    Logs, uploads and cache files can become a delivery path when the include primitive is available.

Case study

phpMyAdmin local file inclusion, CVE-2018-12613

CVE-2018-12613, public advisory
Context

phpMyAdmin 4.8.0 and 4.8.1 allowed an attacker to include local files through a crafted target parameter, affecting installations reachable by an authenticated user.

Discovery

Researchers bypassed the intended whitelist with encoded query separators and traversal, then used the include to read files and, in some configurations, execute code.

Impact

The flaw could expose configuration and session data and could lead to code execution when combined with a writable file such as a log.

Root cause

The include target was derived from user input and the validation did not account for encoded separators and path resolution.

Fix

Map user choices to fixed include paths, never pass raw input to include, disable dangerous wrappers, and keep writable files out of executable paths.

Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.

Attacker lens

A repeatable testing sequence

  1. Find page, view, template, language, module and help parameters.
  2. Try `../`, encoded traversal and absolute paths.
  3. Observe whether an extension is appended and whether wrappers are accepted.
  4. Target the secret file or a file that contains the flag.

Where smart people get fooled

  • An appended `.php` extension can sometimes be bypassed.
  • Only local files can still be a full confidentiality breach.
  • A blocked dot-dot string is not canonical path validation.
Detection engineering

What would the logs show?

A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.

  • Parameters containing ../, php://, file://, data:// or expect://
  • Include errors that reveal filesystem paths
  • Requests for log, session or upload files through a page parameter
  • A page parameter that accepts arbitrary names rather than a fixed key
  • Code execution from a file inside an upload or log directory
Starter query
index=web sourcetype=app_logs | eval lfi=if(match(uri,"(?i)(php://|file://|data://|expect://|\.\./|%2e%2e)"),1,0) | where lfi=1 | stats count values(uri) by clientip, status | where status=200
The defender's move

Fix the decision, then prove it stays fixed

  • Map page IDs to an allowlist of files instead of accepting paths.
  • Canonicalize and verify the resolved path inside the include root.
  • Disable dangerous wrappers and run with least privilege.
Vulnerable patternphp
$page = $_GET['page'];
include __DIR__ . '/pages/' . $page . '.php';
Safer patternphp
$pages = [
  'home' => __DIR__ . '/pages/home.php',
  'billing' => __DIR__ . '/pages/billing.php',
];
$key = $_GET['page'] ?? 'home';
if (!array_key_exists($key, $pages)) { http_response_code(404); exit; }
include $pages[$key];

Use a fixed map from a public key to a server path. Do not sanitize a user path and hope it is safe. Disable remote includes and keep uploads outside executable roots.

Regression tests a reviewer would ask for

Traversal include

GivenA page parameter selects a template

WhenThe value is ../../secret/flag.txt

ThenThe request is rejected and no file is included

Wrapper

GivenThe runtime supports stream wrappers

WhenThe value uses php://filter or data://

ThenOnly a fixed key map is accepted and wrappers never reach the include

Log poisoning

GivenLogs are writable and the include primitive exists

WhenA crafted value is written to a log and included

ThenThe include path cannot reach log files

Uploaded file

GivenA user uploads a file with executable content

WhenThe file is requested or included

ThenThe upload directory is non-executable and the file is served as inert data

False positives

What this finding is not

Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.

  • The page shows the filename but not its content: inclusion may be blocked while reflection remains.
  • A traversal payload returns an error: try wrappers, encoded separators and alternate parameters.
  • Only .php files are allowed: test null bytes on old runtimes, path truncation and wrapper syntax.
  • The included file is static: confirm whether it is parsed as code or only read.
Working vocabulary

Terms you should be able to use precisely

LFI
Local file inclusion: a user-influenced path causes the server to include a local file.
RFI
Remote file inclusion: the include path points to an attacker-controlled remote resource.
Stream wrapper
A runtime protocol such as php://filter that changes how a file is read.
Log poisoning
Writing executable content into a log that is later included by a vulnerable page.
Include map
A fixed table mapping public keys to trusted server paths.
Non-executable upload root
A directory configured so uploaded files are served as data, never executed.
Interview drill

Answer these without reciting a payload

How does LFI become RCE?

The attacker finds a writable file they can influence, such as a log, session file or upload, then includes it so the runtime executes the embedded code.

Why is a fixed include map better than blacklisting ../?

A map has no user-controlled path to traverse. Blacklists miss encodings, wrappers and platform-specific separators.

What is the difference between LFI and path traversal?

Path traversal is the path escape primitive. LFI is the dangerous sink that includes or executes the resolved file.

Knowledge check

Can you separate the mechanism from the payload?

Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.

4 questions
01A page parameter includes any local file. What is the most direct fix?
02Which wrapper is commonly used to read source code through LFI?
03How does log poisoning support LFI exploitation?
04Where should uploaded files live?
الخلاصة الموسّعة
لماذا هذا مهم؟

LFI ليست مجرد قراءة ملف؛ هي أن يقرر الخادم تضمين ملف اختاره المستخدم وتنفيذه بصلاحياته.

الحل ليس حجب ../ فقط، بل خريطة ثابتة من مفتاح عام إلى مسار موثوق. عطّل التضمين البعيد والـwrappers الخطرة، واجعل مجلد الرفع غير قابل للتنفيذ. وتذكّر أن السجلات والجلسات والملفات المرفوعة قد تصبح هي نفسها وسيلة تنفيذ إذا استطاع المهاجم الكتابة فيها.

What file did the server actually open after normalization?