#!/bin/bash # # Black Magic - the script # by: Aaron Conole # # Released under the 3-clause BSD License, which I won't include here verbatim. # Essentially, give me credit and don't you forget it. PATH=/usr/bin:/bin GDB=`which gdb` WHO=`id -un` PS=`which ps` GREP=`which grep` HEAD=`which head` AWK=`which awk` READLINK=`which readlink` MKTEMP=`which mktemp` RM=`which rm` BINARY="" print_help() { echo "Black Magic shell script. WARNING: this does dangerous and completely" echo "-INSANE- things to running processes. Don't blame me if you create" echo "extreme havok on your systems." echo "" echo "Options:" echo "" echo "Set one or the other (or both)" echo "--pid " echo "--binary " echo "" echo "Specify the posix function to run" echo "--function " echo "" echo "Arguments to function (provide in-order)" echo "--string \"\"" echo "--literal " echo "" echo "String's are quoted. Literals are not." } if [ "$1" = "-h" -o "$1" = "--help" ]; then print_help exit 0 fi PROCESS_PID=0 PROCESS_BINARY="" POSIX_FUNCTION="" POSIX_FUNCTION_ARGS="" while [ $# -gt 0 ]; do case "$1" in --pid) shift echo "Setting PID: $1" PROCESS_PID=$1 ;; --binary) shift echo "Setting binary: $1" PROCESS_BINARY=$1 ;; --function) shift echo "Will call: $1" POSIX_FUNCTION=$1 ;; --string) shift if [ "$POSIX_FUNCTION_ARGS" != "" ]; then POSIX_FUNCTION_ARGS="$POSIX_FUNCTION_ARGS, \"$1\"" else POSIX_FUNCTION_ARGS="\"$1\"" fi ;; --literal) shift if [ "$POSIX_FUNCTION_ARGS" != "" ]; then POSIX_FUNCTION_ARGS="$POSIX_FUNCTION_ARGS, $1" else POSIX_FUNCTION_ARGS="$1" fi ;; *) print_help echo "Error: Unknown option \"$1\"" exit 1 ;; esac shift done if [ $PROCESS_PID -eq 0 -a "$PROCESS_BINARY" == "" ]; then print_help echo "ERROR: Must set at LEAST process PID or BINARY" exit 1 fi if [ "$POSIX_FUNCTION" == "" ]; then print_help echo "ERROR: Must set at LEAST a posix function" exit 1 fi if [ $PROCESS_PID -eq 0 ]; then PROCESS_PID=`$PS aux | $GREP ${WHO} | $GREP ${PROCESS_BINARY} | $HEAD -n 1 | $CUT -d" " -f2` if [ "$PROCESS_PID" == "" ]; then PROCESS_BIN_NAME=`$BASENAME ${PROCESS_BINARY}` PROCESS_PID=`$PS aux | $GREP ${WHO} | $GREP ${PROCESS_BIN_NAME} | $HEAD -n 1 | $AWK '{print $2}'` fi if [ "$PROCESS_PID" == "" ]; then print_help echo "ERROR: unknown binary. best specify both on the command line." exit 1 fi fi if [ "$PROCESS_BINARY" == "" ]; then PROCESS_BINARY=`$READLINK -f /proc/${PROCESS_PID}/exe` if [ "$PROCESS_BINARY" == "" ]; then print_help echo "ERROR: couldn't get process name from ID. Best specify both." exit 1 fi fi POSIX_CALL="$POSIX_FUNCTION($POSIX_FUNCTION_ARGS)" TMPFILE=`$MKTEMP` echo "Black magic about to happen" echo "---------------------------" echo "" echo "Modify running image of: $PROCESS_BINARY" echo "Image PID: $PROCESS_PID" echo "Function call looks like: ${POSIX_CALL}" echo "Temporary file: $TMPFILE" echo "" echo "Pray!" cat > $TMPFILE <