PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/rex/socket/parameters.rb

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