Check Ghost vulnerability

cool_penguin_smallAs you might already know, Ghost is a newly discovered GNU C Library (glibc) vulnerability and may leave your system open to remote code execution. glibc versions lesser than 2.18 are vulnerable to remote code execution via a vulnerability in the gethostbyname function. This may allow a remote attacker to take control of an affected system.

You can check the glibc version using the following command:

$ ldd --version

To test if your system is vulnerable or not, save the following script in a text file, make the file executable and run it:

#!/usr/bin/env bash

SCRIPT_TAG="GHOST_CHECK_`date +%Y%m%d`.0"

TMP="/var/tmp"

if [ ! -d ${TMP} ]; then
	mkdir -pv ${TMP}
fi

GHOST_SRC="${TMP}/ghost.c"
GHOST_BIN="${TMP}/ghost"

cat > ${GHOST_SRC} << EOF
#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;
  char name[sizeof(temp.buffer)];
  memset(name, '0', len);
  name[len] = '';

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

  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);
}
EOF

gcc ${GHOST_SRC} -o ${GHOST_BIN}
if [ $? -ne 0 ]; then
	echo "! ERROR: GCC failed to compile ghost"
	echo "${SCRIPT_TAG}_ERR_01"
	exit 1
fi
cd ${TMP}
GHOST_BIN_NAME=`basename ${GHOST_BIN}`
GHOST_OUT=`./${GHOST_BIN_NAME}`
echo "+ Ghost output is ${GHOST_OUT}"
if [ "${GHOST_OUT}" == 'vulnerable' ]; then
	echo "GHOST_FOUND_VULN"
	echo "+ getting list of information"
	rpm -qa | grep -i glibc
	echo "${SCRIPT_TAG}_ISVULN"
        rm -rf ${GHOST_SRC}
        rm -rf ${GHOST_BIN}
	exit 0
else
	echo "GHOST_NOT_FOUND_VULN"
	echo "${SCRIPT_TAG}_NOTVULN"
        rm -rf ${GHOST_SRC}
        rm -rf ${GHOST_BIN}
	exit 0
fi

As you can see, the script generates a small C program and runs it to test the vulnerability. Here’s the original source to which I have made minor changes to delete the generated files.

Output from my system:

$ ./test_ghost.sh 
+ Ghost output is not vulnerable
GHOST_NOT_FOUND_VULN
GHOST_CHECK_20150128.0_NOTVULN

Yay!

On an affected system the output should show the line:

GHOST_CHECK_20150128.0_ISVULN

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s