Home php教程 php手册 6个常见的php安全攻击

6个常见的php安全攻击

Jun 06, 2016 pm 07:54 PM
http php Safety common attack

http://blog.csdn.net/czhphp/article/details/9673607 1、SQL注入 SQL注入是一种恶意攻击,用户利用在表单字段输入SQL语句的方式来影响正常的SQL执行。还有一种是通过system()或exec()命令注入的,它具有相同的SQL注入机制,但只针对shell命令。 [php] view

http://blog.csdn.net/czhphp/article/details/9673607


1、SQL注入 

SQL注入是一种恶意攻击,用户利用在表单字段输入SQL语句的方式来影响正常的SQL执行。还有一种是通过system()或exec()命令注入的,它具有相同的SQL注入机制,但只针对shell命令。

[php] view plaincopy

  1. $username = $_POST['username'];  
  2. $query = "select * from auth where username = '".$username."'";  
  3. echo $query;  
  4. $db = new mysqli('localhost''demo', ‘demo', ‘demodemo');  
  5. $result = $db->query($query);  
  6. if ($result && $result->num_rows) {  
  7.     echo "
    Logged in successfully"
    ;  
  8. else {  
  9.     echo "
    Login failed"
    ;  
  10. }  

上面的代码,在第一行没有过滤或转义用户输入的值($_POST['username'])。因此查询可能会失败,甚至会损坏数据库,这要看$username是否包含变换你的SQL语句到别的东西上。

防止SQL注入 

选项: 
  • 使用mysql_real_escape_string()过滤数据  或 htmlspecialchars
  • 手动检查每一数据是否为正确的数据类型
  • 使用预处理语句并绑定变量

使用准备好的预处理语句 
  • 分离数据和SQL逻辑
  • 预处理语句将自动过滤(如:转义)
  • 把它作为一个编码规范,可以帮助团队里的新人避免遇到以上问题

[php] view plaincopy

  1. $query = 'select name, district from city where countrycode=?';  
  2. if ($stmt = $db->prepare($query) )  
  3. {  
  4.     $countrycode = 'hk';  
  5.     $stmt->bind_param("s"$countrycode);    
  6.     $stmt->execute();  
  7.     $stmt->bind_result($name$district);  
  8.     while ( $stmt ($stmt->fetch() ){  
  9.         echo $name.', '.$district;  
  10.         echo '
    '
    ;  
  11.     }  
  12.     $stmt->close();  
  13. }  

2、XSS攻击 

XSS(跨站点脚本攻击)是一种攻击,由用户输入一些数据到你的网站,其中包括客户端脚本(通常JavaScript)。如果你没有过滤就输出数据到另一个web页面,这个脚本将被执行。

接收用户提交的文本内容 

[php] view plaincopy

  1. if (file_exists('comments')) {  
  2.     $comments = get_saved_contents_from_file('comments');  
  3. else {  
  4.     $comments = '';  
  5. }  
  6.   
  7. if (isset($_POST['comment'])) {  
  8.     $comments .= '
    '
     . $_POST['comment'];  
  9.     save_contents_to_file('comments'$comments);  
  10. }  
  11. >  

输出内容给(另一个)用户 

[php] view plaincopy

  1. 'xss.php' method='POST'>  
  2. Enter your comments here: 
      

  3.   
  4. 'submit' value='Post comment' />  


  5.   
  6.   
  7. echo $comments; ?>  

将会发生什么事? 
  • 烦人的弹窗
  • 刷新或重定向
  • 损坏网页或表单
  • 窃取cookie
  • AJAX(XMLHttpRequest)

防止XSS攻击 

为了防止XSS攻击,使用PHP的htmlentities()函数过滤再输出到浏览器。htmlentities()的基本用法很简单,但也有许多高级的控制,请参阅 XSS速查表。 

3、会话固定 

会话安全,假设一个PHPSESSID很难猜测。然而,PHP可以接受一个会话ID通过一个Cookie或者URL。因此,欺骗一个受害者可以使用一个特定的(或其他的)会话ID 或者钓鱼攻击。

6个常见的php安全攻击

4、会议捕获和劫持 

这是与会话固定有着同样的想法,然而,它涉及窃取会话ID。如果会话ID存储在Cookie中,攻击者可以通过XSS和JavaScript窃取。如果会话ID包含在URL上,也可以通过嗅探或者从代理服务器那获得。

防止会话捕获和劫持 
  • 更新ID
  • 如果使用会话,请确保用户使用SSL

5、跨站点请求伪造(CSRF) 

CSRF攻击,是指一个页面发出的请求,看起来就像是网站的信任用户,但不是故意的。它有许多的变体,比如下面的例子: 

[xml] view plaincopy

  1. img src='http://example.com/single_click_to_buy.php?user_id=123&item=12345'>  

防止跨站点请求伪造 

一般来说,确保用户来自你的表单,并且匹配每一个你发送出去的表单。有两点一定要记住: 
  1. 对用户会话采用适当的安全措施,例如:给每一个会话更新id和用户使用SSL。
  2. 生成另一个一次性的令牌并将其嵌入表单,保存在会话中(一个会话变量),在提交时检查它。

6、代码注入 

代码注入是利用计算机漏洞通过处理无效数据造成的。问题出在,当你不小心执行任意代码,通常通过文件包含。写得很糟糕的代码可以允许一个远程文件包含并执行。如许多PHP函数,如require可以包含URL或文件名,例如:

[php] view plaincopy

  1. Choose theme:  
  2.     
  3.           
  4.           
  5.           
  6.       
  7.       
  8.   
  9.     if($theme) {  
  10.         require($theme.'.txt');  
  11.     }  
  12. ?>  

在上面的例子中,通过传递用户输入的一个文件名或文件名的一部分,来包含以"http://"开头的文件。 

防止代码注入 
  • 过滤用户输入
  • 在php.ini中设置禁用allow_url_fopen和allow_url_include。这将禁用require/include/fopen的远程文件。
  
其他的一般原则 

1. 不要依赖服务器配置来保护你的应用,特别是当你的web服务器/ PHP是由你的ISP管理,或者当你的网站可能迁移/部署到别处,未来再从别处迁移/部署在到其他地方。请在网站代码中嵌入带有安全意识的检查/逻辑(HTML、JavaScript、PHP,等等)。  
2. 设计服务器端的安全脚本: 
—例如,使用单行执行 - 单点身份验证和数据清理   
—例如,在所有的安全敏感页面嵌入一个PHP函数/文件,用来处理所有登录/安全性逻辑检查 
3. 确保你的代码更新,并打上最新补丁。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles