#!/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 #include #include #include #include #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] = '\0'; 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" exit 0 else echo "GHOST_NOT_FOUND_VULN" echo "${SCRIPT_TAG}_NOTVULN" exit 0 fi