LoginSignup
7
7

More than 5 years have passed since last update.

GHOST脆弱性を検証してみた

Last updated at Posted at 2015-01-28

検証してみたと言うほどではないですが・・・。

検証コードにprintfを追加して動作を追って見ました。

GHOST.c
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define CANARY "in_the_coal_mine"

struct {
  char buffer[1024];
  char canary[sizeof(CANARY)];
} temp = { "buffer", CANARY };

int main(void) {
  struct hostent resbuf;
  struct hostent *result;
  int herrno;
  int retval;

  /*** strlen (name) = size_needed - sizeof (*host_addr) - sizeof (*h_addr_ptrs) - 1; ***/
  size_t len = sizeof(temp.buffer) - 16*sizeof(unsigned char) - 2*sizeof(char *) - 1;
  printf("sizeof(unsigned char) = %d\n", (int)sizeof(unsigned char));
  printf("sizeof(char *) = %d\n", (int)sizeof(char *));
  printf("sizeof(temp.buffer) - 16*sizeof(unsigned char) - 2*sizeof(char *) - 1 = %d\n", (int)len);

  char name[sizeof(temp.buffer)];
  memset(name, '0', len);
  name[len] = '\0';

  printf("before temp.canary = %s\n", temp.canary);

  retval = gethostbyname_r(name, &resbuf, temp.buffer, sizeof(temp.buffer), &result, &herrno);

  printf("after temp.canary = %s\n", temp.canary);

  if (strcmp(temp.canary, CANARY) != 0) {
    puts("vulnerable");
    exit(EXIT_SUCCESS);
  }
  if (retval == ERANGE) {
    puts("not vulnerable");
    exit(EXIT_SUCCESS);
  }
  puts("should not happen");
  exit(EXIT_FAILURE);
}

実行結果
スクリーンショット 2015-01-29 1.26.12.png

引数で渡されたtemp構造体のcanaryがgethostbyname_rの実行後に0000000で書き換えられている。
つまりここに悪意あるコードが埋め込まれた場合に悪い事が行われてしまうようです。

ただ任意のコードを埋め込み方がいまいちわからなかったので、詳しい方いらっしゃいましたら解説をどうかお願いいたします・・・。

この脆弱性をつくには条件があるようです。
その条件というのがgethostbyname_rの第一引数で渡されるnameには数値のみが入力された状態でなければならないということ。
memsetで0でchar nameを埋めている箇所を文字で埋めるとgethostbyname_r実行後temp.canaryの値は変わる事はなかった。

調べる上で役に立ったサイト

日本語解説記事1
日本語解説記事2
gethostbyname_r

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7