PythonIntermediate · Windows, macOS, or Linux; Python 3.12+

Audit writable PATH directories

Check PATH directories for existence, current-user writability, and duplicate resolution order.

Before you begin

  • Python 3.12 or later.
  • Read access to the selected inputs.
What this script changesRead-only local report. No permissions, accounts, configuration, or input files are changed. Review filenames and metadata before sharing.

Set up your example

  1. Save as security-writable-path-directories.py.
  2. Open a terminal in its folder.
  3. Run python "security-writable-path-directories.py" --help and prepare the listed input files.

This recipe uses your own local input or creates its own example output. Follow the setup steps below.

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is security-writable-path-directories.py.

Run the script

Run command
python "security-writable-path-directories.py"

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

A user-owned tools directory can be writable; a repeated normalized path is marked duplicate.

The complete script

security-writable-path-directories.py
#!/usr/bin/env python3
"""Check PATH directories for existence, current-user writability, and duplicate resolution order."""
import argparse
import sys
import json
from pathlib import Path
import os

def main():
    parser = argparse.ArgumentParser(description='Check PATH directories for existence, current-user writability, and duplicate resolution order.')
    
    args = parser.parse_args()
    seen=set()
    for index,entry in enumerate(os.environ.get('PATH','').split(os.pathsep),1):
        if not entry:print(json.dumps({'position':index,'entry':'','risk':'empty entry can mean current directory'}));continue
        p=Path(os.path.expandvars(entry)).expanduser();key=os.path.normcase(str(p.resolve()))
        print(json.dumps({'position':index,'path':str(p),'exists':p.is_dir(),'writable_by_current_user':os.access(p,os.W_OK),'duplicate':key in seen}))
        seen.add(key)

if __name__ == '__main__':
    try:
        main()
    except (OSError, ValueError, RuntimeError) as exc:
        print(f'Error: {exc}', file=sys.stderr)
        sys.exit(1)

Open source as plain text · Download source and sample files

How it works

  1. Check PATH directories for existence, current-user writability, and duplicate resolution order.
  2. Arguments are explicit and expected input errors produce a message with a nonzero exit code.
  3. Results reflect your selected local data or the specific endpoint you choose; the example illustrates the result shape.

Make it your own

Adapt the task

In the loop, filter writable_by_current_user rows for a smaller report. Writability by your account is not proof of an exploitable configuration.

Adjust presentation

Edit final print statements or json.dumps indentation to adjust the report layout. Keep input validation and exclusive-create file modes intact.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

An input or dependency is unavailable.

Check the path, quote paths with spaces, and use --help to inspect required arguments.

Actual results differ from the example.

Inputs and system state determine values. Review each finding in context; a report is not proof of system health or a complete security assessment.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.