Skip to content

Commit

Permalink
Merge pull request #206 from sreschke80/shared/libcsearch
Browse files Browse the repository at this point in the history
Search libc at user defined place, allow cross plattform analysis
  • Loading branch information
slimm609 committed Dec 15, 2022
2 parents d5b6bc5 + 0b6beb9 commit 41e291b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
53 changes: 41 additions & 12 deletions checksec
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,37 @@ if [[ ${commandsmissing} == true ]]; then
sleep 2
fi

#FS_libc is used across multiple functions
for libc in libc.so.6 libc.so.7 libc.so; do
if [[ -n $(find / -name ${libc}) ]]; then
read -r FS_libc < <(find / -name ${libc})
break
# search for libc
# shall be called before using variable FS_libc
search_libc() {
if [[ -z ${FS_libc} ]]; then
# if a specific search path is given, use it
LIBC_SEARCH_PATH=/
if [[ -n "${LIBC_FILE}" ]]; then
if [[ -f "${LIBC_FILE}" ]]; then
FS_libc=${LIBC_FILE}
elif [[ -d "${LIBC_FILE}" ]]; then
LIBC_SEARCH_PATH=${LIBC_FILE}
fi
fi

if [[ -z ${FS_libc} ]]; then
#FS_libc is used across multiple functions
for libc in libc.so.6 libc.so.7 libc.so; do
if [[ -n $(find "${LIBC_SEARCH_PATH}" -name ${libc}) ]]; then
read -r FS_libc < <(find "${LIBC_SEARCH_PATH}" -name ${libc})
break
fi
done
fi
if [[ -e ${FS_libc} ]]; then
export FS_libc
else
printf "\033[31mError: libc not found.\033[m\n\n"
exit 1
fi
fi
done
if [[ -e ${FS_libc} ]]; then
export FS_libc
else
printf "\033[31mError: libc not found.\033[m\n\n"
exit 1
fi
}

for command in readelf eu-readelf greadelf; do
if (command_exists ${command}); then
Expand Down Expand Up @@ -358,6 +376,8 @@ chk_fortify_file() {
exit 1
fi

search_libc

FS_chk_func_libc=()
FS_functions=()
while IFS='' read -r line; do FS_chk_func_libc+=("$line"); done < <(${readelf} -s "${FS_libc}" 2> /dev/null | grep _chk@@ | awk '{ print $8 }' | cut -c 3- | sed -e 's/_chk@.*//')
Expand Down Expand Up @@ -397,6 +417,8 @@ chk_fortify_proc() {
name=$(head -1 "${N}/status" | cut -b 7-)
echo_message "* Process name (PID) : ${name} (${N})\n" "" "" ""

search_libc

FS_chk_func_libc=()
FS_functions=()
while IFS='' read -r line; do FS_chk_func_libc+=("$line"); done < <(${readelf} -s "${FS_libc}" 2> /dev/null | grep _chk@@ | awk '{ print $8 }' | cut -c 3- | sed -e 's/_chk@.*//')
Expand Down Expand Up @@ -779,6 +801,8 @@ filecheck() {
echo_message '\033[32mNo Symbols\t\033[m ' 'No Symbols,' ' symbols="no"' '"symbols":"no",'
fi

search_libc

FS_filechk_func_libc="$(${readelf} -s "${FS_libc}" 2> /dev/null | sed -ne 's/.*__\(.*_chk\)@@.*/\1/p')"
FS_func_libc="${FS_filechk_func_libc//_chk/}"
FS_func="$(${readelf} --dyn-syms "${1}" 2> /dev/null | awk '{ print $8 }' | sed -e 's/_*//' -e 's/@.*//' -e '/^$/d')"
Expand Down Expand Up @@ -924,6 +948,7 @@ help() {
echo " ## Checksec Options"
echo " --file={file}"
echo " --dir={directory}"
echo " --libcfile={file or search path for libc}"
echo " --listfile={text file with one file per line}"
echo " --proc={process name}"
echo " --proc-all"
Expand Down Expand Up @@ -1719,6 +1744,10 @@ while getopts "${optspec}" optchar; do
OPT=$((OPT + 1))
CHK_FUNCTION="chk_file_list"
;;
libcfile=*)
LIBC_FILE=${OPTARG#*=}
echo LIBC_FILE="${LIBC_FILE}"
;;
proc-all)
OPT=$((OPT + 1))
CHK_FUNCTION="chk_proc_all"
Expand Down
42 changes: 30 additions & 12 deletions src/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,37 @@ if [[ ${commandsmissing} == true ]]; then
sleep 2
fi

#FS_libc is used across multiple functions
for libc in libc.so.6 libc.so.7 libc.so; do
if [[ -n $(find / -name ${libc}) ]]; then
read -r FS_libc < <(find / -name ${libc})
break
# search for libc
# shall be called before using variable FS_libc
search_libc() {
if [[ -z ${FS_libc} ]]; then
# if a specific search path is given, use it
LIBC_SEARCH_PATH=/
if [[ -n "${LIBC_FILE}" ]]; then
if [[ -f "${LIBC_FILE}" ]]; then
FS_libc=${LIBC_FILE}
elif [[ -d "${LIBC_FILE}" ]]; then
LIBC_SEARCH_PATH=${LIBC_FILE}
fi
fi

if [[ -z ${FS_libc} ]]; then
#FS_libc is used across multiple functions
for libc in libc.so.6 libc.so.7 libc.so; do
if [[ -n $(find "${LIBC_SEARCH_PATH}" -name ${libc}) ]]; then
read -r FS_libc < <(find "${LIBC_SEARCH_PATH}" -name ${libc})
break
fi
done
fi
if [[ -e ${FS_libc} ]]; then
export FS_libc
else
printf "\033[31mError: libc not found.\033[m\n\n"
exit 1
fi
fi
done
if [[ -e ${FS_libc} ]]; then
export FS_libc
else
printf "\033[31mError: libc not found.\033[m\n\n"
exit 1
fi
}

for command in readelf eu-readelf greadelf; do
if (command_exists ${command}); then
Expand Down
4 changes: 4 additions & 0 deletions src/footer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ while getopts "${optspec}" optchar; do
OPT=$((OPT + 1))
CHK_FUNCTION="chk_file_list"
;;
libcfile=*)
LIBC_FILE=${OPTARG#*=}
echo LIBC_FILE="${LIBC_FILE}"
;;
proc-all)
OPT=$((OPT + 1))
CHK_FUNCTION="chk_proc_all"
Expand Down
4 changes: 4 additions & 0 deletions src/functions/chk_fortify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ chk_fortify_file() {
exit 1
fi

search_libc

FS_chk_func_libc=()
FS_functions=()
while IFS='' read -r line; do FS_chk_func_libc+=("$line"); done < <(${readelf} -s "${FS_libc}" 2> /dev/null | grep _chk@@ | awk '{ print $8 }' | cut -c 3- | sed -e 's/_chk@.*//')
Expand Down Expand Up @@ -70,6 +72,8 @@ chk_fortify_proc() {
name=$(head -1 "${N}/status" | cut -b 7-)
echo_message "* Process name (PID) : ${name} (${N})\n" "" "" ""

search_libc

FS_chk_func_libc=()
FS_functions=()
while IFS='' read -r line; do FS_chk_func_libc+=("$line"); done < <(${readelf} -s "${FS_libc}" 2> /dev/null | grep _chk@@ | awk '{ print $8 }' | cut -c 3- | sed -e 's/_chk@.*//')
Expand Down
2 changes: 2 additions & 0 deletions src/functions/filecheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ filecheck() {
echo_message '\033[32mNo Symbols\t\033[m ' 'No Symbols,' ' symbols="no"' '"symbols":"no",'
fi

search_libc

FS_filechk_func_libc="$(${readelf} -s "${FS_libc}" 2> /dev/null | sed -ne 's/.*__\(.*_chk\)@@.*/\1/p')"
FS_func_libc="${FS_filechk_func_libc//_chk/}"
FS_func="$(${readelf} --dyn-syms "${1}" 2> /dev/null | awk '{ print $8 }' | sed -e 's/_*//' -e 's/@.*//' -e '/^$/d')"
Expand Down
1 change: 1 addition & 0 deletions src/functions/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ help() {
echo " ## Checksec Options"
echo " --file={file}"
echo " --dir={directory}"
echo " --libcfile={file or search path for libc}"
echo " --listfile={text file with one file per line}"
echo " --proc={process name}"
echo " --proc-all"
Expand Down

0 comments on commit 41e291b

Please sign in to comment.