2014-04-04 331 views
9

我在mod_wsgi/Apache上安装了应用程序安装程序,需要记录用户的IP地址。 request.remote_addr返回“127.0.0.1”和this fix尝试更正,但我发现Django出于安全原因删除了类似的代码。我如何安全地在Flask中使用用户的真实IP地址(使用mod_wsgi)?

有没有更好的方法来安全地获取用户的真实IP地址?

编辑:也许我失去了明显的东西。我申请werkzeug's/Flask's fix,但似乎不有所作为,当我尝试与改变头的请求:

run.py:

from werkzeug.contrib.fixers import ProxyFix 
    app.wsgi_app = ProxyFix(app.wsgi_app) 
    app.run() 

view.py:

for ip in request.access_route: 
     print ip # prints "1.2.3.4" and "my.ip.address" 

此相同结果发生,如果我有ProxyFix启用或不。我觉得我错过了一些完全明显的东西

回答

21

可以使用request.access_route attribute只有当你定义一个列表的信任代理。

access_route属性使用X-Forwarded-For header,回落到REMOTE_ADDR WSGI变量;后者很好,因为你的服务器决定这一点;在X-Forwarded-For可能已被几乎任何人都设定,但如果你信任的代理正确设置值,然后使用第一个(从端)成为信任:

trusted_proxies = {'127.0.0.1'} # define your own set 
route = request.access_route + [request.remote_addr] 

remote_addr = next((addr for addr in reversed(route) 
        if addr not in trusted_proxies), request.remote_addr) 

这样,即使有人用fake_ip1,fake_ip2欺骗X-Forwarded-For标头,代理服务器也会将,spoof_machine_ip添加到最后,并且上述代码会将remote_addr设置为spoof_machine_ip,而不管您的最外层代理还有多少个可信代理。

这是您的链接文章谈到的白名单方法(简要地说,Rails使用它)以及什么Zope implemented over 11 years ago

您的ProxyFix方法工作得很好,但您误解了它的功能。它只有集合request.remote_addr; request.access_route属性不变(X-Forwarded-For标头为而不是由中间件调整)。 但是,我会非常谨慎地盲目代理代理。

将同样的白名单的方法来中间件会是什么样子:

class WhitelistRemoteAddrFix(object): 
    """This middleware can be applied to add HTTP proxy support to an 
    application that was not designed with HTTP proxies in mind. It 
    only sets `REMOTE_ADDR` from `X-Forwarded` headers. 

    Tests proxies against a set of trusted proxies. 

    The original value of `REMOTE_ADDR` is stored in the WSGI environment 
    as `werkzeug.whitelist_remoteaddr_fix.orig_remote_addr`. 

    :param app: the WSGI application 
    :param trusted_proxies: a set or sequence of proxy ip addresses that can be trusted. 
    """ 

    def __init__(self, app, trusted_proxies=()): 
     self.app = app 
     self.trusted_proxies = frozenset(trusted_proxies) 

    def get_remote_addr(self, remote_addr, forwarded_for): 
     """Selects the new remote addr from the given list of ips in 
     X-Forwarded-For. Picks first non-trusted ip address. 
     """ 

     if remote_addr in self.trusted_proxies: 
      return next((ip for ip in reversed(forwarded_for) 
         if ip not in self.trusted_proxies), 
         remote_addr) 

    def __call__(self, environ, start_response): 
     getter = environ.get 
     remote_addr = getter('REMOTE_ADDR') 
     forwarded_for = getter('HTTP_X_FORWARDED_FOR', '').split(',') 
     environ.update({ 
      'werkzeug.whitelist_remoteaddr_fix.orig_remote_addr': remote_addr, 
     }) 
     forwarded_for = [x for x in [x.strip() for x in forwarded_for] if x] 
     remote_addr = self.get_remote_addr(remote_addr, forwarded_for) 
     if remote_addr is not None: 
      environ['REMOTE_ADDR'] = remote_addr 
     return self.app(environ, start_response) 

要明确:这个中间件也只有request.remote_addr; request.access_route保持不变。

+1

我想我只是困惑为什么所谓的“ProxyFix”没有解决这个问题。我应该打扰ProxyFix吗? –

+0

thx再Martijn! –

1

从这篇文章看来,安全问题听起来像是信任X-Forwarding-For头的第一个值。您可以实施文章中的“我的尝试更好的解决方案”部分中的建议,以克服此潜在的安全问题。我用django成功的一个库是django-ipware。通过实施该计划推出自己的烧瓶跟踪(你可以看到,它类似于该文章建议):

https://github.com/un33k/django-ipware/blob/master/ipware/ip.py#L7

+0

我申请从WERKZEUG修复,但它似乎没有任何效果。请参阅我的编辑#2 –

+0

这也使用白名单方法,尽管只允许专用网络IP地址。 –

-3

你不能安全地做到这一点,因为你不能信任所有零件在HTTP(或TCP)连接

+1

您可以信任自己的服务器来正确设置远程连接的IP地址。您可以选择信任特定的远程连接以说实话。结合白名单,您可以建立一个信任链。 –

+0

那么ip欺骗或恶意代理呢? – pbacterio

+0

TCP/IP连接中的IP欺骗要求您可以嗅探数据包;例如你也可以拦截已经发生的任何事情。恶意代理不应该添加到您的可信列表中,* duh *。 –