rex-socket 0.1.51 → 0.1.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c8f90acc30e263d824f20d2b1286a5520a483945985f3f229a58bfcf5b9cde1
4
- data.tar.gz: 41bd0a0c06187b1be6a0b7672b3efd36d100b74ced4dfd9709aeca71cc7c91cc
3
+ metadata.gz: f4e2765900a58243437c2588d16bc2c3fcd65b58f042707a1ebca28d2240786d
4
+ data.tar.gz: b7e46d8ab59b275872e48c418de39f07c615a2fdc480ad7ce0a8cf033eb5297f
5
5
  SHA512:
6
- metadata.gz: dd89e2260a36ebc22e7eb69a1035078f8aceb96c664dde36f7849a76020f8cf8fd276686cc01e857b5a203a59624b34818271dcdd5c01a6789475cf22cadccf1
7
- data.tar.gz: d2e464106473a60359fa268bbe00b1eafac147c9c35b2ef82503824534c5c85fa223bd17decf0dd05d29daa5c36101398ab9a99e8b6afb4000ab01e249262a77
6
+ metadata.gz: 87cdca34534ff3ba9c085659200b40f71802ea51bc023e82bd918df19c3e5b70b2f9132dd954d121655d21a90e2a32bbea0fbed48543877f22ca404cac4d3ee9
7
+ data.tar.gz: 3bb326d55b4b6c3f6741df0bcf6073b329bba6934e5a5ac45cae1a6716b53301f1d8b3bebc80d40de0ed1c55a67e5c6fc37846fe5eb9524f294d373b6fc984c8
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,5 +1,5 @@
1
1
  module Rex
2
2
  module Socket
3
- VERSION = "0.1.51"
3
+ VERSION = "0.1.53"
4
4
  end
5
5
  end
data/lib/rex/socket.rb CHANGED
@@ -122,6 +122,9 @@ module Socket
122
122
  end
123
123
 
124
124
  #
125
+ # Cache our resolver
126
+ @@resolver = nil
127
+
125
128
  # Determine whether this is an IPv4 address
126
129
  #
127
130
  def self.is_ipv4?(addr)
@@ -157,7 +160,33 @@ module Socket
157
160
  (support_ipv6? && addr =~ MATCH_IPV6) || (addr =~ MATCH_IPV4)
158
161
  end
159
162
 
163
+ # Checks to see if an address is an IPv6 address and if so, converts it into its
164
+ # square bracket format for addressing as noted in RFC 6874 which states that an IPv6
165
+ # address literal in a URL is always embedded between [ and ]. Please also refer to
166
+ # RFC5952, RFC3986, and RFC6874 for more info.
167
+ #
168
+ # RFC3986 section 3.2.2 specifically notes "A host identified by an Internet Protocol literal address, version 6
169
+ # [RFC3513] or later, is distinguished by enclosing the IP literal
170
+ # within square brackets ("[" and "]"). This is the only place where
171
+ # square bracket characters are allowed in the URI syntax."
172
+ #
173
+ # RFC6874 reinforces this in section 2 where it notes "In a URI, a literal IPv6 address
174
+ # is always embedded between '[' and ']'".
160
175
  #
176
+ # @param host [String] IP address or hostname to convert to a URI authority.
177
+ # @param port [Integer] Port number to include within the URI authority.
178
+ # @return [String] Returns the URI authority string.
179
+ # @raise [ArgumentError] This function will raise an ArgumentError if the host parameter is not a String.
180
+ def self.to_authority(host, port=nil)
181
+ unless host.kind_of?(String)
182
+ raise ArgumentError.new("Expected a string for the host parameter!")
183
+ end
184
+ host = "[#{host}]" if is_ipv6?(host)
185
+ host += ":#{port}" if port
186
+ host
187
+ end
188
+
189
+
161
190
  # Return true if +addr+ is within the ranges specified in RFC1918, or
162
191
  # RFC5735/RFC3927
163
192
  #
@@ -190,7 +219,11 @@ module Socket
190
219
  return [hostname]
191
220
  end
192
221
 
193
- res = ::Addrinfo.getaddrinfo(hostname, 0, ::Socket::AF_UNSPEC, ::Socket::SOCK_STREAM)
222
+ if @@resolver
223
+ res = self.rex_getaddrinfo(hostname)
224
+ else
225
+ res = ::Addrinfo.getaddrinfo(hostname, 0, ::Socket::AF_UNSPEC, ::Socket::SOCK_STREAM)
226
+ end
194
227
 
195
228
  res.map! do |address_info|
196
229
  address_info.ip_address
@@ -222,7 +255,7 @@ module Socket
222
255
  host, _ = host.split('%', 2)
223
256
  end
224
257
 
225
- ::Socket.gethostbyname(host)
258
+ @@resolver ? self.rex_gethostbyname(host) : ::Socket.gethostbyname(host)
226
259
  end
227
260
 
228
261
  #
@@ -693,6 +726,15 @@ module Socket
693
726
  return [lsock, rsock]
694
727
  end
695
728
 
729
+ #
730
+ # Install Rex::Proto::DNS::CachedResolver, or similar, to pivot DNS
731
+ #
732
+ # @param res [Rex::Proto::DNS::CachedResolver] Resolver object to handle DNS requests
733
+ # @return [Rex::Proto::DNS::CachedResolver] The installed resolver
734
+ def self._install_global_resolver(res)
735
+ @@resolver = res
736
+ end
737
+
696
738
 
697
739
  ##
698
740
  #
@@ -818,6 +860,89 @@ protected
818
860
  attr_writer :context # :nodoc:
819
861
  attr_writer :ipv # :nodoc:
820
862
 
863
+ #
864
+ # @param name [String] The hostname to lookup via the resolver
865
+ # @param resolver [Rex::Proto::DNS::CachedResolver] Resolver to query for the name
866
+ # @return [Array] Array mimicking the native gethostbyname return type
867
+ def self.rex_gethostbyname(name, resolver: @@resolver)
868
+ v4, v6 = self.rex_resolve_hostname(name, resolver: resolver)
869
+ # Build response array
870
+ hostbyname = [name, []]
871
+ unless v4.empty?
872
+ hostbyname << ::Socket::AF_INET
873
+ hostbyname += v4.map(&:address).map(&:address)
874
+ hostbyname << v6[0].address.address unless v6.empty?
875
+ else
876
+ hostbyname << ::Socket::AF_INET6
877
+ hostbyname += v6.map(&:address).map(&:address)
878
+ end
879
+ return hostbyname
880
+ end
881
+
882
+ #
883
+ # @param name [String] The hostname to lookup via the resolver
884
+ # @param resolver [Rex::Proto::DNS::CachedResolver] Resolver to query for the name
885
+ # @return [Array] Array mimicking the native getaddrinfo return type
886
+ def self.rex_getaddrinfo(name, resolver: @@resolver)
887
+ v4, v6 = self.rex_resolve_hostname(name, resolver: resolver)
888
+ # Build response array
889
+ getaddrinfo = []
890
+ v4.each do |a4|
891
+ getaddrinfo << Addrinfo.new(
892
+ self.to_sockaddr(a4.address.to_s, 0),
893
+ ::Socket::AF_INET,
894
+ ::Socket::SOCK_STREAM,
895
+ ::Socket::IPPROTO_TCP,
896
+ ) unless v4.empty?
897
+ end
898
+ v6.each do |a6|
899
+ getaddrinfo << Addrinfo.new(
900
+ self.to_sockaddr(a6.address.to_s, 0),
901
+ ::Socket::AF_INET6,
902
+ ::Socket::SOCK_STREAM,
903
+ ::Socket::IPPROTO_TCP,
904
+ ) unless v6.empty?
905
+ end
906
+ return getaddrinfo
907
+ end
908
+
909
+
910
+ # @param name [String] The hostname to lookup via the resolver
911
+ # @param resolver [Rex::Proto::DNS::CachedResolver] Resolver to query for the name
912
+ # @return [Array] Array of Dnsruby::Message responses for consumers to reformat
913
+ def self.rex_resolve_hostname(name, resolver: @@resolver)
914
+ raise ::SocketError.new(
915
+ "Rex::Socket internal DNS resolution requires passing/setting a resolver"
916
+ ) unless resolver
917
+ raise ::SocketError.new(
918
+ "Rex::Socket internal DNS resolution requires passing a String name to resolve"
919
+ ) unless name.is_a?(String)
920
+ # Pull both record types
921
+ v4 = begin
922
+ resolver.send(name, ::Net::DNS::A).answer.select do |a|
923
+ a.type == Dnsruby::Types::A
924
+ end.sort_by do |a|
925
+ self.addr_ntoi(a.address.address)
926
+ end
927
+ rescue
928
+ []
929
+ end
930
+ v6 = begin
931
+ resolver.send(name, ::Net::DNS::AAAA).answer.select do |a|
932
+ a.type == Dnsruby::Types::AAAA
933
+ end.sort_by do |a|
934
+ self.addr_ntoi(a.address.address)
935
+ end
936
+ rescue
937
+ []
938
+ end
939
+ # Emulate ::Socket's error if no responses found
940
+ if v4.empty? and v6.empty?
941
+ raise ::SocketError.new('getaddrinfo: Name or service not known')
942
+ end
943
+ # Ensure response types (depending on underlying library used) provide required methods
944
+ return v4, v6
945
+ end
821
946
  end
822
947
 
823
948
  end
data.tar.gz.sig CHANGED
Binary file
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.51
4
+ version: 0.1.53
5
5
  platform: ruby
6
6
  authors:
7
7
  - Metasploit Hackers
@@ -93,7 +93,7 @@ cert_chain:
93
93
  EknWpNgVhohbot1lfVAMmIhdtOVaRVcQQixWPwprDj/ydB8ryDMDosIMcw+fkoXU
94
94
  9GJsSaSRRYQ9UUkVL27b64okU8D48m8=
95
95
  -----END CERTIFICATE-----
96
- date: 2023-05-17 00:00:00.000000000 Z
96
+ date: 2023-09-05 00:00:00.000000000 Z
97
97
  dependencies:
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: rake
metadata.gz.sig CHANGED
Binary file