Collated some knowledge about PHP security

阿神
Release: 2023-03-06 17:18:02
Original
2408 people have browsed it

Collated some knowledge about PHP security

The security environment discussed in this article is Linux+Apache+Mysql+PHP. Security issues beyond this scope are beyond the scope of this article

1. Apache server security settings

1 , run as Nobody user

Generally, Apache is run by Root to install and run. If the Apache Server process has Root user privileges, it will pose a great threat to the security of the system. You should ensure that Apache The Server process runs as the lowest possible privileged user. Run Apache as the Nobody user by modifying the following options in the httpd.conf file achieve relative safety.

User nobody

Group# -1

2, ServerRoot directory permissions

In order to ensure that all configurations are appropriate and secure, access to the Apache home directory needs to be strictly controlled so that non-superusers cannot modify the contents of this directory. The home directory of Apache corresponds to the Server Root control item in the Apache Server configuration file httpd.conf, which should be:

Server Root /usr/local/apache

3. SSI configuration

Add the Includes NO EXEC option to the Options directive in the configuration file access.conf or httpd.conf to disable the execution function in Apache Server. Prevent users from directly executing the execution program in the Apache server, which will cause the server system to be exposed.

Options Includes Noexec

4. Prevent users from modifying system settings

Make the following settings in the Apache server configuration file to prevent users from creating and modifying .htaccess files and preventing users from exceeding the definable system security features.

AllowOveride None

Options None

Allow from all

Then configure the specific directories appropriately.

5. Change the default access characteristics of the Apache server

Apache's default settings can only guarantee a certain degree of security. If the server can find the file through normal mapping rules, the client will obtain the file. For example, local host/~ root/ will allow the user to access the entire file system. Add the following content to the server file:

order deny,ellow

Deny from all

Default access to the file system will be disabled.

6. Security considerations for CGI scripts

CGI scripts are a series of programs that can be run through a web server. In order to ensure the security of the system, you should ensure that the author of the CGI is trustworthy. For CGI, it is best to limit it to a specific purpose Recorded, such as under cgi-bin, for easy management; in addition, it should be ensured that the files in the CGI directory are not writable to prevent some deceptive programs from residing or mixing in them; if it can provide users with a sense of security Using good CGI program modules as a reference may reduce many unnecessary troubles and security risks; remove all non-business application scripts in the CGI directory to prevent abnormal information leakage.

7. SSL link encryption

The above common measures can give Apache Server a basic safe operating environment. Obviously, further detailed analysis is needed in the specific implementation to formulate a security configuration plan that is consistent with actual applications.

2. PHP security settings

The server cannot prevent all security issues, such as program vulnerabilities, user input form issues, PHP file permission issues, etc. You can also use some means to confuse hackers or those with ulterior motives.

1. Program code vulnerability issues

The major weaknesses in many PHP programs are not problems with the PHP language itself, but are caused by programmers' low security awareness. Therefore, you must always pay attention to possible problems in each piece of code to discover the possible impact of incorrect data submission.

<?php 
    unlink ($evil_var); 
    fwrite ($fp, $evil_var); 
    system ($evil_var); 
    exec ($evil_var); 
?>
Copy after login

You must always pay attention to your code to ensure that every variable submitted from the client is properly checked

Check it out and ask yourself some questions:

Does this script only affect expected files?

Can abnormal data be effective after being submitted?

Can this script be used for unintended purposes?

Can this script be combined with other scripts to do bad things?

Are all transactions adequately documented?

在写代码的时候问自己这些问题,否则以后可能要为了增加安全性而重写代码了。注意了这些问题的话,也许还不完全能保证系统的安全,但是至少可以提高安全性。

还可以考虑关闭 register_globals,magic_quotes 或者其它使编程更方便但会使某个变量的合法性,来源和其值被搞乱的设置。

2、用户输入表单问题

验证用户输入的任何数据,保证PHP代码的安全。

注意1:JS只是为了提高来访用户的体验而产生的,而不是验证的工具。因为任何一个来访的用户都可能会,也有可能无意间就禁用了客户端脚本的执行,从而跳过这层验证。所以我们必须在PHP的服务器端程序上检验这些数据。

注意2:不要使用$_SERVER['HTTP_REFERER']这个超级变量来检查数据的来源地址,一个很小的菜鸟黑客都会利用工具来伪造这个变量的数据,尽可能利用Md5,或者rand等函数来产生一个令牌,验证来源的时候,验证这个令牌是否匹配。

3、PHP文件权限问题

PHP 被设计为以用户级别来访问文件系统,所以完全有可能通过编写一段 PHP 代码来读取系统文件如 /etc/passwd,更改网络连接以及发送大量打印任务等等。因此必须确保 PHP 代码读取和写入的是合适的文件。 请看下面的代码,用户想要删除自己主目录中的一个文件。假设此情形是通过 web 界面来管理文件系统,因此 Apache 用户有权删除用户目录下的文件。

<?php 
    $username = $_POST[&#39;user_submitted_name&#39;]; 
    $homedir = "/home/$username"; 
    $file_to_delete = "$userfile"; 
    unlink ("$homedir/$userfile"); 
    echo "$file_to_delete has been deleted!"; 
?>
Copy after login

既然 username 变量可以通过用户表单来提交,那就可以提交别人的用户名和文件名,并删除该文件。这种情况下,就要考虑其它方式的认证:

-只给 PHP 的 web 用户很有限的权限。 -检查所有提交上来的变量。 -以下是更加安全的文件名和变量的验证和检查:

<?php 
    $username = $_SERVER[&#39;REMOTE_USER&#39;]; 
    $homedir = "/home/$username"; 
    if (!ereg(&#39;^[^./][^/]*$&#39;, $userfile)) 
        die(&#39;bad filename&#39;); 
    if (!ereg(&#39;^[^./][^/]*$&#39;, $username)) 
        die(&#39;bad username&#39;); 
?>
Copy after login

4、隐藏PHP扩展名

一般而言,通过隐藏的手段提高安全性被认为是作用不大的做法。但某些情况下,尽可能的多增加一份安全性都是值得的。

一些简单的方法可以帮助隐藏 PHP,这样做可以提高攻击者发现系统弱点的难度。在 php.ini 文件里设置 expose_php = off ,可以减少他们能获得的有用信息。

另一个策略就是让 web 服务器用 PHP 解析不同扩展名。无论是通过 .htaccess 文件还是 Apache 的配置文件,都可以设置能误导攻击者的文件扩展名:

# 使PHP看上去像其它的编程语言

AddType application/x-httpd-php .asp .py .pl

# 使 PHP 看上去像未知的文件类型

AddType application/x-httpd-php .bop .foo .133t

# 使 PHP 代码看上去像 HTML 页面

AddType application/x-httpd-php .htm .html

要让此方法生效,必须把 PHP 文件的扩展名改为以上的扩展名。这样就通过隐藏来提高了安全性,虽然防御能力很低而且有些缺点。

三、Mysql数据库安全性设置

PHP 本身并不能保护数据库的安全。下面的章节只是讲述怎样用 PHP 脚本对数据库进行基本的访问和操作。记住一条简单的原则:深入防御。保护数据库的措施越多,攻击者就越难获得和使用数据库内的信息。正确地设计和应用数据库可以减少被攻击的担忧。

1、数据库设计问题

应用程序永远不要使用数据库所有者或超级用户帐号来连接数据库,因为这些帐号可以执行任意的操作,比如说修改数据库结构(例如删除一个表)或者清空整个数据库的内容。以下截图的用户设置是危险的。

Collated some knowledge about PHP security

应该为程序的每个方面创建不同的数据库帐号,并赋予对数据库对象的极有限的权限。仅分配给能完成其功能所需的权限,避免同一个用户可以完成另一个用户的事情。这样即使攻击者利用程序漏洞取得了数据库的访问权限,也最多只能做到和该程序一样的影响范围。

2.数据库连接问题

把连接建立在 SSL 加密技术上可以增加客户端和服务器端通信的安全性,或者 SSH 也可以用于加密客户端和数据库之间的连接。如果使用了这些技术的话,攻击者要监视服务器的通信或者得到数据库的信息是很困难的。

3.数据库数据的加密

SSL/SSH 能保护客户端和服务器端交换的数据,但 SSL/SSH 并不能保护数据库中已有的数据。SSL 只是一个加密网络数据流的协议。

如果攻击者取得了直接访问数据库的许可(绕过 web 服务器),敏感数据就可能暴露或者被滥用,除非数据库自己保护了这些信息。对数据库内的数据加密是减少这类风险的有效途径,但是只有很少的数据库提供这些加密功能。

对于这个问题,有一个简单的解决办法,就是创建自己的加密机制,然后把它用在 PHP 程序内,最常见的例子就是把密码经过 MD5 加密后的散列存进数据库来代替原来的明文密码。

<?php 
$query = sprintf("INSERT INTO users(name,pwd) VALUES(&#39;%s&#39;,&#39;%s&#39;);", 
addslashes($username), md5($password)); 
$result = pg_query($connection, $query); 
$query = sprintf("SELECT 1 FROM users WHERE name=&#39;%s&#39; AND pwd=&#39;%s&#39;;", 
addslashes($username), md5($password)); 
$result = pg_query($connection, $query); 
if (pg_num_rows($result) > 0) { 
echo &#39;Welcome, $username!&#39;; 
} else { 
echo &#39;Authentication failed for $username.&#39;; 
} 
?>
Copy after login

4、SQL注入问题

直接 SQL 命令注入就是攻击者常用的一种创建或修改已有 SQL 语句的技术,从而达到取得隐藏数据,或覆盖关键的值,甚至执行数据库主机操作系统命令的目的。这是通过应用程序取得用户输入并与静态参数组合成 SQL 查询来实现的。下面将会给出一些真实的例子。

<?php 
$query = "SELECT id, name, inserted, size FROM products 
WHERE size = &#39;$size&#39; 
ORDER BY $order LIMIT $limit, $offset;"; 
$result = odbc_exec($conn, $query); 
?>
Copy after login

可以在原来的查询的基础上添加另一个 SELECT 查询来获得密码: union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable; 假如上述语句(使用 ' 和 –)被加入到 $query 中的任意一个变量的话,那么就麻烦了。

这些攻击总是建立在发掘安全意识不强的代码上的。所以,永远不要信任外界输入的数据,特别是来自于客户端的,包括选择框、表单隐藏域和 cookie。就如上面的第一个例子那样,就算是正常的查询也有可能造成灾难。

永远不要使用超级用户或所有者帐号去连接数据库。要用权限被严格限制的帐号。 检查输入的数据是否具有所期望的数据格式。PHP 有很多可以用于检查输入的函数,从简单的变量函数和字符类型函数(比如 is_numeric(),ctype_digit())到复杂的 Perl 兼容正则表达式函数都可以完成这个工作。

如果程序等待输入一个数字,可以考虑使用 is_numeric() 来检查,或者直接使用 settype() 来转换它的类型,也可以用 sprintf() 把它格式化为数字。

一个更安全的防止SQL注入的分页显示方法:

<?php 
settype($offset, &#39;integer&#39;); 
$query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; 
$query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;", 
$offset); 
?>
Copy after login

The above is the detailed content of Collated some knowledge about PHP security. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template