Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Usuwanie pliku przez użytkownika
tosiek
post 6.07.2009, 09:36:35
Post #1





Grupa: Zarejestrowani
Postów: 142
Pomógł: 0
Dołączył: 7.07.2008

Ostrzeżenie: (0%)
-----


Witam. Mam prosty zamiar dać użytkownikowi możliwość usunięcia pliku.

Aktualnie zapisuję sobie:
  1. <?php
  2. $czas = time();
  3. $nazwa = $czas . $_FILES['userfile']['name'];
  4. file_put_contents(dirname(__FILE__) . "/../../../../wynikowe/" . $nazwa, $plik);
  5. ?>


No i podaję bezpośredni link do pobrania. I link do usunięcia który działa w ten sposób:
  1. <?php
  2. if(isset($_GET['usun']))
  3. {
  4. unlink(dirname(__FILE__) . "/../../../../wynikowe/" . urldecode($_GET['usun']));
  5. echo "plik usuniety";
  6. }
  7. echo '<a href="http://tosiek.pl/converter/?usun=' . urlencode($nazwa) . '">Usuń plik: ' . $_FILES['userfile']['name'] . '</a>';
  8. ?>


I teraz pytanie czy przez to unlink da się usunąć inne pliki, poza katalogiem wynikowe ? I jak to zabezpieczyć ?


--------------------
Go to the top of the page
+Quote Post
leovandamon
post 6.07.2009, 10:25:56
Post #2





Grupa: Zarejestrowani
Postów: 41
Pomógł: 3
Dołączył: 5.01.2008
Skąd: Czwarty Świat

Ostrzeżenie: (0%)
-----


Witam!

Napisałem kiedyś funkcyjkę... Może ci się przyda. Ja używam jej do czyszczenia zmiennej z nazwą includowanego pliku.

  1. <?php
  2. function clear($text,$mode='easy') {
  3.    switch($mode) {
  4.        case 'full' : // For filenames, etc. Very secure clearing
  5.            $text = str_replace('~','',$text); $text = str_replace('!','',$text);
  6.            $text = str_replace('`','',$text); $text = str_replace('@','',$text);
  7.            $text = str_replace('#','',$text); $text = str_replace('(','',$text);
  8.            $text = str_replace('$','',$text); $text = str_replace(')','',$text);
  9.            $text = str_replace('%','',$text); $text = str_replace('_','',$text);
  10.            $text = str_replace('^','',$text); $text = str_replace('-','',$text);
  11.            $text = str_replace('&','',$text); $text = str_replace('+','',$text);
  12.            $text = str_replace('*','',$text); $text = str_replace('=','',$text);
  13.            $text = str_replace('{','',$text); $text = str_replace('|','',$text);
  14.            $text = str_replace('}','',$text); $text = str_replace(':','',$text);
  15.            $text = str_replace('[','',$text); $text = str_replace(';','',$text);
  16.            $text = str_replace(']','',$text); $text = str_replace('"','',$text);
  17.            $text = str_replace('','',$text); $text = str_replace('&#092;'','',$text);
  18.            $text = str_replace('<','',$text); $text = str_replace('.','',$text);
  19.            $text = str_replace(',','',$text); $text = str_replace('?','',$text);
  20.            $text = str_replace('>','',$text); $text = str_replace('/','',$text);
  21.            $text = str_replace('ą','a',$text); $text = str_replace('ł','l',$text);
  22.            $text = str_replace('ć','c',$text); $text = str_replace('ń','n',$text);
  23.            $text = str_replace('ę','e',$text); $text = str_replace('ó','o',$text);
  24.            $text = str_replace('ś','s',$text); $text = str_replace('ż','z',$text);
  25.            $text = str_replace('ź','z',$text); $text = str_replace('Ź','z',$text);
  26.            $text = str_replace('Ą','A',$text); $text = str_replace('Ł','L',$text);
  27.            $text = str_replace('Ć','C',$text); $text = str_replace('Ń','N',$text);
  28.            $text = str_replace('Ę','E',$text); $text = str_replace('Ó','O',$text);
  29.            $text = str_replace('Ś','S',$text); $text = str_replace('Ż','Z',$text);
  30.            break;
  31.        case 'normal' : // Normal clearing
  32.            $text = str_replace('&','&',$text);
  33.            $text = str_replace('=','=',$text); $text = str_replace('!','!',$text);
  34.            $text = str_replace('`','`',$text); $text = str_replace('@','@',$text);
  35.            $text = str_replace('#','#',$text); $text = str_replace('(','(',$text);
  36.            $text = str_replace('$','$',$text); $text = str_replace(')',')',$text);
  37.            $text = str_replace('%','&percent;',$text); $text = str_replace('_','_',$text);
  38.            $text = str_replace('^','^',$text); $text = str_replace('-','-',$text);
  39.            $text = str_replace('+','+',$text); $text = str_replace('~','~',$text);
  40.            $text = str_replace('{','{',$text); $text = str_replace('|','|',$text);
  41.            $text = str_replace('}','}',$text); $text = str_replace(':',':',$text);
  42.            $text = str_replace('[','[',$text); $text = str_replace(';',';',$text);
  43.            $text = str_replace(']',']',$text); $text = str_replace('"','"',$text);
  44.            $text = str_replace('','&#092;',$text); $text = str_replace(''','´',$text);
  45.            $text = str_replace('<','<',$text); $text = str_replace('.','.',$text);
  46.            $text = str_replace(',',',',$text); $text = str_replace('?','?',$text);
  47.            $text = str_replace('>','>',$text); $text = str_replace('/','⁄',$text);
  48.            $text = str_replace('ą','a',$text); $text = str_replace('ł','l',$text);
  49.            $text = str_replace('ć','c',$text); $text = str_replace('ń','n',$text);
  50.            $text = str_replace('ę','e',$text); $text = str_replace('ó','o',$text);
  51.            $text = str_replace('ś','s',$text); $text = str_replace('ż','z',$text);
  52.            $text = str_replace('ź','z',$text); $text = str_replace('Ź','z',$text);
  53.            $text = str_replace('Ą','A',$text); $text = str_replace('Ł','L',$text);
  54.            $text = str_replace('Ć','C',$text); $text = str_replace('Ń','N',$text);
  55.            $text = str_replace('Ę','E',$text); $text = str_replace('Ó','O',$text);
  56.            $text = str_replace('Ś','S',$text); $text = str_replace('Ż','Z',$text);
  57.            break;
  58.        case 'easy' : // Base clearing
  59.            $text = str_replace('','',$text); $text = str_replace('&#092;'','',$text);
  60.            $text = str_replace('./','',$text);
  61.            break;
  62.        default : echo '<br />Fatal error: wrong param defined for function clear on line '.__LINE__.'<br />';
  63.    }
  64.    return $text;
  65. }
  66. ?>


i użycie:

  1. <?php
  2. $nazwa = clear(urlencode($_GET['usun']));
  3. ?>


Możesz podać drugi parametr (easy, normal, full). Easy jest domyślnym i jest "akurat" do nazw plików. Normal zamienia większość znaków na encje HTML i do tego polskie znaki diakrytyczne na te bez "kropek" i "ogonków". Full całkiem usuwa wszystkie bardziej "niebezpieczne" znaki.

Na pewno możnaby to zrobić lepiej, jednak pisałem to parę lat temu. Mnie wystarcza.

Poza tym, niedawno pisałem "eksplorator", być może coś ci się przyda.

http://dragonsheart.eu/-/php/manager.txt

Część kodu to funkcje bezpośrednio to użycia w moim programie, więc nie zwracaj uwagi. winksmiley.jpg

Pozdrawiam,
Leo van Damon


--------------------
Mój blog: http://test.dragonsheart.eu/
DH Corp: http://www.dhcorp.pl/
Go to the top of the page
+Quote Post
pyro
post 6.07.2009, 10:30:35
Post #3





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

Ostrzeżenie: (0%)
-----


leovandamon, Twój kod jest totalnie bez sensu. Wywołujesz tę samą funkcję kilkadziesiąt razy, a wystarczy:

  1. <?php
  2. $text = str_replace(array('#', '$'), '', $text); // i analogicznie dla wszystkich znaków
  3. ?>


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
tosiek
post 6.07.2009, 10:33:42
Post #4





Grupa: Zarejestrowani
Postów: 142
Pomógł: 0
Dołączył: 7.07.2008

Ostrzeżenie: (0%)
-----


Czyli do zastapienia są ../ jak i ./ oraz / i // ? nic więcej ? smile.gif


--------------------
Go to the top of the page
+Quote Post
leovandamon
post 6.07.2009, 10:38:01
Post #5





Grupa: Zarejestrowani
Postów: 41
Pomógł: 3
Dołączył: 5.01.2008
Skąd: Czwarty Świat

Ostrzeżenie: (0%)
-----


pyro, jak już mówiłem, pisałem to x lat temu, kiedy zaczynałem z php. Wiem, że jest to bez sensu, bo można szybciej. Ale wykorzystuję przeważnie tryb "easy" i działa szybko. Mimo wszystko, dzięki. Unowocześnię kod.

tosiek, tak. User nie może wychodzić do katalogu wyżej, bo wtedy mógłby usunąć ci dowolny plik z serwera (powiedzmy index.php). Dajesz coś takiego:

  1. <?php
  2. $katalog = 'upload'; // katalog uploadu
  3. if($_GET['usun']) {
  4. $usun = clear($_GET['usun']);
  5. $plik = $katalog.'/'.$usun;
  6. unlink($plik);
  7. }
  8. ?>


Pisałem z palca. winksmiley.jpg

User może wywalić Ci coś z katalogu upload, ale nie może wyjść wyżej. Pozostaje kwestia sprawdzania, czy user może usunąć plik, ale jeśli jako $katalog dasz katalog usera, w którym pliki należą do niego, to tylko te może usunąć. Możesz też sprawdzać w bazie MySQL lub na setki innych sposobów, decyzję pozostawiam Tobie.

Ten post edytował leovandamon 6.07.2009, 10:41:25


--------------------
Mój blog: http://test.dragonsheart.eu/
DH Corp: http://www.dhcorp.pl/
Go to the top of the page
+Quote Post
tosiek
post 15.07.2009, 10:17:26
Post #6





Grupa: Zarejestrowani
Postów: 142
Pomógł: 0
Dołączył: 7.07.2008

Ostrzeżenie: (0%)
-----


tym razem napisalem coś takiego:

tablica danger:
Kod
$danger = array("../", "./", "//", "/", "\\\\", "\\", ".htaccess", "index.htm", );


  1. <?php
  2. if (isset($_GET["usun"]))
  3. {
  4.    $usun = urldecode($_GET['usun']);
  5.  
  6.    if(!file_exists(dirname(__FILE__) . "/../../../../wynikowe/" . realpath($usun)))
  7.    {
  8.        echo "<h2><span style=\"color:red;\">Plik nie istnieje lub został już usunięty</span></h2>";
  9.    }
  10.    else
  11.        {
  12.        if(preg_match("|" . $danger . "|", $usun))
  13.        {
  14.            echo "<h2><span style=\"color:red;\">Zabraniam Ci usuwania...</span></h2>";
  15.  
  16.        }
  17.        else
  18.        {
  19.            if (unlink(dirname(__FILE__)."/../../../../wynikowe/".$usun))
  20.                {
  21.                echo"<h2><span style=\"color:green;\">Usunięto plik $usun</span></h2>";
  22.                }
  23.            else
  24.                {
  25.                echo"<h2><span style=\"color:red;\">Błąd usuwania {$usun}</span></h2> <p>$php_errormsg</p>";
  26.                }
  27.        }
  28. }
  29. }
  30. ?>


ale już nie mam pomysłu czemu nadal moge usuwać ../plik.php .htaccess i inne ;/

zmiana:
!file_exists(dirname(__FILE__) . "/../../../../wynikowe/" . realpath($usun))
na
!file_exists(realpath(dirname(__FILE__) . "/../../../../wynikowe/" . $usun))

nic nie zmienia tongue.gif a innych pomysłów nie mam ;/

Ten post edytował tosiek 15.07.2009, 10:19:56


--------------------
Go to the top of the page
+Quote Post
planet
post 16.07.2009, 10:00:16
Post #7





Grupa: Zarejestrowani
Postów: 61
Pomógł: 6
Dołączył: 15.07.2009
Skąd: Śląsk->Ruda Śląska

Ostrzeżenie: (0%)
-----


powiedz mi w jaki sposób przechowujesz nazwy plików? są one w bazie? bo jeśli tak to byś mógł rozdzielić nazwę na: nazwa_pliku -> rozszerzenie w liście plików do usunięcia dajesz tylko nazwy potem przy usuwaniu robisz selecta po nazwie i dorzucasz rozszerzenie: unlink("ścieżka_katalogu/".$nazwa_pliku.".".$rozszerzenie_pliku); - jak user wprowadzi nazwę, której nie ma w bazie to nie usunie pliku winksmiley.jpg. - alternatywne rozwiązanie, ale z pewnością wolniejsze.
Go to the top of the page
+Quote Post
tosiek
post 16.07.2009, 10:20:33
Post #8





Grupa: Zarejestrowani
Postów: 142
Pomógł: 0
Dołączył: 7.07.2008

Ostrzeżenie: (0%)
-----


Niestety nigdzie nie przechowuje listy plików, jest to mi zbędne winksmiley.jpg

użycia basename() wystarcza, przynajmniej tak mi się wydaje ;P

  1. <?php
  2. if (isset($_GET["usun"]))
  3. {
  4.    $usun = "/home/tosiek/domains/tosiek.pl/public_html/wynikowe/" . basename(urldecode($_GET['usun']));
  5.    if(!file_exists($usun))
  6.    {
  7.        echo "<h2><span style=\"color:red;\">Plik nie istnieje lub został już usunięty</span></h2>";
  8.    }
  9.    else
  10.    {
  11.        $usun2 = basename($usun);
  12.        if($usun2 === ".htaccess" || $usun2 === "index.htm")
  13.        {
  14.            echo "<h2><span style=\"color:red;\">Zabraniam Ci usuwania...</span></h2>";
  15.        }
  16.        else
  17.        {
  18.            if (unlink($usun))
  19.            {
  20.                echo"<h2><span style=\"color:green;\">Usunięto plik $usun2</span></h2>";
  21.            }
  22.            else
  23.            {
  24.                echo"<h2><span style=\"color:red;\">Błąd usuwania {$usun2}</span></h2> <p>$php_errormsg</p>";
  25.            }
  26.        }
  27.    }
  28. }
  29. ?>


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.06.2025 - 04:54