Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Basic C Language Question for malloc() - Buffer Overflow

So with the following codes below for testing out buffer-overflow, why does the output of the variable name and command always come out different, is it due to the usage of memory not being 'static' - always flactuating? Variable name and command as in the address of 'Name' and 'Command'.

Tutorial is gotten from some guy on youtube, am still learning..
Quote:#include <stdio.h>
void main()
{
char *name;
char *command;

name=(char *)malloc(10);
command=(char *)malloc(128);

printf("address of Name is : %d\n", name);
printf("address of Command is : %d\n", command);
printf("Difference between address is : %d\n", command - name);
printf("Hey enter your name: ");
gets(name);
printf("Hello %s\n", name);
system(command);
}

(Feb 15, 2016 07:56 AM)DarkETHOS Wrote:  So with the following codes below for testing out buffer-overflow, why does the output of the variable name and command always come out different, is it due to the usage of memory not being 'static' - always flactuating? Variable name and command as in the address of 'Name' and 'Command'.

and what does the asterisk in name and *command meant, I read that is deference from a pointer, can someone confirm?

Tutorial is gotten from some guy on youtube, am still learning..

Those are different because every-time the program executes, they are given a different place in memory that they are put in. It doesn't have to do with just malloc, even a normal variable will have a different place in memory each time it's run.

And for the
and &. Look here: http://stackoverflow.com/questions/57483...7492#57492
(Feb 15, 2016 08:30 AM)Annabelle Lee Wrote:  Those are different because every-time the program executes, they are given a different place in memory that they are put in. It doesn't have to do with just malloc, even a normal variable will have a different place in memory each time it's run.

And for the * and &. Look here: http://stackoverflow.com/questions/57483...7492#57492

Thanks a lot!! Omg, all this pointer and acronym EAX EBX is killing me.. need to practice this for a coming buffer overflow test.
malloc allocates the specified number of bytes in the heap, the function is implemented by the operation system, the OS "gives" to the process a allocated location with the specified number of bytes, so each time the location is different.
The locations of any variables on the stack or within other segments of the binary will be randomized if ASLR is enabled (OS may vary on implementation) but essentially this means that the base address of the program's allocated virtual memory will be randomized. This way, you cannot refer to the same spot in memory and know that it always uses holds the same data.

However, malloc() is not completely like this. It will grab from an available place in heap memory that is not determined, even if ASLR is completely off. On unix, it uses the sbrk system call which dynamically raises the size of the data segment, but as this is dynamic throughout the program, there is no guarantee from where the memory will be allocated. In windows, I believe it uses HeapAlloc, but there are other functions that you would use like VirtualAlloc or new[] (in C++). The principle, I believe, is the same.

ALSO FREE YOUR MEMORY

Any time I see malloc() or calloc(), there had better be a corresponding free() call.
PHP Code

<?php 
name
=(char *)malloc(10);
command=(char *)malloc(128);
// ... some code here ...
free(name);
free(command); 

Also, @p007 that is right, but keep in mind that it gives you at least N number of bytes in malloc(N). Alignment needs and whatnot may give you more, but if the command succeeds, you are guaranteed to have at least N number of usable bytes.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by xx1ben99xxApr 30, 2020 09:05 PM 22 556 Apr 30, 2020 09:05 PM
Last Post: 0xDEAD10CC
[C] by Consulting DetectiveApr 3, 2020 05:31 PM 8 358 Apr 3, 2020 05:31 PM
Last Post: teh0n3bro
[C] by SIM-1673779Apr 17, 2018 05:04 PM 3 205 Apr 17, 2018 05:04 PM
Last Post: SIM-1673779