-download-manager-security-patch-

Gesponsorde koppelingen

PHP script bestanden

  1. -download-manager-security-patch-

« Lees de omschrijving en reacties

Het originele bestand heet download.php, voor het gemak heb ik deze hieronder 'files.php' genoemd.

Vaak gebruik ik bij voorkeur endif; en endwhile; i.p.v.
accolades. Dat zie je dit in onderstaand script terugkomen.

Je kan het natuurlijk zelf veranderen in accolades... de werking
blijft gewoon hetzelfde ;-)

Gewijzigd:
* Ik was vergeten om 'code' te gebruiken bij het eerste script...
* De omschrijving aangepast
* nieuwe routine om misbruik door te voorkomen .
* tevens wordt de bestandsgrootte met elkaar vergeleken als
extra beveiliging.



---- files.php (verniewd!!!)--------------------------------------------------------

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php    
# Download manager, onderdeel van QCMS
# http://qcms.heathernova.us/
#
#
# Auteur : Cynthia Fridsma


error_reporting(E_ALL);

if (isset($_GET['new_url'])):
    $new_url = $_GET['new_url'];
    else:
    $new_url = "";
endif;




// Dit is het path met de downloads,

$path="downloads/";

// open de directory (deze moet natuurlijk wel aanwezig zijn)!
$dh = opendir($path);
echo $new_url . "<br>";
echo ("<table border='0'><tr>\n");

// Dit is de eerste keer dat het programma gebruikt wordt
if ($new_url ==""):

while ((@$file = readdir($dh)) !== false) :
   @
$file = trim($file);
   @
$test=strlen($file);      
   @
$total = $path . $file;
   @
$bytes = filesize($total);          

// Hebben we te maken met een directory of met een file?
   if(is_dir($total)):
      $type="dir";      
   else :
     $type="file";
   endif;

  
  
// Indien het een directory betreft:
   if ( $type == "dir" & $file != "..")  :  
   ?>
<td width="25" height="25">Dir </td>
   <td width="100%"><a href="files.php?new_url=<?php echo $path. "" . $file; ?>/" target="_self"><?php echo $file;?></a>
   open this directory</td></tr>
   <?php      
   endif;  

// indien het een file betreft :    
   if ( $type != "dir" )  :          
   ?>

   <td width="25" height="25">File </td>
   <td width="100%"><a title="open or view <?php echo $file; ?> <?php echo $type; ?>" href="get.php?start=<?php echo $path ; ?>&file_name=<?php echo $file; ?>&file_size=<?php echo $bytes;?>" >
   <?php echo $file; ?></a> <?php echo $bytes; ?>
    bytes</td></tr>
    <?php        
   endif;
endwhile;


closedir($dh);
echo ("</table>");
endif;


// Gebruiker wil in een sub directory kijken....
if ($new_url !=""):

// beveilings patch


# het download path moet terugkomen in
# new_url, indien niet dan probeert gebruiker verder te
# kijken, hetgeen we natuurlijk niet willen...

# Dit lossen we op met de functie ereg.

$patt = $path;

if(ereg($patt,$new_url)) :
// Het is inorde, we vervolgen de code  

    // voorkom dat de gebruiker via een omweg inbreekt,
    // dit doen we met str_replace

    $new_url=str_replace("../", "", $new_url);
    $new_url=str_replace("./", "", $new_url);
    $new_url=str_replace("/..", "", $new_url);
    $new_url=str_replace("/../", "", $new_url);
    $new_url=str_replace(".", "", $new_url);
    $new_url=str_replace(chr(92), "", $new_url);
    $new_url=str_replace("?", "", $new_url);

    // test of gebruiker een slash voor de path plaatst om
    // in te breken.


    $test = "/" . $path;

    if (ereg($test, $new_url)):
    // Vervang de slash voor het path....
        $new_url=str_replace($test, $path, $new_url);
    endif;

    
      // test of de directory bestaat

    if (is_dir($_GET['new_url'])):
        @
$path=$new_url;
        @
$dh = opendir($path);
        echo ("<table border=0><tr>\n");

         while ((@$file = readdir($dh)) !== false) :
                 @
$file = trim($file);
                 @
$test=strlen($file);      
                 @
$total = $path . $file;
                 @
$bytes = filesize($total);  
          
                if(is_dir($total)) :
                    $type="dir";    
                  else:
                      $type="file";
                endif;
  
              
          // Als de directory de huidige directory betreft, en de
          // gebruiker klikt hierop, keren we weer terug naar de basis....

    
         if ($test ==1)  :  
           ?>

             <td width="25" height="25">Dir </td><td width="100%"><a href="files.php" target="_self">..</a>open this directory</td></tr>
            <?php     
        endif;    
       // Gebruiker wil een niveau verder kijken  
       if ( $type == "dir" & $test >2)  :  
        ?>
<td width="25" height="25">Dir </td><td width="100%"><a href="files.php?new_url=<?php echo $path. "" . $file; ?>/" target="_self"><?php echo $file;?></a>
        open this directory</td></tr>
        <?php
       endif;    
       //we hebben te maken met een bestand.
       if ( $type != "dir" )  :          
         ?>

          <td width="25" height="25">File </td>
          <td width="100%"><a title="download <?php echo $file; ?> <?php echo $type; ?>" href="get.php?start=<?php echo $path; ?>&file_name=<?php echo $file; ?>&file_size=<?php echo $bytes;?>"><?php echo $file; ?></a> <?php echo $bytes; ?>
          bytes</td></tr>
           <?php  
       endif;    
       endwhile;  

       @
closedir($dh);
       echo ("</table>");
    
    else:
    // $new_url heeft een ongeldige waarde opgeleverd ( zie regel nr 101 )
      echo "<br><hr>U krijgt geen toestemming om hier te komen...<br>";
    endif;

    
else:
 // Gebruiker wil in een directory kijken waar hij/zij niets te
 // zoeken heeft ..

   echo "<br><hr>U krijgt geen toestemming om hier te komen...<br>";
endif;

endif;


?>

-------------- einde files.php ------------------------------------------------

Files.php maakt gebruik van een ander script, get.php, welke ervoor zorgt dat de gebruiler een download venster in zijn/haar browser krijgt te zien.

Het gewenste bestand wordt in een buffer geplaatst en wordt stap-voor-stap gedownload naar de computer van de gebruiker....

------------ get.php (update!) -----------------------------------------------------------
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
# Copyright (c) 2006, ReFri Software / Internet Publication
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following
# conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this
#      list of conditions and the following disclaimer in the documentation and/or
#      other materials provided with the distribution.
#    * Neither the name of ReFri Software / Internet Publicion nor the names of its contributors
#      may be used to endorse or promote products derived from this software without
#     specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# get.php
# Author : Cynthia Fridsma <[email protected]>
#

# Security patch : October 10, 2007

// receive the desired file, via download.php & header.php





if (isset($_GET['start'])):
            
    # filter the url for unwanted tags (security patch)
        
    $new_url = $_GET['start'];
    $patt="downloads/";  
    $new_url=str_replace(chr(92), "", $new_url);
    $new_url=str_replace("?", "", $new_url);
      $new_url=strip_tags ($new_url);    

    
    if(ereg($patt,$new_url)) :
      // It's alright, new_url is part of the download path

       // Now replace unwanted 'break in' URL-S

           $new_url=str_replace("../", "", $new_url);
           $new_url=str_replace("./", "", $new_url);
           $new_url=str_replace("/..", "", $new_url);
           $new_url=str_replace("/../", "", $new_url);
           $new_url=str_replace(".", "", $new_url);
           $new_url=str_replace(chr(92), "", $new_url);
           $new_url=str_replace("?", "", $new_url);
           $new_url=str_replace($patt, $patt, $new_url);
      // check is user uses a slash in front of path to break in
      
           $test = "/downloads/";
           if (ereg($test, $new_url)):
               $new_url=str_replace($test, $patt, $new_url);
              endif;
        
      
          
           // It's alright, new_url is part of the download path
           # now we're going to check if the download directory is valid!

           if (is_dir($new_url)):
                   // download directory exists (security patch)
                # prepare the download file                     

        
               $file_name = trim($_GET['file_name']);
                  $file_name=str_replace("../", "", $file_name);
               $file_name=str_replace("./", "", $file_name);
               $file_name=str_replace("/..", "", $file_name);
               $file_name=str_replace("/../", "", $file_name);
               $file_name=str_replace(chr(92), "", $file_name);
               $file_name=str_replace("?", "", $file_name);

               $filecontent = trim($new_url) . "" . trim($file_name);
          
               if (is_file($filecontent)):        
               $size = trim(filesize ($filecontent));
               $bytes =trim($_GET['file_size']);              
                       // is the file size the same with the reals size?
                           if ($bytes == $size):                      
                               $bufsize = 2000;    
                      
                              
                               header("HTTP/1.1 200 OK");
                                 header("Content-Length: $size");
                               header("Content-disposition: attachment; filename=$file_name");
                               header("Content-Type: application/force-download");
                                  header("Content-Disposition: attachment; filename=$file_name");
                                header("Content-Transfer-Encoding: binary");

                                // open the file in binary and output it in the download document
                                # send the file to the header.

                                $fd = fopen($filecontent, "rb") or die ("<b>invalid file</b>");
                                    while (!feof($fd)) {
                                      $contents = fread($fd, $bufsize);
                                      echo $contents;                  
                                    }

                                fclose ($fd);           
                               endif;
                    endif;               
              endif;

        header("Location: index.php");

        endif;

    else:
    // do nothing!
endif;
header("Location: index.php");

?>

----------- einde get.php ---------------------------------------------

P.s. als je deze scripts op je server plaatst, zorg er dan wel voor dat de map 'downloads' bestaat. Ik had in de scripts geen routine gebouwd om te checken of de directory bestaat want het is bedoeld voor QCMS en daar zit standaard de directory 'downloads' bij.

Ook als je bestanden deelt met andere gebruikers, moet je er wel opletten dat je je bestanden in Binary modus upload naar je server (en niet in ASCII), want anders komt het bestand verminkt aan.

Veel plezier verder met dit script.

Groetjes,

Cynthia

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.