Class: Rex::Socket::Comm::Local
- Inherits:
-
Object
- Object
- Rex::Socket::Comm::Local
- Includes:
- Rex::Socket::Comm, Singleton
- Defined in:
- lib/rex/socket/comm/local.rb
Overview
Local communication class factory.
Defined Under Namespace
Classes: UnitTest
Class Method Summary collapse
-
.create(param) ⇒ Object
Creates an instance of a socket using the supplied parameters.
-
.create_by_type(param, type, proto = 0) ⇒ Object
Creates a socket using the supplied Parameter instance.
-
.create_ip(param) ⇒ Object
Creates a raw IP socket using the supplied Parameter instance.
-
.create_jruby(param) ⇒ Object
Creates an instance of a socket using the supplied parameters.
-
.deregister_event_handler(handler) ⇒ Object
:nodoc:.
-
.each_event_handler(handler) ⇒ Object
:nodoc:.
- .proxy(sock, type, host, port) ⇒ Object
-
.register_event_handler(handler) ⇒ Object
Registration.
Methods included from Rex::Socket::Comm
#chainable?, #deregister_event_handler, #each_event_handler, #notify_before_socket_create, #notify_socket_created, #register_event_handler
Class Method Details
.create(param) ⇒ Object
Creates an instance of a socket using the supplied parameters.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rex/socket/comm/local.rb', line 23 def self.create(param) # Work around jRuby socket implementation issues if(RUBY_PLATFORM == 'java') return self.create_jruby(param) end case param.proto when 'tcp' return create_by_type(param, ::Socket::SOCK_STREAM, ::Socket::IPPROTO_TCP) when 'udp' return create_by_type(param, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP) when 'ip' return create_ip(param) else raise Rex::UnsupportedProtocol.new(param.proto), caller end end |
.create_by_type(param, type, proto = 0) ⇒ Object
Creates a socket using the supplied Parameter instance.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rex/socket/comm/local.rb', line 117 def self.create_by_type(param, type, proto = 0) # Whether to use IPv6 addressing usev6 = false # Detect IPv6 addresses and enable IPv6 accordingly if ( Rex::Socket.support_ipv6?()) # Allow the caller to force IPv6 if (param.v6) usev6 = true end # Force IPv6 mode for non-connected UDP sockets if (type == ::Socket::SOCK_DGRAM and not param.peerhost) # FreeBSD allows IPv6 socket creation, but throws an error on sendto() if (not Rex::Compat.is_freebsd()) usev6 = true end end local = Rex::Socket.resolv_nbo(param.localhost) if param.localhost peer = Rex::Socket.resolv_nbo(param.peerhost) if param.peerhost if (local and local.length == 16) usev6 = true end if (peer and peer.length == 16) usev6 = true end if (usev6) if (local and local.length == 4) if (local == "\x00\x00\x00\x00") param.localhost = '::' elsif (local == "\x7f\x00\x00\x01") param.localhost = '::1' else param.localhost = '::ffff:' + Rex::Socket.getaddress(param.localhost) end end if (peer and peer.length == 4) if (peer == "\x00\x00\x00\x00") param.peerhost = '::' elsif (peer == "\x7f\x00\x00\x01") param.peerhost = '::1' else param.peerhost = '::ffff:' + Rex::Socket.getaddress(param.peerhost) end end param.v6 = true end else # No IPv6 support param.v6 = false end # Notify handlers of the before socket create event. self.instance.notify_before_socket_create(self, param) # Create the socket sock = nil if (param.v6) sock = ::Socket.new(::Socket::AF_INET6, type, proto) else sock = ::Socket.new(::Socket::AF_INET, type, proto) end # Bind to a given local address and/or port if they are supplied if (param.localhost || param.localport) begin sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true) sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport)) rescue ::Errno::EADDRNOTAVAIL,::Errno::EADDRINUSE sock.close raise Rex::AddressInUse.new(param.localhost, param.localport), caller end end # Configure broadcast support for all datagram sockets if (type == ::Socket::SOCK_DGRAM) sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true) end # If a server TCP instance is being created... if (param.server?) sock.listen(256) if (param. == false) klass = Rex::Socket::TcpServer if (param.ssl) klass = Rex::Socket::SslTcpServer end sock.extend(klass) sock.initsock(param) end # Otherwise, if we're creating a client... else chain = [] # If we were supplied with host information if (param.peerhost) begin ip = param.peerhost port = param.peerport if param.proxies chain = param.proxies.dup chain.push(['host',param.peerhost,param.peerport]) ip = chain[0][1] port = chain[0][2].to_i end begin Timeout.timeout(param.timeout) do sock.connect(Rex::Socket.to_sockaddr(ip, port)) end rescue ::Timeout::Error raise ::Errno::ETIMEDOUT end rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL sock.close raise Rex::HostUnreachable.new(param.peerhost, param.peerport), caller rescue ::Errno::EADDRNOTAVAIL,::Errno::EADDRINUSE sock.close raise Rex::AddressInUse.new(param.peerhost, param.peerport), caller rescue Errno::ETIMEDOUT sock.close raise Rex::ConnectionTimeout.new(param.peerhost, param.peerport), caller rescue ::Errno::ECONNRESET,::Errno::ECONNREFUSED,::Errno::ENOTCONN,::Errno::ECONNABORTED sock.close raise Rex::ConnectionRefused.new(param.peerhost, param.peerport), caller end end if (param. == false) case param.proto when 'tcp' klass = Rex::Socket::Tcp sock.extend(klass) sock.initsock(param) when 'udp' sock.extend(Rex::Socket::Udp) sock.initsock(param) end end if chain.size > 1 chain.each_with_index { |proxy, i| next_hop = chain[i + 1] if next_hop proxy(sock, proxy[0], next_hop[1], next_hop[2]) end } end # Now extend the socket with SSL and perform the handshake if(param. == false and param.ssl) klass = Rex::Socket::SslTcp sock.extend(klass) sock.initsock(param) end end # Notify handlers that a socket has been created. self.instance.notify_socket_created(self, sock, param) sock end |
.create_ip(param) ⇒ Object
Creates a raw IP socket using the supplied Parameter instance. Special-cased because of how different it is from UDP/TCP
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rex/socket/comm/local.rb', line 94 def self.create_ip(param) self.instance.notify_before_socket_create(self, param) sock = ::Socket.open(::Socket::PF_INET, ::Socket::SOCK_RAW, ::Socket::IPPROTO_RAW) sock.setsockopt(::Socket::IPPROTO_IP, ::Socket::IP_HDRINCL, 1) # Configure broadcast support sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true) if (param. == false) sock.extend(::Rex::Socket::Ip) sock.initsock(param) end self.instance.notify_socket_created(self, sock, param) sock end |
.create_jruby(param) ⇒ Object
Creates an instance of a socket using the supplied parameters. Use various hacks to make this work with jRuby
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rex/socket/comm/local.rb', line 46 def self.create_jruby(param) sock = nil # Notify handlers of the before socket create event. self.instance.notify_before_socket_create(self, param) case param.proto when 'tcp' if (param.server?) sock = TCPServer.new(param.localport, param.localhost) klass = Rex::Socket::TcpServer if (param.ssl) klass = Rex::Socket::SslTcpServer end sock.extend(klass) else sock = TCPSocket.new(param.peerhost, param.peerport) klass = Rex::Socket::Tcp if (param.ssl) klass = Rex::Socket::SslTcp end sock.extend(klass) end when 'udp' if (param.server?) sock = UDPServer.new(param.localport, param.localhost) klass = Rex::Socket::UdpServer sock.extend(klass) else sock = UDPSocket.new(param.peerhost, param.peerport) klass = Rex::Socket::Udp sock.extend(klass) end else raise Rex::UnsupportedProtocol.new(param.proto), caller end sock.initsock(param) self.instance.notify_socket_created(self, sock, param) return sock end |
.deregister_event_handler(handler) ⇒ Object
:nodoc:
404 405 406 |
# File 'lib/rex/socket/comm/local.rb', line 404 def self.deregister_event_handler(handler) # :nodoc: self.instance.deregister_event_handler(handler) end |
.each_event_handler(handler) ⇒ Object
:nodoc:
408 409 410 |
# File 'lib/rex/socket/comm/local.rb', line 408 def self.each_event_handler(handler) # :nodoc: self.instance.each_event_handler(handler) end |
.proxy(sock, type, host, port) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rex/socket/comm/local.rb', line 301 def self.proxy(sock, type, host, port) #$stdout.print("PROXY\n") case type.downcase when 'http' setup = "CONNECT #{host}:#{port} HTTP/1.0\r\n\r\n" size = sock.put(setup) if (size != setup.length) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller end begin ret = sock.get_once(39,30) rescue IOError raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller end if ret.nil? raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller end resp = Rex::Proto::Http::Response.new resp.update_cmd_parts(ret.split(/\r?\n/)[0]) if resp.code != 200 raise Rex::ConnectionProxyError.new(host, port, type, "The proxy returned a non-OK response"), caller end when 'socks4' setup = [4,1,port.to_i].pack('CCn') + Socket.gethostbyname(host)[3] + Rex::Text.rand_text_alpha(rand(8)+1) + "\x00" size = sock.put(setup) if (size != setup.length) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller end begin ret = sock.get_once(8, 30) rescue IOError raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller end if (ret.nil? or ret.length < 8) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a complete response from the proxy"), caller end if ret[1,1] != "\x5a" raise Rex::ConnectionProxyError.new(host, port, type, "Proxy responded with error code #{ret[0,1].unpack("C")[0]}"), caller end when 'socks5' auth_methods = [5,1,0].pack('CCC') size = sock.put(auth_methods) if (size != auth_methods.length) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller end ret = sock.get_once(2,30) if (ret[1,1] == "\xff") raise Rex::ConnectionProxyError.new(host, port, type, "The proxy requires authentication"), caller end if (Rex::Socket.is_ipv4?(host)) addr = Rex::Socket.gethostbyname(host)[3] setup = [5,1,0,1].pack('C4') + addr + [port.to_i].pack('n') elsif (Rex::Socket.support_ipv6? and Rex::Socket.is_ipv6?(host)) # IPv6 stuff all untested addr = Rex::Socket.gethostbyname(host)[3] setup = [5,1,0,4].pack('C4') + addr + [port.to_i].pack('n') else # Then it must be a domain name. # Unfortunately, it looks like the host has always been # resolved by the time it gets here, so this code never runs. setup = [5,1,0,3].pack('C4') + [host.length].pack('C') + host + [port.to_i].pack('n') end size = sock.put(setup) if (size != setup.length) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller end begin response = sock.get_once(10, 30) rescue IOError raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller end if (response.nil? or response.length < 10) raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a complete response from the proxy"), caller end if response[1,1] != "\x00" raise Rex::ConnectionProxyError.new(host, port, type, "Proxy responded with error code #{response[1,1].unpack("C")[0]}"), caller end else raise RuntimeError, "The proxy type specified is not valid", caller end end |
.register_event_handler(handler) ⇒ Object
Registration
400 401 402 |
# File 'lib/rex/socket/comm/local.rb', line 400 def self.register_event_handler(handler) # :nodoc: self.instance.register_event_handler(handler) end |