add timeout 120 when executing external binary

This commit is contained in:
carlospolop 2025-05-17 16:06:35 +02:00
parent c0b171a5c1
commit 85684b39ad

View File

@ -2,7 +2,7 @@
# ID: execBin
# Author: Carlos Polop
# Last Update: 22-08-2023
# Description: Write and exected an embedded binary
# Description: Write and execute an embedded binary
# License: GNU GPL
# Version: 1.0
# Functions Used: print_3title, print_info
@ -13,19 +13,42 @@
# Small linpeas: 1
execBin(){
TOOL_NAME=$1
TOOL_LINK=$2
B64_BIN=$3
PARAMS=$4
if [ "$B64_BIN" ]; then
echo ""
print_3title "Running $TOOL_NAME"
print_info "$TOOL_LINK"
echo "$B64_BIN" | base64 -d > $Wfolder/bin
chmod +x $Wfolder/bin
eval "$Wfolder/bin $PARAMS"
rm -f $Wfolder/bin
echo ""
execBin() {
TOOL_NAME=$1 # Display name
TOOL_LINK=$2 # Reference URL
B64_BIN=$3 # base64encoded executable
PARAMS=$4 # Arguments to the tool
[ -z "$B64_BIN" ] && return 0 # nothing to do
echo
print_3title "Running $TOOL_NAME"
print_info "$TOOL_LINK"
TMP_BIN=$(mktemp "${Wfolder:-/tmp}/bin.XXXXXX") || { echo "mktemp failed"; return 1; }
printf '%s' "$B64_BIN" | base64 -d > "$TMP_BIN" || { echo "decode failed"; rm -f "$TMP_BIN"; return 1; }
chmod +x "$TMP_BIN"
# ---------------- 120second wallclock timeout ----------------
if command -v timeout >/dev/null 2>&1; then # GNU/BSD timeout
timeout --preserve-status 120 "$TMP_BIN" $PARAMS
elif command -v gtimeout >/dev/null 2>&1; then # Homebrew coreutils (macOS)
gtimeout --preserve-status 120 "$TMP_BIN" $PARAMS
else # POSIX fallback
(
"$TMP_BIN" $PARAMS & # run in background
cmdpid=$!
( sleep 120 && kill -0 "$cmdpid" 2>/dev/null && kill -TERM "$cmdpid" ) &
watcher=$!
wait "$cmdpid"
rc=$?
kill -9 "$watcher" 2>/dev/null
exit $rc
)
fi
rc=$?
rm -f "$TMP_BIN"
echo
return $rc
}