Home Backend Development PHP Tutorial General method of obtaining user IP address and common security risks (HTTP_X_FORWARDED_FOR)_PHP tutorial

General method of obtaining user IP address and common security risks (HTTP_X_FORWARDED_FOR)_PHP tutorial

Jul 21, 2016 pm 03:10 PM
for http and analyze address Safety common method user Obtain process this Universal

Analysis process
This comes from some projects. It is common and frequently used to obtain user IP and record user operation behavior. Most friends will see the following general method of obtaining an IP address.

Copy code The code is as follows:

function getIP() {
if (isset($_SERVER[ 'HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP '];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
return $realip;
}


这个是网上常见获取,ip函数,用这些值获取IP,我们首先要弄清楚,这些数据是从那个地方传过来的。

IP获取来源

1.'REMOTE_ADDR' 是远端IP,默认来自tcp 连接是,客户端的Ip。可以说,它最准确,确定是,只会得到直接连服务器客户端IP。如果对方通过代理服务器上网,就发现。获取到的是代理服务器IP了。

如:a->b(proxy)->c ,如果c 通过'REMOTE_ADDR' ,只能获取到b的IP,获取不到a的IP了。

另外:该IP想篡改将很难实现,在传递知道生成php server值,都是直接生成的。

2.'HTTP_X_FORWARDED_FOR','HTTP_CLIENT_IP' 为了能在大型网络中,获取到最原始用户IP,或者代理IP地址。对HTTp协议进行扩展。定义了实体头。

HTTP_X_FORWARDED_FOR = clientip,proxy1,proxy2 所有IP用”,”分割。 HTTP_CLIENT_IP 在高级匿名代理中,这个代表了代理服务器IP。既然是http协议扩展一个实体头,并且这个值对于传入端是信任的,信任传入方按照规则格式输入的。以下以x_forword_for例子加以说明,正常情况下,这个值变化过程。

image


分析Bug风险点:

通过刚刚分析我们发现,其实这些变量,来自http请求的:x-forword-for字段,以及client-ip字段。 正常代理服务器,当然会按rfc规范来传入这些值。但是,当一个用户直接构造该x-forword-for值,发送给用户用户,那将会怎么样呢?

image图(1)

第2步,修改x-forword-fox值,我们看看结果

image

 

第三步,我们再修改下看看会怎么样?

image

Haha, did you see the above result? x-forwarded-for can not only set the value by itself, but also can set the value in any format. In this way, it is like having a field that can write any value. And the server directly reads, or writes to the database, or displays. It will bring danger, just like operating the data source without any filtering and testing on the input. And it is easy to bring concealment.

Conclusion:

The above getip function, except that the client can forge IP at will, and can pass in IP in any format. This will cause two major problems. First, if you set up a certain page and impose IP restrictions. The other party can easily change the IP and continuously request the page. Secondly, if you use this kind of data directly, it will bring vulnerabilities such as SQL registration and cross-site attacks. As for the first one, you can set restrictions on the business, and it is best not to use IP restrictions. For the second one, this type can bring huge cyber risks. We must correct it.

Getip needs to be modified to obtain a safe getip function.

Such problems are actually very easy to occur. I have used this to defraud a large number of fake votes in the past. It has its hiddenness. In fact, as long as we understand the ins and outs of certain values. Once you understand how it works, it will be very easy to fix this type of bug.

Off topic, there are three steps to do technology. First, you must be able to do it and solve it; secondly, you must think about why you want to do it, what are the reasons and principles; finally, how to do it, and are there any other methods? Ask yourself more and you will find that you are getting closer to the technical truth. You will become more and more comfortable in doing things!

Author: chengmo QQ:8292669

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327104.htmlTechArticleThe analysis process comes from some projects. It is common and frequently used to obtain user IP and record user operation behavior. of. Most friends will see the following general method of obtaining an IP address...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

Mobile phone film has become one of the indispensable accessories with the popularity of smartphones. To extend its service life, choose a suitable mobile phone film to protect the mobile phone screen. To help readers choose the most suitable mobile phone film for themselves, this article will introduce several key points and techniques for purchasing mobile phone film. Understand the materials and types of mobile phone films: PET film, TPU, etc. Mobile phone films are made of a variety of materials, including tempered glass. PET film is relatively soft, tempered glass film has good scratch resistance, and TPU has good shock-proof performance. It can be decided based on personal preference and needs when choosing. Consider the degree of screen protection. Different types of mobile phone films have different degrees of screen protection. PET film mainly plays an anti-scratch role, while tempered glass film has better drop resistance. You can choose to have better

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

How should the Java framework security architecture design be balanced with business needs? How should the Java framework security architecture design be balanced with business needs? Jun 04, 2024 pm 02:53 PM

Java framework design enables security by balancing security needs with business needs: identifying key business needs and prioritizing relevant security requirements. Develop flexible security strategies, respond to threats in layers, and make regular adjustments. Consider architectural flexibility, support business evolution, and abstract security functions. Prioritize efficiency and availability, optimize security measures, and improve visibility.

How to add and manage users in Google Manager How to add and manage users in Google Manager Sep 02, 2024 pm 02:41 PM

How to add and manage users in Google Manager? Google Chrome supports multiple users to log in, so we don’t have to worry about logging in across devices. If we have many users, we need to add management. Some friends may not know how to operate. Don't worry, the editor has compiled a detailed step-by-step tutorial for everyone today. If you are interested, come and take a look with the editor. Detailed step-by-step tutorial instructions 1. After turning on the computer, find the installed Google Chrome icon on the desktop and double-click to open it, as shown in the picture below. 2. Click the three dots icon in the upper right corner of Google Chrome, as shown in the picture below. 3. Click the [Settings] option in the drop-down menu of Google Chrome, as shown in the figure below. 4. In the Google Chrome settings interface that opens, click [Manage ch

An effective way to quickly remove age spots (teach you the skin care secrets to quickly and easily eliminate age spots) An effective way to quickly remove age spots (teach you the skin care secrets to quickly and easily eliminate age spots) May 03, 2024 pm 12:01 PM

Age spots are a common skin problem that tend to appear on the faces of older adults on exposed areas such as the neck and back of hands. It will also make people look older. It not only brings trouble to people's appearance. With the advancement of science and technology and the development of beauty technology, there are now many simple and quick ways to remove age spots, however. To restore young and healthy skin and help you eliminate age spots quickly, this article will share some effective skin care tips. 1. The importance of actively protecting the skin from ultraviolet damage, so it is very important to avoid prolonged exposure to the sun. Ultraviolet rays are one of the main causes of age spots. 2. Choose skin care products reasonably and use products containing antioxidants and whitening ingredients. Skin care products containing antioxidants and whitening ingredients can help reduce the appearance of age spots.

Security configuration and hardening of Struts 2 framework Security configuration and hardening of Struts 2 framework May 31, 2024 pm 10:53 PM

To protect your Struts2 application, you can use the following security configurations: Disable unused features Enable content type checking Validate input Enable security tokens Prevent CSRF attacks Use RBAC to restrict role-based access

See all articles