通过 php 添加管理 mysql 用户的安全方法
我正在构建一个在 mysql 中添加和删除用户的 php 系统。目前的设置如下: - 用户尝试登录网站 - 在projectdb.users中查找用户名和哈希值(sha256、salt、密码)(这也是mysql用户凭据) - 找到它,然后使用该用户名和密码在登录时执行任何 mysql_connect
因此,当我在系统中添加用户时,我将其凭据添加到表中,并将它们添加为仅对projectdb数据库具有最小权限的mysql用户。
但是当我添加管理员帐户时,我授予他们对该数据库的完全权限,并授予他们 mysql 授予/添加用户权限,以便他们可以添加更多用户。这似乎太过强大,即使这些用户无法直接连接并登录 mysql(但如果可以的话,他们可以添加任何人)。
与其实际将 mysql 用户添加到系统中,不如在 php 文件中硬编码单个用户和管理员权限更好吗?还有比这两种想法更安全的方法吗?
I'm building a php system that adds and deletes users in mysql. It's currently set up like so:
- user attempts to log in to site
- looks up username and hash(sha256, salt, password) in projectdb.users (which is also a mysql users credentials)
- finds it and then uses that username and password to do any mysql_connect's while logged in
So when I add a user in the system I'm adding their credentials to a table and adding them as a mysql user with minimal permissions only on the projectdb database.
But when I add an admin account I'm giving them full permission to that db and giving them mysql grant/add user permissions so they can add more users. This seems like too much power, even though these users can't directly connect and log into mysql (but if they could they could add anyone).
Instead of actually adding mysql users to the system is it better to hard code a single user and admin's permissions in the php files? Is there a safer way than those two ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果用户不直接访问数据库,则不必直接向他们授予/添加用户权限。
大多数 CMS 都会实现一个权限表,允许 PHP 代码检查用户是否具有执行特定操作的适当权限。因此,您的 PHP 代码可以简单地检查登录用户是否具有权限表设置的权限,然后让实际的管理员用户(即 root)自行创建用户。它更加迂回,但通过在该权限结构上创建额外的层来提供稍微更细粒度的控制。
If the user is not accessing the database directly, you don't have to give them the grant/add user permission directly.
Most CMS implements a permissions table that will allow the PHP code to check if the user has the proper permissions to perform a certain action. As such, your PHP code can simply check to if the logged-in user has the permission set by the permissions table, then have an actual admin user (i.e., root) create the user itself. It is more roundabout, but gives slightly more fine-grain control by creating an extra layer upon that permission structure.
根据 David 的建议...如果这是一个 Web 应用程序,为什么要将它们添加到 MySQL 用户中?这是为了记录吗?如果没有真正令人信服的理由,我不认为以编程方式添加用户是一种好的做法。这通常是有意抽象的,以最大限度地降低风险。
Per David's suggestion... If this is a web app, why are you adding them to the MySQL users at all? Is this for logging? I can't see programmatically adding users as a good practice without a really compelling reason. This is usually abstracted on purpose and to minimize risk.