简体   繁体   English

如何保护PHP文件

[英]How to secure a php file

I did a payment gateway integration on an existing website. 我在现有网站上进行了支付网关集成。 The payment gateway gave me an API which I used and did the coding and stored it in a php file pay.php 支付网关给了我一个API,我使用它进行编码并将其存储在php文件pay.php

I have a checkout form in the main website and on clicking submit, it sends all the needed data to pay.php via post and then this file does the rest and sends the data over to the payment gateway. 我在主网站上有一个结帐表格,单击“提交”后,它将通过post将所有需要的数据发送到pay.php ,然后该文件执行其余操作,并将数据发送到支付网关。

Since this pay.php file stores sensetive data, I want to secure it so that no one else can access it via web brwoser like entering http://domain.com/pay.php or be able to download it. 由于此pay.php文件存储了pay.php数据,因此我想对其进行保护,以使其他任何人都无法通过Web浏览器访问它,例如输入http://domain.com/pay.php或能够下载它。

Now while I know that normally you can't just download .php files, I saw a site the other day which was capable of downloading my wordpress blog along with the .php files. 现在,尽管我知道通常不能只下载.php文件,但前几天我看到了一个站点,该站点能够与.php文件一起下载我的wordpress博客。

Also every time the pay.php file is run a payment link is generated, so I need to make sure only the website http://www.domain.com is able to use this file and no 3rd party. 另外,每次运行pay.php文件时, pay.php生成一个付款链接,因此我需要确保只有网站http://www.domain.com可以使用此文件,并且没有第三方。

I already have a few ideas in my head like limiting access using remote IP and such but would want to know the best possible way to address the issue. 我已经有了一些想法,例如使用远程IP限制访问等,但我​​想知道解决该问题的最佳方法。

To add to the answer of Commusoft and to give a slight alternative. 为了补充Commusoft的答案并给出一些替代方案。

This piece of .htaccess denies php file requests via the browser and makes sure you can only include the file from a different php file. 这段.htaccess文件会拒绝通过浏览器发送的php文件请求,并确保您只能包含来自其他php文件的文件。

<Files *.php>
Deny from All
</Files>

Put this in a separate directory where pay.php is located. 将其放在pay.php所在的单独目录中。

I think the best way of dealing with this payment issue is to turn the pay.php into a full class and use it object oriented. 我认为处理此付款问题的最佳方法是将pay.php转换为完整类并使用其面向对象。 That way you can use the functionality in any location and pass variables to it easily. 这样,您可以在任何位置使用该功能并将变量轻松传递给它。

  1. You may perform only POST requests in this file (also you have to validate even POST requests). 您只能在此文件中执行POST请求(甚至还必须验证POST请求)。 GET request will return nothing or an error. GET请求将不返回任何内容或错误。
  2. Usually you cannot download php file in easy way. 通常,您无法以简单的方式下载php文件。 There is should some holes in the security of the site to do this. 为此,站点的安全性应该存在一些漏洞。
  3. You may direct all request into the one php file. 您可以将所有请求定向到一个php文件中。 Usually it's index.php. 通常是index.php。 Try to see how to configure it via .htaccess . 尝试看看如何通过.htaccess配置它。 And users will not know about your real PHP files. 而且用户不会知道您真正的PHP文件。
  4. You may protect your files via permissions if needed. 如果需要,您可以通过权限保护文件。
  • If you configure your webserver (Apache,...) correctly, the webserver will render the PHP so no one will ever have the opportunity to see the file's source code . 如果您正确配置了Web服务器(Apache,...),则Web服务器将呈现 PHP,因此没有人可以看到文件的源代码 So there is some inherent protection . 因此存在一些固有的保护 But needless to say, it can be hacked . 但是不用说,它可以被黑客入侵

  • Now hackers of course wish to hack the website. 现在,黑客当然希望入侵该网站。 And you don't have any contol on what data will be posted: that's up to the browser. 而且您对要发布的数据没有任何控制:这取决于浏览器。 Hackers sometimes modify the content before they send the request in the hope that the PHP script will fail somehow and reveal for instance SQL queries, etc. The first thing you thus better do is turn off error reporting : 黑客有时会在发送请求之前修改内容,以期PHP脚本会以某种方式失败并显示SQL查询等。因此,您最好做的第一件事就是关闭错误报告

     error_reporting(0); 

    Since hackers can find some warnings/errors most helpful to figure out the underlying algorithm and corrupt it. 由于黑客可以找到一些警告/错误,最有助于找出潜在的算法并将其破坏。 By doing this explicitly in the PHP file, you prevent that if you modify the settings of the server, all of a sudden errors reappear. 通过在PHP文件中显式执行此操作,可以防止如果您修改服务器的设置,突然出现所有错误。

  • You furthermore better make sure a user has no direct access to the sensitive data anyhow. 您还可以更好地确保用户无论如何都不能直接访问敏感数据。 This can be achieved by putting the sensitive data into a separate file (say secure.php ) and include_once() it so you can read it's data. 可以通过将敏感数据放入一个单独的文件 (例如secure.php )并对其进行include_once() ,以便您可以读取其数据。

  • As @Havenard and @AlexHowansky say, you can also store such files ( secure.php ) better outside the publicly accessible directories . 正如@Havenard和@AlexHowansky所说,您还可以将这些文件( secure.php )更好地存储在公共可访问目录之外 For instance above public_html . 例如在public_html以上。 This does not always fully resolves the problem because some webservers are sensitive to URL-injection (by specifying http://www.domain.com/../securefolder/secure.php , you can sometimes access a file above the publicly available directory). 由于某些Web服务器对URL注入敏感(通过指定http://www.domain.com/../securefolder/secure.php ,您有时可以访问公用目录上方的文件,所以这不能始终完全解决问题。 )。

  • As @TomKriek says, you can also provide additiona protection to the "secure folder" by inserting a .htaccess file that contains the following lines: 正如@TomKriek所说,您还可以通过插入包含以下几行的.htaccess文件来为“安全文件夹”提供附加保护:

     <Files *.php> Deny from All </Files> 

    It means that the webserver - again if configured correctly - will prevent the users access from all .php files in the directory. 这意味着,如果配置正确,则网络服务器将阻止用户访问目录中的所有.php文件。

  • Finally, you better give the files proper permissions : if the PHP engine runs as the same user as the owner of the files, you can give it 600 access rights ( chmod 600 secure.php ) or 640 in case the PHP engine runs on a different user than the owner of the file, but in the same user group, or worst-case 644 so that a hacker can't modify the file (or is at least not supposed to). 最后,最好为文件赋予适当的权限 :如果PHP引擎以与文件所有者相同的用户身份运行,则可以给它600 访问权限chmod 600 secure.php )或640 ,以防PHP引擎在文件所有者上运行。用户与文件所有者不同,但用户属于同一用户组,或者在最坏的情况下属于644因此黑客无法修改文件(或至少不应修改文件)。 You can also change the owner of the file to the www-data by running chown www-data secure.php so that the owner is the webserver and you can thus make the permissions more tight. 您还可以通过运行chown www-data secure.php将文件的所有者更改为www-data ,以便所有者为网络服务器,从而使权限更加严格。 The rule of thumb is always to give it the least access rights to do its job correctly . 经验法则始终是赋予它最少的访问权限,以正确执行其工作

  • You can also make the files read-only once they are implemented by setting the rights to 400 , 440 or worst-case 444 . 您也可以使文件只读一旦被权设置为执行400440或最坏情况下的444 In that case at least a hacker can't enter his/her own bank account as the receiver of the payment ;). 在这种情况下,至少黑客无法输入他/她自己的银行帐户作为付款的接收方;)。

To conclude, when designing/implementing a secure server, one better uses a layered approach where several measures are taken so that if one measure might fail, others hopefully will still prevent a hacker from accessing the server/files. 总而言之,在设计/实现安全服务器时,一种更好的方法是采用分层方法,其中采取了多种措施,以便如果一种措施可能失败,则希望其他措施仍然可以防止黑客访问服务器/文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM