New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Daemon #586

Closed
doktor1360 opened this Issue Apr 5, 2017 · 10 comments

Comments

Projects
None yet
10 participants
@doktor1360
Copy link

doktor1360 commented Apr 5, 2017

Knowing this issue (#337) is closed, however I thought I'd add this; not perfect by any means but works like a champ...
file : /etc/init.d/gophish

#!/bin/bash
# /etc/init.d/gophish
# initialization file for stop/start of gophish application server
#
# chkconfig: - 64 36
# description: stops/starts gophish application server
# processname:gophish
# config:/opt/goapps/src/github.com/gophish/gophish/config.json

# define script variables

processName=Gophish
process=gophish
appDirectory=/opt/goapps/src/github.com/gophish/gophish
logfile=/var/log/gophish/gophish.log
errfile=/var/log/gophish/gophish.error

start() {
    echo 'Starting '${processName}'...'
    cd ${appDirectory}
    nohup ./$process >>$logfile 2>>$errfile &
    sleep 1
}

stop() {
    echo 'Stopping '${processName}'...'
    pid=$(/usr/sbin/pidof ${process})
    kill ${pid}
    sleep 1 
}

status() {
    pid=$(/usr/sbin/pidof ${process})
    if [[ "$pid" != "" ]]; then
        echo ${processName}' is running...'
    else
        echo ${processName}' is not running...'
    fi
}

case $1 in
    start|stop|status) "$1" ;;
esac

Change directory to '/etc/init.d/' and make the file executable - 'chmod +x gophish'. Use 'chkconfig --add gophish' and 'chkconfig --levels [0123456] gophish on' to set and configure the init.d process - set the runlevels according to your system. I used 2345 for startup and shutdown scripts. You'll also need to create the '/var/log/gophish' directory accordingly for the log & error files, they'll be created auto-magically for ya when gophish is started.

Hopefully this helps someone out...

@jordan-wright

This comment has been minimized.

Copy link
Collaborator

jordan-wright commented Apr 6, 2017

Excellent, thanks @doktor1360! I've gone ahead and added this to the User Guide.

Thanks again for sending this over!

@KhasMek

This comment has been minimized.

Copy link

KhasMek commented Apr 27, 2017

This is really handy, thank you for posting it. I do have one nitpick however.

line 1

##!/bin/bash

really needs to be

#!/bin/bash

Otherwise the interpreter isn't invoked properly and the system returns a Exec format error.

@doktor1360

This comment has been minimized.

Copy link
Author

doktor1360 commented Apr 27, 2017

Updated the comment - must have been a copy/paste error, wasn't actually in the script - however, agreed... it won't work - bash won't like it all like that...

Thx @KhasMek for spotting it!

@ironc0in

This comment has been minimized.

Copy link

ironc0in commented Feb 27, 2018

Can someone plz post equivalent commands for Ubuntu?

@S0larflare

This comment has been minimized.

Copy link
Collaborator

S0larflare commented Feb 27, 2018

This is a bash script, should be fine in Ubuntu

@dudsan

This comment has been minimized.

Copy link

dudsan commented Jun 8, 2018

Thank you for posting this!

I've used your script as a starting point to set up a systemd .service unit and I thought I could pay back by posting it here.

file: /lib/systemd/system/gophish.service

[Unit]
Description=Gophish service
After=network-online.target

[Service]
Environment="GOPHISH_BIN_PATH=/opt/gophish/"
Environment="GOPHISH_LOG_PATH=/var/log/"
ExecStart=/bin/bash /root/gophish.sh
RestartSec=1
Restart=on-failure

[Install]
WantedBy=multi-user.target

file: /root/gophish.sh

#!/bin/bash

GOPHISH_LOG_FILE=gophish.log
GOPHISH_ERR_FILE=gophish.err

check_bin_path() {
    if [[ -z "$GOPHISH_BIN_PATH" ]]; then
        exit 1
    fi
}

check_log_path() {
    if [[ -z "$GOPHISH_LOG_PATH" ]]; then
        exit 2
    fi
}

create_new_log_err() {
    GOPHISH_STAMP=`date +%Y%m%d%H%M%S-%N`
    if [[ -e $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE ]]; then
        mv $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE-$GOPHISH_STAMP
    fi
    
    if [[ -e $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE ]]; then
        mv $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE-$GOPHISH_STAMP
    fi
    
    touch $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE
    touch $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE
}

launch_gophish() {
    cd $GOPHISH_BIN_PATH
    ./gophish >> $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE 2>> $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE
}

check_bin_path
check_log_path
create_new_log_err
launch_gophish

After everything is in place # chmod +x /root/gophish.sh, then # systemctl daemon-reload. After this use # systemctl normally to start/status/stop/etc. gophish. This will create/stamp separate logs each time the service (re)starts and continuously check for exits on failure in order to restart it.

@Rzqu

This comment has been minimized.

Copy link

Rzqu commented Sep 14, 2018

@doktor1360 Hi, for some reason when i try to use your code it returns an error with syntax for a (, Im using a TurnKey Linux distro based on debian, not sure if that makes a difference or not?

@jaredsiegel

This comment has been minimized.

Copy link

jaredsiegel commented Oct 3, 2018

@dudsan Your instructions worked for me on Ubuntu 16.04.05. Thank you.

@acklenx

This comment has been minimized.

Copy link

acklenx commented Oct 16, 2018

@dudsan Thank you for the systemd version. Most critically for any of this to work for me I needed to enable this to run on reboot (which is the entire point for me).
sudo systemctl enable gophish.service

Also for me (running the AWS Ubuntu 18.04 AMI the default install was NOT /opt/gophish - so it was important to change the path to the gophish binary to in the file /lib/systemd/system/gophish.service to:
[Service]
Environment="GOPHISH_BIN_PATH=go/src/github.com/gophish/gophis"

Thank you @doktor1360 for getting this party started. Sorry for posting to closed topic but the "enable" command is just too important to not say something

@manchot0

This comment has been minimized.

Copy link

manchot0 commented Oct 22, 2018

Thanks to @dudsan for the unit script and the bash script. For those who want to run gophish with an unpriviliged user (not root) here is the unit script modified :

[Unit]
Description=Gophish service
After=network-online.target

[Service]
Environment="GOPHISH_BIN_PATH=/opt/gophish-data/"
Environment="GOPHISH_LOG_PATH=/opt/gophish-data/log/"
ExecStart=/bin/bash /opt/gophish-data/gophish.sh

User=gophish
Group=gophish

AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

I have just added the User and Group directive plus the AmbientCapabilities to allow the user to bind to a privileged ports (below 1024).
Of course the user must be the owner of the directory and the files where gophish is installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment