C # obtain and analyze real IP addresses

Source: Internet
Author: User
There are bugs in the so-called "getting real IP addresses" method on the Internet, but the multi-layer transparent proxy is not taken into account.
Majority Code For example: String IPaddress = (Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ] ! = Null
&& Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ] ! = String. Empty)
? Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ]
: Httpcontext. Current. Request. servervariables [ " Remote_addr " ];

 

In fact, the above Code only tries to use a layer-1 proxy with the user. if the user has a layer-2, layer-3 http_x_forwarded_for, the value is: "The real IP address of the Local Machine, layer-1 proxy IP address, layer-2 proxy IP address ,..... ", if the length of the IP field stored in your data is very small (15 bytes), the database reports an error.

In practice, there are few users who use multi-layer transparent proxy.

In other cases, more and more websites are using proxy acceleration. For example, Sina and Sohu news all use squid as proxy and use multiple servers for traffic distribution. Squid itself is similar to a transparent proxy and will send "http_x_forwarded_for". http_x_forwarded_for includes the customer's IP address. If the customer already uses a transparent proxyProgramThe obtained "http_x_forwarded_for" includes two IP addresses. (I have encountered 3 IP addresses and 4 have not)

Therefore, to obtain the "real" IP address, you should also determine whether "," comma exists in "http_x_forwarded_for" or whether the length is too long (more than 15 bytes of XXX. XXX ).

The Code should be as follows:

 

/**/
///   <Summary>
/// Obtain the real IP address of the client. If a proxy exists, the first non-Intranet address is used.
///   </Summary>
Public   Static   String IPaddress
{
Get
{
String Result = String. empty;
Result = Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ];
If (Result ! =   Null   && Result ! = String. Empty)
{
// There may be proxies
If (Result. indexof ( " . " ) =   - 1 ) // No "." is definitely not in IPv4 format
Result =   Null ;
Else
{
If (Result. indexof ( " , " ) ! =   - 1 )
{
// There are ",". It is estimated that there are multiple proxies. Obtain the first IP address that is not an intranet IP address.
Result = Result. Replace ( "   " , "" ). Replace ( " ' " , "" );
String [] Temparyip = Result. Split ( " ,; " . Tochararray ());
For ( Int I =   0 ; I < Temparyip. length; I ++ )
{
If (Text. isipaddress (temparyip [I])
&& Temparyip [I]. substring ( 0 , 3 ) ! =   " 10. "
&& Temparyip [I]. substring ( 0 , 7 ) ! =   " 192.168 "
&& Temparyip [I]. substring ( 0 , 7 ) ! =   " 172.16. " )
{
Return Temparyip [I]; // Locate an address that is not an intranet address
}
}
}
Else   If (Text. isipaddress (result )) // Proxy is IP Format Isipaddress: how to determine whether it is an IP address ,
Return Result;
Else
Result =   Null ; // The content in the proxy is not an IP address.
}

}

String IPaddress = (Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ] ! =   Null   && Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ] ! = String. Empty) ? Httpcontext. Current. Request. servervariables [ " Http_x_forwarded_for " ]: Httpcontext. Current. Request. servervariables [ " Remote_addr " ];

If ( null = result || result = string. empty)
result = httpcontext. current. request. servervariables [ " remote_addr " ];

If (result = null || result = string. empty)
result = httpcontext. current. request. userhostaddress;

ReturnResult;
}
}

 

Disadvantages of "http_x_forwarded_for.

Http_x_forwarded_for is part of the HTTP header and does not affect TCP communication. In other words, the client can actually send http_x_forwarded_for any content, which is a counterfeit IP address. The simplest is the IP address record of the web program, which was originally intended to record the real IP address. Instead, it was cheated by hackers. When your application records the client's access IP address, denies or permits access from some IP addresses, error logs may occur, or even kill by mistake.

Therefore, the necessary security logs should record the complete "http_x_forwarded_for" (at least 3*15 + 2 bytes are allocated to the fields in the database to record at least 3 IP addresses) and "remote_addr ". It is also essential to check the IP Format of http_x_forwarded_for.

 

 

 

 

 

 

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.