Page cover
githubEdit

align-slashPython Format String Injection

.format() method allows accessing object attributes and methods. When user input is used in format strings, attackers can access sensitive application internals

circle-exclamation
chevron-rightDetectionhashtag
circle-info

Step 1: Identify Format String Usage

Look for these patterns in source code:
.format()
f"{variable}"
"{0}".format()
Template().substitute()
circle-info

Step 2: Test for Execution

Test basic arithmetic
curl "https://target.com/greet?name={7*7}"
circle-info

Step 3: Test Object Access

Try accessing object attributes
curl "https://target.com/greet?name={obj.__class__}"
  • If returns <class 'str'> or similar, vulnerable

circle-info

Testing Checklist

chevron-rightExploitation Techniqueshashtag
triangle-exclamation
triangle-exclamation
triangle-exclamation
chevron-rightCommon Framework Targetshashtag
Flask:
{app}                    # Application object
{config}                 # Configuration
{request}                # Current request
{session}                # Session data
{g}                      # Global context
Django:
{settings}               # Django settings
{request.user}          # Current user
{request.META}          # Request metadata
chevron-rightPreventionhashtag
circle-info

Method 1: Never Use User Input in Format

circle-info

Method 2: Use Safe Alternatives

  • Only substitutes valid identifiers, can't access attributes.

circle-info

Method 3: Whitelist Allowed Values

circle-info

Method 4: Escape User Input

Last updated