简体   繁体   中英

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. 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.

I've been considering the different alternatives but I decided to come here for some different opinions.

I'm running with 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.

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:

Flexible and secure.

Here is a tutorial: 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. maybe something like:

deny: all allow 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. Only other option IMO would be using LDAP or other directory authentication, which likely isn't ideal for most situations.

You can use basic HTTP auth, but this method is not 100% secure, it is vulnerable to middle man attacks

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

http://evertpot.com/223/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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