Advertisement
mage_1868

Untitled

Aug 18th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.12 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI;
  4. use Digest::MD5 qw(md5_hex);
  5.  
  6. $cgi = new CGI;
  7. $SESSDIR = "/tmp/";
  8. $sessfile = $cgi->cookie("diagsess");
  9. $arg0 = $cgi->param("arg");
  10. $action = $cgi->param("action");
  11. $arg = &safestr($arg0);
  12.  
  13. if (! defined($sessfile) )
  14. {
  15.     if ( md5_hex($cgi->param("sechash")) =~ /^000000000000.*$/)
  16.     {
  17.         $sesshash{'user'} = 'admin';
  18.     }
  19.     else
  20.     {
  21.         $sesshash{'user'}  = 'guest';
  22.     }
  23.     $sesshash{'ip'} = &get_ip;
  24.  
  25.     $diagsess = md5_hex( $sesshash{'user'} . '|||' . $sesshash{'ip'} );
  26.     $cookie = "diagsess=$diagsess;";
  27.     &write_session;
  28.     print $cgi->header(-cookie => $cookie,
  29.             -expires => 'Mon, 01 Jan 1999 00:00:00 GMT',
  30.             -'cache-control' => 'no-cache',
  31.             -pragma => 'no-cache',-'location'=> 'dana-na.cgi?sechash=' );
  32.  
  33.     exit 0;
  34. }
  35. else
  36. {
  37.     print $cgi->header();
  38.     &read_session;
  39.     &print_menu;
  40. }
  41. if (defined ($action) && length($action)>0)
  42. {
  43.     if ($action =~ /^print_session$/)
  44.     {
  45.         &print_session;
  46.         exit 0;
  47.     }
  48.     if ($action =~ /^curl$/)
  49.     {
  50.         &curl($arg);
  51.         exit 0;
  52.     }
  53.     if ($action =~ /^ping$/ )
  54.     {
  55.         &ping($arg);
  56.         exit 0;
  57.     }
  58.     if ($action =~ /^traceroute$/)
  59.     {
  60.         &traceroute ($arg);
  61.         exit 0;
  62.     }
  63.     if ($action =~ /^shell$/)
  64.     {
  65.         &shell($arg);
  66.         exit 0;
  67.     }
  68.  
  69. }
  70.  
  71. sub curl
  72. {
  73.     $host = shift;
  74.     print "<pre><textarea rows=24 cols=80>";
  75.     if (defined($host) && length($host)>1)
  76.     {
  77.         open(GG,"/usr/bin/curl -s $host |") and do
  78.         {
  79.             while(<GG>)
  80.             {
  81.                 print;
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. sub ping
  88. {
  89.     my $host = shift;
  90.     print "<pre>";
  91.     if(defined($host) && length($host)>1)
  92.     {
  93.         open(GG,"/bin/ping -c3 $host |") and do
  94.         {
  95.             while(<GG>)
  96.             {
  97.  
  98.                 print;
  99.             }
  100.  
  101.         };
  102.         close GG;
  103.  
  104.     }
  105. }
  106.  
  107. sub traceroute
  108. {
  109.     my $host = shift;
  110.     print "<pre>";
  111.     if(defined($host) && length($host)>1)
  112.     {
  113.         open(GG,"/usr/sbin/traceroute -d -n -w 5 $host |") and do
  114.         {
  115.             while(<GG>)
  116.             {
  117.                 print;
  118.             }
  119.  
  120.         };
  121.         close GG;
  122.     }
  123. }
  124.  
  125. sub read_session
  126. {
  127.     undef %sesshash;
  128.     if(! -f "$SESSDIR/$sessfile")
  129.     {
  130.         print "session error!";
  131.         return;
  132.     }
  133.     open(GG, "$SESSDIR/$sessfile") and do {
  134.         while (<GG>) {
  135.             eval($_);
  136.         }
  137.         close GG;
  138.     };
  139. }
  140.  
  141. sub write_session
  142. {
  143.     open(GG, ">$SESSDIR/$diagsess") and do
  144.     {
  145.         foreach (sort keys %sesshash)
  146.         {
  147.             print GG "\$sesshash{'$_'} = '$sesshash{$_}';\n";
  148.         }
  149.     };
  150.     close GG;
  151. }
  152.  
  153. sub print_session
  154. {
  155.     foreach (sort keys %sesshash) {
  156.         print "$_=$sesshash{$_}\n";
  157.     }
  158. }
  159.  
  160. sub shell
  161. {
  162.     $cmd = shift;
  163.     print "<pre>";
  164.     if (  $sesshash{'user'} eq 'admin' )
  165.     {
  166.         open(GG, "$cmd |") and do
  167.         {
  168.             print;
  169.         };
  170.     }
  171.     else
  172.     {
  173.         print "sorry $sesshash{'user'}! you're not admin!\n";
  174.  
  175.     }
  176. }
  177.  
  178. sub print_menu
  179. {
  180.     $arg0 =~ s/\</\<\;/g;
  181.     open(GG,"cat menu.html |") and do
  182.     {
  183.         while(<GG>)
  184.         {
  185.             $_ =~ s/\%\%arg\%\%/$arg0/g;   
  186.             print $_;
  187.         }
  188.         close GG;
  189.     };
  190. }
  191.  
  192. sub get_ip
  193. {
  194.     $h1 = $ENV{'REMOTE_ADDR'};
  195.     $h2 = $ENV{'HTTP_CLIENT_IP'};
  196.     $h3 = $ENV{'HTTP_X_FORWARDED_FOR'};
  197.  
  198.     if (length($h3)>0)
  199.     {
  200.         return $h3;
  201.     }
  202.     elsif (length($h2)>0)
  203.     {
  204.         return $h2;
  205.     }
  206.     else
  207.     {
  208.         return $h1;
  209.     }
  210.     return "UNKNOWN";
  211. }
  212.  
  213. sub safestr
  214. {
  215.     my $str = shift;
  216.  
  217.     $str =~  s/([;<>\*\|`&\$!#\(\)\[\]\{\}:'"])/\\$1/g;;
  218.     return $str;
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement