asp.net access to IP related information _ practical skills

Source: Internet
Author: User
ASP.net obtains the client's IP, most commonly uses the following code:
Copy Code code as follows:

String user_ip = system.web.httpcontext.current.request.servervariables["REMOTE_ADDR"];

For those who understand the proxy server situation, we will know that if the user uses a proxy server, the above code obtains the proxy server's IP address, and if the user uses multiple proxy servers, it is the IP address of the last proxy server that reaches the server.

REMOTE_ADDR Description:

Access the IP address of the client.
This information cannot be modified by the user.
If it is really changed, you and the server can not connect, the server is to follow this to establish a connection with the client and communication. Actually I test to modify this servervariables, have no effect at all. Still get is the actual value.
Another: Request.userhostaddress and request.servervariables["REMOTE_ADDR" are actually the same value.

How to bypass the proxy server to obtain the user's real IP address? At this time we are generally similar to the following code (here I simply do not make some boundary judgments)

private static string GetIP () {
if (system.web.httpcontext.current.request.servervariables["Http_via"]!= null)
return system.web.httpcontext.current.request.servervariables["Http_x_forwarded_for"]. Split (New char[]{', '}) [0];
Else
return system.web.httpcontext.current.request.servervariables["REMOTE_ADDR"];
}
Is that enough? This is a problem, http_x_forwarded_for, Http_via can be an impostor. If there's an SQL injection problem right here, it's very serious.

Let's take a look at the two servervariables of Http_via and Http_x_forwarded_for.

Http_via

If you have this message, you are using a proxy server, and the address of the proxy server is the following number.

Http_x_forwarded_for

If this information is available, it also proves that the address of the proxy proxy server you are using is the following number.

Note that the Http_x_forwarded_for value is not necessarily a single IP address, and the following information is also possible, one record per line. The data below is based on csdn actual data.
10.194.73.11
Unknown, unknown, 211.100.22.30
203.98.182.163, 203.98.182.163, 203.129.72.215
172.16.20.110, 202.116.64.196, 203.81.21.61
10.194.75.83, 10.194.73.11, 10.194.73.11, unknown
192.168.120.57, unknown, unknown, 211.10.10.195
10.2.4.211, 219.141.250.3
3.242.165.168, 218.108.22.164
Unknown, 211.100.22.30
192.168.83.56, 210.21.224.233
218.94.136.176, 203.81.21.61
Unknown, 210.75.1.181
10.161.196.218, 202.104.134.23
222.216.6.148, 222.216.6.146
155.161.59.47, Unknown


It should be noted that both values can be changed.

For these three values: REMOTE_ADDR, Http_via, http_x_forwarded_for, can be divided into the following five kinds of situations:

First, do not use the proxy server situation:

REMOTE_ADDR = Your IP
Http_via = no value or no display
Http_x_forwarded_for = no value or no display

Second, the use of transparent proxy server situation: transparent proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Your real IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

This type of proxy server will still be able to forward your information to your Access object, not to hide the real identity.

Third, the use of ordinary anonymous proxy server situation: Anonymous proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
http_x_forwarded_for = Proxy Server IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Hides your real IP, but reveals to the Access object that you are using a proxy server to access them.

Iv. use of deceptive proxy servers: distorting proxies

REMOTE_ADDR = Proxy Server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Random IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Told the Access object that you used the proxy server, but fabricated a fake random IP instead of your real IP to deceive it.

V. The use of High anonymous proxy server: Gao anonymity proxies (Elite proxies)

REMOTE_ADDR = Proxy Server IP
Http_via = no value or no display
Http_x_forwarded_for = no value or display, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Completely replaces all your information with the proxy server's information, as if you were using that proxy server directly to access the object.

Server-side userip.aspx page code:
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e) {
StringBuilder info = new StringBuilder (); We are concerned about three ServerVariables
Info. AppendFormat ("Http_via = {0} <br/>\r\n",
request.servervariables["Http_via"]);
Info. AppendFormat ("http_x_forwarded_for = {0} <br/>\r\n",
request.servervariables["Http_x_forwarded_for"]);
Info. AppendFormat ("REMOTE_ADDR = {0} <br/>\r\n",
request.servervariables["REMOTE_ADDR"]);
Info. Appendline ("*********** \r\n<br/>");
Other ServerVariables with reference value
foreach (string key in Request.ServerVariables.AllKeys) {
Info. AppendFormat ("{0} = {1} <br/>\r\n", Key, Request.servervariables[key]);
}
Response.Clear ();
Response.Write (Info. ToString ());
Response.End ();
}
The client sends the requested code:
[Code]
static void Main (string[] args) {
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create ("http://localhost:7867/MyTestWebSite/UserIP.aspx") ;
Request. Headers.add ("Remote_addr", "192.168.5.88");
Request. Headers.add ("VIA", "ghj1976");
Request. Headers.add ("X_forwarded_for", "0.0.0.0");
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
StreamReader stream = new StreamReader (response. GetResponseStream ());
string info = stream. ReadToEnd ();
Stream. Close ();
Response. Close ();
request = NULL;
Console.Write (info);
Console.ReadLine ();
}

Important information in the results of the test: the results of the test are more data, I only listed a few important.

Http_via = ghj1976 <br/>
http_x_forwarded_for = 0.0.0.0 <br/>
REMOTE_ADDR = 127.0.0.1 <br/>
***********
REMOTE_ADDR = 127.0.0.1 <br/>
Http_via = ghj1976 <br/>
http_remote_addr = 192.168.5.88 <br/>
http_x_forwarded_for = 0.0.0.0 <br/>

The test code above needs to be noted:

We have set up three HTTP header information in the client code, respectively:

REMOTE_ADDR, VIA, x_forwarded_for
Actually in the server segment, these three values should be read by the following three properties

Http_remote_addr, Http_via, http_x_forwarded_for pay attention to their differences!!

The Remote_addr property cannot be set. Or, the set is not what we want.

Summary:

1, REMOTE_ADDR can not be modified, but may get the proxy server IP, rather than the actual client IP.
2, through Http_via, http_x_forwarded_for we can obtain Proxy Server Agent information, but this relies on proxy server support. In addition, these two values can be modified. The information we get through it may not be true. Alternatively, the http_x_forwarded_for information may be a collection that does not contain the proxy server IP in REMOTE_ADDR.
There is no perfect solution to obtain the client IP address, we can only choose from the above 2 information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.