Guest User

Untitled

a guest
Jul 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """Create a TOR hidden service
  3.  
  4. $ ./hidden_service.py [port]
  5.  
  6. When no argument is provided, port defaults to 22. The script creates a hidden
  7. service where 127.0.0.1:[port] is served over Tor. The onion address and
  8. authentication cookie are printed on standard output. The private key and the
  9. authentication cookie are saved in a pickle file (keys.pkl) to be reused on
  10. subsequent runs.
  11.  
  12. @author Suvayu Ali
  13. @date 2018-07-19
  14.  
  15. """
  16.  
  17. import os
  18. import pickle
  19.  
  20. from stem.control import Controller
  21.  
  22.  
  23. def hs_options(overrides):
  24. """Default ephemeral hidden service options
  25.  
  26. Options can be overridden by passing an override dictionary.
  27.  
  28. """
  29. opts = {
  30. 'key_type': 'NEW',
  31. 'key_content': 'BEST',
  32. 'discard_key': False,
  33. 'await_publication': True,
  34. 'basic_auth': {'bob': None},
  35. }
  36. opts.update(overrides)
  37. return opts.copy()
  38.  
  39.  
  40. def hs_restore(store):
  41. """Restore the private key and authentication credentials from disk.
  42.  
  43. On first run, the private key and authentication credentials are stored in
  44. a pickle file on disk. `store` is the path to the pickle file.
  45.  
  46. """
  47. if os.path.exists(store):
  48. with open(store, mode='br') as store:
  49. key = pickle.load(store)
  50. return dict(key_type=key.private_key_type,
  51. key_content=key.private_key,
  52. basic_auth=key.client_auth)
  53. else:
  54. return {}
  55.  
  56.  
  57. def hs_store(key, store):
  58. """Save the private key and authentication credentials"""
  59. if not os.path.exists(store):
  60. with open(store, mode='bw') as store:
  61. pickle.dump(key, store)
  62.  
  63.  
  64. if len(sys.argv) < 2:
  65. port = 22 # default to SSH
  66. else:
  67. port = sys.argv[1]
  68.  
  69.  
  70. with Controller.from_port(port=9051) as ctrlr:
  71. ctrlr.authenticate()
  72.  
  73. opts = hs_options(hs_restore('keys.pkl'))
  74. res = ctrlr.create_ephemeral_hidden_service(port, **opts)
  75.  
  76. print('onion: {}.onion'.format(res.service_id))
  77. print('auth: {}'.format(res.client_auth.get(
  78. 'bob', opts['basic_auth'].get('bob'))))
  79.  
  80. hs_store(res, 'keys.pkl')
  81.  
  82. try:
  83. while True:
  84. pass
  85. except KeyboardInterrupt:
  86. pass
  87. finally:
  88. print('Stopping hidden service')
  89. code = ctrlr.remove_ephemeral_hidden_service(res.service_id)
  90. print('success: {}'.format(code))
Add Comment
Please, Sign In to add comment