简体   繁体   English

如何确保除我自己的域下的脚本之外的所有脚本都不能包含db连接文件?

[英]How to make sure no scripts except those under my own domain, can include the db connection file?

I would like to ensure that any scripts that are trying to "include" my database connection file are located under my own domain. 我想确保所有试图“包括”我的数据库连接文件的脚本都位于我自己的域下。 I don't want a hacker to include the database connection file to their malicious script and gain access to my database that way. 我不希望黑客将数据库连接文件包含到他们的恶意脚本中并以此方式访问我的数据库。 My connection file's name is pretty easy to guess, it's called "connect.php". 我的连接文件的名称很容易猜到,叫做“ connect.php”。 So without renaming it and taking the security through obscurity route, how can I protect it by making sure all connection requests are made by scripts residing under my own domain name? 因此,如果不重命名它并通过晦涩的路由获取安全性,那么如何通过确保所有连接请求均由驻留在我自己域名下的脚本发出来保护它呢? How can this be checked using PHP? 如何使用PHP进行检查?

Generally speaking if someone tries to include a file on your domain, they will see the results of the execution of that file. 一般来说,如果有人尝试在您的域中包含文件,他们将看到该文件执行的结果。 What do you see when you load the connect.php script in your web browser? 在Web浏览器中加载connect.php脚本时会看到什么? Thats what they'll see as well if they try to include a remote file. 如果他们尝试包含远程文件,那也将是他们看到的。

That said, its generally a good idea to keep important files inaccessible from the outside of your public web space. 就是说,从公共Web空间外部访问重要文件通常是一个好主意。 So, if your website is /var/www/yoursite/ then keep your connect.php in /some/dev/dir/yoursite and include the files from your pages using require_once '/some/dev/dir/yoursite/connect.php'; 因此,如果您的网站是/var/www/yoursite/则将connect.php保留在/some/dev/dir/yoursite并使用require_once '/some/dev/dir/yoursite/connect.php'; /some/dev/dir/yoursite connect.php包含页面中的文件require_once '/some/dev/dir/yoursite/connect.php';

thetaiko's answer addresses the fundamental issues here - but if anyone else has access to run code on the server (ie its a shared server) then access to the file will depend on how the server is configured. thetaiko的答案解决了这里的基本问题-但是,如果其他任何人都有权在服务器(即其共享服务器)上运行代码,则对文件的访问将取决于服务器的配置方式。

There are lots of ways that access might be constrained - eg suphp, base_opendir, multiple chrooted servers. 可能有很多限制访问的方法-例如suphp,base_opendir,多个chroot服务器。 The only way to find out what's going on for sure is to casr yourself in the role of the hacker and see if you can access files outside your designated area. 唯一确定出问题的方法是让自己扮演黑客的角色,并查看是否可以访问指定区域之外的文件。

C. C。

What do you mean by including your connection file? 包含连接文件是什么意思? If a script does include "connect.php" then they can see the source code of the file, so whatever security measures you add to that file will be pointless, as it will be like: 如果脚本中确实包含“ connect.php”,则他们可以看到文件的源代码,因此,添加到该文件的任何安全措施将毫无意义,就像:

if($notFromHostname)
{
echo "DONT LOOK AT THIS";
die();
}
define('DB_PASS',"myPassword");
...

And the "hacker" will clearly be able to see your password. 而且“黑客”将显然能够看到您的密码。 You are probably better off using something like iptables to deny hosts that are not from a specific domain. 您可能最好使用iptables之类的东西来拒绝不是来自特定域的主机。

Are you on a shared server and don't want other users of the same server instance to be able to get at your files? 您是否在共享服务器上,并且不希望同一服务器实例的其他用户能够访问您的文件? That'd be up to your server provider, then, to provide some sort of chroot or virtual system to keep your things in. For Apache, mod_suid can accomplish this nicely, and each vhost gets its own userid and permissions set. 然后,这将取决于您的服务器提供商,以提供某种chroot或虚拟系统来保存您的东西。对于Apache, mod_suid可以很好地完成此任务,并且每个虚拟主机都有自己的用户ID和权限集。

If you want external users to not be able to get at your files, then unless you've badly munged your code, or the server's badly misconfigured, then all they'll get when they visit http://yourserver.com/connect.php is a blank page 如果您希望外部用户无法获取您的文件,那么除非您严重破坏了代码或服务器的配置不正确,否则他们在访问http://yourserver.com/connect时将获得全部收益。 php是空白页

No other user than yourself should have access to your PHP files in any way, as Felix mentioned. 正如Felix所述,除了您自己之外,没有其他用户可以通过任何方式访问您的PHP文件。 However, this is how you'd check in PHP: 但是,这是您检入PHP的方式:

if($_SERVER['SERVER_NAME'] != "example.com")
    die("I've been kidnapped!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保只有我的应用程序才能将数据插入数据库? - How to make sure only my app can insert data into the DB? 如何确保我的MySQL数据库中快速变化的数据能够在PHP脚本中准确表示? - How do I make sure the rapidly changing data from my MySQL DB is accurately represented in php scripts? 如何确保对服务器的请求来源合法? - how can I make sure that the origin of the request to my server is legit? 无法将电子邮件发送到我自己的域 - Can't send email to my own domain 除了Connection之外,PHP脚本不起作用 - PHP scripts are not working except the Connection 如何确保服务器与客户端保持连接 - How to make sure server keeps connection with client 如何确定PHP json_encode将对我从数据库读取的任何数据进行编码? - How can I make sure PHP json_encode will encode whatever data I read from the db? 如何将我自己的代码加载到Laravel中? 那些方式之间的差异? - How to load my own code into Laravel? Differences between those ways? 如何在laravel中使用我自己的类进行身份验证? - How I can make authentication using my own class in laravel? 我如何在php中创建自己的模板/编程引擎 - How can i make my own template/programming engine in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM