cowrie/bin/cowrie

205 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
#Change the below to yes if you are using authbind to listen to port 22
AUTHBIND_ENABLED=no
# To override the default virtual enviroment, either set COWRIE_VIRTUAL_ENV or
# activate it before starting Cowrie
#COWRIE_VIRTUAL_ENV=my-env
#Change the below to -n to disable daemonizing (for instance when using supervisor)
DAEMONIZE=""
################################################################################
## don't modify below here ##
################################################################################
# The default Python virtual environment is "cowrie-env". If you set the variable
# COWRIE_VIRTUAL_ENV you can override this
DEFAULT_VIRTUAL_ENV=cowrie-env
first_time_use() {
echo
echo "Join the Cowrie community at: http://bit.ly/cowrieslack"
echo
}
find_cowrie_directory() {
# Determine Cowrie directory
if [[ "$0" = /* ]]
then
COWRIEDIR=$(dirname $0)/..
else
COWRIEDIR=$(dirname $PWD/$0)/..
fi
COWRIEDIR=$(cd ${COWRIEDIR} && pwd -P 2>/dev/null || pwd)
}
activate_venv() {
# Activate Python virtual environment
VENV="$1"
if [ ! -f "$VENV/bin/activate" ]
then
return 1
fi
. $VENV/bin/activate
return 0
}
cowrie_status() {
# Print status
PID=$(cat ${PIDFILE} 2>/dev/null || echo "")
if [ -n "$PID" ]; then
if ps -p "$PID" 2>&1 >/dev/null; then
echo "cowrie is running (PID: ${PID})."
else
echo "cowrie is not running (PID: ${PID})."
echo "Removing stale PID file"
rm -f ${PIDFILE}
fi
else
echo "cowrie is not running."
fi
}
cowrie_start() {
# Start Cowrie
COWRIEARGS="$*"
TWISTEDARGS="${DAEMONIZE} ${XARGS} --umask 0022 --pidfile ${PIDFILE}"
# For Docker log to stdout, for non-Docker log to file
if [ "$DOCKER" = "yes" ]; then
TWISTEDARGS="${TWISTEDARGS} -l -"
else
TWISTEDARGS="${TWISTEDARGS} --logger cowrie.python.logfile.logger"
fi
# 1. Check if any virtual environment is active
# 2. Try COWRIE_VIRTUAL_ENV if defined
# 3. Try DEFAULT_VIRTUAL_ENV
# 4. Try ../DEFAULT_VIRTUAL_ENV
# 5. Try without virtual environment
if [ ! -z "$VIRTUAL_ENV" ]; then
echo 2>&1 "Using activated Python virtual environment \"$VIRTUAL_ENV\""
elif activate_venv "$COWRIE_VIRTUAL_ENV"; then
echo 2>&1 "Using custom Python virtual environment \"$VIRTUAL_ENV\""
elif activate_venv "$DEFAULT_VIRTUAL_ENV"; then
echo 2>&1 "Using default Python virtual environment \"$VIRTUAL_ENV\""
# Look one directory higher for the virtual env to not pollute the Cowrie dir
elif activate_venv "../$DEFAULT_VIRTUAL_ENV"; then
echo 2>&1 "Using default Python virtual environment \"$VIRTUAL_ENV\""
else
echo 2>&1 "Not using Python virtual environment"
fi
echo "Starting cowrie: [twistd ${TWISTEDARGS} cowrie ${COWRIEARGS}]..."
if [ "$AUTHBIND_ENABLED" = "no" ]
then
twistd ${TWISTEDARGS} ${COWRIEARGS} cowrie
else
authbind --deep twistd ${TWISTEDARGS} ${COWRIEARGS} cowrie
fi
}
cowrie_stop () {
# Stop Cowrie
PID=$(cat ${PIDFILE} 2>/dev/null || echo "")
if [ -n "$PID" ]; then
echo "Stopping cowrie..."
if kill -TERM $PID; then
echo -n
else
echo "Removing stale PID file"
rm -f ${PIDFILE}
fi
else
echo "cowrie is not running."
fi
}
cowrie_force_stop () {
# Force Stop Cowrie
PID=$(cat ${PIDFILE} 2>/dev/null || echo -n "")
if [ -n "$PID" ]; then
echo -n "Stopping cowrie..."
if kill -TERM $PID; then
((t = 60))
while ((t > 1)); do
sleep 1
echo -n .
if kill -0 $PID 2>/dev/null; then
((t -= 1))
else
echo "terminated."
return
fi
done
kill -KILL $PID
echo "killed."
else
echo "Removing stale PID file"
rm -f ${PIDFILE}
fi
else
echo "cowrie is not running."
fi
}
cowrie_usage() {
echo "usage: $0 <start|stop|force-stop|restart|status>"
}
################################################################################
## Main script
################################################################################
if [ "$#" = 0 ]
then
cowrie_usage
exit 1
fi
find_cowrie_directory $0
cd ${COWRIEDIR}
export PYTHONPATH=${PYTHONPATH}:${COWRIEDIR}/src
# This is for the move to src/ on 2018-07-21
# It deletes old compiled python code
# Can be removed at some time in the future
for dir in ${COWRIEDIR}/twisted ${COWRIEDIR}/cowrie; do
[ -d ${dir} ] && find ${dir} -name '*pyc' | xargs rm -f
done
PIDFILE=var/run/cowrie.pid
set -e
if [ ! -f ${COWRIEDIR}/log/cowrie.log ]
then
first_time_use
fi
key=$1
shift 1
case $key in
stop)
cowrie_stop $*
;;
force-stop)
cowrie_force_stop $*
;;
start)
cowrie_start $*
;;
restart)
cowrie_stop $*
cowrie_start $*
;;
status)
cowrie_status $*
;;
*)
cowrie_usage
exit 1
;;
esac