fun bug fix

This commit is contained in:
2026-10-13 13:05:44 +02:00
parent df5dca96c6
commit 9723a6ad93
3 changed files with 155 additions and 44 deletions
+144 -41
View File
@@ -37,11 +37,84 @@
load_conf "fun"
# ------------------------------------------------------------------------------
# Make non-IT peoples think you're busy doing something hard
# Shared helpers of the demo functions below (busy, fake_compile, hack).
# They are exported because the public functions are.
# True when $1 is a non-negative integer. Every value that ends up in an
# arithmetic context must pass this first (this includes values coming from the
# configuration file): bash evaluates non-numeric strings inside [[ -gt ]] or
# $(( )) as expressions, which can run arbitrary commands.
# Usage: _fun_is_uint <value>
_fun_is_uint()
{
[[ "$1" =~ ^[0-9]+$ ]]
}
export -f _fun_is_uint
# Convert a number of milliseconds into a seconds string usable by sleep
# (1500 -> 1.500), in pure bash (no awk fork per call).
# Usage: _fun_ms_to_s <ms>
_fun_ms_to_s()
{
printf '%d.%03d' $(( $1 / 1000 )) $(( $1 % 1000 ))
}
export -f _fun_ms_to_s
# Fail with a message unless a terminal is available: the demos wait for a key
# read from /dev/tty to stop.
# Usage: _fun_need_tty <command-name>
_fun_need_tty()
{
if ! { : </dev/tty; } 2>/dev/null; then
disp E "$1: a terminal is required (it runs until a key is pressed)."
return 1
fi
}
export -f _fun_need_tty
# Wait for a key press, then stop the background job $1 and its children.
# The job runs as "( ... ) &"; with job control (interactive shell) it is the
# leader of its own process group, which is signalled as a whole. Without job
# control that group does not exist, so its direct children (the pipeline) are
# killed first, then the subshell. SIGINT is trapped meanwhile so that Ctrl-C
# stops the job like a key press instead of aborting the caller and leaving it
# running. The wait is a polling loop of short timed reads: a blocking read is
# restarted by bash after a trapped signal and would never notice the Ctrl-C.
# Usage: _fun_stop_on_key <pid>
_fun_stop_on_key()
{
local pid=$1 old_int interrupted=0
old_int=$(trap -p INT)
trap 'interrupted=1' INT
while (( ! interrupted )); do
IFS= read -r -n 1 -s -t 0.3 _ </dev/tty && break
# Status above 128 is just the timeout; anything else (such as end of
# input on the terminal) ends the wait.
(( $? > 128 )) || break
done
kill -- -"$pid" 2>/dev/null || {
pkill -P "$pid" 2>/dev/null
kill "$pid" 2>/dev/null
}
wait "$pid" 2>/dev/null
if [[ -n "$old_int" ]]; then
eval "$old_int"
else
trap - INT
fi
return 0
}
export -f _fun_stop_on_key
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make non-IT peoples think you're busy doing something hard: print the lines of
# a hexdump of random data that contain a pattern, until a key is pressed.
# Usage: busy [options] [pattern]
# Options:
# --delay=<ms> : add a delay between each line output (milliseconds)
# pattern : the string to search for in the hexdump output (default is "ca fe")
# -p, --pattern <hex> : the string to search for in the hexdump output
# (default is "ca fe"); may also be given as argument
# -d, --delay <ms> : add a delay between each line output (milliseconds)
busy()
{
local pattern="${BUSY_DEFAULT_PATTERN:-ca fe}" delay_ms="${BUSY_DEFAULT_DELAY:-0}"
@@ -55,9 +128,8 @@ busy()
disp E "Invalid options, use \"busy --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -67,7 +139,8 @@ busy()
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
printf "\t-p, --pattern PATTERN\tHex pattern to search (default: \"ca fe\")\n"
printf "\t-d, --delay MS\t\tDelay between matches in milliseconds\n"
printf "\t-d, --delay MS\t\tDelay between matches in milliseconds\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-p|--pattern)
@@ -76,10 +149,6 @@ busy()
;;
-d|--delay)
delay_ms="$2"
if ! [[ "$delay_ms" =~ ^[0-9]+$ ]]; then
disp E "Invalid delay: must be an integer (milliseconds)."
return 1
fi
shift 2
;;
--)
@@ -93,29 +162,34 @@ busy()
esac
done
# Convert milliseconds to seconds for 'sleep'
# A pattern given as argument (words joined by spaces) overrides -p
[[ $# -gt 0 ]] && pattern="$*"
# Checked here so that the configured default is validated as well as -d
if ! _fun_is_uint "$delay_ms"; then
disp E "Invalid delay '${delay_ms}': must be an integer (milliseconds)."
return 1
fi
local delay_s
delay_s=$(awk "BEGIN{
printf \"%.3f\", $delay_ms / 1000 }")
delay_s=$(_fun_ms_to_s "$delay_ms")
command -v hexdump >/dev/null 2>&1 || {
disp E "busy: 'hexdump' is required but not installed (util-linux or bsdmainutils)."
return 1
}
_fun_need_tty busy || return 1
# Monitor /dev/urandom
# Monitor /dev/urandom. grep -e protects a pattern starting with "-";
# IFS= read keeps the line's leading blanks.
(
hexdump -C < /dev/urandom | grep -iF --line-buffered "$pattern" | \
while read -r line; do
echo "$line"
hexdump -C < /dev/urandom | grep -iF --line-buffered -e "$pattern" | \
while IFS= read -r line; do
printf '%s\n' "$line"
[[ $delay_ms -gt 0 ]] && sleep "$delay_s"
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f busy
# ------------------------------------------------------------------------------
@@ -144,9 +218,8 @@ fake_compile()
disp E "Invalid options, use \"fake_compile --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -158,7 +231,8 @@ fake_compile()
printf "\t-n, --min-delay MS\t\tMin delay between lines in milliseconds (default: 40)\n"
printf "\t-x, --max-delay MS\t\tMax delay between lines in milliseconds (default: 150)\n"
printf "\t-l, --lang LANG\t\t\tLanguage preset: c, cpp, java, python, random (default: c)\n"
printf "\t-e, --errors\t\t\tInject fake compilation errors at the end\n"
printf "\t-e, --errors\t\t\tInject fake compilation errors at the end\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-n|--min-delay)
@@ -203,15 +277,35 @@ fake_compile()
esac
done
# Validated here (not only in the options) so that the values coming from
# the configuration file are checked too.
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
disp E "min-delay and max-delay must be integers (milliseconds)."
return 1
fi
case "$lang" in
c|cpp|java|python|random) ;;
*)
disp E "Invalid lang '${lang}': must be one of: c, cpp, java, python, random."
return 1
;;
esac
if [[ $# -gt 0 ]]; then
disp E "Unexpected argument: $1. Use \"fake_compile --help\" to display usage."
return 1
fi
if [[ $min_ms -gt $max_ms ]]; then
disp E "min-delay ($min_ms) must be <= max-delay ($max_ms)."
return 1
fi
_fun_need_tty fake_compile || return 1
(
# Sleep for a random duration between min_ms and max_ms (RANDOM only
# spans 0..32767, which bounds the usable range).
rand_sleep() {
local r=$(( min_ms + RANDOM % (max_ms - min_ms + 1) ))
sleep "$(awk "BEGIN{ printf \"%.3f\", $r / 1000 }")"
sleep "$(_fun_ms_to_s "$r")"
}
c_files=( "main" "utils" "parser" "lexer" "codegen" "optimizer"
@@ -298,10 +392,7 @@ fake_compile()
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f fake_compile
# ------------------------------------------------------------------------------
@@ -329,9 +420,8 @@ hack()
disp E "Invalid options, use \"hack --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -342,7 +432,8 @@ hack()
printf "\t-h, --help\t\t\tDisplay this help screen\n"
printf "\t-t, --target IP\t\t\tTarget IP address (default: random)\n"
printf "\t-n, --min-delay MS\t\tMin delay between output lines in milliseconds (default: 60)\n"
printf "\t-x, --max-delay MS\t\tMax delay between output lines in milliseconds (default: 250)\n"
printf "\t-x, --max-delay MS\t\tMax delay between output lines in milliseconds (default: 250)\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-t|--target)
@@ -376,15 +467,26 @@ hack()
esac
done
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
disp E "min-delay and max-delay must be integers (milliseconds)."
return 1
fi
if [[ $# -gt 0 ]]; then
disp E "Unexpected argument: $1. Use \"hack --help\" to display usage."
return 1
fi
if [[ $min_ms -gt $max_ms ]]; then
disp E "min-delay ($min_ms) must be <= max-delay ($max_ms)."
return 1
fi
_fun_need_tty hack || return 1
(
# Sleep for a random duration between min_ms and max_ms (RANDOM only
# spans 0..32767, which bounds the usable range).
rand_sleep() {
local r=$(( min_ms + RANDOM % (max_ms - min_ms + 1) ))
sleep "$(awk "BEGIN{ printf \"%.3f\", $r / 1000 }")"
sleep "$(_fun_ms_to_s "$r")"
}
rand_ip() { printf '%d.%d.%d.%d\n' \
$(( RANDOM%223+1 )) $(( RANDOM%255 )) \
@@ -405,7 +507,11 @@ hack()
fixed_target="$target"
while true; do
[[ -z "$fixed_target" ]] && target="$(rand_ip)" || target="$fixed_target"
if [[ -z "$fixed_target" ]]; then
target="$(rand_ip)"
else
target="$fixed_target"
fi
printf "[*] Initializing attack sequence against %s\n" "$target"
rand_sleep
@@ -484,10 +590,7 @@ hack()
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f hack
# ------------------------------------------------------------------------------