Skip to content

Instantly share code, notes, and snippets.

@norpol
Last active April 6, 2018 09:42
Show Gist options
  • Save norpol/38cddf0d1a5e4b4f5f69a807759a48f8 to your computer and use it in GitHub Desktop.
Save norpol/38cddf0d1a5e4b4f5f69a807759a48f8 to your computer and use it in GitHub Desktop.
openssh via. tor on debian

Setup opensshd and seperate onion service

Features:

  • Not leaking your default sshd host keys, reducing the possibility of deanymizing you
  • Warns you of missing dependencies (systemd, tor, openssh-server)
  • Setups a hidden (aka onion) service and point it to your openssh port
  • Starts it automatically
  • Shows you public key fingerprint
  • Script can be run multiple times, without overriding/changing existing files
  • Secure ssh config # FIXME, config needs review
  • Configure localhost address, port and onion config via. shell variables

Usage:

git clone https://github.com/norpol/opensshd-tor-easy-setup
sudo ./opensshd-tor-easy-setup/setup_tor_ssh.sh install

More:

setup_tor_ssh.sh help
setup_tor_ssh.sh uninstall
setup_tor_ssh.sh purge

Note:

MIT License
Copyright (c) 2018 Phi|eas |ebada
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/sh
# setup a hidden (aka onion) service and point it to SSH
# can be run multiple times, doesn't override files or configs when run multiple times
# Note: It's recommended using a seperate user.
set -ue
SCRIPT_NAME="${0}"
SCRIPT_DIR="${SCRIPT_NAME%/*}"
_check_deps() {
grep -qs '^systemd$' /proc/1/comm || { echo "no systemd detected"; exit 1; }
tor -h >/dev/null || { echo "no tor detected"; exit 1; }
sshd --help 2>&1 | grep -qs OpenSSH || { echo "No OpenSSH detected"; exit 1; }
}
_mktee() {
file="${1}"
content="${2}"
if ! grep -qs "${content}" "${file}" 2>/dev/null; then
printf '%s' "Append to ${file}: "
printf '%s\n' "${content}" | tee -a "${file}"
else
echo "${content} in ${file} exists"
fi
}
_log_cmd() {
echo "$ ${*}"
eval "${*}"
}
_mkcp() {
from="${1}"
to="${2}"
mkdir -vp "${to}"
cp -n "${from}" "${to}"
}
_install() {
_check_deps
_mktee /etc/tor/torrc "HiddenServiceDir /var/lib/tor/ssh_hidden_service"
_mktee /etc/tor/torrc "HiddenServicePort 22 127.0.1.7:22"
_mkcp ssh-tor.service "/etc/systemd/system/"
_mkcp sshd_config "/etc/ssh_tor/"
# Note: I'm not sure if you are supposed generating keys like this,
# but it's the common way referred
[ -f "/etc/ssh_tor/ssh_host_ed25519_key" ] || \
ssh-keygen -N "" -t ed25519 -f /etc/ssh_tor/ssh_host_ed25519_key
_log_cmd systemctl daemon-reload
_log_cmd systemctl restart tor
_log_cmd systemctl restart ssh-tor
_log_cmd systemctl enable ssh-tor
fingerprint="$(ssh-keyscan -p 22 -t "ed25519" 127.0.1.7 2>/dev/null)"
onion_addr="$(cat /var/lib/tor/ssh_hidden_service/hostname)"
echo "Success; Now you can proceed with https://trac.torproject.org/projects/tor/wiki/doc/TorifyHOWTO/ssh"
echo "You can add this to your known-hosts:"
echo "${onion_addr} ${fingerprint#* }"
}
_uninstall() {
systemctl stop ssh-tor 2>/dev/null || true
systemctl disable ssh-tor 2>/dev/null || true
rm -fv /etc/systemd/system/ssh-tor.service
rm -fv /etc/ssh_tor/sshd_config
sed -i "/HiddenServiceDir \\/var\\/lib\\/tor\\/ssh_hidden_service/d" /etc/tor/torrc
sed -i "/HiddenServicePort 22 127.0.1.7:22/d" /etc/tor/torrc
}
_purge() {
_uninstall
rm -rfv "/etc/ssh_tor"
rm -rfv "/var/lib/tor/ssh_hidden_service"
}
_main() {
cd "${SCRIPT_DIR}"
case "${1:-}" in
install) _install
;;
uninstall) _uninstall
;;
purge) _purge
;;
help|-h|--help|-H|*) echo "$0 install|uninstall|purge"; exit 0
esac
}
# set EVAL=true to source
[ "${EVAL:-0}" = "1" ] || _main "${1:-}"
[Service]
ExecStart=/usr/bin/sshd -D -f /etc/ssh_tor/sshd_config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
[Install]
WantedBy=multi-user.target
# This service file runs an SSH daemon that forks for each incoming connection.
# If you prefer to spawn on-demand daemons, use sshd.socket and [email protected].
#/etc/tor_ssh/sshd_config
Port 22
ListenAddress 127.0.1.7
HostKey /etc/ssh_tor/ssh_host_ed25519_key
PasswordAuthentication no
AuthenticationMethods publickey
X11Forwarding no
TCPKeepAlive yes
LogLevel VERBOSE
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# optional recommendation:
# AllowTcpForwarding no
# ClientAliveInterval 300
# ClientAliveCountMax 0
# Reduce allowed access to explicit user list
#AllowUsers <your tor user>
#AllowGroups ssh-tor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment