manhole.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from twisted.conch.manhole import ColoredManhole
  15. from twisted.conch.insults import insults
  16. from twisted.conch import manhole_ssh
  17. from twisted.cred import checkers, portal
  18. from twisted.conch.ssh.keys import Key
  19. PUBLIC_KEY = (
  20. "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEArzJx8OYOnJmzf4tfBEvLi8DVPrJ3/c9k2I/Az"
  21. "64fxjHf9imyRJbixtQhlH9lfNjUIx+4LmrJH5QNRsFporcHDKOTwTTYLh5KmRpslkYHRivcJS"
  22. "kbh/C+BR3utDS555mV"
  23. )
  24. PRIVATE_KEY = """-----BEGIN RSA PRIVATE KEY-----
  25. MIIByAIBAAJhAK8ycfDmDpyZs3+LXwRLy4vA1T6yd/3PZNiPwM+uH8Yx3/YpskSW
  26. 4sbUIZR/ZXzY1CMfuC5qyR+UDUbBaaK3Bwyjk8E02C4eSpkabJZGB0Yr3CUpG4fw
  27. vgUd7rQ0ueeZlQIBIwJgbh+1VZfr7WftK5lu7MHtqE1S1vPWZQYE3+VUn8yJADyb
  28. Z4fsZaCrzW9lkIqXkE3GIY+ojdhZhkO1gbG0118sIgphwSWKRxK0mvh6ERxKqIt1
  29. xJEJO74EykXZV4oNJ8sjAjEA3J9r2ZghVhGN6V8DnQrTk24Td0E8hU8AcP0FVP+8
  30. PQm/g/aXf2QQkQT+omdHVEJrAjEAy0pL0EBH6EVS98evDCBtQw22OZT52qXlAwZ2
  31. gyTriKFVoqjeEjt3SZKKqXHSApP/AjBLpF99zcJJZRq2abgYlf9lv1chkrWqDHUu
  32. DZttmYJeEfiFBBavVYIF1dOlZT0G8jMCMBc7sOSZodFnAiryP+Qg9otSBjJ3bQML
  33. pSTqy7c3a2AScC/YyOwkDaICHnnD3XyjMwIxALRzl0tQEKMXs6hH8ToUdlLROCrP
  34. EhQ0wahUTCk1gKA4uPD6TMTChavbh4K63OvbKg==
  35. -----END RSA PRIVATE KEY-----"""
  36. def manhole(username, password, globals):
  37. """Starts a ssh listener with password authentication using
  38. the given username and password. Clients connecting to the ssh
  39. listener will find themselves in a colored python shell with
  40. the supplied globals.
  41. Args:
  42. username(str): The username ssh clients should auth with.
  43. password(str): The password ssh clients should auth with.
  44. globals(dict): The variables to expose in the shell.
  45. Returns:
  46. twisted.internet.protocol.Factory: A factory to pass to ``listenTCP``
  47. """
  48. checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(
  49. **{username: password}
  50. )
  51. rlm = manhole_ssh.TerminalRealm()
  52. rlm.chainedProtocolFactory = lambda: insults.ServerProtocol(
  53. ColoredManhole,
  54. dict(globals, __name__="__console__")
  55. )
  56. factory = manhole_ssh.ConchFactory(portal.Portal(rlm, [checker]))
  57. factory.publicKeys['ssh-rsa'] = Key.fromString(PUBLIC_KEY)
  58. factory.privateKeys['ssh-rsa'] = Key.fromString(PRIVATE_KEY)
  59. return factory