DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C - Example Buffer OverFlow

11.01.2006
| 10046 views |
  • submit to reddit
        /*
 *
 * Esempio di Buffer Overflow ...
 */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	char *buffer1 = (char *)calloc(5, sizeof(char));
	char *buffer2 = (char *)calloc(15, sizeof(char));
	char *tmp;
	
	strcpy(buffer2, "ls -a --color");
	strcpy(buffer1, argv[1]);

	// Indirizzi di memoria...
	printf("%p <-- buffer1\n", buffer1);
	printf("%p <-- buffer2\n", buffer2);
	printf("\n\n");

	// Stampa indirizzi...
	printf("Start code....\n");
	tmp=buffer1;
	while(tmp<buffer2+15)
	{
		printf("%p: %c (0x%x)\n", tmp, *tmp, *(unsigned int *)tmp);
		tmp++;
	}

	printf("\n");
	system(buffer2);
	return 0;
}