PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rex/socket/parameters.rb

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