Home > CAPEC List > CAPEC-135: Format String Injection (Version 2.4)  

CAPEC-135: Format String Injection

 
Format String Injection
Definition in a New Window Definition in a New Window
Attack Pattern ID: 135
Abstraction: Standard
Status: Draft
Completeness: Complete
+ Description

Summary

An attacker includes formatting characters in a string input field on the target application. Most applications assume that users will provide static text and may respond unpredictably to the presence of formatting character. For example, in certain functions of the C programming languages such as printf, the formatting character %s will print the contents of a memory location expecting this location to identify a string and the formatting character %n prints the number of DWORD written in the memory. An attacker can use this to read or write to memory locations or files, or simply to manipulate the value of the resulting text in unexpected ways. Reading or writing memory may result in program crashes and writing memory could result in the execution of arbitrary code if the attacker can write to the program stack.

Attack Execution Flow

Explore
  1. Survey application:

    The attacker takes an inventory of the entry points of the application.

    Attack Step Techniques

    IDAttack Step Technique DescriptionEnvironments
    1

    Spider web sites for all available links

    env-Web
    2

    List parameters, external variables, configuration files variables, etc. that are possibly used by the application.

    env-All

    Outcomes

    IDTypeOutcome Description
    1Success
    At least one data input to application identified.
    2Inconclusive
    No inputs to application identified. Note that just because no inputs are identified does not mean that the application will not accept any.
Experiment
  1. Determine user-controllable input susceptible to format string injection:

    Determine the user-controllable input susceptible to format string injection. For each user-controllable input that the attacker suspects is vulnerable to format string injection, attempt to inject formatting characters such as %n, %s, etc.. The goal is to manipulate the string creation using these formatting characters.

    Attack Step Techniques

    IDAttack Step Technique DescriptionEnvironments
    1

    Inject probe payload which contains formatting characters (%s, %d, %n, etc.) through input parameters.

    env-Web

    Indicators

    IDTypeIndicator DescriptionEnvironments
    1Negative

    Attacker receives normal response from server.

    env-Web env-Peer2Peer env-CommProtocol env-ClientServer
    2Positive

    Attacker receives an abnormal message (let's say with a partial dump of the memory) from the application which indicates that the format string was successfully manipulated.

    env-Web env-Peer2Peer env-CommProtocol env-ClientServer

    Outcomes

    IDTypeOutcome Description
    1Success
    At least one user-controllable input susceptible to injection found.
    2Failure
    No user-controllable input susceptible to injection found.

    Security Controls

    IDTypeSecurity Control Description
    1Detective
    Search for and report format string injection indicators such as the use of %s, %n, %d, etc. in submitted user input
    2Preventative
    Refrain from using format strings when not necessary, for example fprintf(str) can be replaced by fputs(str), etc.
Exploit
  1. Try to exploit the Format String Injection vulnerability:

    After determining that a given input is vulnerable to format string injection, hypothesize what the underlying usage looks like and the associated constraints.

    Attack Step Techniques

    IDAttack Step Technique DescriptionEnvironments
    1

    Insert various formatting characters to read or write the memory, e.g. overwrite return address, etc.

    env-Web

    Indicators

    IDTypeIndicator DescriptionEnvironments
    1Positive

    Probing via format character injection was successful in identifying vulnerable input.

    env-Web env-Peer2Peer env-CommProtocol env-ClientServer
    2Negative

    Probing via format character injection failed in identifying vulnerable input.

    env-Web env-Peer2Peer env-CommProtocol env-ClientServer

    Outcomes

    IDTypeOutcome Description
    1Success
    Attacker achieves goal of reading or writing the memory, manipulating the formatting string
    2Inconclusive
    Attacker unable to exploit the format string injection vulnerability
+ Attack Prerequisites
  • The target application must accept a strings as user input, fail to sanitize string formatting characters in the user input, and process this string using functions that interpret string formatting characters.

+ Typical Severity

High

+ Typical Likelihood of Exploit

Likelihood: High

+ Methods of Attack
  • Injection
+ Examples-Instances

Description

Untrusted search path vulnerability in the add_filename_to_string function in intl/gettext/loadmsgcat.c for Elinks 0.11.1 allows local users to cause Elinks to use an untrusted gettext message catalog (.po file) in a "../po" directory, which can be leveraged to conduct format string attacks.

Related Vulnerabilities

+ Attacker Skills or Knowledge Required

Skill or Knowledge Level: High

In order to discover format string vulnerabilities it takes only low skill, however, converting this discovery into a working exploit requires advanced knowledge on the part of the attacker.

+ Resources Required

No special resources are required beyond the ability to provide string input to the target.

+ Solutions and Mitigations

Limit the usage of formatting string functions.

Strong input validation - All user-controllable input must be validated and filtered for illegal formatting characters.

+ Attack Motivation-Consequences
ScopeTechnical ImpactNote
Integrity
Modify memory
Confidentiality
Read memory
Integrity
Modify files or directories
Confidentiality
Read files or directories
Integrity
Modify application data
Confidentiality
Read application data
Gain privileges / assume identity
Execute unauthorized code or commands
Run Arbitrary Code
Bypass protection mechanism
+ Injection Vector

User-controllable input used as formatting string.

+ Payload

Formatting characters associated with malicious string content intended to reveal information or modify the memory.

+ Activation Zone

Application (server, client, etc.)

+ Relevant Security Requirements

User-controllable input shall not be used directly inside a formatting string function e.g., fprintf(user_controllable). Special formatting characters in user-controllable input must be escaped before use by the application in a formatting string function.

Ensure that all format string functions are passed a static string which cannot be controlled by the user and that the proper number of arguments are always sent to that function as well. If at all possible, use functions that do not support the %n operator in format strings.

+ Purposes
  • Penetration
  • Exploitation
+ CIA Impact
Confidentiality Impact: HighIntegrity Impact: HighAvailability Impact: Medium
+ Technical Context
Architectural Paradigms
Client-Server
n-Tier
Frameworks
All
Platforms
All
Languages
All
+ References
[R.135.1] Hal Burch and Brendan Saulsbury. "FIO30-C. Exclude user input from format strings". CERT. May 2011. <https://www.securecoding.cert.org/confluence/display/seccode/FIO30-C.+Exclude+user+input+from+format+strings>.
[R.135.2] [REF-1] Robert Auger. "WASC Threat Classification 2.0". WASC-06 - Format String. The Web Application Security Consortium (WASC). Feb 2009. <http://projects.webappsec.org/Format-String>.
[R.135.3] [REF-5] Fortify. "The OWASP Application Security Desk Reference". Format String. The Open Web Application Security Project (OWASP). 2010. <https://www.owasp.org/index.php/Format_String>.
+ Content History
Submissions
SubmitterOrganizationDateComments
John StevenCigital, Inc2007-02-10Initial core pattern content
Modifications
ModifierOrganizationDateCommentsSource
Chiradeep B. ChhayaCigital, Inc2007-02-23Fleshed out pattern with extra content
Richard StruseVOXEM, Inc2007-03-26Review and feedback leading to changes in Attack Execution Flow, Attack Prerequisites, Examples and Solutions
Sean BarnumCigital, Inc2007-04-13Modified pattern content according to review and feedback
Paco HopeCigital, Inc.2007-10-20Added extended Attack Execution Flow
Romain GaucherCigital, Inc.2007-10-20Performed a review of content and added additional content
CAPEC Content TeamThe MITRE Corporation2007-10-20Review and added additional content.
CAPEC Content TeamThe MITRE Corporation2013-12-18Updated Related_Attack_PatternsInternal

Page Last Updated: April 10, 2014