1. SQL injection
#By inserting SQL commands into Web form submissions or entering query strings for domain names or page requests, it ultimately deceives the server into executing Malicious SQL commands.
Defense: First filter, then perform parameter binding.
2. XSS
Cross Site Scripting
Principle: Embed scripts into web pages in different ways to achieve attack purposes.
Defense: Filter input.
$id = (int) $_REQUEST['id'];if( $id > 0 ){}
$name = htmlentities($_REQUEST['name'], ENT_QUOTES, "UTF-8");// 注意,如果这里不进行转化也是可以的,// 只要在输出的时候进行转化(建立在已经参数绑定的情况下)。
3. CSRF
##Cross-site request forgery Cross-site request forgery
Principle: Helen logged into Weibo and then browsed a harmful site. A fake post on Weibo was forged on the harmful site. At this time, Helen posted a Weibo without knowing it.
Defense: Embed a random token in the Weibo page, and the Weibo server verifies the token value.
4. Clickjacking
5. Upload filesClickJacking
##Principle: Approximately There are two ways. One is that the attacker uses a transparent iframe, covering it on a web page, and then induces the user to operate on the page. At this time, the user will click on the transparent iframe page without knowing it; the other is The attacker uses an image to cover the web page, blocking the original location of the web page.Defense: Use js to determine whether the frame is under the same domain name. Add header directive: X-Frame-Options.
// js if (top.location.hostname !== self.location.hostname) { alert("您正在访问不安全的页面,即将跳转到安全页面!"); top.location.href = self.location.href;}// Apache 配置:Header always append X-Frame-Options SAMEORIGIN// nginx 配置:add_header X-Frame-Options SAMEORIGIN;
Principle: Various types of illegal software may be uploaded.6. zip *Defense: detection type, detection size.
Principle: Some zip files look very small but are very large after decompression7. Session hijackingDefense: Do not do this: decompress, process, and then compress compressed files uploaded by users. Because when you decompress, your server is likely to be crowded.
Principle: When the client and server communicate, the hacker captures the packet and obtains the sessionid , and eventually the hacker communicates with the server.8. Password storageDefense:
Set HttpOnly and reset sessionid from time to time.
$hashedPassword = password_hash('password', PASSWORD_DEFAULT);password_verify('the wrong password', $hashedPassword); // falsepassword_verify('my super cool password', $hashedPassword); // true
9. Brute force cracking/**防御:1. 限制次数2. 验证码3. 防火墙分析 类似 fail2ban*/
server Security1. Modify port 22
vi /etc/ssh/sshd_configfirewall-cmd --list-allfirewall-cmd --add-port=8888/tcp --permanentfirewall-cmd --reload# 如果是阿里云服务器,不要忘记修改阿里云安全组
2. Modify mysql root name# 修改 mysql 数据库中的 user 表,然后 flush privileges
3. Prohibit root remote loginvi /etc/ssh/sshd_configPermitRootLogin
web security
1. sql injection
By inserting SQL commands into Web form submissions or entering domain names or query strings for page requests, the server is ultimately tricked into executing malicious SQL commands.2. XSSDefense: First filter, then perform parameter binding.
Cross Site Scripting3. CSRFPrinciple: Embed scripts into web pages in different ways to achieve attack purposes.
Defense: Filter input.
$id = (int) $_REQUEST['id'];if( $id > 0 ){}$name = htmlentities($_REQUEST['name'], ENT_QUOTES, "UTF-8");// 注意,如果这里不进行转化也是可以的,// 只要在输出的时候进行转化(建立在已经参数绑定的情况下)。
##Cross-site request forgery Cross-site request forgery
Principle: Helen logged into Weibo and then browsed a harmful site. A fake post on Weibo was forged on the harmful site. At this time, Helen posted a Weibo without knowing it.
Defense: Embed a random token in the Weibo page, and the Weibo server verifies the token value.
4. Clickjacking
ClickJacking
原理:大概有两种方式,一是攻击者使用一个透明的 iframe ,覆盖在一个网页上,然后诱使用户在该页面上进行操作,此时用户将在不知情的情况下点击透明的 iframe 页面;二是攻击者使用一张图片覆盖在网页,遮挡网页原有位置。
防御:使用 js 判断框架是否在同一个域名下。添加头部指令: X-Frame-Options。
// js if (top.location.hostname !== self.location.hostname) { alert("您正在访问不安全的页面,即将跳转到安全页面!"); top.location.href = self.location.href;}// Apache 配置:Header always append X-Frame-Options SAMEORIGIN// nginx 配置:add_header X-Frame-Options SAMEORIGIN;
5. 上传文件
原理:可能会被上传各种类型的非法软件。
防御:检测类型,检测大小。
6. zip *
原理:有些 zip 文件看起来很小,解压后非常大
防御:不要做这样的操作:对用户上传的压缩文件解压,处理,再压缩。因为你解压的时候,很可能你的服务器就被挤爆了。
7. session 劫持
原理:客户端和服务端通信时候,黑客抓包,获取 sessionid ,最终黑客与服务器通信。
防御:
设置 HttpOnly,时常重设 sessionid。
8. 密码存储
$hashedPassword = password_hash('password', PASSWORD_DEFAULT);password_verify('the wrong password', $hashedPassword); // falsepassword_verify('my super cool password', $hashedPassword); // true
9. 暴力破解
/**防御:1. 限制次数2. 验证码3. 防火墙分析 类似 fail2ban*/
服务器安全
1. 修改 22 端口
vi /etc/ssh/sshd_configfirewall-cmd --list-allfirewall-cmd --add-port=8888/tcp --permanentfirewall-cmd --reload# 如果是阿里云服务器,不要忘记修改阿里云安全组
2. 修改 mysql root 名称
# 修改 mysql 数据库中的 user 表,然后 flush privileges
3. 禁止 root 远程登陆
vi /etc/ssh/sshd_configPermitRootLogin
相关推荐:
The above is the detailed content of Detailed explanation of PHP security examples. For more information, please follow other related articles on the PHP Chinese website!

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
