Skip to content

Absolute path file inclusion (direct file read without traversal)

Absolute path file inclusion happens when a parameter is passed to a file API that accepts an absolute path, so an attacker supplies a full path such as /etc/passwd or C:\windows\win.ini directly — no ../ traversal required. It occurs when the code opens the raw value instead of joining it to a trusted base directory.

Linux / UnixWindowsArbitrary file readInformation disclosureNo filter

How it works

Some code passes the user value straight to fopen/include/open without prepending a base directory, or uses an API that treats a leading / (or drive letter) as an absolute path and discards any base it was joined to.

In many languages, joining a base with an absolute component yields the absolute component: Python's os.path.join('/var/www', '/etc/passwd') returns '/etc/passwd', and PHP include simply opens whatever path it is given. The attacker therefore names the target file outright.

This is often simpler and more reliable than ../ climbing because there is no depth to guess and no traversal sequence for a filter to catch.

When it works and when it fails

Works when

  • The file API accepts absolute paths and the base directory is not enforced after joining.
  • Filters only look for ../ and ignore a bare leading / or a Windows drive/UNC prefix.
  • The language's path-join discards the base when the second argument is absolute.

Fails when

  • The code strips leading slashes or rejects absolute inputs before joining.
  • A prefix check confirms the resolved path begins with the base directory.
  • Input is resolved through an allowlist of keys rather than used as a path.

Representative payloads

Adapt these to your authorized target; they are shown in canonical form. Payloads are never translated.

  • /etc/passwd
    Linux / UnixNo filter

    Direct absolute path. Works when the handler opens the raw value or when path-join discards the trusted base.

  • C:\windows\win.ini
    WindowsNo filter

    Windows absolute path with drive letter; a reliable read-confirmation target on Windows hosts.

  • /proc/self/cmdline
    Linux / UnixNo filter

    Absolute read of the current process command line — discloses how the app was launched and sometimes secrets passed as arguments.

    Note: See the /proc inclusion technique for turning this into more impact.

  • file:///etc/passwd
    Linux / UnixPHP wrapper

    The file:// scheme forces an absolute local read in handlers that route the value through a URL/stream opener.

Detection

In logs

  • Parameters whose value begins with / , a drive letter (C:\), or a scheme (file://) where a relative filename is expected.
  • Requests naming sensitive absolute paths (/etc/shadow, /root/.ssh/id_rsa, C:\inetpub\...).

In code

  • Path joins where the user-controlled component can be absolute (os.path.join, path.join, Paths.get) with no prior leading-slash check.
  • File APIs called with the raw parameter and no base directory at all.
  • URL/stream openers (allow_url_fopen contexts) reachable from a file parameter.

Detection rule: Flag request parameters matching ^(/|[A-Za-z]:\\|file://) for a field that should be a bare filename.

Remediation

  • Treat the parameter as a key, not a path: resolve it through a server-side allowlist/map to a fixed absolute path.
  • Reject any value that is absolute — strip leading slashes, reject drive letters and UNC prefixes, and reject URL schemes — before building the path.
  • After building the path, call realpath/normalize and verify the result is within the intended base directory.
  • Use basename() (or the platform equivalent) to discard any directory component when only a filename should be accepted.

Vulnerable (python)

import os
base = '/var/www/files'
name = request.args['f']
open(os.path.join(base, name)).read()  # f=/etc/passwd -> /etc/passwd

Fixed

import os
base = '/var/www/files'
name = os.path.basename(request.args['f'])
path = os.path.realpath(os.path.join(base, name))
if not path.startswith(base + os.sep):
    abort(400)
open(path).read()

basename() drops any directory part so an absolute path cannot escape the base.

FAQ

Why does /etc/passwd work without any ../?
When the handler opens the value directly, or when path-join discards the base on an absolute second argument, the absolute path selects the file on its own. No upward traversal is needed.

References