> For the complete documentation index, see [llms.txt](https://docs.cooku222.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cooku222.kr/security/misc/dreamhack/dreamhack-addition-quiz.md).

# \[Dreamhack] addition-quiz

{% embed url="<https://dreamhack.io/wargame/challenges/1114>" %}

```
// Name: chall.c
// Compile Option: gcc chall.c -o chall -fno-stack-protector

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>

#define FLAG_SIZE 0x45

void alarm_handler() {
    puts("TIME OUT");
    exit(1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    signal(SIGALRM, alarm_handler);
}

int main(void) {
    int fd;
    char *flag;

    initialize();
    srand(time(NULL)); 

    flag = (char *)malloc(FLAG_SIZE);
    fd = open("./flag", O_RDONLY);
    read(fd, flag, FLAG_SIZE);
    close(fd);

    int num1 = 0;
    int num2 = 0;
    int inpt = 0; 

    for (int i = 0; i < 50; i++){
        alarm(1);
        num1 = rand() % 10000;
        num2 = rand() % 10000;
        printf("%d+%d=?\n", num1, num2);
        scanf("%d", &inpt);

        if(inpt != num1 + num2){
            printf("Wrong...\n");
            return 0;
        }
    } 
    
    puts("Nice!");
    puts(flag);

    return 0;
}
```

서버가 생성한 랜덤한 4자리 숫자를 받은 다음 두 숫자를 더해서 합을 서버에게 다시 보내준다. 그 보낸 값이 scanf(%d)로 들어가서 wrong이 출력 안 되도록 익스를 짜면 된다.

&#x20;

**exploit.py**

```
from pwn import *

#원격 서버에 연결

conn = remote('host3.dreamhack.games', 15810)

for _ in range(50):
	#서버로부터 문제를 받음
    question = conn.recvline().decode().strip()
	
    #'=' 기준으로 숫자를 분리
    num_str, _ = question.split('=')

	#'+' 기호를 기준으로 숫자를 분리
    num1, num2 = map(int, num_str.split('+'))

	#덧셈을 계산하고 결과를 서버에 전송
    conn.sendline(str(num1 + num2))

#결과를 출력
print(conn.recvall().decode())
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cooku222.kr/security/misc/dreamhack/dreamhack-addition-quiz.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
