Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S12 - checksec implementation fix #463

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions helpers/helpers_emba_print.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@
# Description: All functions for colorizing terminal output and handling logging

## Color definition
RED="\033[0;31m"
GREEN="\033[0;32m"
ORANGE="\033[0;33m"
BLUE="\033[0;34m"
MAGENTA="\033[0;35m"
CYAN="\033[0;36m"
NC="\033[0m" # no color
export RED="\033[0;31m"
export GREEN="\033[0;32m"
export ORANGE="\033[0;33m"
export BLUE="\033[0;34m"
export MAGENTA="\033[0;35m"
export CYAN="\033[0;36m"
export NC="\033[0m" # no color

export RED_="\x1b[31m"
export GREEN_="\x1b[32m"
export ORANGE_="\x1b[33m"
export BLUE_="\x1b[34m"
export MAGENTA_="\x1b[35m"
export CYAN_="\x1b[36m"
export NC_="\x1b[0m"

## Attribute definition
BOLD="\033[1m"
ITALIC="\033[3m"
export BOLD="\033[1m"
export ITALIC="\033[3m"

MODULE_NUMBER="--"
SUB_MODULE_COUNT=0
Expand Down
6 changes: 4 additions & 2 deletions modules/F50_base_aggregator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ output_binaries() {
PIE=$(grep -c "No PIE" "$LOG_DIR"/"$S12_LOG" || true)
STRIPPED=$(grep -c "No Symbols" "$LOG_DIR"/"$S12_LOG" || true)
BINS_CHECKED=$(grep -c "RELRO.*NX.*RPATH" "$LOG_DIR"/"$S12_LOG" || true)
# we have to remove the first line of the original output:
(( BINS_CHECKED-- ))
if [[ "$BINS_CHECKED" -gt 0 ]]; then
# we have to remove the first line of the original output:
(( BINS_CHECKED-- ))
fi
fi

if [[ "${CANARY:-0}" -gt 0 ]]; then
Expand Down
2 changes: 1 addition & 1 deletion modules/S120_cwe_checker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cwe_check() {
# do not try to analyze kernel modules:
[[ "$BINARY" == *".ko" ]] && continue
if [[ "$THREADED" -eq 1 ]]; then
local MAX_MOD_THREADS=$(("$(grep -c ^processor /proc/cpuinfo || true)" / 2))
local MAX_MOD_THREADS=$(("$(grep -c ^processor /proc/cpuinfo || true)" / 3))
if [[ $(grep -i -c S09_ "$LOG_DIR"/"$MAIN_LOG_FILE" || true) -eq 1 ]]; then
local MAX_MOD_THREADS=1
fi
Expand Down
73 changes: 68 additions & 5 deletions modules/S12_binary_protection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,80 @@ S12_binary_protection()
local BINARY=""

if [[ -f "$EXT_DIR"/checksec ]] ; then
print_output "RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE"
echo "RELRO;STACK CANARY;NX;PIE;RPATH;RUNPATH;Symbols;FORTIFY Fortified;Fortifiable;FILE" > "$CSV_LOG"
echo "RELRO;STACK CANARY;NX;PIE;RPATH;RUNPATH;Symbols;FORTIFY;FILE" >> "$CSV_LOG"
printf "\t%-13.13s %-16.16s %-11.11s %-8.8s %-11.11s %-11.11s %-11.11s %-5.5s %s\n" \
"RELRO" "CANARY" "NX" "PIE" "RPATH" "RUNPATH" "SYMBOLS" "FORTIFY" "FILE" | tee -a "$TMP_DIR"/s12.tmp

for BINARY in "${BINARIES[@]}" ; do
if ( file "$BINARY" | grep -q ELF ) ; then
print_output "$( "$EXT_DIR"/checksec --file="$BINARY" | grep -v "CANARY" | rev | cut -f 2- | rev )""\\t""$NC""$(print_path "$BINARY")"
"$EXT_DIR"/checksec --format=csv --file="$BINARY" >> "$CSV_LOG"
CSV_BIN_OUT=$("$EXT_DIR"/checksec --format=csv --file="$BINARY")

# we usually use ; instead of , for csv ...
CSV_BIN_OUT=${CSV_BIN_OUT//,/\;}
echo "$CSV_BIN_OUT" >> "$CSV_LOG"

# coloring the output from csv
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ RELRO)/${RED_}&$NC_/g")
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(Partial\ RELRO)/${ORANGE_}&$NC_/g")
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(Full\ RELRO)/${GREEN_}&$NC_/g")

CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(DSO)/${ORANGE_}&$NC_/g")

CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(NX\ enabled)/${GREEN_}&$NC_/g")
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(NX\ disabled)/${RED_}&$NC_/g")

if [[ "$CSV_BIN_OUT" == *"No PIE"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ PIE)/${RED_}&$NC_/g")
elif [[ "$CSV_BIN_OUT" == *"PIE enabled"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(PIE\ enabled)/${GREEN_}&$NC_/g")
elif [[ "$(echo "$CSV_BIN_OUT" | cut -d\; -f4)" == "REL" ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | awk -F ';' -v repl="${ORANGE_}REL$NC_" '$4 == "REL"{OFS = ";"; $4=repl}1')
fi

if [[ "$CSV_BIN_OUT" == *"No Canary found"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ Canary\ found)/${RED_}&$NC_/g")
else
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(Canary\ found)/${GREEN_}&$NC_/g")
fi

if [[ "$CSV_BIN_OUT" == *"No RPATH"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ RPATH)/${GREEN_}&$NC_/g")
else
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(RPATH)/${RED_}&$NC_/g")
fi

if [[ "$CSV_BIN_OUT" == *"No RUNPATH"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ RUNPATH)/${GREEN_}&$NC_/g")
else
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(RUNPATH)/${RED_}&$NC_/g")
fi

if [[ "$CSV_BIN_OUT" == *"No Symbols"* ]]; then
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(No\ Symbols)/${GREEN_}&$NC_/g")
else
CSV_BIN_OUT=$(echo "$CSV_BIN_OUT" | sed -r "s/(Symbols)/${RED_}&$NC_/g")
fi

RELRO=$(echo "$CSV_BIN_OUT" | cut -d\; -f1)
CANARY=$(echo "$CSV_BIN_OUT" | cut -d\; -f2)
NX=$(echo "$CSV_BIN_OUT" | cut -d\; -f3)
PIE=$(echo "$CSV_BIN_OUT" | cut -d\; -f4)
RPATH=$(echo "$CSV_BIN_OUT" | cut -d\; -f5)
RUNPATH=$(echo "$CSV_BIN_OUT" | cut -d\; -f6)
SYMBOLS=$(echo "$CSV_BIN_OUT" | cut -d\; -f7)
FORTIFY=$(echo "$CSV_BIN_OUT" | cut -d\; -f8)
FILE=$(echo "$CSV_BIN_OUT" | cut -d\; -f11)
FILE=$(print_path "$FILE")

printf "\t%-22.22s %-25.25s %-20.20s %-20.20s %-20.20s %-20.20s %-20.20s %-5.5s %s\n" \
"$RELRO" "$CANARY" "$NX" "$PIE" "$RPATH" "$RUNPATH" "$SYMBOLS" "$FORTIFY" "$FILE" | tee -a "$TMP_DIR"/s12.tmp
BIN_PROT_COUNTER=$((BIN_PROT_COUNTER+1))
fi
done

[[ -f "$TMP_DIR"/s12.tmp ]] && cat "$TMP_DIR"/s12.tmp >> "$LOG_FILE"
else
print_output "[-] Binary protection analyzer $EXT_DIR/checksec not found - check your installation."
print_output "[-] Binary protection analyzer $ORANGE$EXT_DIR/checksec$NC not found - check your installation."
fi

module_end_log "${FUNCNAME[0]}" "$BIN_PROT_COUNTER"
Expand Down