Displaying stack contents

I

Ioannis Vranos

Here is a similar code to one that I saw in a video on the web:


#include <cstdio>
#include <cstring>


void somefunc(const char *input)
{
using namespace std;

char buf[5];

// Displays the stack
printf("Stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");

//Buffer overflow
strcpy(buf, input);

printf("%s\n", buf);

printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}

void somefunc2()
{
printf("somefunc2()\n");
}


int main(int argc, char *argv[])
{
using namespace std;

printf("Address of somefunc = %p\n", somefunc);

printf("Address of somefunc2 = %p\n", somefunc2);

somefunc(argv[1]);
}



So, can we be sure that we can display the contents of the stack in this way?
 
V

Victor Bazarov

Ioannis said:
Here is a similar code to one that I saw in a video on the web:


#include <cstdio>
#include <cstring>


void somefunc(const char *input)
{
using namespace std;

char buf[5];

// Displays the stack
printf("Stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");

//Buffer overflow
strcpy(buf, input);

printf("%s\n", buf);

printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}

void somefunc2()
{
printf("somefunc2()\n");
}


int main(int argc, char *argv[])
{
using namespace std;

printf("Address of somefunc = %p\n", somefunc);

printf("Address of somefunc2 = %p\n", somefunc2);

somefunc(argv[1]);
}



So, can we be sure that we can display the contents of the stack in this
way?

Definitely not. Calling 'printf' with fewer arguments than fields
specified by the format string causes undefined behaviour. What happens
in that case *could* be that 'printf' shows you the stack contents or it
*could* be that your hard drive is reformatted or that all your friends
receive obscene e-mails originating from you.

V
 
M

Matthias Kaeppler

Ioannis said:
Here is a similar code to one that I saw in a video on the web:


#include <cstdio>
#include <cstring>


void somefunc(const char *input)
{
using namespace std;

char buf[5];

// Displays the stack
printf("Stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");

//Buffer overflow
strcpy(buf, input);

printf("%s\n", buf);

printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}

void somefunc2()
{
printf("somefunc2()\n");
}


int main(int argc, char *argv[])
{
using namespace std;

printf("Address of somefunc = %p\n", somefunc);

printf("Address of somefunc2 = %p\n", somefunc2);

somefunc(argv[1]);
}



So, can we be sure that we can display the contents of the stack in this
way?

What's the deal with the %p?
`info coreutils printf` tells me it evaluates to AM or PM, depending on
your locale settings. It's a date/time specific thing o_O
 
I

Ioannis Vranos

Matthias said:
What's the deal with the %p?
`info coreutils printf` tells me it evaluates to AM or PM, depending on
your locale settings. It's a date/time specific thing o_O

?
 
P

Pete Becker

Matthias said:
What's the deal with the %p?
`info coreutils printf` tells me it evaluates to AM or PM, depending on
your locale settings. It's a date/time specific thing o_O

That's what it means in calls to strftime. In calls to printf and its
relatives it displays the value of a pointer.
 
I

Ioannis Vranos

Victor said:
Definitely not. Calling 'printf' with fewer arguments than fields
specified by the format string causes undefined behaviour. What happens
in that case *could* be that 'printf' shows you the stack contents or it
*could* be that your hard drive is reformatted or that all your friends
receive obscene e-mails originating from you.


OK, so ISO C++ speaking it is not guaranteed this to work. However in practice it looks
like it is working. Have you seen this before?

I got the code from a code-security oriented video.
 
V

Victor Bazarov

Ioannis said:
OK, so ISO C++ speaking it is not guaranteed this to work. However in
practice it looks like it is working. Have you seen this before?

No, I hadn't. Nor would I trust hacker instructional videos when
learning about language features.
 
I

Ioannis Vranos

Victor said:
No, I hadn't. Nor would I trust hacker instructional videos when
learning about language features.


Actually it was about code security and protecting from hackers and not the opposite. This
shows how buffer overruns look like, and just to provide a useful summary on this, the
bottom line was that apart from using strncpy() etc (which can also be circumvented with
various tricks), in all these types of attacked programs the data are not checked at the
point of input, and we should consider *any* input as unsafe and validate it at the point
of its introduction.
 
V

Victor Bazarov

Ioannis said:
Victor said:
No, I hadn't. Nor would I trust hacker instructional videos when
learning about language features.



Actually it was about code security and protecting from hackers and not
the opposite. [...]

Just to let you know that the best security algorithms are invented by
hackers, and knowing how a system can be broken is necessary to be able
to protect it. Instructional videos for hackers or for security personnel
are interchangeable. If you want to be able to break into a system you
might want to learn what is taught to those who are trying to protect it
and vice versa.

And my recommendation for you: if you want your code to be safe, you
should use all means possible to avoid undefined behaviour. Using printf
in the manner you asked about may not be that susceptible to any hacking,
but considering it OK because "it looks like it is working" is a very
dangerous practice.

V
 
I

Ioannis Vranos

Victor said:
And my recommendation for you: if you want your code to be safe, you
should use all means possible to avoid undefined behaviour. Using printf
in the manner you asked about may not be that susceptible to any hacking,
but considering it OK because "it looks like it is working" is a very
dangerous practice.


Of course. I found it interesting to display the stack in this way though. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top