2020-10-08 13:20:42 +00:00
|
|
|
from stem.control import Controller
|
|
|
|
import stem.connection
|
|
|
|
import paho.mqtt.client as mqtt
|
2020-10-20 10:16:19 +00:00
|
|
|
import socks
|
2020-10-08 13:20:42 +00:00
|
|
|
import ssl
|
|
|
|
import socket
|
|
|
|
import json
|
|
|
|
import configparser
|
|
|
|
import argparse
|
|
|
|
from datetime import datetime
|
|
|
|
from os import environ
|
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
|
2020-10-08 13:20:42 +00:00
|
|
|
def main():
|
2020-10-19 22:20:33 +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()
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
config_path = args.configPath
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
if "TORCH_CONFIG_DIR" in environ:
|
|
|
|
config_path = environ.get("TORCH_CONFIG_DIR")
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
if not config_path.endswith("/"):
|
|
|
|
config_path = config_path + "/"
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
print("Using torch configuration path: " + config_path)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
config = configparser.ConfigParser()
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
configuration_file_path = config_path + "torch.conf"
|
2020-10-22 08:50:24 +00:00
|
|
|
print("Reading configuration file at '%s'" % configuration_file_path)
|
2020-10-21 10:31:45 +00:00
|
|
|
config.read(configuration_file_path)
|
|
|
|
|
2020-10-22 06:51:01 +00:00
|
|
|
tor_proxy_host = config['tor'].get('ProxyHost', fallback="127.0.0.1")
|
2020-10-19 22:20:33 +00:00
|
|
|
tor_proxy_port = config['tor'].getint('ProxyPort', fallback=9050)
|
|
|
|
tor_controller_port = config['tor'].getint('ControllerPort', fallback=9051)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
ssh_port = config['ssh'].getint('Port', fallback=22)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
mqtt_config = config['mqtt']
|
|
|
|
mqtt_broker_host = mqtt_config.get('BrokerHost', fallback="localhost")
|
|
|
|
mqtt_broker_port = mqtt_config.getint('BrokerPort', fallback=1883)
|
2020-10-21 10:31:45 +00:00
|
|
|
mqtt_broker_using_tor = mqtt_broker_host.endswith(".onion")
|
2020-10-19 22:20:33 +00:00
|
|
|
client_id = mqtt_config.get('ClientID', fallback=socket.gethostname())
|
|
|
|
mqtt_topic = mqtt_config.get('Topic', fallback="torch/%s/onion_url" % client_id)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
mqtt_require_certificate = mqtt_config.getboolean(
|
|
|
|
'RequireCertificate',
|
|
|
|
fallback=False)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
mqtt_ca_file = config_path + mqtt_config.get('CaFile')
|
|
|
|
mqtt_cert_file = config_path + mqtt_config.get('CertFile')
|
|
|
|
mqtt_key_file = config_path + mqtt_config.get('KeyFile')
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-22 08:32:23 +00:00
|
|
|
print("Connecting to local TOR controller on port %s" % tor_controller_port)
|
2020-10-21 10:31:45 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
with Controller.from_port(port=tor_controller_port) as controller:
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
protocol_info = stem.connection.get_protocolinfo(controller)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
stem.connection.authenticate_safecookie(controller, protocol_info.cookie_path)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
print("Creating TOR Hidden Service...")
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
service = controller.create_ephemeral_hidden_service(ssh_port, detached=True)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
onion_address = "%s.onion" % service.service_id
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
print("Created Tor Hidden Service for local service on port %s at %s" % (ssh_port, onion_address))
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-22 08:32:23 +00:00
|
|
|
def on_publish(pub_client):
|
|
|
|
print("Published payload!")
|
|
|
|
pub_client.disconnect()
|
|
|
|
|
2020-10-22 08:50:24 +00:00
|
|
|
def on_disconnect(pub_client):
|
2020-10-22 08:32:23 +00:00
|
|
|
print("Disconnected!")
|
2020-10-22 08:50:24 +00:00
|
|
|
pub_client.loop_stop()
|
2020-10-22 08:32:23 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
client = mqtt.Client()
|
2020-10-22 08:32:23 +00:00
|
|
|
client.on_publish = on_publish
|
|
|
|
client.on_disconnect = on_disconnect
|
2020-10-22 08:50:24 +00:00
|
|
|
client.loop_start()
|
2020-10-22 08:32:23 +00:00
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
protocol = "mqtt"
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
if mqtt_broker_using_tor:
|
|
|
|
client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr=tor_proxy_host, proxy_port=tor_proxy_port)
|
2020-10-20 18:05:30 +00:00
|
|
|
else:
|
|
|
|
if mqtt_require_certificate:
|
|
|
|
protocol = "mqtts"
|
|
|
|
client.tls_set(
|
|
|
|
ca_certs=mqtt_ca_file,
|
|
|
|
certfile=mqtt_cert_file,
|
|
|
|
keyfile=mqtt_key_file,
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED)
|
2020-10-08 13:20:42 +00:00
|
|
|
|
2020-10-21 10:31:45 +00:00
|
|
|
print("Connecting to MQTT broker: %s://%s:%s/%s" % (protocol, mqtt_broker_host, mqtt_broker_port, mqtt_topic))
|
|
|
|
if mqtt_broker_using_tor:
|
|
|
|
print("--> Using TOR proxy: %s:%s" % (tor_proxy_host, tor_proxy_port))
|
|
|
|
|
2020-10-19 22:20:33 +00:00
|
|
|
client.connect(mqtt_broker_host, mqtt_broker_port, 60)
|
2020-10-21 10:31:45 +00:00
|
|
|
print("Connected to MQTT Broker")
|
|
|
|
|
|
|
|
payload = json.dumps({
|
|
|
|
'clientId': client_id,
|
|
|
|
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
|
|
|
|
'onionAddress': onion_address,
|
|
|
|
'sshPort': ssh_port
|
|
|
|
})
|
|
|
|
|
|
|
|
print("Publishing payload: " + payload)
|
2020-10-22 08:06:06 +00:00
|
|
|
client.publish(mqtt_topic, payload, qos=1)
|