Class: Rex::Proto::DNS::Server

Inherits:
Object
  • Object
show all
Includes:
IO::GramServer
Defined in:
lib/rex/proto/dns/server.rb

Defined Under Namespace

Classes: Cache, MockDnsClient

Constant Summary collapse

Packet =
Rex::Proto::DNS::Packet

Instance Attribute Summary collapse

Attributes included from IO::GramServer

#dispatch_request_proc, #listener_thread, #send_response_proc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IO::GramServer

#send_response, #wait

Constructor Details

#initialize(lhost = '0.0.0.0', lport = 53, udp = true, tcp = false, res = nil, comm = nil, ctx = {}, dblock = nil, sblock = nil) ⇒ Server

Returns a new instance of Server.


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rex/proto/dns/server.rb', line 186

def initialize(lhost = '0.0.0.0', lport = 53, udp = true, tcp = false, res = nil, comm = nil, ctx = {}, dblock = nil, sblock = nil)

  @serve_udp = udp
  @serve_tcp = tcp
  @sock_options = {
    'LocalHost' => lhost,
    'LocalPort' => lport,
    'Context'   => ctx,
    'Comm'      => comm
  }
  self.fwd_res = res.nil? ? Rex::Proto::DNS::Resolver.new(:comm => comm, :context => ctx) : res
  self.listener_thread = nil
  self.dispatch_request_proc = dblock
  self.send_response_proc = sblock
  self.cache = Cache.new
  @lock = Mutex.new
end

Instance Attribute Details

#cacheRex::Proto::DNS::Server

Create DNS Server

Parameters:

  • lhost (String)

    Listener address

  • lport (Fixnum)

    Listener port

  • udp (TrueClass, FalseClass)

    Listen on UDP socket

  • tcp (TrueClass, FalseClass)

    Listen on TCP socket

  • res (Rex::Proto::DNS::Resolver)

    Resolver to use, nil to create a fresh one

  • ctx (Hash)

    Framework context for sockets

  • dblock (Proc)

    Handler for :dispatch_request flow control interception

  • sblock (Proc)

    Handler for :send_response flow control interception

Returns:


184
185
186
# File 'lib/rex/proto/dns/server.rb', line 184

def cache
  @cache
end

#fwd_resRex::Proto::DNS::Server

Create DNS Server

Parameters:

  • lhost (String)

    Listener address

  • lport (Fixnum)

    Listener port

  • udp (TrueClass, FalseClass)

    Listen on UDP socket

  • tcp (TrueClass, FalseClass)

    Listen on TCP socket

  • res (Rex::Proto::DNS::Resolver)

    Resolver to use, nil to create a fresh one

  • ctx (Hash)

    Framework context for sockets

  • dblock (Proc)

    Handler for :dispatch_request flow control interception

  • sblock (Proc)

    Handler for :send_response flow control interception

Returns:


184
185
186
# File 'lib/rex/proto/dns/server.rb', line 184

def fwd_res
  @fwd_res
end

#lockObject (readonly)

Returns the value of attribute lock


185
186
187
# File 'lib/rex/proto/dns/server.rb', line 185

def lock
  @lock
end

#serve_tcpRex::Proto::DNS::Server

Create DNS Server

Parameters:

  • lhost (String)

    Listener address

  • lport (Fixnum)

    Listener port

  • udp (TrueClass, FalseClass)

    Listen on UDP socket

  • tcp (TrueClass, FalseClass)

    Listen on TCP socket

  • res (Rex::Proto::DNS::Resolver)

    Resolver to use, nil to create a fresh one

  • ctx (Hash)

    Framework context for sockets

  • dblock (Proc)

    Handler for :dispatch_request flow control interception

  • sblock (Proc)

    Handler for :send_response flow control interception

Returns:


184
185
186
# File 'lib/rex/proto/dns/server.rb', line 184

def serve_tcp
  @serve_tcp
end

#serve_udpRex::Proto::DNS::Server

Create DNS Server

Parameters:

  • lhost (String)

    Listener address

  • lport (Fixnum)

    Listener port

  • udp (TrueClass, FalseClass)

    Listen on UDP socket

  • tcp (TrueClass, FalseClass)

    Listen on TCP socket

  • res (Rex::Proto::DNS::Resolver)

    Resolver to use, nil to create a fresh one

  • ctx (Hash)

    Framework context for sockets

  • dblock (Proc)

    Handler for :dispatch_request flow control interception

  • sblock (Proc)

    Handler for :send_response flow control interception

Returns:


184
185
186
# File 'lib/rex/proto/dns/server.rb', line 184

def serve_udp
  @serve_udp
end

#sock_optionsObject (readonly)

Returns the value of attribute sock_options


185
186
187
# File 'lib/rex/proto/dns/server.rb', line 185

def sock_options
  @sock_options
end

#tcp_sockObject (readonly)

Returns the value of attribute tcp_sock


185
186
187
# File 'lib/rex/proto/dns/server.rb', line 185

def tcp_sock
  @tcp_sock
end

#udp_sockObject (readonly)

Returns the value of attribute udp_sock


185
186
187
# File 'lib/rex/proto/dns/server.rb', line 185

def udp_sock
  @udp_sock
end

Class Method Details

.hardcore_alias(*args) ⇒ Object

Returns the hardcore alias for the DNS service


322
323
324
# File 'lib/rex/proto/dns/server.rb', line 322

def self.hardcore_alias(*args)
  "#{(args[0] || '')}#{(args[1] || '')}"
end

Instance Method Details

#aliasObject

DNS server.


329
330
331
# File 'lib/rex/proto/dns/server.rb', line 329

def alias
  "DNS Server"
end

#default_dispatch_request(cli, data) ⇒ Object

Default DNS request dispatcher, attempts to find response records in cache or forwards request upstream

Parameters:

  • cli (Rex::Socket::Tcp, Rex::Socket::Udp)

    Client sending the request

  • data (String)

    raw DNS request data


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rex/proto/dns/server.rb', line 287

def default_dispatch_request(cli,data)
  return if data.strip.empty?
  req = Packet.encode_drb(data)
  forward = req.dup
  # Find cached items, remove request from forwarded packet
  req.question.each do |ques|
    cached = self.cache.find(ques.qname, ques.qtype.to_s)
    if cached.empty?
      next
    else
      req.answer = req.answer + cached
      forward.question.delete(ques)
    end
  end
  # Forward remaining requests, cache responses
  if forward.question.count > 0 and @fwd_res
    forwarded = self.fwd_res.send(validate_packet(forward))
    req.answer = req.answer + forwarded.answer
    forwarded.answer.each do |ans|
      self.cache.cache_record(ans)
    end
    req.header.ra = true # Set recursion bit
  end
  # Finalize answers in response
  # Check for empty response prior to sending
  if req.answer.size < 1
    req.header.rCode = Dnsruby::RCode::NOERROR
  end
  req.header.qr = true # Set response bit
  send_response(cli, validate_packet(req).data)
end

#dispatch_request(cli, data) ⇒ Object

Process client request, handled with dispatch_request_proc if set

Parameters:

  • cli (Rex::Socket::Tcp, Rex::Socket::Udp)

    Client sending the request

  • data (String)

    raw DNS request data


273
274
275
276
277
278
279
# File 'lib/rex/proto/dns/server.rb', line 273

def dispatch_request(cli, data)
  if self.dispatch_request_proc
    self.dispatch_request_proc.call(cli,data)
  else
    default_dispatch_request(cli,data)
  end
end

#monitor_listenerObject (protected)

This method monitors the listener socket for new connections and calls the on_client_connect callback routine.


339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rex/proto/dns/server.rb', line 339

def monitor_listener
  while true
    rds = [self.udp_sock]
    wds = []
    eds = [self.udp_sock]

    r,_,_ = ::IO.select(rds,wds,eds,1)

    if (r != nil and r[0] == self.udp_sock)
      buf,host,port = self.udp_sock.recvfrom(65535)
      # Mock up a client object for sending back data
      cli = MockDnsClient.new(host, port, r[0])
      dispatch_request(cli, buf)
    end
  end
end

#on_client_data(cli) ⇒ Object (protected)

Processes request coming from client

Parameters:

  • cli (Rex::Socket::Tcp)

    Client sending request


360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/rex/proto/dns/server.rb', line 360

def on_client_data(cli)
  begin
    data = cli.read(65535)

    raise ::EOFError if not data
    raise ::EOFError if data.empty?
    dispatch_request(cli, data)
  rescue EOFError => e
    self.tcp_socket.close_client(cli) if cli
    raise e
  end
end

#running?Boolean

Check if server is running

Returns:

  • (Boolean)

220
221
222
# File 'lib/rex/proto/dns/server.rb', line 220

def running?
  self.listener_thread and self.listener_thread.alive?
end

#start(start_cache = true) ⇒ Object

Start the DNS server and cache

Parameters:

  • start_cache (TrueClass, FalseClass) (defaults to: true)

    stop the cache


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rex/proto/dns/server.rb', line 227

def start(start_cache = true)

  if self.serve_udp
    @udp_sock = Rex::Socket::Udp.create(self.sock_options)
    self.listener_thread = Rex::ThreadFactory.spawn("UDPDNSServerListener", false) {
      monitor_listener
    }
  end

  if self.serve_tcp
    @tcp_sock = Rex::Socket::TcpServer.create(self.sock_options)
    self.tcp_sock.on_client_data_proc = Proc.new { |cli|
      on_client_data(cli)
    }
    self.tcp_sock.start
    if !self.serve_udp
      self.listener_thread = tcp_sock.listener_thread
    end
  end

  self.cache.start if start_cache
end

#stop(flush_cache = false) ⇒ Object

Stop the DNS server and cache

Parameters:

  • flush_cache (TrueClass, FalseClass) (defaults to: false)

    Flush eDNS cache on stop


254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rex/proto/dns/server.rb', line 254

def stop(flush_cache = false)
  ensure_close = [self.udp_sock, self.tcp_sock].compact
  begin
    self.listener_thread.kill if self.listener_thread.respond_to?(:kill)
    self.listener_thread = nil
  ensure
    while csock = ensure_close.shift
      csock.stop if csock.respond_to?(:stop)
      csock.close unless csock.respond_to?(:close) and csock.closed?
    end
  end
  self.cache.stop(flush_cache)
end

#switchns(ns = []) ⇒ Object

Switch DNS forwarders in resolver with thread safety

Parameters:

  • ns (Array, String) (defaults to: [])

    List of (or single) nameservers to use


208
209
210
211
212
213
214
215
# File 'lib/rex/proto/dns/server.rb', line 208

def switchns(ns = [])
  if ns.respond_to?(:split)
    ns = [ns]
  end
  self.lock.synchronize do
    self.fwd_res.nameserver = ns
  end
end