Home > Backend Development > PHP Tutorial > A brief discussion on Web attacks in PHP security protection

A brief discussion on Web attacks in PHP security protection

迷茫
Release: 2023-03-05 10:28:02
Original
1858 people have browsed it

Common web attacks are divided into two categories: one is to exploit the vulnerabilities of the web server to attack, such as CGI buffer overflow, directory traversal vulnerability exploitation and other attacks; the other is to exploit the security vulnerabilities of the web page itself, such as SQL injection, Cross-site scripting attacks, etc. The following article mainly introduces PHP security protection against Web attacks. Friends who need it can refer to it. Let’s take a look together.

SQL injection attack (SQL Injection)

The attacker inserts SQL commands into the input field of the web form or the string of the page request, tricking the server into executing malicious SQL Order. In some forms, user input is used directly to construct (or affect) dynamic SQL commands, or as input parameters for stored procedures. Such forms are particularly vulnerable to SQL injection attacks.

Common SQL injection attack process categories are as follows:

1. A certain web application has a login page. This login page controls whether the user has the right to access the application. It requires The user enters a name and password;

2. The content entered on the login page will be directly used to construct dynamic SQL commands, or directly used as parameters of stored procedures;

For example:

$query = 'SELECT * from Users WHERE login = ' . $username . ' AND password = ' . $password;
Copy after login

3. The attacker enters something like ' or '1'='1 in the user name and password input box;

4. User input After the content is submitted to the server, the server runs the above code to construct a SQL command to query the user. However, because the content entered by the attacker is very special, the final SQL command becomes:

SELECT * from Users WHERE login = '' or '1'='1' AND password = '' or '1'='1';
Copy after login

5. Server execution Query or stored procedure to compare the identity information entered by the user with the identity information saved in the server;

6. Since the SQL command has actually been modified by an injection attack, the user's identity cannot be truly verified. , so the system will incorrectly authorize the attacker.

If the attacker knows that the application will directly use the content entered in the form for identity verification queries, he will try to enter some special SQL strings to tamper with the query to change its original function and deceive The system grants access.

The system environment is different, and the damage that an attacker may cause is also different. This is mainly determined by the security permissions of the application to access the database. If the user's account has administrator or other relatively advanced rights, the attacker may perform various operations on the database tables that he wants to do, including adding, deleting or updating data, or even directly deleting the table

Prevention methods:

## 1. Check the variable data type and format


2. Filter special symbols


3. Bind variables and use precompiled statements

Cross Site Scripting (XSS)

The attacker injects malicious code into the web page, and other Code will be executed when the user loads the web page, and the attacker may obtain various contents including but not limited to higher permissions (such as performing some operations), private web content, sessions, cookies, etc. These malicious codes are usually JavaScript, HTML and other client-side scripting languages.

For example:

<?php
echo "欢迎您,".$_GET[&#39;name&#39;];
Copy after login

If a script<script>[code]</script> is passed in, the script will also be executed. Using such a URL will execute the JavaScript alert function and pop up a dialog box: http://localhost/test.php?name=<script>alert(123456)</script>


Commonly used attack methods include:

Steal cookies to obtain sensitive information;


Use iframe, frame, XMLHttpRequest or the above-mentioned Flash to (Under attack) The user's identity performs some management actions, or performs some general operations such as posting on Weibo, adding friends, sending private messages, etc.;


Use the domain that can be attacked to be trusted by other domains Features: Requesting some operations that are not normally allowed as a trusted source, such as conducting inappropriate voting activities; The effects of DDoS attacks.


Prevention method: Use the htmlspecialchars function to convert special characters into HTML encoding, and filter the output variables


Cross Site Request Forgeries (CSRF)

The attacker forges the HTTP request of the target user, and then sends this request to a website with a CSRF vulnerability. After the website executes this request, a cross-site request forgery attack is triggered. The attacker uses a covert HTTP connection to allow the target user to click this link without noticing. Since the user clicked it himself and is a legitimate user with legitimate permissions, the target user can execute specific HTTP commands within the website. link to achieve the attacker's purpose.

It is different from the XSS attack method. interests of the victimized users.


For example:


某个购物网站购买商品时,采用http://www.shop.com/buy.php?item=watch&num=100,item参数确定要购买什么物品,num参数确定要购买数量,如果攻击者以隐藏的方式发送给目标用户链接
,那么如果目标用户不小心访问以后,购买的数量就成了100个

防范方法:

1、检查网页的来源

2、检查内置的隐藏变量

3、使用POST,不要使用GET,处理变量也不要直接使用$_REQUEST

Session固定攻击(Session Fixation)

这种攻击方式的核心要点就是让合法用户使用攻击者预先设定的session id来访问被攻击的应用程序,一旦用户的会话ID被成功固定,攻击者就可以通过此session id来冒充用户访问应用程序。

例如:

1.攻击者访问网站http:///www.bank.com,获取他自己的session id,如:SID=123;

2.攻击者给目标用户发送链接,并带上自己的session id,如:http:///www.bank.com/?SID=123;

3.目标用户点击了http:///www.bank.com/?SID=123,像往常一样,输入自己的用户名、密码登录到网站;

4.由于服务器的session id不改变,现在攻击者点击http:///www.bank.com/?SID=123,他就拥有了目标用户的身份,可以为所欲为了。

防范方法:

1.定期更改session id

session_regenerate_id(TRUE);//删除旧的session文件,每次都会产生一个新的session id。默认false,保留旧的session
Copy after login

2.更改session的名称

session的默认名称是PHPSESSID,此变量会保存在cookie中,如果攻击者不抓包分析,就不能猜到这个名称,阻挡部分攻击

session_name("mysessionid");
Copy after login

3.关闭透明化session id

透明化session id指当浏览器中的http请求没有使用cookie来制定session id时,sessioin id使用链接来传递

int_set("session.use_trans_sid", 0);
Copy after login

4.只从cookie检查session id

int_set("session.use_cookies", 1);//表示使用cookies存放session id
int_set("session.use_only_cookies", 1);//表示只使用cookies存放session id
Copy after login

5.使用URL传递隐藏参数

$sid = md5(uniqid(rand()), TRUE));
$_SESSION["sid"] = $sid;//攻击者虽然能获取session数据,但是无法得知$sid的值,只要检查sid的值,就可以确认当前页面是否是web程序自己调用的
Copy after login

Session劫持攻击(Session Hijacking)

会话劫持是指攻击者利用各种手段来获取目标用户的session id。一旦获取到session id,那么攻击者可以利用目标用户的身份来登录网站,获取目标用户的操作权限。

攻击者获取目标用户session id的方法:

1.暴力破解:尝试各种session id,直到破解为止;

2.计算:如果session id使用非随机的方式产生,那么就有可能计算出来;

3.窃取:使用网络截获,xss攻击等方法获得

防范方法:

      1.定期更改session id

      2.更改session的名称

      3.关闭透明化session id

      4.设置HttpOnly。通过设置Cookie的HttpOnly为true,可以防止客户端脚本访问这个Cookie,从而有效的防止XSS攻击。

文件上传漏洞攻击(File Upload Attack)

文件上传漏洞指攻击者利用程序缺陷绕过系统对文件的验证与处理策略将恶意代码上传到服务器并获得执行服务器端命令的能力。

常用的攻击手段有:

上传Web脚本代码,Web容器解释执行上传的恶意脚本;

上传Flash跨域策略文件crossdomain.xml,修改访问权限(其他策略文件利用方式类似);

上传病毒、木马文件,诱骗用户和管理员下载执行;

上传包含脚本的图片,某些浏览器的低级版本会执行该脚本,用于钓鱼和欺诈。

总的来说,利用的上传文件要么具备可执行能力(恶意代码),要么具备影响服务器行为的能力(配置文件)。

防范方法:

      1.文件上传的目录设置为不可执行;

      2.判断文件类型,设置白名单。对于图片的处理,可以使用压缩函数或者resize函数,在处理图片的同时破坏图片中可能包含的HTML代码;

      3.使用随机数改写文件名和文件路径:一个是上传后无法访问;再来就是像shell.php.rar.rar和crossdomain.xml这种文件,都将因为重命名而无法攻击;

      4.单独设置文件服务器的域名:由于浏览器同源策略的关系,一系列客户端攻击将失效,比如上传crossdomain.xml、上传包含Javascript的XSS利用等问题将得到解决。

以上就是关于PHP安全防护之Web攻击的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。感谢大家支持php中文网!

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