본문 바로가기

FINAL 1번 본문

보안/워게임

FINAL 1번

Seongjun_You 2021. 11. 28. 21:29
#include "../common/common.c"
#include <syslog.h>
#define NAME "final1"
#define UID 0
#define GID 0
#define PORT 2994

char username[128];
char hostname[64];

void logit(char *pw)
{
  char buf[512];

  snprintf(buf, sizeof(buf), "Login from %s as [%s] with password [%s]\n", hostname, username, pw);

  syslog(LOG_USER|LOG_DEBUG, buf);
}

void trim(char *str)
{
  char *q;

  q = strchr(str, '\r');
  if(q) *q = 0;
  q = strchr(str, '\n');
  if(q) *q = 0;
}

void parser()
{
  char line[128];

  printf("[final1] $ ");

  while(fgets(line, sizeof(line)-1, stdin)) {
      trim(line);
      if(strncmp(line, "username ", 9) == 0) {
          strcpy(username, line+9);
      } else if(strncmp(line, "login ", 6) == 0) {
          if(username[0] == 0) {
              printf("invalid protocol\n");
          } else {
              logit(line + 6);
              printf("login failed\n");
          }
      }
      printf("[final1] $ ");
  }
}

void getipport()
{
  int l;
  struct sockaddr_in sin;

  l = sizeof(struct sockaddr_in);
  if(getpeername(0, &sin, &l) == -1) {
      err(1, "you don't exist");
  }

  sprintf(hostname, "%s:%d", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
}

int main(int argc, char **argv, char **envp)
{
  int fd;
  char *username;

  /* Run the process as a daemon */
  background_process(NAME, UID, GID); 
  
  /* Wait for socket activity and return */
  fd = serve_forever(PORT);

  /* Set the client socket to STDIN, STDOUT, and STDERR */
  set_io(fd);

  getipport();
  parser();

}

snprintf에서 취약점이 발생한다.

FSB를 이용하여 gotusername의 주소를 넣는다.

username은 미리 인자 값으로 쉘 코드를 삽입해둔다..

 

 

%x로 메모리를 유출시켜 got주소의 위치를 찾았다.

2byte 끊어서 넣을 예정이다.

각각 27번째 28번째에 위치한다.

 

 

 

usename : 0x0804a220

 

0xa220 - 화면 출력값 = offset1

0x10804 - 0xa220 = 0ffset2

 

shellcode는 간단하게 만들었다.

 

성공

 

 

 

 

전체 exploit code이다.

 

 

'보안 > 워게임' 카테고리의 다른 글

off_by_one_001  (0) 2021.11.28
FINAL 2번  (0) 2021.11.28
FINAL 0번  (0) 2021.11.28
NET 2번  (0) 2021.11.28
NET 1번  (0) 2021.11.28
Comments