PEASS-ng/linPEAS/builder/linpeas_parts/functions/execBin.sh
carlospolop 4c16f72ae2 fix
2025-05-17 16:09:36 +02:00

54 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Title: LinPeasBase - execBin
# ID: execBin
# Author: Carlos Polop
# Last Update: 22-08-2023
# Description: Write and execute an embedded binary
# License: GNU GPL
# Version: 1.0
# Functions Used: print_3title, print_info
# Global Variables: $Wfolder
# Initial Functions:
# Generated Global Variables: $TOOL_NAME, $TOOL_LINK, $B64_BIN, $PARAMS, $TMP_BIN, $cmdpid, $watcher, $rc
# Fat linpeas: 0
# Small linpeas: 1
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
}