2020-10-02 14:34:30 +00:00
|
|
|
from stem.control import Controller
|
|
|
|
import stem.connection
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import ssl
|
2020-10-05 11:18:22 +00:00
|
|
|
import socks
|
2020-10-05 11:49:34 +00:00
|
|
|
import socket
|
|
|
|
import json
|
2020-10-02 14:34:30 +00:00
|
|
|
import configparser
|
2020-10-05 14:36:04 +00:00
|
|
|
import argparse
|
|
|
|
from datetime import datetime
|
2020-10-06 09:49:02 +00:00
|
|
|
from os import environ
|
2020-10-02 14:34:30 +00:00
|
|
|
|
2020-10-05 14:36:04 +00:00
|
|
|
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
|
|
|
|
|
2020-10-06 09:49:02 +00:00
|
|
|
if "TORCH_CONFIG_DIR" in environ:
|
|
|
|
configPath = environ.get("TORCH_CONFIG_DIR")
|
|
|
|
|
2020-10-05 14:36:04 +00:00
|
|
|
if not configPath.endswith("/"):
|
|
|
|
configPath = configPath + "/"
|
2020-10-02 14:34:30 +00:00
|
|
|
|
2020-10-06 09:49:02 +00:00
|
|
|
print("Using torch configuration path: " + configPath)
|
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(configPath + "torch.conf")
|
|
|
|
|
2020-10-05 11:18:22 +00:00
|
|
|
torProxyPort = config['tor'].getint('ProxyPort', fallback = 9050)
|
2020-10-02 14:34:30 +00:00
|
|
|
torControllerPort = config['tor'].getint('ControllerPort', fallback = 9051)
|
2020-10-05 14:36:04 +00:00
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
sshPort = config['ssh'].getint('Port', fallback = 22)
|
2020-10-05 14:36:04 +00:00
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
mqttConfig = config['mqtt']
|
|
|
|
mqttBrokerHost = mqttConfig.get('BrokerHost', fallback = "localhost")
|
|
|
|
mqttBrokerPort = mqttConfig.getint('BrokerPort', fallback = 1883)
|
2020-10-05 11:49:34 +00:00
|
|
|
clientID = mqttConfig.get('ClientID', fallback = socket.gethostname())
|
|
|
|
mqttTopic = mqttConfig.get('Topic', fallback = "torch/%s/onion_url" % (clientID))
|
2020-10-02 14:34:30 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-10-06 09:49:02 +00:00
|
|
|
print("Connected to Tor on port %s" % (torControllerPort))
|
|
|
|
|
2020-10-06 11:50:23 +00:00
|
|
|
service = controller.create_ephemeral_hidden_service(
|
|
|
|
sshPort,
|
|
|
|
detached = True)
|
2020-10-02 14:34:30 +00:00
|
|
|
|
|
|
|
onionAddress = "%s.onion" % (service.service_id)
|
|
|
|
|
2020-10-06 09:49:02 +00:00
|
|
|
print("Created Tor Hidden Service for local port %s at %s" % (sshPort, onionAddress))
|
|
|
|
|
2020-10-05 14:36:04 +00:00
|
|
|
payload = {
|
|
|
|
'clientId': clientID,
|
|
|
|
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
|
|
|
|
'onionAddress': onionAddress,
|
|
|
|
'sshPort': sshPort
|
|
|
|
}
|
2020-10-05 11:49:34 +00:00
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
client = mqtt.Client()
|
2020-10-06 09:49:02 +00:00
|
|
|
protocol = "mqtt"
|
2020-10-02 14:34:30 +00:00
|
|
|
|
|
|
|
if mqttRequireCertificate:
|
|
|
|
client.tls_set(
|
|
|
|
ca_certs = mqttCaFile,
|
|
|
|
certfile = mqttCertFile,
|
|
|
|
keyfile = mqttKeyFile,
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED)
|
2020-10-06 09:49:02 +00:00
|
|
|
protocol = "mqtts"
|
2020-10-02 14:34:30 +00:00
|
|
|
|
2020-10-05 11:18:22 +00:00
|
|
|
if mqttBrokerHost.endswith(".onion"):
|
|
|
|
client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr="localhost", proxy_port=torProxyPort)
|
|
|
|
client.tls_insecure_set(True)
|
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
client.connect(mqttBrokerHost, mqttBrokerPort, 60)
|
2020-10-05 11:49:34 +00:00
|
|
|
client.publish(mqttTopic, json.dumps(payload))
|
2020-10-06 09:49:02 +00:00
|
|
|
print("Connected to MQTT Broker at %s://%s:%s/%s" % (protocol, mqttBrokerHost, mqttBrokerPort, mqttTopic))
|
|
|
|
print("Published payload: " + json.dumps(payload))
|
|
|
|
|
2020-10-02 14:34:30 +00:00
|
|
|
client.disconnect()
|
2020-10-06 09:49:02 +00:00
|
|
|
print("Disconnected from MQTT Broker")
|