From f0a0f8538fc2f293e2a51da4f5c0596ae9112ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20K=C3=BCderli?= Date: Sun, 16 Aug 2026 13:37:42 +0200 Subject: [PATCH] making this a repo for backup --- kill_unwanted.sh | 293 +++++++++++++++++++++++++++++++++++++++++++++++ victim.py | 43 +++++++ 2 files changed, 336 insertions(+) create mode 100755 kill_unwanted.sh create mode 100755 victim.py diff --git a/kill_unwanted.sh b/kill_unwanted.sh new file mode 100755 index 0000000..3bc9bd9 --- /dev/null +++ b/kill_unwanted.sh @@ -0,0 +1,293 @@ +#!/bin/bash + +# === SETTINGS === +EXLUDE_USR=(root jhub nginx) + +RESCAN_S=3 +MAX_CPU_P=50 +MAX_RAM_GB=32 +IMMUNITY_TIME_S=15 +HARDKILL_DELAY_S=10 + +NERDFONT=0 +COLOR=0 +KILL=0 +HARDKILL=0 +# === === === + +# ansi +UNDERLINE="\e[4m" +BOLD="\e[1m" +BLINK="\e[5m" +ORANGE="\e[38;5;208m" +RED="\033[31m" +GREEN="\033[32m" +RESET="\033[0m" + +# icons +ICON_RAM=" " +ICON_CPU=" " +ICON_MAG="󰍉" +ICON_SKULL=" " + +SIGTERM=15 +SIGKILL=9 +SCANNED_PIDS=() + +print_banner() { +cat <<"EOF" + _ _ _ _ _ _ +| | (_) | | | | | | +| | ___| | |_____ _ _ _ ____ ____ _ _ __ | |_ ___ __| | +| |/ / | | |_____| | | | '_ \ \ /\ / / _` | '_ \| __/ _ \/ _` | +| <| | | | | |_| | | | \ V V / (_| | | | | || __/ (_| | +|_|\_\_|_|_| \__,_|_| |_|\_/\_/ \__,_|_| |_|\__\___|\__,_| +EOF +} + +show_help() { +cat < /dev/zero; then + # still alive + echo -e "${ICON_SKULL}Terminating (${VICTIM_PID})" + kill "-${SIGKILL}" "${VICTIM_PID}" + else + echo "{ICON_SKULL}Died successfully ($VICTIM_PID)" + fi + ) & +} + +# === ARG PARSING === +print_banner +echo "" + +while [ "$#" -gt 0 ]; do + case "$1" in + -m|--memory) + if [[ -n $2 && "$2" != -* ]]; then + MAX_RAM_GB="${2}" + echo -e "- Maximum allowed RAM set to ${2}GiB" + shift + else + echo -e "- Maximum allowed RAM usage: ${MAX_RAM_GB}GiB" + fi + + ;; + + -t|--time) + if [[ -n $2 && "$2" != -* ]]; then + IMMUNITY_TIME_S="${2}" + echo -e "- Process immunity time set to ${2}s" + shift + else + echo -e "- Process immunity time: ${IMMUNITY_TIME_S}s" + fi + ;; + + -u|--utilization) + if [[ -n $2 && "$2" != -* ]]; then + MAX_CPU_P="${2}" + echo -e "- Maximum allowed CPU usage set to ${2}%" + shift + else + echo -e "- Maximum allowed CPU usage: ${MAX_CPU_P}%" + fi + ;; + + -r|--refresh) + if [[ -n $2 && "$2" != -* ]]; then + RESCAN_S="${2}" + shift + else + echo -e "- Rescan time set to: ${RESCAN_S}s" + fi + ;; + + -k|--kill) + KILL=1 + echo "- Killing enabled" + ;; + + -h|--hardkill) + KILL=1 + HARDKILL=1 + if [[ -n $2 && "$2" != -* ]]; then + HARDKILL_DELAY_S="${2}" + shift + fi + echo -e "- Hardkill enabled with delay: ${HARDKILL_DELAY_S}s" + ;; + + -n|--nerdfonts) + # enable nerdfont icons + NERDFONT=1 + ;; + + -c|--color) + # enable color + COLOR=1 + ;; + + --help) + show_help + exit 0 + ;; + + *) + echo "! ERROR: unknown command: ${1}" + exit 0 + esac + shift +done + + +# setting icons according to input +if [[ "$NERDFONT" == "0" ]]; then + ICON_RAM="" + ICON_CPU="" + ICON_MAG="" + ICON_SKULL="" +fi + +# setting ansi according to input +if [[ "$COLOR" == "0" ]]; then + UNDERLINE="" + BOLD="" + BLINK="" + ORANGE="" + RED="" + GREEN="" + RESET="" +fi + + +sleep 0.4 +echo -e "${GREEN}${UNDERLINE}\n${ICON_MAG} starting scan..${RESET}" + +# === SCAN === + +while true; do + #echo "SCANNED: ${SCANNED_PIDS[@]}" + while read -r PID TIME USER MEM CPU; do + # === Check if process should be excluded === + skip=0 + MEM_GB=$(awk "BEGIN {print $MEM/1024/1024}") # KiB -> GiB + + for user_i in "${EXLUDE_USR[@]}"; do + if [[ "$user_i" == "$USER" ]]; then + skip=1 + break + fi + done + + for element in "${SCANNED_PIDS[@]}"; do + if [[ "$PID" == "$element" ]]; then + skip=1 + break + fi + done + + if [[ "$TIME" -lt "$IMMUNITY_TIME_S" ]]; then + skip=1 + fi + + if [[ $skip -eq 1 ]]; then + continue + fi + + # === Check if ps should be killed === + + NAME="$(ps -p $PID -o args=)" + KILL_PREFIX="-> \"${RED}${NAME}${RESET}\" (PID: ${PID})" + should_die=0 + + # check memory usage + if awk "BEGIN {exit !(${MEM_GB} > ${MAX_RAM_GB})}"; then + # kill ps + echo -e "${KILL_PREFIX} - ${ICON_RAM}RAM usage too high (${ORANGE}${MEM_GB}GiB${RESET} > ${MAX_RAM_GB}GiB)" + should_die=1 + fi + + # check cpu usage + if awk "BEGIN {exit !(${CPU} > ${MAX_CPU_P})}"; then + # kill ps + echo -e "${KILL_PREFIX} - ${ICON_CPU}CPU usage too high (${ORANGE}${CPU}%${RESET} > ${MAX_CPU_P}%)" + should_die=1 + fi + + if [[ "$should_die" == 1 ]]; then + SCANNED_PIDS+=($PID) + kill_ps "$PID" + continue + fi + + # next ps + done < <(ps -eo pid,etimes,user,rss,%cpu --no-headers | tr -s " ") + + ## no process found - repeat + sleep ${RESCAN_S} +done diff --git a/victim.py b/victim.py new file mode 100755 index 0000000..297cc73 --- /dev/null +++ b/victim.py @@ -0,0 +1,43 @@ +#!/bin/python3 + +import sys +import time +import signal + +# Ignore SIGTERM +def handle_sigterm(signum, frame): + print("SIGTERM received but ignored") + +sig="NONE" +while not sig in ["y", "n"]: + sig = input("Signal? (y|n)").strip() +if sig=="y": + print("Capturing SIGTERM") + signal.signal(signal.SIGTERM, handle_sigterm) + +# 4 GiB in bytes +mem = input("enter allocated mem in GB\n> ") +TARGET_SIZE = int(mem) * 1024 * 1024 * 1024 + +CHUNK_SIZE = 100 * 1024 * 1024 # 100 MB chunks + +blocks = [] +total = 0 + +print("Allocating memory...") + +try: + while total < TARGET_SIZE: + blocks.append(bytearray(CHUNK_SIZE)) + total += CHUNK_SIZE + print(f"Allocated: {total / (1024**3):.2f} GB") + time.sleep(0.01) + + print("Done. Holding memory... Press Ctrl+C to exit.") + + while True: + time.sleep(1) + +except KeyboardInterrupt: + print("\nExiting, releasing memory...") + blocks.clear()