Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. import sys
  2. import socket
  3. import time
  4. import array
  5. import paramiko
  6. import xml.etree.ElementTree as ET
  7.  
  8.  
  9. class gmp:
  10. gmv_hostname = '127.0.0.1'
  11. gvm_port = 22
  12. gvm_username = 'omp'
  13. gvm_password = ''
  14. gvm_timeout = 5
  15.  
  16. gvm_gmp_sock = ''
  17. gvm_gmp_stdin = ''
  18. gvm_gmp_stdout = ''
  19.  
  20. def __init__(self):
  21. pass
  22.  
  23. def connect(self):
  24. pass
  25.  
  26. def connect(self, hostname, port, username, password, timeout):
  27. self.gvm_hostname = hostname
  28. self.gvm_port = port
  29. self.gvm_username = username
  30. self.gvm_password = password
  31. self.gvm_timeout = timeout
  32.  
  33. self.gvm_gmp_sock = paramiko.SSHClient()
  34. self.gvm_gmp_sock.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  35. try:
  36. self.gvm_gmp_sock.connect(hostname=hostname, username=username, password=password, timeout=timeout, port=port)
  37. except (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException, socket.error) as e:
  38. print('SSH Connection failed: ' + str(e))
  39. sys.exit(1)
  40.  
  41. channel = self.gvm_gmp_sock.invoke_shell()
  42.  
  43. self.gvm_gmp_stdin = channel.makefile('wb')
  44. self.gvm_gmp_stdout = channel.makefile('r')
  45.  
  46. self.gvm_gmp_stdin.write('/usr/bin/socat UNIX-CONNECT:/usr/local/var/run/openvasmd.sock -\n')
  47. time.sleep(0.5)
  48. self.gvm_gmp_stdout.read(len(self.gvm_gmp_stdout.channel.in_buffer))
  49.  
  50. print(self.gvm_hostname
  51. + ' ' + str(self.gvm_port)
  52. + ' ' + self.gvm_username
  53. + ' ' + self.gvm_password
  54. + ' ' + str(self.gvm_timeout))
  55.  
  56. def close(self):
  57. self.gvm_gmp_sock.close()
  58.  
  59. def authenticate(self, username='admin', password='eebd0396-6aae-4c03-94ae-5d1bd53da656'):
  60. cmd = '<authenticate><credentials><username>' + username + '</username><password>' + password + '</password></credentials></authenticate>'
  61. self.sendCommand(cmd)
  62.  
  63. def getTasks(self):
  64. self.sendCommand('<get_tasks/>')
  65.  
  66. def sendCommand(self, cmd):
  67. #print('About to send this here: ' + cmd)
  68. self.gvm_gmp_stdin.write(str(cmd) + '\n')
  69. time.sleep(0.2)
  70. buf_len = len(self.gvm_gmp_stdout.channel.in_buffer)
  71. data_len = len(cmd) + 2
  72. raw_xml_data = self.gvm_gmp_stdout.read(buf_len).decode()[data_len:]
  73. print(raw_xml_data)
  74.  
  75. decoded_data = array
  76. root = ET.fromstring(raw_xml_data)
  77.  
  78. #return ET.tostringlist(root)
  79. for child in root.findall('.'):
  80. # if there are children, get their text content as well.
  81. if len(child):
  82. for subchild in child:
  83. print(subchild.text)
  84. # else just get the current child text.
  85. else:
  86. print(child.text)
  87.  
  88.  
  89.  
  90.  
  91.  
  92. import sys
  93. import argparse
  94. import gmp
  95. import code
  96.  
  97. def main(argv):
  98. # hostname = 'localhost'
  99. # username = ''
  100. quick_xml_command = ''
  101.  
  102. gvm = gmp.gmp()
  103. gvm.connect(hostname='172.30.10.188', port=22, username='omp', password='', timeout=5)
  104. #test.connect(hostname='172.30.10.188', port=22, username='omp', password='', timeout=5)
  105. #print(argv)
  106.  
  107. if args.xml and len(args.xml) != 0:
  108. # Start execute single xml command
  109. startSingleMode(gvm, args.xml)
  110. elif args.interactive:
  111. # Start the interactive Shell
  112. startInteractiveMode(gvm)
  113.  
  114. input_data = ''
  115. while True:
  116. input_data = input('gvm-shell: ')
  117. if input_data == 'quit':
  118. break
  119. gvm.sendCommand(input_data)
  120.  
  121. gvm.close()
  122.  
  123. def startSingleMode(gvm, xml_command):
  124. gvm.sendCommand(xml_command)
  125. gvm.close()
  126. sys.exit(0)
  127.  
  128. def startInteractiveMode(gvm):
  129. code.interact(banner='GVM Interactive Console', local=dict(globals(), **locals()))
  130.  
  131. if __name__ == '__main__':
  132. parser = argparse.ArgumentParser(prog='gvm-shell')
  133. group = parser.add_mutually_exclusive_group()
  134. group.add_argument('-X', '--xml', help='Send a xml command to OpenVAS Manager Socket')
  135. group.add_argument('-i', '--interactive', action='store_true', default=False, help='Start an interactive shell')
  136. parser.add_argument('-I', '--input', help='Read a pre written gmp file. It will be executed after successful connection')
  137.  
  138. args = parser.parse_args()
  139.  
  140. main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement