Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # This is the client. It'll be responsible for:
  2. # -- receiving a connection from the server (i know, i know...)
  3. # The client will prompt the user for command-line input
  4. # - package that up, ship it over to the server
  5. # - receive feedback from the server
  6. # - display the output to the user.
  7.  
  8. import socket # because networking yo
  9. import ipaddress # let's see if I wind up using this or not
  10. import os # for return value
  11. import re # for passphrase
  12. import sys # for exit
  13. import pickle # for transfers
  14.  
  15. def catchSessionStartup(s):
  16.  
  17. while True:
  18.  
  19. # The user can control-C, of course
  20. print("[*] Beginning listening loop")
  21. s.listen()
  22.  
  23. conn, addr = s.accept()
  24. print(f"[*] Connection from {addr[0]}. Verifying passphrase")
  25. passphrase = conn.recv(512)
  26. if re.search("ATDT18005551234", passphrase.decode()):
  27. print(f"\t[*] Connection from {addr[0]} is valid!")
  28. conn.send("ATA".encode())
  29. confirmation = conn.recv(512)
  30. if re.search("CONNECT", confirmation.decode()):
  31. # Checks are good, moving on.
  32. pass
  33. else:
  34. print("[!] {addr[0]} did not complete the connection!")
  35. conn = None
  36. else:
  37. print(f"\t[!] Connection from {addr[0]} is invalid!")
  38. print(f"\t[!] Passphrase tendered: {passphrase.decode()}")
  39. return None
  40.  
  41. if conn is None:
  42. print("[!] NO VALID CONNECTION ESTABLISHED. RECOMMEND EVASIVE MANEUVERS.")
  43.  
  44. with conn:
  45. print("[*] CONNECTION ESTABLISHED. WOULD YOU LIKE TO PLAY A GAME?")
  46. while True:
  47. command = input("[*] INSTRUCTION: ")
  48. if command == "ATH":
  49. return 0
  50. print(f"I'm sending: {command}")
  51. try:
  52. conn.sendall(command.encode())
  53. except OSError as e:
  54. print(f"Exception: {e}")
  55. return -1
  56. output = conn.recv(1024)
  57. try:
  58. obj = pickle.loads(output)
  59. except EOFError as e:
  60. print("[!] ERROR: PICKLE NOT RECEIVED")
  61. #return -1
  62. print(obj) # This is ugly, but it's also minimum viable product
  63. '''
  64. if obj.stdout:
  65. print(obj.stdout)
  66. elif obj.stderr:
  67. print(obj.stderr)
  68. '''
  69.  
  70. def main():
  71. print("Initializing Client")
  72. # From the SO question: "How to change tcp keepalive timer using python script"
  73. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  74. s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  75. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
  76. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
  77. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
  78. try:
  79. s.bind(("0.0.0.0", 31337)) # Tee-hee
  80. except OSError as e:
  81. print("[!] Initializing failed: Could not bind port 31337; is it in use?")
  82. print(f"OS Error: {e}")
  83. return 1
  84.  
  85. # Establish our connection object..and then do everything else, because
  86. # decomposing this just caused errors.
  87. results = catchSessionStartup(s)
  88.  
  89. if __name__ == "__main__":
  90. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement