Advertisement
Guest User

Untitled

a guest
Apr 27th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import requests
  4. import json
  5. import time
  6. import warnings
  7. from datetime import date
  8.  
  9. warnings.filterwarnings("ignore", category=DeprecationWarning)
  10. import telnetlib
  11.  
  12. TELNET_PORT = 23
  13.  
  14. def pass_of_the_day():
  15. def gcd(a, b):
  16. return a if not b else gcd(b, a % b)
  17.  
  18. curdate = date.today()
  19. month, day = curdate.month, curdate.day
  20. return f"{month:x}{month:02}-{day:02x}{gcd(month, day):02}"
  21.  
  22.  
  23. def enable_telnet(nas_ip):
  24. url = f"http://{nas_ip}:5000/webman/start_telnet.cgi"
  25.  
  26. try:
  27. res = requests.get(url)
  28. response = res.json()
  29.  
  30. if res.status_code == 200:
  31. response = res.json()
  32. if "success" in response:
  33. return response["success"]
  34. else:
  35. print(f"WARNING: got unexpected response from NAS:\n"
  36. f"{json.dumps(response, indent=4)}")
  37. return False
  38. else:
  39. print(f"ERROR: NAS returned http error {res.status_code}")
  40. return False
  41. except Exception as e:
  42. print(f"ERROR: got exception {e}")
  43.  
  44. return False
  45.  
  46.  
  47. def telnet_try_login(telnet, password):
  48. # Wait for login prompt
  49. telnet.read_until(b"login: ")
  50. telnet.write("root".encode("ascii") + b'\n')
  51.  
  52. # Wait for password prompt
  53. telnet.read_until(b"Password: ")
  54. telnet.write(password.encode("ascii") + b'\n')
  55.  
  56. login_succeeded = True
  57.  
  58. for i in range(5):
  59. # skip the remote side wrong password delay and crap it sends
  60. line = telnet.read_until(b'\n', 1)
  61. if not line or line == b"\r\n":
  62. continue
  63.  
  64. if b"Login incorrect" in line:
  65. login_succeeded = False
  66. break
  67.  
  68. # consider NAS telnet prompt as successful login
  69. if len(line) > 30:
  70. break
  71.  
  72. return login_succeeded
  73.  
  74.  
  75. def exec_cmd_via_telnet(host, port, command):
  76. no_rtc_pass = "101-0101"
  77.  
  78. try:
  79. telnet = telnetlib.Telnet(host, port, timeout=10)
  80. print(f"INFO: connected via telnet to {host}:{port}")
  81.  
  82. rc = telnet_try_login(telnet, pass_of_the_day())
  83. if not rc:
  84. print("INFO: password of the day didn't work, retrying with "
  85. "the 'no RTC' password")
  86. rc = telnet_try_login(telnet, no_rtc_pass)
  87.  
  88. if rc:
  89. print("INFO: telnet login successful")
  90. else:
  91. print("ERROR: telnet login failed")
  92. return False
  93.  
  94. # flush lengthy NAS telnet prompt
  95. telnet.read_until(b"built-in shell (ash)", 1)
  96.  
  97. # Run the command
  98. telnet.write(command.encode("ascii") + b'\n')
  99.  
  100. time.sleep(1)
  101.  
  102. telnet.write(b"exit\n") # Close the session
  103.  
  104. # Read output (if any)
  105. #output = telnet.read_all().decode("ascii")
  106. print("INFO: command executed. Telnet session closed.")
  107. #print("DEBUG: output:\n", output)
  108.  
  109. except Exception as e:
  110. print("Telnet error:", e)
  111. return False
  112.  
  113. return True
  114.  
  115.  
  116. def main():
  117. if len(sys.argv) != 2:
  118. print(f"Usage:\npython3 {sys.argv[0]} <NAS_IP>")
  119. return -1
  120.  
  121. nas_ip = sys.argv[1]
  122.  
  123. rc = enable_telnet(nas_ip)
  124. if rc:
  125. print("INFO: successfully enabled telnet on NAS")
  126. else:
  127. print("ERROR: failed to enable telnet, stopping")
  128. return -1
  129.  
  130. rc = exec_cmd_via_telnet(nas_ip, TELNET_PORT,
  131. "while true; do touch /tmp/installable_check_pass; sleep 1; done &")
  132.  
  133. return 0 if rc else -1
  134.  
  135.  
  136. if __name__ == "__main__":
  137. exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement