Suppose the end user IP address is x.x.x.x and proxy IP address is y.y.y.y. For the web server's point of view, proxy server is the client with IP address is y.y.y.y. Since the end user is connecting to the web server via the proxy server, the X-Forwarded-For field will have the IP address x.x.x.x when the request reaches the web server. At the web server, if a comparison is made the client IP address (y.y.y.y) is obviously different from the IP address in X-Forwarded-For field (x.x.x.x). Hence this indicates the use of a proxy server.
Now you may think by spoofing the X-Forwarded-For field by setting the value to y.y.y.y (which is the proxy IP) it is possible to hide the use of a proxy server. This is not always true, which can be seen from the following demo Python script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lxml.html as lh | |
import urllib2 | |
url = "http://www.murl.mobi/headers.php" | |
http_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:25.0)" + | |
"Gecko/20100101 Firefox/25.0", "X-Forwarded-For":"65.182.107.98"} | |
request_object = urllib2.Request(url, None, http_headers) | |
xpath_s = "/html/body/table/tr[2]/td/table/tr/td[2]/div[2]/text()" | |
#Installing proxy server details to be used by urllib2 | |
proxy = urllib2.ProxyHandler({'http': '65.182.107.98:3128'}) | |
opener = urllib2.build_opener(proxy) | |
urllib2.install_opener(opener) | |
doc = lh.parse(urllib2.urlopen(request_object)) | |
headers = doc.xpath(xpath_s) | |
print 'Output of the request with the use of a proxy server and spoofed x-forwarded-for header' | |
print '\n'.join(headers) |
Output of the above Python script is:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output of the request with the use of a proxy server and spoofed x-forwarded-for header | |
Accept-Encoding: identity | |
Host: www.murl.mobi | |
Connection: close | |
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0 | |
X-Forwarded-For: 65.182.107.98, xxx.xxx.xxx.xxx |