真正的取真实IP地址及利弊

网上流行的取真实IP地址方法有bug,未考虑多层透明代理情况,可能导致数据库报错。越来越多网站用代理加速,取“HTTP_X_FORWARDED_FOR”有弊端,客户端可伪造IP,影响应用程序记录。必要的安全日志应记录完整信息并检查IP格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前网上流行的所谓“取真实IP地址”的方法,都有bug,没有考虑到多层透明代理的情况。

多数代码类似:

None.gifstring IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null 
None.gif            
&& HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"!=String.Empty)
None.gif            
?HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
None.gif            :HttpContext.Current.Request.ServerVariables[
"REMOTE_ADDR"];

事实上,上面的代码只试用与用户只使用了1层代理,如果用户有2层,3层HTTP_X_FORWARDED_FOR 的值是:“本机真实IP,1层代理IP,2层代理IP,.....” ,如果这个时候你的数据中保存IP字段的长度很小(15个字节),数据库就报错了。

实际应用中,因为使用多层透明代理的情况比较少,所以这种用户并不多。

其他应用情况,现在越来越多的网站使用了代理加速方式,比如 新浪、SOHU的新闻 都使用Squid做代理方式,利用多台服务器分流。Squid本身类似透明代理,会发送“HTTP_X_FORWARDED_FOR” ,HTTP_X_FORWARDED_FOR 中包括客户的IP地址,如果此时客户已经使用了一层透明代理,那么程序取的 “HTTP_X_FORWARDED_FOR” 就包括两个IP地址。(我遇到过3个IP地址的情况,4个的未遇到过)

所以取“真正”IP地址的方式,还应该判断  “HTTP_X_FORWARDED_FOR”  中是否有“,”逗号,或者长度是否超长(超过15字节 xxx.xxx.xxx.xxx)。

所以代码应该如下:

 

ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif
/// 取得客户端真实IP。如果有代理则取第一个非内网地址
InBlock.gif
/// by flower.b
ExpandedBlockEnd.gif
/// </summary>

None.gifpublic static string IPAddress
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
get
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string result = String.Empty;
InBlock.gif
InBlock.gif        result 
= HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
InBlock.gif        
if(result!=null&&result!= String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//可能有代理
InBlock.gif
            if(result.IndexOf(".")==-1)    //没有“.”肯定是非IPv4格式
InBlock.gif
                result = null;
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(result.IndexOf(",")!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//有“,”,估计多个代理。取第一个不是内网的IP。
InBlock.gif
                    result = result.Replace(" ","").Replace("'","");
InBlock.gif                    
string[] temparyip = result.Split(",;".ToCharArray());
InBlock.gif                    
for(int i=0;i<temparyip.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if( Text.IsIPAddress(temparyip[i])
InBlock.gif                            
&& temparyip[i].Substring(0,3)!="10."
InBlock.gif                            
&& temparyip[i].Substring(0,7)!="192.168"
InBlock.gif                            
&& temparyip[i].Substring(0,7)!="172.16.")
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
return temparyip[i];    //找到不是内网的地址
ExpandedSubBlockEnd.gif
                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(Text.IsIPAddress(result)) //代理即是IP格式
InBlock.gif
                    return result;
InBlock.gif                
else
InBlock.gif                    result 
= null;    //代理中的内容 非IP,取IP
ExpandedSubBlockEnd.gif
            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
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"];
InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif        
if (null == result || result == String.Empty)
InBlock.gif            result 
= HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
InBlock.gif    
InBlock.gif        
if (result == null || result == String.Empty)
InBlock.gif            result 
= HttpContext.Current.Request.UserHostAddress;
InBlock.gif
InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

取“HTTP_X_FORWARDED_FOR” 的弊端。

HTTP_X_FORWARDED_FOR 是HTTP协议中头的一部分,不影响TCP的通讯。也就是说实际上客户端可以发送任意内容的 HTTP_X_FORWARDED_FOR,以就是伪造IP。最简单的是WEB程序的IP记录,本来是要记录真实IP的,反而被“黑客”欺骗。当你的应用程序记录客户的访问IP、拒绝或允许部分IP的访问、错误日志 都会出错,甚至误杀。

因此必要的安全日志应该记录 完整的 “HTTP_X_FORWARDED_FOR” (至少给数据库中的字段分配 3*15+2 个字节,以记录至少3个IP) 和 “REMOTE_ADDR”。对 HTTP_X_FORWARDED_FOR 的IP格式检查也是不可少的。

转载于:https://www.cnblogs.com/TalkStudy/archive/2005/12/30/308314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值