Module: Aikido::Zen::Sinks::Socket::Helpers

Defined in:
lib/aikido/zen/sinks/socket.rb

Class Method Summary collapse

Class Method Details

.scan(hostname, socket, operation) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aikido/zen/sinks/socket.rb', line 19

def self.scan(hostname, socket, operation)
  # We're patching IPSocket.open(..) method.
  # The IPSocket class hierarchy is:
  #             IPSocket
  #            /        \
  #       TCPSocket    UDPSocket
  #       /       \
  # TCPServer     SOCKSSocket
  #
  # Because we want to scan only HTTP requests, we skip in case the
  # socket is not *exactly* an instance of TCPSocket — it's any
  # of it subclasses
  return unless socket.instance_of?(TCPSocket)

  # ["AF_INET", 80, "10.0.0.1", "10.0.0.1"]
  address_family, _port, _hostname, numeric_address = socket.peeraddr(:numeric)

  # We only care about IPv4 (AF_INET) or IPv6 (AF_INET6) sockets
  # This might be overcautious, since this is _IP_Socket, so you
  # would expect it's only used for IP connections?

  # Code coverage is disabled here because the then clause is a no-op,
  # so there is nothing to cover.
  # :nocov:
  return unless address_family.start_with?("AF_INET")
  # :nocov:

  context = Aikido::Zen.current_context
  if context
    context["dns.lookups"] ||= Scanners::SSRF::DNSLookups.new
    context["dns.lookups"].add(hostname, numeric_address)
  end

  SINK.scan(
    hostname: hostname,
    addresses: [numeric_address],
    request: context && context["ssrf.request"],
    operation: operation
  )
end