/src/client.py

https://github.com/guesslin/Hale · Python · 214 lines · 95 code · 38 blank · 81 comment · 19 complexity · cf2257aecfa17971e3a2249225f240e8 MD5 · raw file

  1. ################################################################################
  2. # (c) 2011, The Honeynet Project
  3. # Author: Patrik Lantz patrik@pjlantz.com
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. ################################################################################
  20. import xmlrpclib, socket
  21. import cmd, sys, os, getpass
  22. from conf import configHandler
  23. class CLI(cmd.Cmd):
  24. """
  25. Handles command line input with support for
  26. command line completion and command history.
  27. Additionally the cmd module provides a nice help feature,
  28. the comments for the do_command functions are shown
  29. when issuing help <command>
  30. """
  31. def __init__(self):
  32. """
  33. Constructor, sets up cmd variables and other
  34. data structures holding modules, configs etc.
  35. Starts a manager thread taking care of newly
  36. added modules and errors from module threads
  37. """
  38. cmd.Cmd.__init__(self)
  39. print " __ __ ___ "
  40. print "/\ \/\ \ /\_ \\"
  41. print "\ \ \_\ \ __ \//\ \ __"
  42. print " \ \ _ \ /'__`\ \ \ \ /'__`\\"
  43. print " \ \ \ \ \/\ \L\.\_ \_\ \_/\ __/"
  44. print " \ \_\ \_\ \__/.\_\/\____\ \____\\"
  45. print " \/_/\/_/\/__/\/_/\/____/\/____/\n"
  46. self.prompt = ">> "
  47. self.intro = "\nType help or '?' for a list of commands\n"
  48. self.conf = configHandler.ConfigHandler().loadHaleConf()
  49. host = self.conf.get("client", "server")
  50. port = self.conf.get("client", "port")
  51. self.config = configHandler.ConfigHandler()
  52. while True:
  53. self.user = raw_input("login: ")
  54. passwd = getpass.getpass(prompt="password: ")
  55. host = self.conf.get("client", "server")
  56. port = self.conf.get("client", "port")
  57. url = "http://" + self.user + ":" + passwd + "@" + host + ":" + port
  58. self.config = configHandler.ConfigHandler()
  59. self.proxy = xmlrpclib.ServerProxy(url)
  60. try:
  61. self.proxy.auth("")
  62. except xmlrpclib.ProtocolError:
  63. print "Incorrect login/password\n"
  64. continue
  65. except socket.error:
  66. print "Incorrect login/password\n"
  67. continue
  68. break
  69. def do_exec(self, arg):
  70. """
  71. Execute a module with the current config.
  72. Usage: exec modulename identifier
  73. """
  74. config = self.config.getConfig()
  75. configHash = self.config.getCurrentHash()
  76. try:
  77. response = self.proxy.execmod(arg, config, configHash)
  78. print response
  79. except xmlrpclib.ProtocolError:
  80. print "Operation denied"
  81. def do_stop(self, arg):
  82. """
  83. Stops a module identified by id
  84. Usage: stop id
  85. """
  86. try:
  87. response = self.proxy.stopmod(arg)
  88. print response
  89. except xmlrpclib.ProtocolError:
  90. print "Operation denied"
  91. def do_lsmod(self, arg):
  92. """
  93. List all modules currently installed
  94. """
  95. try:
  96. response = self.proxy.lsmod(arg)
  97. print response
  98. except xmlrpclib.ProtocolError:
  99. print "Operation denied"
  100. def do_lsexec(self, arg):
  101. """
  102. List all modules being executed at the moment
  103. """
  104. try:
  105. response = self.proxy.lsexec(arg)
  106. print response
  107. except xmlrpclib.ProtocolError:
  108. print "Operation denied"
  109. def do_execinfo(self, arg):
  110. """
  111. List info about executing module
  112. Usage: execinfo id
  113. """
  114. try:
  115. response = self.proxy.execinfo(arg)
  116. print response
  117. except xmlrpclib.ProtocolError:
  118. print "Operation denied"
  119. def do_lsconf(self, arg):
  120. """
  121. List all configurations
  122. """
  123. print self.config.listConf()
  124. def do_reload(self, arg):
  125. """
  126. Reload a module if changes have been made to it
  127. Usage: reload modulename
  128. """
  129. try:
  130. response = self.proxy.reloadmod(arg)
  131. print response
  132. except xmlrpclib.ProtocolError:
  133. print "Operation denied"
  134. def do_useconf(self, arg):
  135. """
  136. Set the current config to use, if argument
  137. is empty, current config used is printed out
  138. """
  139. print self.config.useConf(arg)
  140. def default(self, line):
  141. """
  142. Called when command input is not recognized
  143. and outputs an error message
  144. """
  145. print "Unkown command: " + line
  146. def emptyline(self):
  147. """
  148. Called when empty line was entered at the prompt
  149. """
  150. pass
  151. def do_showlog(self, arg):
  152. """
  153. Show recent logs from the monitor
  154. and the modules
  155. """
  156. try:
  157. response = self.proxy.showlog(arg)
  158. print response
  159. except xmlrpclib.ProtocolError:
  160. print "Operation denied"
  161. def do_quit(self, arg):
  162. """
  163. Exit the program gracefully
  164. """
  165. self.do_exit(arg)
  166. def do_exit(self, arg):
  167. """
  168. Exit the program gracefully
  169. """
  170. print "Bye!"
  171. sys.exit(0)
  172. if __name__ == "__main__":
  173. """
  174. Main program starts
  175. """
  176. CLI().cmdloop()