简体   繁体   English

如何在Go / Java中修改HTTP请求中的IP地址

[英]How to modify ip address in http request in Go/Java

I am doing a test tool to test a web server. 我正在做一个测试工具来测试Web服务器。 The tool can construct a simple http request, and sends to the server. 该工具可以构造一个简单的http请求,并将其发送到服务器。 But each request should have a different src ip addr. 但是每个请求应该有一个不同的src ip addr。

My question is. 我的问题是。 Is there some way I can build an ip package from a http request, modify the ip addr, and sends directly into net? 有什么方法可以通过http请求构建ip包,修改ip addr并直接发送到net?

I use java or go (new to go). 我使用java或go(新功能)。 Many thanks! 非常感谢! :) :)

IP addresses are handled at a lower level than HTTP - specifically, it's done by the TCP/IP protocol. IP地址的处理级别比HTTP低-具体来说,它是由TCP / IP协议完成的。 Can I trust the source IP of an HTTP request? 我可以信任HTTP请求的源IP吗? provides a good overview on why the IP you get from an HTTP client is trustworthy (and difficult to spoof). 很好地概述了从HTTP客户端获得的IP为什么值得信赖(并且难以欺骗)的原因。

That said, the HTTP headers support the idea of "suggesting" the IP address obtained from TCP is not correct - most common way of doing this is the X-Forwarded-For header. 也就是说,HTTP标头支持“建议”从TCP获得的IP地址不正确的想法-最常见的方式是X-Forwarded-For标头。 Assuming your server relies on this value (which it probably shouldn't!), you can spoof your IP quite easily: 假设您的服务器依赖于此值(可能不应该这样),则可以很容易地欺骗IP:

req, err := http.NewRequest("GET", "http://example.com", nil)
// ...
req.Header.Add("X-Forwarded-For", "1.2.3.4")
resp, err := client.Do(req)

Of course, most server won't rely on this field, as the potential for abuse is large. 当然,大多数服务器不会依赖此领域,因为滥用的可能性很大。

If you're writing a test tool for your own server, the best way is to add logic to your server that will check for the existence of the X-Forwarded-For header (or something similar) and override the IP address if it's set. 如果您要为自己的服务器编写测试工具,最好的方法是向服务器添加逻辑,以检查X-Forwarded-For标头(或类似名称)是否存在,并覆盖设置的IP地址。 Make sure you disable this feature in production, though. 但是,请确保在生产中禁用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM