diff --git a/.gitignore b/.gitignore index 324c2d5..82dc975 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .vagrant build +build_deb venv /dist/ /*.egg-info diff --git a/make-pkg.sh b/build-deb.sh similarity index 62% rename from make-pkg.sh rename to build-deb.sh index 6026f7a..b2df38a 100755 --- a/make-pkg.sh +++ b/build-deb.sh @@ -10,25 +10,32 @@ if [[ -z "${DEBEMAIL}" ]]; then exit 1 fi +BUILD_DIR=build_deb TORCH_VERSION=$(git describe --tags) DEBIAN_PKG=torch-agent-$TORCH_VERSION -PKG_ROOT=build/$DEBIAN_PKG -rf -rf $PKG_ROOT +rm -rf $BUILD_DIR +rm -rf dist -mkdir -p $PKG_ROOT -cp -r debian $PKG_ROOT +mkdir $BUILD_DIR + +python3 setup.py clean +python3 setup.py sdist +cp dist/$DEBIAN_PKG.tar.gz $BUILD_DIR/ +cd $BUILD_DIR +tar -xzmf $DEBIAN_PKG.tar.gz +cd .. + +PKG_ROOT=$BUILD_DIR/$DEBIAN_PKG mkdir -p $PKG_ROOT/src/etc/torch -cp torch.conf $PKG_ROOT/src/etc/torch/ - -mkdir -p $PKG_ROOT/src/usr/share/torch-agent -cp torch-agent.py $PKG_ROOT/src/usr/share/torch-agent/ +cp -r debian $PKG_ROOT/ cp torch-agent.service $PKG_ROOT/debian/ +cp torch.conf $PKG_ROOT/src/etc/torch/ cd $PKG_ROOT export USER=`whoami` dh_make --createorig -e $DEBEMAIL -s -y -dpkg-buildpackage -k$DEBEMAIL \ No newline at end of file +dpkg-buildpackage -k$DEBEMAIL diff --git a/debian/README.Debian b/debian/README.Debian deleted file mode 100644 index 699fb17..0000000 --- a/debian/README.Debian +++ /dev/null @@ -1,6 +0,0 @@ -torch-agent for Debian ---------------------- - - - - -- Benjamin Dweck Tue, 06 Oct 2020 15:53:02 +0200 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..9f1d48e --- /dev/null +++ b/debian/compat @@ -0,0 +1,2 @@ +11 + diff --git a/debian/control b/debian/control index 4052515..5ded10d 100644 --- a/debian/control +++ b/debian/control @@ -2,15 +2,17 @@ Source: torch-agent Section: net Priority: optional Maintainer: Benjamin Dweck -Build-Depends: debhelper-compat (= 12) +Build-Depends: debhelper (>=11~), dh-python, python3-all Standards-Version: 4.4.1 -Homepage: https://rudefox.io +Homepage: https://git.rudefox.io/bj/torch-agent +X-Python3-Version: >= 3.2 #Vcs-Browser: https://salsa.debian.org/debian/torch-agent #Vcs-Git: https://salsa.debian.org/debian/torch-agent.git Package: torch-agent Architecture: all -Depends: ssh, tor, python3-pip, ${misc:Depends} +Multi-Arch: foreign +Depends: ssh, tor, python3-pip, ${misc:Depends}, ${python3:Depends} Description: TORch is a solution for creating an SSH-via-Tor backdoor on a remote device as a means of fallback remote - management and initial headless device configuration. \ No newline at end of file + management and initial headless device configuration. diff --git a/debian/copyright b/debian/copyright index b1e631f..1a04b25 100644 --- a/debian/copyright +++ b/debian/copyright @@ -1,7 +1,7 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: torch-agent Upstream-Contact: bjdweck@gmail.com -Source: https://rudefox.io +Source: https://git.rudefox.io/bj/torch-agent Files: debian/* Copyright: 2020 Benjamin Dweck @@ -20,4 +20,4 @@ License: GPL-2+ along with this program. If not, see . On Debian systems, the complete text of the GNU General - Public License version 2 can be found in "/usr/share/common-licenses/GPL-2". \ No newline at end of file + Public License version 2 can be found in "/usr/share/common-licenses/GPL-2". diff --git a/debian/postinst b/debian/postinst index ed588b4..669ec00 100644 --- a/debian/postinst +++ b/debian/postinst @@ -53,11 +53,10 @@ case "$1" in useradd -r -g $GROUP $USER fi - chown $USER /etc/torch - chown $USER /etc/torch/torch.conf - chown $USER /usr/share/torch-agent/torch-agent.py + chown $USER /etc/torch + chown $USER /etc/torch/torch.conf - configure_tor_controller + configure_tor_controller ;; abort-upgrade|abort-remove|abort-deconfigure) diff --git a/debian/postrm b/debian/postrm index b95016c..7f60f00 100644 --- a/debian/postrm +++ b/debian/postrm @@ -23,7 +23,7 @@ USER="torch" case "$1" in purge|abort-install) rm -rf /etc/torch - rm -f /usr/share/torch-agent/torch-agent.py + if [ -x "$(command -v deluser)" ]; then deluser --quiet --system $USER > /dev/null || true else diff --git a/debian/preinst b/debian/preinst index 3e46feb..6ae9a66 100755 --- a/debian/preinst +++ b/debian/preinst @@ -16,10 +16,7 @@ set -e case "$1" in install|upgrade) - sudo -H pip3 install stem paho-mqtt PySocks - mkdir -p /etc/torch - mkdir -p /usr/share/torch-agent ;; abort-upgrade) diff --git a/debian/rules b/debian/rules index 1f72afe..7c9e5a3 100755 --- a/debian/rules +++ b/debian/rules @@ -13,9 +13,12 @@ # package maintainers to append LDFLAGS #export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed +export PYBUILD_NAME=torch-agent +export PYBUILD_SYSTEM=distutils +export PYBUILD_DISABLE=test %: - dh $@ + dh $@ --with python3 --buildsystem=pybuild override_dh_installsystemd: dh_installsystemd --no-start --no-enable diff --git a/debian/source/include-binaries b/debian/source/include-binaries deleted file mode 100755 index 65663a9..0000000 --- a/debian/source/include-binaries +++ /dev/null @@ -1,2 +0,0 @@ -src/usr/share/torch-agent/torch-agent.py -src/etc/torch/torch.conf \ No newline at end of file diff --git a/debian/src/etc/torch/torch.conf b/debian/src/etc/torch/torch.conf deleted file mode 100755 index d9d0afd..0000000 --- a/debian/src/etc/torch/torch.conf +++ /dev/null @@ -1,19 +0,0 @@ -[tor] -ProxyPort = 9050 -ControllerPort = 9051 - -[ssh] -Port = 22 - -[mqtt] -BrokerHost = mqtt.example.com # OR example1i3uyrbfoi3fi.onion -BrokerPort = 1883 -ClientID = my-client -Topic = example/topic - -### Options for Using TLS - -#RequireCertificate = true -#CaFile = ca.crt -#CertFile = client.crt -#KeyFile = client.key \ No newline at end of file diff --git a/debian/src/usr/share/torch-agent/torch-agent.py b/debian/src/usr/share/torch-agent/torch-agent.py deleted file mode 100755 index 98fd542..0000000 --- a/debian/src/usr/share/torch-agent/torch-agent.py +++ /dev/null @@ -1,98 +0,0 @@ -from stem.control import Controller -import stem.connection -import paho.mqtt.client as mqtt -import ssl -import socks -import socket -import json -import configparser -import argparse -from datetime import datetime -from os import environ - -parser = argparse.ArgumentParser(description='Broadcast SSH hidden service hostname via MQTT') - -parser.add_argument('--config-dir', nargs='?', dest='configPath', default='/etc/torch', - help='configuration directory (default: /etc/torch)') - -args = parser.parse_args() - -configPath = args.configPath - -if "TORCH_CONFIG_DIR" in environ: - configPath = environ.get("TORCH_CONFIG_DIR") - -if not configPath.endswith("/"): - configPath = configPath + "/" - -print("Using torch configuration path: " + configPath) - -config = configparser.ConfigParser() -config.read(configPath + "torch.conf") - -torProxyPort = config['tor'].getint('ProxyPort', fallback = 9050) -torControllerPort = config['tor'].getint('ControllerPort', fallback = 9051) - -sshPort = config['ssh'].getint('Port', fallback = 22) - -mqttConfig = config['mqtt'] -mqttBrokerHost = mqttConfig.get('BrokerHost', fallback = "localhost") -mqttBrokerPort = mqttConfig.getint('BrokerPort', fallback = 1883) -clientID = mqttConfig.get('ClientID', fallback = socket.gethostname()) -mqttTopic = mqttConfig.get('Topic', fallback = "torch/%s/onion_url" % (clientID)) - -mqttRequireCertificate = mqttConfig.getboolean( - 'RequireCertificate', - fallback = False) - -mqttCaFile = configPath + mqttConfig.get('CaFile') -mqttCertFile = configPath + mqttConfig.get('CertFile') -mqttKeyFile = configPath + mqttConfig.get('KeyFile') - -with Controller.from_port(port = torControllerPort) as controller: - - protocolInfo = stem.connection.get_protocolinfo(controller) - - stem.connection.authenticate_safecookie( - controller, - protocolInfo.cookie_path) - - print("Connected to Tor on port %s" % (torControllerPort)) - - service = controller.create_ephemeral_hidden_service( - sshPort, - detached = True) - - onionAddress = "%s.onion" % (service.service_id) - - print("Created Tor Hidden Service for local port %s at %s" % (sshPort, onionAddress)) - -payload = { - 'clientId': clientID, - 'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"), - 'onionAddress': onionAddress, - 'sshPort': sshPort - } - -client = mqtt.Client() -protocol = "mqtt" - -if mqttRequireCertificate: - client.tls_set( - ca_certs = mqttCaFile, - certfile = mqttCertFile, - keyfile = mqttKeyFile, - cert_reqs=ssl.CERT_REQUIRED) - protocol = "mqtts" - -if mqttBrokerHost.endswith(".onion"): - client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr="localhost", proxy_port=torProxyPort) - client.tls_insecure_set(True) - -client.connect(mqttBrokerHost, mqttBrokerPort, 60) -client.publish(mqttTopic, json.dumps(payload)) -print("Connected to MQTT Broker at %s://%s:%s/%s" % (protocol, mqttBrokerHost, mqttBrokerPort, mqttTopic)) -print("Published payload: " + json.dumps(payload)) - -client.disconnect() -print("Disconnected from MQTT Broker") diff --git a/debian/torch-agent.install b/debian/torch-agent.install index 4db3ce4..44d2d6f 100644 --- a/debian/torch-agent.install +++ b/debian/torch-agent.install @@ -1,2 +1 @@ -src/usr/share/torch-agent /usr/share/ -src/etc/torch /etc/ \ No newline at end of file +src/etc/torch /etc/ diff --git a/example/torch-agent_0.0.1-1_all.deb b/example/torch-agent_0.0.1-1_all.deb index ca57b2d..0b6da1c 100644 Binary files a/example/torch-agent_0.0.1-1_all.deb and b/example/torch-agent_0.0.1-1_all.deb differ diff --git a/setup.py b/setup.py index 4438912..33cae5a 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import setuptools with open("README.md", "r") as fh: diff --git a/torch-agent.service b/torch-agent.service index 6816b64..9b8df28 100755 --- a/torch-agent.service +++ b/torch-agent.service @@ -5,9 +5,9 @@ Requires=tor.service ssh.service [Service] Environment=PYTHONUNBUFFERED=1 -ExecStart=/usr/bin/python3 /usr/share/torch-agent/torch-agent.py +ExecStart=/usr/bin/torch-agent User=torch Group=debian-tor [Install] -WantedBy=multi-user.target ssh.service tor.service \ No newline at end of file +WantedBy=multi-user.target ssh.service tor.service diff --git a/torch.conf b/torch.conf index d9d0afd..a6dd07d 100644 --- a/torch.conf +++ b/torch.conf @@ -16,4 +16,5 @@ Topic = example/topic #RequireCertificate = true #CaFile = ca.crt #CertFile = client.crt -#KeyFile = client.key \ No newline at end of file +#KeyFile = client.key +