简体   繁体   English

保护PHP脚本不受未经授权的访问

[英]Protect PHP Scripts from unauthorized access

I'm currently developing a set of php scripts that act as a private API that is only to be used by some authorized machines. 我目前正在开发一组php脚本,这些脚本充当私有API,仅可用于某些授权计算机。 The scripts interface directly with the MySQL database so I must make sure that no public unauthorized access, as in making the scripts run successfully, is possible. 脚本直接与MySQL数据库交互,因此,我必须确保无法成功进行公共未经授权的访问,就像使脚本成功运行一样。

I've been considering the different alternatives but I decided to come here for some different opinions. 我一直在考虑不同的选择,但我决定来这里寻求不同的意见。

I'm running with nginx. 我正在使用Nginx。

The API is used externally by a C# program (using WebClient) and a few other servers, which IPs may change, so I was avoiding a IP based approach. 该API由C#程序(使用WebClient)和其他一些服务器在外部使用,其IP可能会更改,因此我避免使用基于IP的方法。

I was considering passing a "key" parameter through post to check for a match 我正在考虑通过邮政传递“关键”参数来检查是否匹配

if($_POST['key'] == 'key')

There are probably better alternatives, what you do think? 可能还有更好的选择,您认为呢?

You can configure TLS authorization using client certificates: 您可以使用客户端证书配置TLS授权:

Flexible and secure. 灵活而安全。

Here is a tutorial: http://blog.nategood.com/client-side-certificate-authentication-in-ngi 这是一个教程: http : //blog.nategood.com/client-side-certificate-authentication-in-ngi

I usually use the key method you wrote but I definitely believe in a defense in depth method. 我通常使用您编写的密钥方法,但我绝对相信深度防御方法。 SO I would add some server level protection, check out nginx access control methods. 因此,我将添加一些服务器级别的保护,请查看nginx访问控制方法。 maybe something like: 也许像这样:

deny: all allow IP: 127.0.0.1 拒绝:全部允许IP:127.0.0.1

I would use either a public/private key authentication or use a username and password/api key for each client. 我将使用公钥/私钥身份验证,或者为每个客户端使用用户名和密码/ api密钥。 Only other option IMO would be using LDAP or other directory authentication, which likely isn't ideal for most situations. 仅有其他选择IMO将使用LDAP或其他目录身份验证,这在大多数情况下可能并不理想。

You can use basic HTTP auth, but this method is not 100% secure, it is vulnerable to middle man attacks 您可以使用基本的HTTP身份验证,但是此方法不是100%安全的,它容易受到中间人攻击

http://www.php.net/manual/en/features.http-auth.php http://www.php.net/manual/en/features.http-auth.php

http://evertpot.com/223/ http://evertpot.com/223/

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

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