#!/bin/bash
#####################################################################
#
# Fortify SCA FPR파일 내 분석된 소스파일 수와 실 디렉토리 내에
# 소스파일 수를 비교한다.
#
# 윈도우에서 실행 시:
# bash -c "./comp_src_count.sh"
#
# by 이존석(hasu0707@esvali.com)
#
#####################################################################
DEBUG_ON=0
VERSION=0.1
WIN_MINGW_DIR="/cygdrive/c/PortableApps/cmd_cygwin_x86_64"
FPR_TMP_DIR="./fpr_tmp"
RESULT_FILE="./comp_src_count.csv"
FPR_DIR="/root/fpr"
FPR_SRCCOUNT=0
SRCFILE_COUNT=0
#####################################################################
#
# 사용방법 출력
#
#####################################################################
func_usage() {
echo "$0 ver.${VERSION}"
}
#####################################################################
#
# 초기화
#
#####################################################################
func_init() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_init()"
fi
if [ -z "${WINDIR}" ]; then
IS_WINDOWS=0
else
IS_WINDOWS=1
fi
if [ ${IS_WINDOWS} -eq 1 ]; then
DEVNULL=null.dev
WINCMD="/cygdrive/c/Windows/System32/cmd.exe /C"
PATH=${WIN_MINGW_DIR}/bin:${PATH}
ECHO_CMD="${WIN_MINGW_DIR}/bin/echo"
FIND_CMD="${WIN_MINGW_DIR}/bin/find"
else
DEVNULL=/dev/null
WINCMD=""
ECHO_CMD="echo"
FIND_CMD="find"
fi
}
#####################################################################
#
# 필요한 유틸리티가 있는지 검사
#
#####################################################################
func_check_utils() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_check_utils()"
fi
local IS_EXIT=0;
UTILNAMES=( "grep" "unzip" "find")
# 윈도우 유틸리티 체킹
if [ ${IS_WINDOWS} -eq 1 ]; then
if [ ! -e ${WIN_MINGW_DIR}/bin/xmllint ] || [ ! -e ${WIN_MINGW_DIR}/bin/unzip ] || [ ! -e ${WIN_MINGW_DIR}/bin/sed ] || [ ! -e ${WIN_MINGW_DIR}/bin/basename ]; then
echo "ERROR: ${WIN_MINGW_DIR} not found !"
IS_EXIT=1
fi
if [ ${IS_EXIT} -ne 0 ]; then
exit 1
fi
return
fi
# 리눅스 유틸리티 체킹
for LOOP1 in "${UTILNAMES[@]}"
do
which ${LOOP1} > ${DEVNULL}
if [ $? -ne 0 ]; then
echo "ERROR: ${LOOP1} not found !"
IS_EXIT=1
fi
done
# 없는 유틸리티가 있으면 스크립트 종료
if [ ${IS_EXIT} -ne 0 ]; then
exit 1
fi
unset UTILNAMES
}
#####################################################################
#
# 임시 및 불필요한 파일 삭제
#
#####################################################################
func_clean() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_clean()"
fi
if [ ${IS_WINDOWS} -eq 1 ]; then
rm -f ${DEVNULL}
fi
rm -rf ${FPR_TMP_DIR}
}
#####################################################################
#
# FPR파일 unzip
# $1 : fpr 파일명
#
#####################################################################
func_unzip() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_unzip()"
fi
if [ -d ${FPR_TMP_DIR} ]
then
rm -rf ${FPR_TMP_DIR}
fi
mkdir ${FPR_TMP_DIR}
unzip $1 -d ${FPR_TMP_DIR} audit.fvdl audit.xml filtertemplate.xml &> ${DEVNULL}
}
#####################################################################
#
# audit.fvdl에서 파일수를 추출한다.
#
#####################################################################
func_get_srccount() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_get_srccount()"
fi
FPR_SRCCOUNT=$(grep -e "<NumberFiles>.*</NumberFiles>" ${FPR_TMP_DIR}/audit.fvdl | cut -d ">" -f2 | cut -d "<" -f1)
# 파일 갯수 출력
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> fpr src count: ${FPR_SRCCOUNT}"
fi
}
#####################################################################
#
# 특정 디렉토리에서 소스파일을 찾는다.
#
#####################################################################
func_get_file_count() {
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> func_get_file_count()"
fi
SRCFILE_COUNT=$(${FIND_CMD} "${1}" -type f -name *.java -o -name *.php -o -name *.xml -o -name *.html -o -name *.js -o -name *.jsp -o -name *.kt -o -name *.conf -o -name *.cs -o -name *.py -o -name *.properties | wc -l)
# 파일 갯수 출력
if [ ${DEBUG_ON} -eq 1 ]
then
echo ">> file src count: ${SRCFILE_COUNT}"
fi
}
#####################################################################
#
# Prefix를 받아서 해당 업무의 가장 최근 FPR 파일명을 알아낸다.
#
#####################################################################
get_last_fpr_filename() {
unset ${FPR_FILENAME}
FPR_FILENAME=`ls -1r ${FPR_DIR}/${1}_*.fpr | head -n 1`
}
#####################################################################
#
# 결과를 출력한다.
#
#####################################################################
func_print_result() {
${ECHO_CMD} "${1},${2},${FPR_SRCCOUNT},${SRCFILE_COUNT}"
${ECHO_CMD} "${1},${2},${FPR_SRCCOUNT},${SRCFILE_COUNT}" >> ${RESULT_FILE}
}
#####################################################################
#
# 하나의 업무에 대한 분석 실행
#
#####################################################################
func_comp_src_count() {
# FPR 디렉토리에서 가장 최근의 FPR 파일의 파일명을 알아낸다.
get_last_fpr_filename ${1}
if [ ! ${FPR_FILENAME} ]
then
echo "ERROR: FPR file not found ! (${1})"
return 1
fi
# FPR 파일 존재 여부 체크
#if [ ! -e ${1} ]; then
# echo "ERROR: ${1} not found !"
# return
#fi
# 소스 디렉토리 존재 여부 체크
if [ ! -d ${2} ]; then
echo "ERROR: ${2} not found !"
return
fi
func_unzip "${FPR_FILENAME}"
func_get_srccount
func_get_file_count "${2}"
func_print_result "${FPR_FILENAME}" "${2}"
func_clean
}
#####################################################################
#
# main
#
#####################################################################
func_usage
func_init
func_check_utils
${ECHO_CMD} "FPR,SRCDIR,COUNT_IN_FPR,COUNT_IN_SRCDIR" > ${RESULT_FILE}
#####################################################################
# 이 아래로 업무의 이니셜과 소스 디렉토리를 나열한다.
#####################################################################
func_comp_src_count "webgoat" "./webgoat"
func_comp_src_count "webgoat" "./webgoat"
func_comp_src_count "webgoat" "./webgoat"