rex-socket 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f946c5cc7215552b58adcf60f5311e83525b26dc
4
- data.tar.gz: 284b383b909b5e670ea0cdf324342e6a1382b83e
3
+ metadata.gz: 902535274c4d182988dbaa9575b42dfb98fa04f4
4
+ data.tar.gz: b5b5ae8fed2ba779b2bb9a457f904feaf955495d
5
5
  SHA512:
6
- metadata.gz: bf121014ba89e58b75ed715195b4f42696ca90bad6af254a09bd988b08d82d7f705d667419120558a314dedc8b6b9fef6caf5c17f09578ab897e910953cb74e2
7
- data.tar.gz: 31a9106b784350b7da638534b42d798da001bca87d141c5531b6588b951d4304a05396aa8e5d4291bfa3a036447abb41590e4e76e4306483d68502c0cba38c8e
6
+ metadata.gz: 8fed81f4863ea30cda9ef78637e41f4505faf7fb077a112fb667c06add342513ddfc62e0950a3048d6658be66c35dec998910ac6e212e9e5c72333eb3a624b7b
7
+ data.tar.gz: 6cfd80dd79ddfe0ce788cf08aafc3eb71e866dba3555c7e8323a16efb2497228f69133a541dc6ca7813d93897c0a6010e2e05c64b438fd9682e12c5b5fbb34af
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -745,6 +745,24 @@ module Socket
745
745
  return peer_name
746
746
  end
747
747
 
748
+ #
749
+ # Returns peer information (host + port) in host:port format.
750
+ #
751
+ def peerinfo
752
+ if (pi = getpeername_as_array)
753
+ return pi[1] + ':' + pi[2].to_s
754
+ end
755
+ end
756
+
757
+ #
758
+ # Returns local information (host + port) in host:port format.
759
+ #
760
+ def localinfo
761
+ if (pi = getlocalname)
762
+ return pi[1] + ':' + pi[2].to_s
763
+ end
764
+ end
765
+
748
766
  #
749
767
  # Returns a string that indicates the type of the socket, such as 'tcp'.
750
768
  #
@@ -52,10 +52,10 @@ class Rex::Socket::Comm::Local
52
52
 
53
53
  case param.proto
54
54
  when 'tcp'
55
- if (param.server?)
55
+ if param.server?
56
56
  sock = TCPServer.new(param.localport, param.localhost)
57
57
  klass = Rex::Socket::TcpServer
58
- if (param.ssl)
58
+ if param.ssl
59
59
  klass = Rex::Socket::SslTcpServer
60
60
  end
61
61
  sock.extend(klass)
@@ -63,13 +63,13 @@ class Rex::Socket::Comm::Local
63
63
  else
64
64
  sock = TCPSocket.new(param.peerhost, param.peerport)
65
65
  klass = Rex::Socket::Tcp
66
- if (param.ssl)
66
+ if param.ssl
67
67
  klass = Rex::Socket::SslTcp
68
68
  end
69
69
  sock.extend(klass)
70
70
  end
71
71
  when 'udp'
72
- if (param.server?)
72
+ if param.server?
73
73
  sock = UDPServer.new(param.localport, param.localhost)
74
74
  klass = Rex::Socket::UdpServer
75
75
  sock.extend(klass)
@@ -101,7 +101,7 @@ class Rex::Socket::Comm::Local
101
101
  # Configure broadcast support
102
102
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true)
103
103
 
104
- if (param.bare? == false)
104
+ if !param.bare?
105
105
  sock.extend(::Rex::Socket::Ip)
106
106
  sock.initsock(param)
107
107
  end
@@ -117,59 +117,41 @@ class Rex::Socket::Comm::Local
117
117
  #
118
118
  def self.create_by_type(param, type, proto = 0)
119
119
 
120
- # Whether to use IPv6 addressing
121
- usev6 = false
122
-
123
120
  # Detect IPv6 addresses and enable IPv6 accordingly
124
- if ( Rex::Socket.support_ipv6?())
125
-
126
- # Allow the caller to force IPv6
127
- if (param.v6)
128
- usev6 = true
129
- end
130
-
131
- # Force IPv6 mode for non-connected UDP sockets
132
- if (type == ::Socket::SOCK_DGRAM and not param.peerhost)
133
- # FreeBSD allows IPv6 socket creation, but throws an error on sendto()
134
- # Windows 7 SP1 and newer also fail to sendto with IPv6 udp sockets
135
- unless Rex::Compat.is_freebsd or Rex::Compat.is_windows
136
- usev6 = true
137
- end
138
- end
121
+ if Rex::Socket.support_ipv6?
139
122
 
140
123
  local = Rex::Socket.resolv_nbo(param.localhost) if param.localhost
141
124
  peer = Rex::Socket.resolv_nbo(param.peerhost) if param.peerhost
142
125
 
143
- if (local and local.length == 16)
144
- usev6 = true
145
- end
126
+ # Enable IPv6 dual-bind mode for unbound UDP sockets on Linux
127
+ if type == ::Socket::SOCK_DGRAM && Rex::Compat.is_linux && !local && !peer
128
+ param.v6 = true
146
129
 
147
- if (peer and peer.length == 16)
148
- usev6 = true
130
+ # Check if either of the addresses is 16 octets long
131
+ elsif (local && local.length == 16) || (peer && peer.length == 16)
132
+ param.v6 = true
149
133
  end
150
134
 
151
- if (usev6)
152
- if (local and local.length == 4)
153
- if (local == "\x00\x00\x00\x00")
135
+ if param.v6
136
+ if local && local.length == 4
137
+ if local == "\x00\x00\x00\x00"
154
138
  param.localhost = '::'
155
- elsif (local == "\x7f\x00\x00\x01")
139
+ elsif local == "\x7f\x00\x00\x01"
156
140
  param.localhost = '::1'
157
141
  else
158
142
  param.localhost = '::ffff:' + Rex::Socket.getaddress(param.localhost, true)
159
143
  end
160
144
  end
161
145
 
162
- if (peer and peer.length == 4)
163
- if (peer == "\x00\x00\x00\x00")
146
+ if peer && peer.length == 4
147
+ if peer == "\x00\x00\x00\x00"
164
148
  param.peerhost = '::'
165
- elsif (peer == "\x7f\x00\x00\x01")
149
+ elsif peer == "\x7f\x00\x00\x01"
166
150
  param.peerhost = '::1'
167
151
  else
168
152
  param.peerhost = '::ffff:' + Rex::Socket.getaddress(param.peerhost, true)
169
153
  end
170
154
  end
171
-
172
- param.v6 = true
173
155
  end
174
156
  else
175
157
  # No IPv6 support
@@ -181,16 +163,23 @@ class Rex::Socket::Comm::Local
181
163
 
182
164
  # Create the socket
183
165
  sock = nil
184
- if (param.v6)
166
+ if param.v6
185
167
  sock = ::Socket.new(::Socket::AF_INET6, type, proto)
186
168
  else
187
169
  sock = ::Socket.new(::Socket::AF_INET, type, proto)
188
170
  end
189
171
 
190
172
  # Bind to a given local address and/or port if they are supplied
191
- if param.localport or param.localhost
173
+ if param.localport || param.localhost
192
174
  begin
193
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
175
+
176
+ # SO_REUSEADDR has undesired semantics on Windows, intead allowing
177
+ # sockets to be stolen without warning from other unprotected
178
+ # processes.
179
+ unless Rex::Compat.is_windows
180
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
181
+ end
182
+
194
183
  sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport))
195
184
 
196
185
  rescue ::Errno::EADDRNOTAVAIL,::Errno::EADDRINUSE
@@ -200,17 +189,17 @@ class Rex::Socket::Comm::Local
200
189
  end
201
190
 
202
191
  # Configure broadcast support for all datagram sockets
203
- if (type == ::Socket::SOCK_DGRAM)
192
+ if type == ::Socket::SOCK_DGRAM
204
193
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true)
205
194
  end
206
195
 
207
196
  # If a server TCP instance is being created...
208
- if (param.server?)
197
+ if param.server?
209
198
  sock.listen(256)
210
199
 
211
- if (param.bare? == false)
200
+ if !param.bare?
212
201
  klass = Rex::Socket::TcpServer
213
- if (param.ssl)
202
+ if param.ssl
214
203
  klass = Rex::Socket::SslTcpServer
215
204
  end
216
205
  sock.extend(klass)
@@ -222,7 +211,7 @@ class Rex::Socket::Comm::Local
222
211
  chain = []
223
212
 
224
213
  # If we were supplied with host information
225
- if (param.peerhost)
214
+ if param.peerhost
226
215
 
227
216
  # A flag that indicates whether we need to try multiple scopes
228
217
  retry_scopes = false
@@ -310,7 +299,7 @@ class Rex::Socket::Comm::Local
310
299
  end
311
300
  end
312
301
 
313
- if (param.bare? == false)
302
+ if !param.bare?
314
303
  case param.proto
315
304
  when 'tcp'
316
305
  klass = Rex::Socket::Tcp
@@ -422,7 +411,7 @@ class Rex::Socket::Comm::Local
422
411
  when 'http'
423
412
  setup = "CONNECT #{host}:#{port} HTTP/1.0\r\n\r\n"
424
413
  size = sock.put(setup)
425
- if (size != setup.length)
414
+ if size != setup.length
426
415
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller
427
416
  end
428
417
 
@@ -445,7 +434,7 @@ class Rex::Socket::Comm::Local
445
434
  when 'socks4'
446
435
  setup = [4,1,port.to_i].pack('CCn') + Socket.gethostbyname(host)[3] + Rex::Text.rand_text_alpha(rand(8)+1) + "\x00"
447
436
  size = sock.put(setup)
448
- if (size != setup.length)
437
+ if size != setup.length
449
438
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller
450
439
  end
451
440
 
@@ -455,7 +444,7 @@ class Rex::Socket::Comm::Local
455
444
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller
456
445
  end
457
446
 
458
- if (ret.nil? or ret.length < 8)
447
+ if ret.nil? || ret.length < 8
459
448
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a complete response from the proxy"), caller
460
449
  end
461
450
  if ret[1,1] != "\x5a"
@@ -464,18 +453,18 @@ class Rex::Socket::Comm::Local
464
453
  when 'socks5'
465
454
  auth_methods = [5,1,0].pack('CCC')
466
455
  size = sock.put(auth_methods)
467
- if (size != auth_methods.length)
456
+ if size != auth_methods.length
468
457
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller
469
458
  end
470
459
  ret = sock.get_once(2,30)
471
- if (ret[1,1] == "\xff")
460
+ if ret[1,1] == "\xff"
472
461
  raise Rex::ConnectionProxyError.new(host, port, type, "The proxy requires authentication"), caller
473
462
  end
474
463
 
475
- if (Rex::Socket.is_ipv4?(host))
464
+ if Rex::Socket.is_ipv4?(host)
476
465
  addr = Rex::Socket.gethostbyname(host)[3]
477
466
  setup = [5,1,0,1].pack('C4') + addr + [port.to_i].pack('n')
478
- elsif (Rex::Socket.support_ipv6? and Rex::Socket.is_ipv6?(host))
467
+ elsif Rex::Socket.support_ipv6? && Rex::Socket.is_ipv6?(host)
479
468
  # IPv6 stuff all untested
480
469
  addr = Rex::Socket.gethostbyname(host)[3]
481
470
  setup = [5,1,0,4].pack('C4') + addr + [port.to_i].pack('n')
@@ -487,7 +476,7 @@ class Rex::Socket::Comm::Local
487
476
  end
488
477
 
489
478
  size = sock.put(setup)
490
- if (size != setup.length)
479
+ if size != setup.length
491
480
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to send the entire request to the proxy"), caller
492
481
  end
493
482
 
@@ -497,7 +486,7 @@ class Rex::Socket::Comm::Local
497
486
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a response from the proxy"), caller
498
487
  end
499
488
 
500
- if (response.nil? or response.length < 10)
489
+ if response.nil? || response.length < 10
501
490
  raise Rex::ConnectionProxyError.new(host, port, type, "Failed to receive a complete response from the proxy"), caller
502
491
  end
503
492
  if response[1,1] != "\x00"
@@ -513,7 +502,6 @@ class Rex::Socket::Comm::Local
513
502
  # Registration
514
503
  #
515
504
  ##
516
-
517
505
  def self.register_event_handler(handler) # :nodoc:
518
506
  self.instance.register_event_handler(handler)
519
507
  end
@@ -525,5 +513,4 @@ class Rex::Socket::Comm::Local
525
513
  def self.each_event_handler(handler) # :nodoc:
526
514
  self.instance.each_event_handler(handler)
527
515
  end
528
-
529
516
  end
@@ -53,24 +53,6 @@ module Rex::Socket::Tcp
53
53
  end
54
54
  end
55
55
 
56
- #
57
- # Returns peer information (host + port) in host:port format.
58
- #
59
- def peerinfo
60
- if (pi = getpeername_as_array)
61
- return pi[1] + ':' + pi[2].to_s
62
- end
63
- end
64
-
65
- #
66
- # Returns local information (host + port) in host:port format.
67
- #
68
- def localinfo
69
- if (pi = getlocalname)
70
- return pi[1] + ':' + pi[2].to_s
71
- end
72
- end
73
-
74
56
  # returns socket type
75
57
  def type?
76
58
  return 'tcp'
@@ -1,5 +1,5 @@
1
1
  module Rex
2
2
  module Socket
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rex-socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Maloney
@@ -88,7 +88,7 @@ cert_chain:
88
88
  G+Hmcg1v810agasPdoydE0RTVZgEOOMoQ07qu7JFXVWZ9ZQpHT7qJATWL/b2csFG
89
89
  8mVuTXnyJOKRJA==
90
90
  -----END CERTIFICATE-----
91
- date: 2016-11-21 00:00:00.000000000 Z
91
+ date: 2017-01-10 00:00:00.000000000 Z
92
92
  dependencies:
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: bundler
metadata.gz.sig CHANGED
@@ -1,2 +1,3 @@
1
- KN���,�$������vN�����8gN�`��ԶVE�7l�0F��DJ���ل �`L4����G����N0�~,�N]����vZ�~ҁ�0A�<�/� ����ƻ�N�J�?�<
2
- B��<��WS�X`��?�˷������P!������M�PHS��ZZ)�y�����Ec� �r�!���������Q%���FT�=� \��k� ��zm�%􁭪)��N�X[�9[�kY��>��|�Ԡb$.5FR*
1
+ O`�¬ydރ��Z ��2�fT���!����Ay$l� �FI�ӂ;�2jf=ſ7y�B4��' J�����,��׋�^���xϴ4G�x��ވ�',@�A3x}�jv�b��;��gw��V����OMˬ�U
2
+ %����2�$�ȦL
3
+ o���FܾU�~@�o���(^����`�q.=��ezV