0

I have a vhost on my webserver whose sole and unique goal is to return the client IP adress:

petrus@bzn:~$ cat /home/vhosts/domain.org/index.php
<?php echo $_SERVER['REMOTE_ADDR']; echo "\n" ?>

This helps me troubleshoot networking issues, especially when NAT is involved. As such, I don't always have domain name resolution and this service needs to work even if queried by its IP address.

I'm using it this way:

petrus@hive:~$ echo "GET /" | nc 88.191.133.41 80
191.51.4.55

petrus@hive:~$ echo "GET /" | nc ydct.org 80
191.51.4.55

router#more http://88.191.133.41/index.php
88.191.124.254

However I found that it wasn't working from at least a computer:

petrus@seth:~$ echo "GET /" | nc ydct.org 80
petrus@seth:~$

petrus@seth:~$ echo "GET /" | nc 88.191.133.41 80
petrus@seth:~$

What I checked:

This is not related to ipv6:

petrus@seth:~$ echo "GET /" | nc -4 ydct.org 80
petrus@seth:~$ 

petrus@hive:~$ echo "GET /" | nc ydct.org 80
2a01:e35:ee8c:180:21c:77ff:fe30:9e36

netcat version is the same (except platform, i386 vs x64):

petrus@seth:~$ type nc
nc est haché (/bin/nc)
petrus@seth:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@seth:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2010-06-26 14:01 /etc/alternatives/nc -> /bin/nc.openbsd

petrus@hive:~$ type nc
nc est haché (/bin/nc)
petrus@hive:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@hive:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2011-05-26 01:23 /etc/alternatives/nc -> /bin/nc.openbsd

It works when used without the pipe:

petrus@seth:~$ nc ydct.org 80
GET /
2a01:e35:ee8c:180:221:85ff:fe96:e485

And the piping works at least with a test service (netcat listening on 1234/tcp and output to stdout)

petrus@bzn:~$ nc -l -p 1234
GET /
petrus@bzn:~$

petrus@seth:~$ echo "GET /" | nc ydct.org 1234
petrus@seth:~$

I don't know if this issue is more related to netcat or Apache, but I'd appreciate any pointers to troubleshoot this issue !

bzn is the server, hive is a working client and seth is the client on which I have the issue.

Edit: It works also with telnet but telnet doesn't allow piping.

2nd edit in answer to MickeyB:

Indeed host headers are not transmitted, but as seen with hive host above, I've configured the default vhost of Apache to behave like ydct.org vhost:

petrus@bzn:/etc/apache2/sites-available$ cat default
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName 88.191.133.41
        ServerAlias 2a01:e1b:1:132:1a0a:a9ff:fec8:f0a9
        DocumentRoot /home/vhosts/ydct.org/
</VirtualHost>

However, it works with curl!

petrus@seth:~$ curl ydct.org
2a01:e35:ee8c:180:221:85ff:fe96:e485

Output of apache2 -S posted to http://pastebin.com/aSf446Jv

But I'd like to know why it doesn't work with netcat...

petrus
  • 5,297
  • 26
  • 42

3 Answers3

2

In short, its the version of netcat you are running.

I tested the command string on my machines as follows:

Mac OS X Lion:

yvaine:sqlite user$ echo -e "GET /" | nc 88.191.133.41 80
XX.XX.XX.168

FreeBSD:

[root@freebsd82 /usr/ports]# echo -e "GET /" | nc 88.191.133.41 80
XX.XX.XX.168

CentOS:

[root@kvm0001 ~]# echo -e "GET /" | nc 88.191.133.41 80
XX.XX.XX.168

Debian (version 6)

root@debian:~# echo -e "GET /" | nc 88.191.133.41 80
XX.XX.XX.168

Its not until I got to Ubuntu "lucid" (which is what you are apparently are running) is when I got this:

root@ubuntu:~# echo -e "GET /" | nc 88.191.133.41 80
root@ubuntu:~# 

It appears that Ubuntu defaults to nc.openbsd rather that nc.traditional (which is the default on Debian). Once I use the traditional version, I received the desired output:

root@ubuntu:~# echo -e "GET /" | nc.traditional 88.191.133.41 80
XX.XX.XX.168

What is curious that both of your client machines have nc.openbsd, yet they different in behavior. Of course, it could be that one of them either has a different version of nc.openbsd or a different version of Ubuntu altogether. In either that, you can sync your versions of nc.openbsd or switch over to nc.traditional.

petrus
  • 5,297
  • 26
  • 42
Rilindo
  • 5,078
  • 5
  • 28
  • 46
  • Yes! You found it: it works with `nc.traditional` on Ubuntu 10.10, on `seth`. However, it's working on 11.04 with `nc.openbsd` on `hive`... – petrus Nov 25 '11 at 23:26
1

Works for me :)

michael@challenger:~$ echo -e "GET /\n" | nc 88.191.124.41 80
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

If you are trying to hit the vhost you are doing so incorrectly. You need to send a Host: header to identify the virtual host which you want to use.

The best way of using your 'what is my IP' service would be curl. Hey look, I see the same problem on my end when I do what you're doing:

michael@challenger:~$ echo -e "GET /" | nc ip.mydomain.ca 80
michael@challenger:~$ curl ip.mydomain.ca
192.168.0.135


FIX YOUR VHOSTS!!!
michael@challenger:~$ curl -v 88.191.124.41
* About to connect() to 88.191.124.41 port 80 (#0)
*   Trying 88.191.124.41... connected
* Connected to 88.191.124.41 (88.191.124.41) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: 88.191.124.41
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 25 Nov 2011 22:11:31 GMT
< Server: Apache/2.2.14 (Ubuntu)
< Last-Modified: Wed, 25 Aug 2010 08:11:38 GMT
< ETag: "840d09-b1-48ea16e57f5a6"
< Accept-Ranges: bytes
< Content-Length: 177
< Vary: Accept-Encoding
< Content-Type: text/html
< X-Pad: avoid browser bug
< 
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
* Connection #0 to host 88.191.124.41 left intact
* Closing connection #0
MikeyB
  • 39,291
  • 10
  • 105
  • 189
  • Mmh, it works with `curl`. But it should work without host headers. See my edit. – petrus Nov 25 '11 at 21:14
  • You don't have vhosts set up correctly. That's part of the problem. – MikeyB Nov 25 '11 at 21:33
  • Examining the output of 'httpd -S' should help identify why things aren't going to work as you expect in all cases. You need to fix your vhosts. – MikeyB Nov 25 '11 at 21:39
  • MikeyB is right. Apache must be able to know from the client where to redirect to the right virtualhost - hence, the host header request. Otherwise, the server will go to the default DocumentRoot. – Rilindo Nov 25 '11 at 21:42
  • @Rilindo: default DocumentRoot is `/home/vhosts/domain.org`. default and `domain.org` vhost use the same file. Moreover, it works without the host header on `hive`... – petrus Nov 25 '11 at 21:49
  • is http://88.191.124.41/ a valid address? Getting to http://88.191.124.41/index.php returns a 404 message. – Rilindo Nov 25 '11 at 21:56
  • MikeyB, @Rilindo: I see where is my mistake. As said above, 88.191.124.41 is not my IP, I "anonimized" it, without checking if it was answering. I should have used a RFC1918 address... – petrus Nov 25 '11 at 22:30
  • MikeyB, @Rilindo, I edited my answer to set the real domain, `ydct.org`. It resolves to `88.191.133.41`. – petrus Nov 25 '11 at 22:34
  • Maybe it would help if you put the dump of your httpd.conf file in either pastebin or in a gist. – Rilindo Nov 25 '11 at 22:35
  • @Rilindo: output of `apache2 -S` posted to http://pastebin.com/aSf446Jv – petrus Nov 25 '11 at 22:55
  • See my response below. Looks like the issue is client side. – Rilindo Nov 25 '11 at 23:06
0

Set a wait time and try again:

$ echo "GET /" | nc -w 3 88.191.124.41 80
quanta
  • 51,413
  • 19
  • 159
  • 217