C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-18-2009, 03:44 AM   #1
Registered User
 
Join Date: Dec 2009
Posts: 1
Bufferoverflow in c

hi dis is dileep,

iam new to this topic. but i has to learn the things for my academic project. Please anybody give me the sompe sample programs for bufferoverflow in c language.
dileep is offline   Reply With Quote
Old 12-18-2009, 03:47 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,460
What do you understand by "buffer overflow"? You should be able to construct a very simple example yourself given that understanding.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 12-18-2009, 04:06 AM   #3
Registered User
 
Join Date: Sep 2006
Posts: 3,720
gets() would be a great candidate for a good buffer overflow example.

I don't have such an example, but you should be able to find several with a bit of googling.
Adak is offline   Reply With Quote
Old 12-18-2009, 06:02 AM   #4
Learning C.
 
JOZZY& Wakko's Avatar
 
Join Date: Nov 2009
Posts: 59
Quote:
While C's simplicity increases the programmer's control and the efficiency of the resulting programs, it can also result in programs that are vulnerable to buffer overflows and memory leaks if the programmer isn't careful. This means that once a variable is allocated memory, there are no built-in safeguards to ensure that the contents of a variable fit into the allocated memory space. If a programmer wants to put ten bytes of data into a buffer that had only been allocated eight bytes of space, that type of action is allowed, even though it will most likely cause the program to crash. This is known as a buffer overrun or buffer overflow, since the extra two bytes of data will overflow and spill out of the allocated memory, overwriting whatever happens to come next. If a critical piece of data is overwritten, the program will crash. The overflow_example.c code offers an example.
Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
   int value = 5;
   char buffer_one[8], buffer_two[8];
   
   strcpy(buffer_one, "one"); /* Put "one" into buffer_one. */
   strcpy(buffer_two, "two"); /* Put "two" into buffer_two. */
   
   printf("[BEFORE] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
   printf("[BEFORE] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
   printf("[BEFORE] value is at %p and is %d (0x%08x)\n", &value, value, value);
   
   printf("\n[STRCPY] copying %d bytes into buffer_two\n\n",  strlen(argv[1]));
   strcpy(buffer_two, argv[1]); /* Copy first argument into buffer_two. */
   
   printf("[AFTER] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
   printf("[AFTER] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
   printf("[AFTER] value is at %p and is %d (0x%08x)\n", &value, value, value); 
}
Source: Hacking: The Art of Exploitation - Wikipedia, the free encyclopedia
JOZZY& Wakko is offline   Reply With Quote
Old 12-18-2009, 07:31 AM   #5
Registered User
 
Join Date: Oct 2008
Posts: 943
I've always been a fan of the following brain-dead example:

Code:
#include <stdio.h>

int main()
{
  int *a[1];
  int i;

  printf("Main\n");

  for(i = 0; i < 10; i++)
    a[i] = main;
}
EVOEx is offline   Reply With Quote
Old 12-18-2009, 03:21 PM   #6
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
You think that should compile? It shouldn't.
A better example should be:
Code:
int main()
{
    int a[1];

    for (int i = 0; i < 10; i++)
        a[i] = 0;
}
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-18-2009, 03:28 PM   #7
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
Quote:
Originally Posted by Elysia View Post
You think that should compile? It shouldn't.
A better example should be:
Code:
int main()
{
    int a[1];

    for (int i = 0; i < 10; i++)
        a[i] = 0;
}
Your example removes all the coolness. The reason for using main as the value is so that the stack return address gets overwritten with the address of main, causing an infinite loop when the function tries to return. Yes, it might not compile as-is, but it would with an appropriate cast.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-18-2009, 03:32 PM   #8
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
I'm fine with the example if it would compile.
So if we would use a real function pointer, that would be cool.
Code:
typedef int (main_ptr)();
int main()
{
    main_ptr* ptr[1];
    for (int i = 0; i < 10; i++)
        ptr[i] = &main;
}
And let's not rely on old coding styles...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-18-2009, 03:35 PM   #9
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
Quote:
Originally Posted by Elysia View Post
I'm fine with the example if it would compile.
So if we would use a real function pointer, that would be cool.
Well, I just tried it myself and it doesn't work anyway with gcc 4.1.2 at least. I think what is happening is that the store for the 'i' variable is allocated above the memory for the 'a' array, and the loop overwrites i itself with the address of main() and prematurely terminates. I tried declaring i as register, and then it just started crashing.

Would be a cool demo if it did work, and I'm sure it does, on some compilers.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 12-18-2009, 03:36 PM   #10
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
Speaking of overflows....question for the Windows gurus out there.... back in the WinXP/Win2K era there was a way of blue-screening a Windows box with a printf() that went something like:
Code:
for(int x = 0; x < 5; x++)
     printf("\t\t\b\b\b");
Did anyone ever figure out why that happened? Just curious...
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Old 12-18-2009, 03:37 PM   #11
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
Quote:
Originally Posted by brewbuck View Post
Well, I just tried it myself and it doesn't work anyway with gcc 4.1.2 at least. I think what is happening is that the store for the 'i' variable is allocated above the memory for the 'a' array, and the loop overwrites i itself with the address of main() and prematurely terminates. I tried declaring i as register, and then it just started crashing.

Would be a cool demo if it did work, and I'm sure it does, on some compilers.
We can always try
/boots up Visual Studio.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-18-2009, 03:49 PM   #12
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Quote:
Originally Posted by jeffcobb View Post
Speaking of overflows....question for the Windows gurus out there.... back in the WinXP/Win2K era there was a way of blue-screening a Windows box with a printf() that went something like:
Code:
for(int x = 0; x < 5; x++)
     printf("\t\t\b\b\b");
Did anyone ever figure out why that happened? Just curious...
This was a special cheat included by the developers, so if there was a bug that could not be traced, we just leave a "Blue screen of death" event so that most users will be unable to figure out what to blame. Unfortunately, the cheat was a little too simple -- kind of like pressing ctrl-space twice to reload all your weapons.

Just kidding.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 12-18-2009, 04:01 PM   #13
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
I got it working:
Code:
#include <stdlib.h>

typedef int (main_ptr)();
int main()
{
	main_ptr* ptr[1];
	printf("Main!\n");
	for (int i = 0; i < 3; i++)
		ptr[i] = &main;

	__asm sub ebp, 4;
}
This recurs endlessly on Visual Studio.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 12-18-2009 at 04:15 PM.
Elysia is offline   Reply With Quote
Old 12-18-2009, 04:18 PM   #14
and the Rod of Remorse
 
Join Date: Apr 2006
Location: United States
Posts: 3,633
Quote:
Originally Posted by Elysia View Post
And let's not rely on old coding styles...
What are you talking about?
whiteflags is offline   Reply With Quote
Old 12-18-2009, 04:20 PM   #15
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
Taking the address of a function without the &.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C bufferoverflow question. can anyone solve it? asdfgh Linux Programming 2 11-03-2009 01:40 PM
Constructive criticism, suggestions etc BobS0327 C Programming 3 01-08-2006 09:35 AM


All times are GMT -6. The time now is 12:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22