PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rex/socket/parameters.rb

https://github.com/Jonono2/metasploit-framework
Ruby | 363 lines | 143 code | 58 blank | 162 comment | 33 complexity | 79ba1f5d6fe8f2a8fb7cf658d73005ef MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. # -*- coding: binary -*-
  2. require 'rex/socket'
  3. ###
  4. #
  5. # This class represents the set of parameters that are used to create
  6. # a socket, whether it be a server or client socket.
  7. #
  8. # @example
  9. # nsock = Rex::Socket::Tcp.create(
  10. # 'PeerHost' => opts['RHOST'] || rhost,
  11. # 'PeerPort' => (opts['RPORT'] || rport).to_i,
  12. # 'LocalHost' => opts['CHOST'] || chost || "0.0.0.0",
  13. # 'LocalPort' => (opts['CPORT'] || cport || 0).to_i,
  14. # 'SSL' => dossl,
  15. # 'SSLVersion'=> opts['SSLVersion'] || ssl_version,
  16. # 'Proxies' => proxies,
  17. # 'Timeout' => (opts['ConnectTimeout'] || connect_timeout || 10).to_i,
  18. # 'Context' =>
  19. # {
  20. # 'Msf' => framework,
  21. # 'MsfExploit' => self,
  22. # })
  23. #
  24. ###
  25. class Rex::Socket::Parameters
  26. ##
  27. #
  28. # Factory
  29. #
  30. ##
  31. #
  32. # Creates an instance of the Parameters class using the supplied hash.
  33. #
  34. def self.from_hash(hash)
  35. return self.new(hash)
  36. end
  37. ##
  38. #
  39. # Constructor
  40. #
  41. ##
  42. #
  43. # Initializes the attributes from the supplied hash. The following hash
  44. # keys can be specified.
  45. #
  46. # @option hash [String] 'PeerHost' The remote host to connect to
  47. # @option hash [String] 'PeerAddr' (alias for 'PeerHost')
  48. # @option hash [Fixnum] 'PeerPort' The remote port to connect to
  49. # @option hash [String] 'LocalHost' The local host to communicate from, if any
  50. # @option hash [String] 'LocalPort' The local port to communicate from, if any
  51. # @option hash [Bool] 'Bool' Create a bare socket
  52. # @option hash [Bool] 'Server' Whether or not this should be a server
  53. # @option hash [Bool] 'SSL' Whether or not SSL should be used
  54. # @option hash [String] 'SSLVersion' Specify SSL2, SSL3, or TLS1 (SSL3 is
  55. # default)
  56. # @option hash [String] 'SSLCert' A file containing an SSL certificate (for
  57. # server sockets)
  58. # @option hash [String] 'SSLCipher' see {#ssl_cipher}
  59. # @option hash [Bool] 'SSLCompression' enable SSL-level compression where available
  60. # @option hash [String] 'SSLVerifyMode' SSL certificate verification
  61. # mechanism. One of 'NONE' (default), 'CLIENT_ONCE', 'FAIL_IF_NO_PEER_CERT ', 'PEER'
  62. # @option hash [String] 'Proxies' List of proxies to use.
  63. # @option hash [String] 'Proto' The underlying protocol to use.
  64. # @option hash [String] 'IPv6' Force the use of IPv6.
  65. # @option hash [String] 'Comm' The underlying {Comm} object to use to create
  66. # the socket for this parameter set.
  67. # @option hash [Hash] 'Context' A context hash that can allow users of
  68. # this parameter class instance to determine who is responsible for
  69. # requesting that a socket be created.
  70. # @option hash [String] 'Retries' The number of times a connection should be
  71. # retried.
  72. # @option hash [Fixnum] 'Timeout' The number of seconds before a connection
  73. # should time out
  74. def initialize(hash)
  75. if (hash['PeerHost'])
  76. self.peerhost = hash['PeerHost']
  77. elsif (hash['PeerAddr'])
  78. self.peerhost = hash['PeerAddr']
  79. else
  80. self.peerhost = nil
  81. end
  82. if (hash['LocalHost'])
  83. self.localhost = hash['LocalHost']
  84. elsif (hash['LocalAddr'])
  85. self.localhost = hash['LocalAddr']
  86. else
  87. self.localhost = '0.0.0.0'
  88. end
  89. if (hash['PeerPort'])
  90. self.peerport = hash['PeerPort'].to_i
  91. else
  92. self.peerport = 0
  93. end
  94. if (hash['LocalPort'])
  95. self.localport = hash['LocalPort'].to_i
  96. else
  97. self.localport = 0
  98. end
  99. if (hash['Bare'])
  100. self.bare = hash['Bare']
  101. else
  102. self.bare = false
  103. end
  104. if (hash['SSL'] and hash['SSL'].to_s =~ /^(t|y|1)/i)
  105. self.ssl = true
  106. else
  107. self.ssl = false
  108. end
  109. supported_ssl_versions = ['SSL2', 'SSL23', 'TLS1', 'SSL3', :SSLv2, :SSLv3, :SSLv23, :TLSv1]
  110. if (hash['SSLVersion'] and supported_ssl_versions.include? hash['SSLVersion'])
  111. self.ssl_version = hash['SSLVersion']
  112. end
  113. supported_ssl_verifiers = %W{CLIENT_ONCE FAIL_IF_NO_PEER_CERT NONE PEER}
  114. if (hash['SSLVerifyMode'] and supported_ssl_verifiers.include? hash['SSLVerifyMode'])
  115. self.ssl_verify_mode = hash['SSLVerifyMode']
  116. end
  117. if hash['SSLCompression']
  118. self.ssl_compression = hash['SSLCompression']
  119. end
  120. if (hash['SSLCipher'])
  121. self.ssl_cipher = hash['SSLCipher']
  122. end
  123. if (hash['SSLCert'] and ::File.file?(hash['SSLCert']))
  124. begin
  125. self.ssl_cert = ::File.read(hash['SSLCert'])
  126. rescue ::Exception => e
  127. elog("Failed to read cert: #{e.class}: #{e}", LogSource)
  128. end
  129. end
  130. if hash['Proxies']
  131. self.proxies = hash['Proxies'].split('-').map{|a| a.strip}.map{|a| a.split(':').map{|b| b.strip}}
  132. end
  133. # The protocol this socket will be using
  134. if (hash['Proto'])
  135. self.proto = hash['Proto'].downcase
  136. else
  137. self.proto = 'tcp'
  138. end
  139. # Whether or not the socket should be a server
  140. self.server = hash['Server'] || false
  141. # The communication subsystem to use to create the socket
  142. self.comm = hash['Comm']
  143. # The context that was passed in, if any.
  144. self.context = hash['Context'] || {}
  145. # If no comm was supplied, try to use the comm that is best fit to
  146. # handle the provided host based on the current routing table.
  147. if( self.server )
  148. if (self.comm == nil and self.localhost)
  149. self.comm = Rex::Socket::SwitchBoard.best_comm(self.localhost)
  150. end
  151. else
  152. if (self.comm == nil and self.peerhost)
  153. self.comm = Rex::Socket::SwitchBoard.best_comm(self.peerhost)
  154. end
  155. end
  156. # If we still haven't found a comm, we default to the local comm.
  157. self.comm = Rex::Socket::Comm::Local if (self.comm == nil)
  158. # If we are a UDP server, turn off the server flag as it was only set when
  159. # creating the UDP socket in order to avail of the switch board above.
  160. if( self.server and self.proto == 'udp' )
  161. self.server = false
  162. end
  163. # The number of connection retries to make (client only)
  164. if hash['Retries']
  165. self.retries = hash['Retries'].to_i
  166. else
  167. self.retries = 0
  168. end
  169. # The number of seconds before a connect attempt times out (client only)
  170. if hash['Timeout']
  171. self.timeout = hash['Timeout'].to_i
  172. else
  173. self.timeout = 5
  174. end
  175. # Whether to force IPv6 addressing
  176. self.v6 = hash['IPv6'] || false
  177. end
  178. ##
  179. #
  180. # Conditionals
  181. #
  182. ##
  183. #
  184. # Returns true if this represents parameters for a server.
  185. #
  186. def server?
  187. return (server == true)
  188. end
  189. #
  190. # Returns true if this represents parameters for a client.
  191. #
  192. def client?
  193. return (server == false)
  194. end
  195. #
  196. # Returns true if the protocol for the parameters is TCP.
  197. #
  198. def tcp?
  199. return (proto == 'tcp')
  200. end
  201. #
  202. # Returns true if the protocol for the parameters is UDP.
  203. #
  204. def udp?
  205. return (proto == 'udp')
  206. end
  207. #
  208. # Returns true if the protocol for the parameters is IP.
  209. #
  210. def ip?
  211. return (proto == 'ip')
  212. end
  213. #
  214. # Returns true if the socket is a bare socket that does not inherit from
  215. # any extended Rex classes.
  216. #
  217. def bare?
  218. return (bare == true)
  219. end
  220. #
  221. # Returns true if SSL has been requested.
  222. #
  223. def ssl?
  224. return ssl
  225. end
  226. #
  227. # Returns true if IPv6 has been enabled
  228. #
  229. def v6?
  230. return v6
  231. end
  232. ##
  233. #
  234. # Attributes
  235. #
  236. ##
  237. # The remote host information, equivalent to the PeerHost parameter hash
  238. # key.
  239. # @return [String]
  240. attr_accessor :peerhost
  241. # The remote port. Equivalent to the PeerPort parameter hash key.
  242. # @return [Fixnum]
  243. attr_accessor :peerport
  244. # The local host. Equivalent to the LocalHost parameter hash key.
  245. # @return [String]
  246. attr_accessor :localhost
  247. # The local port. Equivalent to the LocalPort parameter hash key.
  248. # @return [Fixnum]
  249. attr_accessor :localport
  250. # The protocol to to use, such as TCP. Equivalent to the Proto parameter
  251. # hash key.
  252. # @return [String]
  253. attr_accessor :proto
  254. # Whether or not this is a server. Equivalent to the Server parameter
  255. # hash key.
  256. # @return [Bool]
  257. attr_accessor :server
  258. # The {Comm} instance that should be used to create the underlying socket.
  259. # @return [Comm]
  260. attr_accessor :comm
  261. # The context hash that was passed in to the structure. (default: {})
  262. # @return [Hash]
  263. attr_accessor :context
  264. # The number of attempts that should be made.
  265. # @return [Fixnum]
  266. attr_accessor :retries
  267. # The number of seconds before a connection attempt should time out.
  268. # @return [Fixnum]
  269. attr_accessor :timeout
  270. # Whether or not this is a bare (non-extended) socket instance that should
  271. # be created.
  272. # @return [Bool]
  273. attr_accessor :bare
  274. # Whether or not SSL should be used to wrap the connection.
  275. # @return [Bool]
  276. attr_accessor :ssl
  277. # What version of SSL to use (SSL2, SSL3, SSL23, TLS1)
  278. # @return [String,Symbol]
  279. attr_accessor :ssl_version
  280. # What specific SSL Cipher(s) to use, may be a string containing the cipher
  281. # name or an array of strings containing cipher names e.g.
  282. # ["DHE-RSA-AES256-SHA", "DHE-DSS-AES256-SHA"]
  283. # @return [String,Array]
  284. attr_accessor :ssl_cipher
  285. # The SSL certificate, in pem format, stored as a string. See
  286. # {Rex::Socket::SslTcpServer#makessl}
  287. # @return [String]
  288. attr_accessor :ssl_cert
  289. # Enables SSL/TLS-level compression
  290. # @return [Bool]
  291. attr_accessor :ssl_compression
  292. #
  293. # The SSL context verification mechanism
  294. #
  295. attr_accessor :ssl_verify_mode
  296. #
  297. # Whether we should use IPv6
  298. # @return [Bool]
  299. attr_accessor :v6
  300. # List of proxies to use
  301. # @return [String]
  302. attr_accessor :proxies
  303. alias peeraddr peerhost
  304. alias localaddr localhost
  305. end