PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/isrcore/dnsserver.pm

http://isr-evilgrade.googlecode.com/
Perl | 190 lines | 138 code | 14 blank | 38 comment | 3 complexity | ac2103ab75f84653608acd4d83bba6be MD5 | raw file
Possible License(s): GPL-2.0
  1. ###############
  2. # dnsserver.pm
  3. #
  4. # Copyright 2010 Francisco Amato
  5. #
  6. # This file is part of isr-evilgrade, www.infobytesec.com .
  7. #
  8. # isr-evilgrade is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation version 2 of the License.
  11. #
  12. # isr-evilgrade is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with isr-evilgrade; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. # '''
  22. ##
  23. package isrcore::dnsserver;
  24. use strict;
  25. #external modules
  26. use IO::Socket;
  27. use isrcore::utils;
  28. use POSIX ":sys_wait_h";
  29. use Data::Dump qw(dump);
  30. use FindBin;
  31. use lib "$FindBin::Bin";
  32. use IO::Socket;
  33. use Sys::Hostname;
  34. use isrcore::Stanford::DNS;
  35. use isrcore::Stanford::DNSserver;
  36. $SIG{INT} = sub { die "$$ dying\n" };
  37. sub catch_zap {
  38. my $signame = shift;
  39. return 1;
  40. }
  41. $SIG{HUP} = \&catch_zap; # best strategy
  42. my $base=
  43. {
  44. 'port' => 53,
  45. 'whoami' => "DNSSERVER",
  46. 'error' => "",
  47. 'enable' => 0,
  48. 'resolve_to' => "127.0.0.1",
  49. 'domains' => (),
  50. };
  51. ##########################################################################
  52. # FUNCTION new
  53. # RECEIVES
  54. # RETURNS
  55. # EXPECTS
  56. # DOES class's constructor
  57. sub new {
  58. my $class = shift;
  59. my $self = {'Base' => $base, @_ };
  60. return bless $self, $class;
  61. }
  62. ##########################################################################
  63. # FUNCTION start
  64. # RECEIVES [shellzobj]
  65. # RETURNS
  66. # EXPECTS
  67. # DOES start webserver
  68. sub start {
  69. my $self = shift;
  70. my $shellz = shift;
  71. #ignore child process avoid zombies.
  72. $SIG{CHLD} = 'IGNORE';
  73. #create socket
  74. if ( $self->{'Base'}->{'enable'} == 0 ){
  75. return;
  76. }
  77. my $nserver = new isrcore::Stanford::DNSserver (
  78. listen_on => ["0.0.0.0"],
  79. port => $self->{'Base'}->{'port'},
  80. daemon => "no",
  81. logfunc => sub { $shellz->printshell("[$self->{'Base'}->{'whoami'}] - $_[0]\n",1) },
  82. debug => 1,
  83. # loopfunc => sub { $shellz->printshell("[$self->{'Base'}->{'whoami'}] - ".dump(@_)."DNS Server Ready. Waiting for Connections\n"); },
  84. );
  85. if( !$nserver )
  86. {
  87. $self->{'Base'}->{'error'} = "[$self->{'Base'}->{'whoami'}] - Cant't create a listening socket: $@";
  88. return;
  89. }else{
  90. $shellz->printshell("[$self->{'Base'}->{'whoami'}] - DNS Server Ready. Waiting for Connections ...\n");
  91. }
  92. my $resolve_ip = unpack('N',inet_aton($self->{'Base'}->{'resolve_to'}) );
  93. my $vhost;
  94. foreach $vhost ( @{$self->{'Base'}->{'domains'}} ){
  95. $nserver->add_static($vhost, T_A, rr_A($resolve_ip));
  96. $nserver->add_static($vhost, T_AAAA, rr_A($resolve_ip));
  97. }
  98. while(1) {
  99. if( $nserver->answer_queries() == 0 ){
  100. $self->{'Base'}->{'error'} = "[$self->{'Base'}->{'whoami'}] - Error Initiating DNS Server";
  101. return 0;
  102. }
  103. }
  104. }
  105. ##########################################################################
  106. # FUNCTION loadconfig
  107. # RECEIVES
  108. # RETURNS
  109. # EXPECTS
  110. # DOES load dns server configuration
  111. sub loadconfig{
  112. my $self=shift;
  113. my $config=shift;
  114. my $vhosts;
  115. my @domains = ();
  116. $self->{'Base'}->{'port'}=$config->{'Base'}->{'options'}->{'DNSPort'}->{'val'};
  117. $self->{'Base'}->{'enable'}=$config->{'Base'}->{'options'}->{'DNSEnable'}->{'val'};
  118. $self->{'Base'}->{'resolve_to'}=$config->{'Base'}->{'options'}->{'DNSAnswerIp'}->{'val'};
  119. # load VHOSTS
  120. foreach my $name (keys %{$config->{'modules'}}){
  121. my $module = $config->{'modules'}->{$name};
  122. if ($module->{'Base'}->{'options'}->{'enable'}->{'val'} == 1) {
  123. $vhosts = $module->{'Base'}->{'vh'};
  124. $vhosts =~ s/\(|\)//g;
  125. push(@domains,split(/\|/,$vhosts));
  126. }
  127. }
  128. $self->{'Base'}->{'domains'} = \@domains;
  129. return 1;
  130. }
  131. ##########################################################################
  132. # FUNCTION stop
  133. # RECEIVES
  134. # RETURNS
  135. # EXPECTS
  136. # DOES stop dns server
  137. sub stop{
  138. my $self=shift;
  139. # hup x kill
  140. kill KILL => $self->{'Base'}->{'child'};
  141. $self->{'Base'}->{'child'}=0;
  142. return;
  143. }
  144. ##########################################################################
  145. # FUNCTION status
  146. # RECEIVES
  147. # RETURNS
  148. # EXPECTS
  149. # DOES dns status
  150. sub status{
  151. my $self = shift;
  152. if ($self->{'Base'}->{'child'} && waitpid($self->{'Base'}->{'child'},WNOHANG) != -1){
  153. return 1;
  154. } else {
  155. $self->{'Base'}->{'child'}=0;
  156. return 0;
  157. }
  158. }
  159. 1;