Advertisement
Guest User

Untitled

a guest
Apr 16th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import md5
  2. import socket
  3. import errno
  4. def digest(seed, pw):
  5. if not pw: return None
  6. m = md5.new()
  7. m.update(seed)
  8. m.update(pw)
  9. return m.hexdigest()
  10.  
  11. class RconClient:
  12. def __init__(self):
  13. self.sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
  14. self.trace = 0
  15. self.challenge = None
  16.  
  17. def connect (self, host, port):
  18. self.sock.connect ((host, port))
  19. response = self.getWelcomeResponse()
  20.  
  21. # Dig out the seed
  22. prefix = '### Digest seed: '
  23. challpos = response.find(prefix)
  24. if challpos != -1:
  25. challpos += len(prefix)
  26. challend = response.find('\n', challpos)
  27. self.challenge = response[challpos:challend]
  28. if self.trace: print "S: challenge: ", self.challenge
  29. if self.trace: print "S:", response
  30. return response
  31.  
  32. def invoke (self, cmd):
  33. if self.trace: print "C:", cmd
  34. self.sock.send ('\x02' + cmd + '\n') # This is a non-interactive command.
  35. response = self.getResponse()
  36. if self.trace: print "S:", response
  37. return response
  38.  
  39. # Read data until two newlines are encountered
  40. def getWelcomeResponse (self):
  41. result = ""
  42. while 1:
  43. data = self.sock.recv(1024)
  44. result += data
  45. lflfpos = result.find('\n\n')
  46. if lflfpos != -1: break
  47. return result
  48.  
  49. def getResponse (self):
  50. result = ""
  51. done = False
  52. while not done:
  53. data = self.sock.recv(1024)
  54. for c in data:
  55. if c == '\x04':
  56. done = True
  57. break
  58. result += c
  59. return result
  60.  
  61.  
  62.  
  63. if __name__ == '__main__':
  64. from getopt import getopt
  65. import sys
  66.  
  67. host = 'localhost'
  68. port = 4711
  69.  
  70. opts, args = getopt(sys.argv[1:], 'h:p:')
  71. for k, v in opts:
  72. if k == '-h': host = v
  73. elif k == '-p': port = int(v)
  74.  
  75. print 'Connecting to %s, port %d..' % (host, port)
  76.  
  77. try:
  78. conn = RconClient()
  79. conn.connect(host, port)
  80. while 1:
  81. pw = raw_input('Password: ')
  82. d = digest(conn.challenge, pw)
  83. result = conn.invoke('login %s' % d)
  84. if result.startswith('Authentication success'): break
  85. print 'Invalid password, please try again'
  86.  
  87. while 1:
  88. cmd = raw_input('rcon> ')
  89. if cmd == 'login':
  90. print 'Already authenticated'
  91. continue
  92.  
  93. if cmd == 'quit': break
  94. result = conn.invoke(cmd)
  95. print result
  96.  
  97. except socket.error, detail:
  98. print 'Network error:', detail[1]
  99.  
  100. except EOFError:
  101. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement