Skip to content

Instantly share code, notes, and snippets.

@xnyhps
Created August 26, 2015 11:32
Show Gist options
  • Save xnyhps/104ff35a9211088d114a to your computer and use it in GitHub Desktop.
Save xnyhps/104ff35a9211088d114a to your computer and use it in GitHub Desktop.
Proof-of-concept script to create ephemeral XMPP servers with Prosody. Not very well tested, use at your own risk.
import stem.control
import time
import socket
import os
telnet = socket.create_connection(("localhost", 5582))
assert(telnet != None)
print(" * Connected to Prosody telnet interface.")
def send_telnet(string):
print(">>> %s" % string)
telnet.send(string + "\r\n")
def read_until(socket, string):
buf = ""
while True:
buf += socket.recv(4096)
if string in buf:
break
return buf
print(read_until(telnet, "| http://prosody.im/doc/console\r\n\r\n"))
print(" * Finding ports...")
send_telnet("port:list()")
ports = read_until(telnet, "| OK")
print(ports)
c2s_port = None
s2s_port = None
for port in ports.split("\n"):
words = port.split(" ")
assert(words[0] == "|")
if words[1] == "s2s:":
s2s_port = int(words[2].replace(",", "").split(":")[-1])
elif words[1] == "c2s:":
c2s_port = int(words[2].replace(",", "").split(":")[-1])
elif words[1] == "OK:":
break
assert(c2s_port != None)
assert(s2s_port != None)
print(" * Ports found: c2s: %d s2s: %d" % (c2s_port, s2s_port))
print(" * Connecting to tor")
with stem.control.Controller.from_port(port = 9151) as controller:
print(" * Authenticated with tor. Requesting new ephemeral hidden service...")
response = controller.create_ephemeral_hidden_service({5269: s2s_port, 5222: c2s_port}, await_publication = True)
print(" * Our service is available at %s.onion" % response.service_id)
# Ensure we only try tor
send_telnet("> configmanager.set(\"%s.onion\", \"onions_tor_all\", true)" % response.service_id)
print(read_until(telnet, "| Result: true"))
# Don't persist anything to disk, just memory.
send_telnet("> configmanager.set(\"%s.onion\", \"storage\", \"memory\")" % response.service_id)
print(read_until(telnet, "| Result: true"))
# Now we can enable the host.
send_telnet("host:activate(\"%s.onion\")" % response.service_id)
print(read_until(telnet, "| Result: true"))
# And load mod_onions.
send_telnet("module:load(\"onions\", \"%s.onion\")" % response.service_id)
print(read_until(telnet, "| OK:"))
# Disable offline messages, as they get stored to disk even with mod_storage_memory.
send_telnet("module:unload(\"offline\", \"%s.onion\")" % response.service_id)
print(read_until(telnet, "| OK:"))
# Create a new user
password = os.urandom(12).encode('base64').replace("\n", "")
print(" * Registering account me@%s.onion with password %s" % (response.service_id, password))
send_telnet("user:create(\"me@%s.onion\", \"%s\")" % (response.service_id, password))
print(read_until(telnet, "| OK: User created"))
print(" * Done. User created:\n\nUsername: me@%s.onion\nPassword: %s\n\nPress ^C to stop the service and erase the host." % (response.service_id, password))
try:
while True:
time.sleep(10000)
except KeyboardInterrupt:
send_telnet("host:deactivate(\"%s.onion\")" % response.service_id)
print(read_until(telnet, "| Result: true"))
raise KeyboardInterrupt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment