Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 16, 2008 8:07 AM
Hi all,
I have been wondering to know whether buffer overflow is really possible in c#? If so please tell how?
Thanks in Advance.
All replies (13)
Tuesday, September 16, 2008 12:44 PM ✅Answered
It is possible but it occurs rarely in C# this is an error you get under circumstances like (when ur trying to login into an account where a username doesnt exist ).This is a condition i have seen in C++ a lot but i have never come across this error in C# but try searching for it in google you might get a bretter prespective .
Tuesday, September 16, 2008 2:19 PM ✅Answered
Should be easily achieved with unsafe code.
Sunday, October 5, 2008 5:03 AM
I was asked this question as how a hacker can exploit buffer overflow to hack the system. Can you give me some nice example around it. Is it somehow achievable in C#.
A code example (may not be in c# if its not possible in it) will be great for understanding.
Thanks in Advance.
Aman.
Thursday, October 23, 2008 9:37 AM
Can anyone explain more with some code sample?
Friday, May 1, 2009 1:01 AM
JohnWein says "Should be easily achieved with unsafe code."
I just setup two test examples where a buffer overflow in C# would occur:
char[] test = new char[20];
char[] test2 = new char[250];
for (int i = 0; i < 250; i++)
test2[i] = 'A';
//Test1
Array.Copy(test2, test, 250);
//Test2
/* for (int i = 0; i < test2.Length; i++)
test[i] = 'A'; */
Test1 response:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
Test2 response:
Index was outside the bounds of the array.
The .NET framework catches it. The application continues running. No user supplied data is written to the stack.
Not exploitable. Can anyone else provide better sample code? I read somewhere that it is possible to mark code as "unsafe"...
Friday, May 1, 2009 1:46 AM
If you use the fixed statement (http://msdn.microsoft.com/en-us/library/f58wzh41.aspx) in an unsafe context, you can make a pointer to test2 and test, and write past your buffer with pointer math.
Most of the pointer buffer overflow issues that can happen in C++ are possible using pointers in C#, but require the context to be unsafe.Reed Copsey, Jr. - http://reedcopsey.com
Friday, May 1, 2009 1:54 AM
ahh cool. Thanks for the info! :)
Friday, June 12, 2009 10:28 PM
Thank you for the answer.
I understand the UNSAFE and FIXED , but how can i crash the process with buffer overflow that has UNSAFE and FIXED.
I examine many but i can't find.
(I want to do sth like this link but this is with C in linux ) -> http://www.securitytube.net/Buffer-Overflow-Primer-Part-1-(Smashing-the-Stack)-video.aspx
Thankx.
Saturday, June 13, 2009 4:48 AM
Hi,
In the previous code example, buffers are allocated on the heap. Thus, you will not be able to hack the stack I think.
If you deliberately want to try this, create the buffer on the stack using 'stackalloc'.
static void Main(string[] args)
{
CrackMe();
Console.WriteLine("I am here.");
}
unsafe static void CrackMe()
{
try
{
int* p = stackalloc int[32];
int i = 36;
while (i > 0)
{
*p++ = 0;
--i;
}
}
catch (Exception)
{
Console.WriteLine("Exception caught.");
}
Console.WriteLine("Finished buffer overflow attack");
}
The above code will crash the application. Enjoy.
(btw, I mis-clicked "Propose As Answer" on your post... lol)
Saturday, June 13, 2009 8:59 AM
Thank you very much. you are so kind.
I have another question : i have a program like you say but i have another method that doesn't call in my program like following :
static void Main(string[] args)
{
CrackMe();
Console.WriteLine("I am here.");
}
unsafe static void DO_IT()
{
Console.WriteLine("Never Enter Here");
}
unsafe static void CrackMe()
{
try
{
int* p = stackalloc int[32];
int i = 36;
while (i > 0)
{
*p++ = 0;
--i;
}
}
catch (Exception)
{
Console.WriteLine("Exception caught.");
}
Console.WriteLine("Finished buffer overflow attack");
}
How can i call DO_IT method with Buffer overflow.
for example store the address of DO_IT method in *(p+33) ...*(p+36) and when it is overflow this address write on stack instead of return address of CrackMe.
Sunday, June 14, 2009 6:08 AM
I understand what u want. But I wonder what is your motivation. CLR works hard to provide a safe programming model. Why not enjoy that?
IF DO_IT() has never been excuted before (no code calls it), I think it has not been JIT compiled. So, it is very likely that your cannot get the method entry address by the time at all.
Theoretically you can do this. But I am not sure how CLR will behave under this circumstance. It may do some runtime check, it may protect some critical region, it may allocate executable code in a secret way so that you cannot find function entry...etc. It is up to Microsoft and it should be transparent to users. And I think it is good to hide the implementation/algorithm details here: we are safer.
Sunday, June 14, 2009 8:29 AM
OH.. , you are right.
I think that like C compiler , all codes have an address that assembler creates.
but in .net we have CLR.
we can read the memory at specific address with (kernel32.dll) but as you say , DO_IT address never create.
Thank you so much.
i just want to increase my knowledge.
Monday, April 1, 2013 8:44 PM
Why exactly does this fail? And if you changed just one line to fix it, how would you do so? The *p++ = 0 is what's throwing me off