/telnetenable.py

https://gitlab.com/fiddlerwoaroof/netgear-telnetenable · Python · 112 lines · 63 code · 21 blank · 28 comment · 12 complexity · 85f89768305b54800c1c7a2555ab993e MD5 · raw file

  1. # Copyright (c) 2009 Paul Gebheim
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. # THE SOFTWARE.
  20. import sys
  21. import socket
  22. import array
  23. from optparse import OptionParser
  24. from Crypto.Cipher import Blowfish
  25. from Crypto.Hash import MD5
  26. TELNET_PORT = 23
  27. # The version of Blowfish supplied for the telenetenable.c implementation
  28. # assumes Big-Endian data, but the code does nothing to convert the
  29. # little-endian stuff it's getting on intel to Big-Endian
  30. #
  31. # So, since Crypto.Cipher.Blowfish seems to assume native endianness, we need
  32. # to byteswap our buffer before and after encrypting it
  33. #
  34. # This helper does the byteswapping on the string buffer
  35. def ByteSwap(data):
  36. a = array.array('i')
  37. if(a.itemsize < 4):
  38. a = array.array('L')
  39. if(a.itemsize != 4):
  40. print "Need a type that is 4 bytes on your platform so we can fix the data!"
  41. exit(1)
  42. a.fromstring(data)
  43. a.byteswap()
  44. return a.tostring()
  45. def GeneratePayload(mac, username, password=""):
  46. # Pad the input correctly
  47. assert(len(mac) < 0x10)
  48. just_mac = mac.ljust(0x10, "\x00")
  49. assert(len(username) <= 0x10)
  50. just_username = username.ljust(0x10, "\x00")
  51. assert(len(password) <= 0x10)
  52. just_password = password.ljust(0x10, "\x00")
  53. cleartext = (just_mac + just_username + just_password).ljust(0x70, '\x00')
  54. md5_key = MD5.new(cleartext).digest()
  55. payload = ByteSwap((md5_key + cleartext).ljust(0x80, "\x00"))
  56. secret_key = "AMBIT_TELNET_ENABLE+" + password
  57. return ByteSwap(Blowfish.new(secret_key, 1).encrypt(payload))
  58. def SendPayload(ip, payload):
  59. for res in socket.getaddrinfo(ip, TELNET_PORT, socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP):
  60. af, socktype, proto, canonname, sa = res
  61. try:
  62. s = socket.socket(af, socktype, proto)
  63. except socket.error, msg:
  64. s = None
  65. continue
  66. try:
  67. s.connect(sa)
  68. except socket.error, msg:
  69. s.close()
  70. s= None
  71. continue
  72. break
  73. if s is None:
  74. print "Could not connect to '%s:%d'" % (ip, TELNET_PORT)
  75. else:
  76. s.send(payload)
  77. s.close()
  78. print "Sent telnet enable payload to '%s:%d'" % (ip, TELNET_PORT)
  79. def main():
  80. args = sys.argv[1:]
  81. if len(args) < 3 or len(args) > 4:
  82. print "usage: python telnetenable.py <ip> <mac> <username> [<password>]"
  83. ip = args[0]
  84. mac = args[1]
  85. username = args[2]
  86. password = ""
  87. if len(args) == 4:
  88. password = args[3]
  89. payload = GeneratePayload(mac, username, password)
  90. SendPayload(ip, payload)
  91. main()