【问题标题】:Is it possible to obtain a cpanel mysql database prefix?是否可以获得 cpanel mysql 数据库前缀?
【发布时间】:2017-09-04 04:06:33
【问题描述】:
$db_connection = $_SERVER['DOCUMENT_ROOT'] . '/includes/install/database_connection.php';
if (!file_exists($db_connection)) {
    require("install/xmlapi.php");
    if (isset($_POST['cpname'])) {
        $opts['user'] = $_POST['cpname'];
        $opts['pass'] = $_POST['cppass'];
        $opts['temp'] = substr(str_shuffle(md5(time())),0,'12');
        $xmlapi = new xmlapi($_SERVER['HTTP_HOST']);
        $xmlapi->set_port( 2083 );
        $xmlapi->password_auth($opts['user'],$opts['pass']);
        $xmlapi->set_debug(0);
        $cpaneluser=$opts['user'];
        $databasename="OSMP_DAT";
        $databaseuser="OSMP_admin";
        $databasepass=$opts['temp'];
        $db = $databasename;
        $user = $databaseuser;
        $pass = $databasepass;
        $loc = 'localhost';
        $createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));
        $usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));
        $addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array("".$cpaneluser."_".$databasename."", "".$cpaneluser."_".$databaseuser."", 'all'));
        include ('install/installer.php');
        exit;
    }
    if (!isset($_POST['dbhost'])) { include ('install/db_installer.php'); }
    if (isset($_POST['dbhost'])) {
            // save connection details to $db_connection
    }
}

上面的代码可以完美地运行,正如人们所期望的那样。

首先它检查database_connection.php 是否存在。如果存在,它包括包含数据库详细信息的文件。

如果不是 - 我们假设它是第一次安装。所以我们向用户询问 cpanel 登录详细信息,我们的脚本创建数据库并将详细信息保存到 database_connection.php。

唯一的问题……是数据库前缀。创建数据库时,如果 WHM 为用户帐户设置了数据库前缀,则数据库前缀将作为数据库名称的前缀。

我想知道如何确定是否有前缀,如果有,如何找出它是什么,以便脚本也可以在数据库名称上添加前缀。

注意我不是在寻找表前缀,而是由 cpanel/whm 添加的数据库前缀

【问题讨论】:

    标签: php mysql database cpanel cpanel-xmlapi


    【解决方案1】:

    显然,如果启用前缀,则默认情况下 cpanel 使用用户名的前 8 个字符,后跟下划线。这用于数据库和数据库名称。

    所以我简单地将上面的代码修改如下:

    $db_connection = $_SERVER['DOCUMENT_ROOT'] . '/includes/install/database_connection.php';
    if (!file_exists($db_connection)) {
        require("install/xmlapi.php");
        if (isset($_POST['cpname'])) {
            $opts['user'] = $_POST['cpname'];
            $prefix = substr($opts['user'],0,8).'_';
            if ($prefix === FALSE) {$prefix = $opts['user'];}
            $opts['pass'] = $_POST['cppass'];
            $opts['temp'] = substr(str_shuffle(md5(time())),0,'12');
            $xmlapi = new xmlapi(localhost);
            $xmlapi->set_port( 2083 );
            $xmlapi->password_auth($opts['user'],$opts['pass']);
            $xmlapi->set_debug(1);
            $cpaneluser=$opts['user'];
            $databasename="OSMP_DAT";
            $databaseuser="osmp";
            $databasepass=$opts['temp'];
            $pass = $databasepass;
            $loc = 'localhost';
            $createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));
            $usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));
            $addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));
            $db = $prefix.$databasename;
            $user = $prefix.$databaseuser;
            include ('install/installer.php');
            exit;
        }
        if (!isset($_POST['dbhost'])) { include ('install/db_installer.php'); }
        if (isset($_POST['dbhost'])) {
            // save connection details to $db_connection
        }
    }
    

    所以现在进行检查以确定用户名的长度,如果超过 8 个字符,则将其截断为 8。然后添加下存储并作为变量输出以传递到脚本的下一部分。

    我可以看到的唯一缺陷是主机是否禁用了 whm 中的前缀,所以我正在顺利进行。

    对于将来尝试使用此代码的任何人 - 请记下您 需要保护包含的文件,否则您将拥有惊人的 安全问题。正如这段代码所代表的那样,有人可以手动调用 install/db_installer.php 或 install/installer.php 并绕过 (!file_exists($db_connection)) 检查和 if (isset($_POST)) 和 (!isset($_POST['dbhost']))。

    如果您不知道如何保护,请勿使用此代码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-21
      • 2013-03-19
      • 2014-10-17
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多