<commit_before>##
# dnscat2_dns.rb
# Created March, 2013
# By <NAME>
#
# See: LICENSE.txt
#
# The DNS dnscat server.
##
$LOAD_PATH << File.dirname(__FILE__) # A hack to make this work on 1.8/1.9
$LOAD_PATH << File.dirname(__FILE__) + '/rubydns/lib'

#require 'rubydns/lib/rubydns'
require 'rubydns'

require 'dnscat2_server'
require 'log'



class DnscatDNS
  IN = Resolv::DNS::Resource::IN

  def max_packet_size()
    return 32 # TODO: do this better
  end

  def initialize(domain)
    @domain = domain
  end

  # I have to re-implement RubyDNS::start_server() in order to disable the
  # annoying logging (I'd love to have a better way!)
  def start_dns_server(options = {}, &block)
    server = RubyDNS::Server.new(&block)
    server.logger.level = Logger::FATAL
    server.logger.info "Starting RubyDNS server (v#{RubyDNS::VERSION})..."

    options[:listen] ||= [[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]]

    EventMachine.run do
      server.fire(:setup)

      # Setup server sockets
      options[:listen].each do |spec|
        server.logger.info "Listening on #{spec.join(':')}"
        if spec[0] == :udp
          EventMachine.open_datagram_socket(spec[1], spec[2], RubyDNS::UDPHandler, server)
        elsif spec[0] == :tcp
          EventMachine.start_server(spec[1], spec[2], RubyDNS::TCPHandler, server)
        end
      end

      server.fire(:start)
    end

    server.fire(:stop)
  end

  def recv()
    domain = @domain

    start_dns_server() do
      match(/\.#{Regexp.escape(domain)}$/, IN::TXT) do |transaction|
puts("Received: #{transaction.name}")
        name = transaction.name.gsub(/\.#{Regexp.escape(domain)}$/, '')
        name = name.gsub(/\./, '')
<<<<<<< HEAD
#puts("Received: #{name}")
        name = [name].pack("H*")
        response = yield(name)
#puts("Sending:  #{response.unpack("H*")}")
=======
        name = [name].pack("H*")
        response = yield(name)
>>>>>>> Started adding commandline options to dnscat_server.rb
        if(response.nil?)
          puts("Sending nil response...")
          response = domain
        else
          response = "#{response.unpack("H*").pop}.#{domain}"
        end
puts("Sending:  #{response}")
        transaction.respond!(response)<commit_msg>Fixed a conflict that I accidentally committed<commit_after>##
# dnscat2_dns.rb
# Created March, 2013
# By <NAME>
#
# See: LICENSE.txt
#
# The DNS dnscat server.
##
$LOAD_PATH << File.dirname(__FILE__) # A hack to make this work on 1.8/1.9
$LOAD_PATH << File.dirname(__FILE__) + '/rubydns/lib'

#require 'rubydns/lib/rubydns'
require 'rubydns'

require 'dnscat2_server'
require 'log'



class DnscatDNS
  IN = Resolv::DNS::Resource::IN

  def max_packet_size()
    return 32 # TODO: do this better
  end

  def initialize(domain)
    @domain = domain
  end

  # I have to re-implement RubyDNS::start_server() in order to disable the
  # annoying logging (I'd love to have a better way!)
  def start_dns_server(options = {}, &block)
    server = RubyDNS::Server.new(&block)
    server.logger.level = Logger::FATAL
    server.logger.info "Starting RubyDNS server (v#{RubyDNS::VERSION})..."

    options[:listen] ||= [[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]]

    EventMachine.run do
      server.fire(:setup)

      # Setup server sockets
      options[:listen].each do |spec|
        server.logger.info "Listening on #{spec.join(':')}"
        if spec[0] == :udp
          EventMachine.open_datagram_socket(spec[1], spec[2], RubyDNS::UDPHandler, server)
        elsif spec[0] == :tcp
          EventMachine.start_server(spec[1], spec[2], RubyDNS::TCPHandler, server)
        end
      end

      server.fire(:start)
    end

    server.fire(:stop)
  end

  def recv()
    domain = @domain

    start_dns_server() do
      match(/\.#{Regexp.escape(domain)}$/, IN::TXT) do |transaction|
puts("Received: #{transaction.name}")
        name = transaction.name.gsub(/\.#{Regexp.escape(domain)}$/, '')
        name = name.gsub(/\./, '')
        name = [name].pack("H*")
        response = yield(name)
        if(response.nil?)
          puts("Sending nil response...")
          response = domain
        else
          response = "#{response.unpack("H*").pop}.#{domain}"
        end
puts("Sending:  #{response}")
        transaction.respond!(response)
{'fraction_non_alphanumeric': 0.16479925303454715, 'fraction_numerical': 0.014005602240896359, 'mean_word_length': 2.978644382544104, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6162138', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1463, 'n_words': 332}