| 1 |
#!/usr/bin/env bash |
| 2 |
# shellcheck disable=SC2154 |
| 3 |
# these top lines are moved during build |
| 4 |
|
| 5 |
# check file(s) |
| 6 |
filecheck() { |
| 7 |
# check for RELRO support |
| 8 |
if ${readelf} -l "${1}" 2> /dev/null | grep -q 'GNU_RELRO'; then |
| 9 |
if ${readelf} -d "${1}" 2> /dev/null | grep -q 'BIND_NOW'; then |
| 10 |
echo_message '\033[32mFull RELRO \033[m ' 'Full RELRO,' '<file relro="full"' " \"${1}\": { \"relro\":\"full\"," |
| 11 |
else |
| 12 |
echo_message '\033[33mPartial RELRO\033[m ' 'Partial RELRO,' '<file relro="partial"' " \"${1}\": { \"relro\":\"partial\"," |
| 13 |
fi |
| 14 |
else |
| 15 |
echo_message '\033[31mNo RELRO \033[m ' 'No RELRO,' '<file relro="no"' " \"${1}\": { \"relro\":\"no\"," |
| 16 |
fi |
| 17 |
|
| 18 |
# check for stack canary support |
| 19 |
if ${readelf} -s "${1}" 2> /dev/null | grep -Eq '__stack_chk_fail|__stack_chk_guard|__intel_security_cookie'; then |
| 20 |
echo_message '\033[32mCanary found \033[m ' 'Canary found,' ' canary="yes"' '"canary":"yes",' |
| 21 |
else |
| 22 |
echo_message '\033[31mNo canary found\033[m ' 'No Canary found,' ' canary="no"' '"canary":"no",' |
| 23 |
fi |
| 24 |
|
| 25 |
# check for NX support |
| 26 |
if ${readelf} -l "${1}" 2> /dev/null | grep -q 'GNU_STACK'; then |
| 27 |
if ${readelf} -l "${1}" 2> /dev/null | grep 'GNU_STACK' | grep -q 'RWE'; then |
| 28 |
echo_message '\033[31mNX disabled\033[m ' 'NX disabled,' ' nx="no"' '"nx":"no",' |
| 29 |
else |
| 30 |
echo_message '\033[32mNX enabled \033[m ' 'NX enabled,' ' nx="yes"' '"nx":"yes",' |
| 31 |
fi |
| 32 |
else |
| 33 |
echo_message '\033[31mNX disabled\033[m ' 'NX disabled,' ' nx="no"' '"nx":"no",' |
| 34 |
fi |
| 35 |
|
| 36 |
# check for PIE support |
| 37 |
if ${readelf} -h "${1}" 2> /dev/null | grep -q 'Type:[[:space:]]*EXEC'; then |
| 38 |
echo_message '\033[31mNo PIE \033[m ' 'No PIE,' ' pie="no"' '"pie":"no",' |
| 39 |
elif ${readelf} -h "${1}" 2> /dev/null | grep -q 'Type:[[:space:]]*DYN'; then |
| 40 |
if ${readelf} -d "${1}" 2> /dev/null | grep -q 'DEBUG'; then |
| 41 |
echo_message '\033[32mPIE enabled \033[m ' 'PIE enabled,' ' pie="yes"' '"pie":"yes",' |
| 42 |
else |
| 43 |
echo_message '\033[33mDSO \033[m ' 'DSO,' ' pie="dso"' '"pie":"dso",' |
| 44 |
fi |
| 45 |
elif ${readelf} -h "${1}" 2> /dev/null | grep -q 'Type:[[:space:]]*REL'; then |
| 46 |
echo_message '\033[33mREL \033[m ' 'REL,' ' pie="rel"' '"pie":"rel",' |
| 47 |
else |
| 48 |
echo_message '\033[33mNot an ELF file\033[m ' 'Not an ELF file,' ' pie="not_elf"' '"pie":"not_elf",' |
| 49 |
fi |
| 50 |
|
| 51 |
if ${extended_checks}; then |
| 52 |
# check for selfrando support |
| 53 |
if ${readelf} -S "${1}" 2> /dev/null | grep -c txtrp | grep -q '1'; then |
| 54 |
echo_message '\033[32mSelfrando enabled \033[m ' |
| 55 |
else |
| 56 |
echo_message '\033[31mNo Selfrando \033[m ' |
| 57 |
fi |
| 58 |
fi |
| 59 |
|
| 60 |
if ${extended_checks}; then |
| 61 |
# check if compiled with Clang CFI |
| 62 |
#if $readelf -s "$1" 2>/dev/null | grep -Eq '\.cfi'; then |
| 63 |
read -r cfifunc <<< "$($readelf -s "${1}" 2> /dev/null | grep .cfi | awk '{ print $8 }')" |
| 64 |
func=${cfifunc/.cfi/} |
| 65 |
if [ -n "$cfifunc" ] && $readelf -s "$1" 2> /dev/null | grep -q "$func$"; then |
| 66 |
echo_message '\033[32mClang CFI found \033[m ' 'with CFI,' ' clangcfi="yes"' '"clangcfi":"yes",' |
| 67 |
else |
| 68 |
echo_message '\033[31mNo Clang CFI found\033[m ' 'without CFI,' ' clangcfi="no"' '"clangcfi":"no",' |
| 69 |
fi |
| 70 |
|
| 71 |
# check if compiled with Clang SafeStack |
| 72 |
if $readelf -s "$1" 2> /dev/null | grep -Eq '__safestack_init'; then |
| 73 |
echo_message '\033[32mSafeStack found \033[m ' 'with SafeStack,' ' safestack="yes"' '"safestack":"yes",' |
| 74 |
else |
| 75 |
echo_message '\033[31mNo SafeStack found\033[m ' 'without SafeStack,' ' safestack="no"' '"safestack":"no",' |
| 76 |
fi |
| 77 |
fi |
| 78 |
|
| 79 |
# check for rpath / run path |
| 80 |
# search for a line that matches RPATH and extract the colon-separated path list within brackets |
| 81 |
# example input: "0x000000000000000f (RPATH) Library rpath: [/lib/systemd:/lib/apparmor]" |
| 82 |
IFS=: read -r -a rpath_array <<< "$(${readelf} -d "${1}" 2> /dev/null | awk -F'[][]' '/RPATH/ {print $2}')" |
| 83 |
if [[ "${#rpath_array[@]}" -gt 0 ]]; then |
| 84 |
if xargs stat -c %A <<< "${rpath_array[*]}" 2> /dev/null | grep -q 'rw'; then |
| 85 |
echo_message '\033[31mRW-RPATH \033[m ' 'RPATH,' ' rpath="yes"' '"rpath":"yes",' |
| 86 |
else |
| 87 |
echo_message '\033[31mRPATH \033[m ' 'RPATH,' ' rpath="yes"' '"rpath":"yes",' |
| 88 |
fi |
| 89 |
else |
| 90 |
echo_message '\033[32mNo RPATH \033[m ' 'No RPATH,' ' rpath="no"' '"rpath":"no",' |
| 91 |
fi |
| 92 |
|
| 93 |
# search for a line that matches RUNPATH and extract the colon-separated path list within brackets |
| 94 |
IFS=: read -r -a runpath_array <<< "$(${readelf} -d "${1}" 2> /dev/null | awk -F'[][]' '/RUNPATH/ {print $2}')" |
| 95 |
if [[ "${#runpath_array[@]}" -gt 0 ]]; then |
| 96 |
if xargs stat -c %A <<< "${runpath_array[*]}" 2> /dev/null | grep -q 'rw'; then |
| 97 |
echo_message '\033[31mRW-RUNPATH \033[m ' 'RUNPATH,' ' runpath="yes"' '"runpath":"yes",' |
| 98 |
else |
| 99 |
echo_message '\033[31mRUNPATH \033[m ' 'RUNPATH,' ' runpath="yes"' '"runpath":"yes",' |
| 100 |
fi |
| 101 |
else |
| 102 |
echo_message '\033[32mNo RUNPATH \033[m ' 'No RUNPATH,' ' runpath="no"' '"runpath":"no",' |
| 103 |
fi |
| 104 |
|
| 105 |
# check for stripped symbols in the binary |
| 106 |
IFS=" " read -r -a SYM_cnt <<< "$(${readelf} --symbols "${1}" 2> /dev/null | grep '\.symtab' | cut -d' ' -f5 | cut -d: -f1)" |
| 107 |
if ${readelf} --symbols "${1}" 2> /dev/null | grep -q '\.symtab'; then |
| 108 |
echo_message "\033[31m${SYM_cnt[0]} Symbols\t\033[m " 'Symbols,' ' symbols="yes"' '"symbols":"yes",' |
| 109 |
else |
| 110 |
echo_message '\033[32mNo Symbols\t\033[m ' 'No Symbols,' ' symbols="no"' '"symbols":"no",' |
| 111 |
fi |
| 112 |
|
| 113 |
# check for FORTIFY SOURCE |
| 114 |
if [[ -e /lib/libc.so.6 ]]; then |
| 115 |
FS_libc=/lib/libc.so.6 |
| 116 |
elif [[ -e /lib/libc.so.7 ]]; then |
| 117 |
FS_libc=/lib/libc.so.7 |
| 118 |
elif [[ -e /lib/libc.so ]]; then |
| 119 |
FS_libc=/lib/libc.so |
| 120 |
elif [[ -e /lib64/libc.so.6 ]]; then |
| 121 |
FS_libc=/lib64/libc.so.6 |
| 122 |
elif [[ -e /lib/i386-linux-gnu/libc.so.6 ]]; then |
| 123 |
FS_libc=/lib/i386-linux-gnu/libc.so.6 |
| 124 |
elif [[ -e /lib/x86_64-linux-gnu/libc.so.6 ]]; then |
| 125 |
FS_libc=/lib/x86_64-linux-gnu/libc.so.6 |
| 126 |
elif [[ -e /lib/arm-linux-gnueabihf/libc.so.6 ]]; then |
| 127 |
FS_libc=/lib/arm-linux-gnueabihf/libc.so.6 |
| 128 |
elif [[ -e /lib/aarch64-linux-gnu/libc.so.6 ]]; then |
| 129 |
FS_libc=/lib/aarch64-linux-gnu/libc.so.6 |
| 130 |
elif [[ -e /usr/x86_64-gentoo-linux-musl/bin/ld ]]; then |
| 131 |
FS_libc=/usr/x86_64-gentoo-linux-musl/bin/ld |
| 132 |
elif [[ -e /usr/lib/loongarch64-linux-gnu/libc.so.6 ]]; then |
| 133 |
FS_libc=/usr/lib/loongarch64-linux-gnu/libc.so.6 |
| 134 |
else |
| 135 |
printf "\033[31mError: libc not found.\033[m\n\n" |
| 136 |
exit 1 |
| 137 |
fi |
| 138 |
|
| 139 |
FS_filechk_func_libc="$(${readelf} -s $FS_libc 2> /dev/null | sed -ne 's/.*__\(.*_chk\)@@.*/\1/p')" |
| 140 |
FS_func_libc="${FS_filechk_func_libc//_chk/}" |
| 141 |
FS_func="$(${readelf} --dyn-syms "${1}" 2> /dev/null | awk '{ print $8 }' | sed -e 's/_*//' -e 's/@.*//' -e '/^$/d')" |
| 142 |
FS_cnt_checked=$(grep -cFxf <(sort <<< "${FS_filechk_func_libc}") <(sort <<< "${FS_func}")) |
| 143 |
FS_cnt_unchecked=$(grep -cFxf <(sort <<< "${FS_func_libc}") <(sort <<< "${FS_func}")) |
| 144 |
FS_cnt_total=$((FS_cnt_unchecked + FS_cnt_checked)) |
| 145 |
|
| 146 |
if grep -q '_chk$' <<< "$FS_func"; then |
| 147 |
echo_message '\033[32mYes\033[m' 'Yes,' ' fortify_source="yes" ' '"fortify_source":"yes",' |
| 148 |
else |
| 149 |
echo_message "\033[31mNo\033[m" "No," ' fortify_source="no" ' '"fortify_source":"no",' |
| 150 |
fi |
| 151 |
echo_message "\t${FS_cnt_checked}\t" "${FS_cnt_checked}", "fortified=\"${FS_cnt_checked}\" " "\"fortified\":\"${FS_cnt_checked}\"," |
| 152 |
echo_message "\t${FS_cnt_total}\t\t" "${FS_cnt_total}" "fortify-able=\"${FS_cnt_total}\"" "\"fortify-able\":\"${FS_cnt_total}\"" |
| 153 |
} |