왜 카나리의 첫 바이트가 항상 \x00로 시작하는 건가요?

분명 카나리 관련 강의 내용에선 랜덤으로 생성된다고 한 것으로 기억하는데 실제 카나리의 첫바이트가 모두 /x00으로 시작하고
이후 강의에서도 추가적인 설명없이 첫바이트가 /x00 인것을 이용하여 데이터를 입력받는데...

작성자 정보
답변 2
Karatus
워게임: 50

Many buffer overflow vulnerabilities are caused by string operations such as gets(), strcpy(), read(). Strings in C are commonly terminated using a single NULL byte (0x00). An attacker would not be able to use such a byte in their payload through a string operation to reconstruct the canary. The 0x0a byte represents a line feed, commonly also terminating string operations. 0xff corresponds to an End Of File (EOF), terminating certain string operations as well.

요약하자면 string의 터미네이터 캐릭터 중 하나가 공격자의 payload 중에 포함되면 거기서 입력이 끊어지는 것을 이용해 canary를 오버라이트하는 걸 방지하기 위함입니다.

If the canary is of the null or terminator kind, it may not be effective in preventing certain writes. As discussed earlier, the read() function will allow us to write null bytes to a buffer, effectively disabling the security the null bytes should add. Furthermore, if multiple buffers can be written sequentially, the attacker may take use of null termination of strings in C to write the required NULL bytes into the canary position.

물론 몇가지 입력 함수(ex. read())는 null byte 같은 터미네이터 캐릭터도 입력 가능하여 의미는 없습니다.

출처: https://www.sans.org/blog/stack-canaries-gingerly-sidestepping-the-cage/

2021.09.05. 09:00
avatar
juno2
답변 등록: 25

위 karatus 님의 답변에 좀 더 첨언을 하자면,

buf[256];

strncpy(buf, src, 256);
puts(buf);

^ 코드가 있다고 할 때, buf + canary + sfp + ret 형태로 스택 프레임이 구성되어있을 것입니다.
buf에 256바이트를 꽉차게 넣어, canary의 유출을 방지하고자하는 목적도 있습니다.

대부분의 아키텍처의 카나리가 null byte로 시작하지만, 아닌 아키텍처도 있습니다.

2021.09.06. 11:35
질문에 대한 답을 알고 계신가요?
지식을 나누고 포인트를 획득해보세요.
답변하고 포인트 받기