Geek pop quiz - SOLVED!
Sep. 2nd, 2008 12:24 pmI have a stupid firewall. It can open a port, but not translate - I can't have port 999 on the outside redirect to an internal IP on port 443, for example.
I have a very smart Ubuntu 8.04 server. It can do anything I want it to, as long as I can tell it what to do.
I have a very, very, very stupid device that *only* listens on 443. The receiving port cannot be changed.
What I want:
1. Stupid firewall opens port 999 to smart server.
2. Smart server sees port 999 traffic and happily routes it off to 443 on device.
3. Device is happy to have 443 traffic, replies to smart server, smart server tosses it back out through the firewall to the end user.
OR
3. Device is happy to have 443 traffic, replies right back through the firewall to the end user without bugging Smart Server.
I don't care which 3 I get.
What are the magic words I need to tell iptables to do this for me? Or the magic words to have *something else* do it for me?
(And yes, I had a very similar question to this in January. Very similar devices, too. Not exactly the same, though. And that time, I just let Apache listen on 444 instead of bothering with fixing it, which is not an option, here.)
EDIT: The correct answer here is "rinetd"
10 seconds to install, 10 seconds to edit the config file with the port and IP I want forwarded, and BANG, everything works perfectly.
This is the kind of solution I really love.
I have a very smart Ubuntu 8.04 server. It can do anything I want it to, as long as I can tell it what to do.
I have a very, very, very stupid device that *only* listens on 443. The receiving port cannot be changed.
What I want:
1. Stupid firewall opens port 999 to smart server.
2. Smart server sees port 999 traffic and happily routes it off to 443 on device.
3. Device is happy to have 443 traffic, replies to smart server, smart server tosses it back out through the firewall to the end user.
OR
3. Device is happy to have 443 traffic, replies right back through the firewall to the end user without bugging Smart Server.
I don't care which 3 I get.
What are the magic words I need to tell iptables to do this for me? Or the magic words to have *something else* do it for me?
(And yes, I had a very similar question to this in January. Very similar devices, too. Not exactly the same, though. And that time, I just let Apache listen on 444 instead of bothering with fixing it, which is not an option, here.)
EDIT: The correct answer here is "rinetd"
10 seconds to install, 10 seconds to edit the config file with the port and IP I want forwarded, and BANG, everything works perfectly.
This is the kind of solution I really love.
(no subject)
Date: 2008-09-02 04:32 pm (UTC)http://csociety.ecn.purdue.edu/~sigos/projects/ssh/forwarding/
It worked with some non-ssh specific traffic last week. You are looking for the heading titled
(no subject)
Date: 2008-09-02 04:35 pm (UTC)What I really want is a rule so that the server knows that *all* traffic on the nonstandard port gets forwarded.
(no subject)
Date: 2008-09-02 04:35 pm (UTC)(no subject)
Date: 2008-09-02 07:00 pm (UTC)(no subject)
Date: 2008-09-02 07:51 pm (UTC)nc -l -v -p 999 | nc localhost 443
netcat -listen -verbose -port(local) 999 | netcat destination_machine portnumber
in Windows the persistent is via -L (instead of -l)
nm, I think I found what you need at http://isc.sans.org/diary.html?date=2005-02-18
There are many ways to get around this problem, such as using a different version of Netcat. However, one of my favorite simple ways to deal with this is to set up the Netcat listener in a while loop as follows:
$ while [ 1 ]; do echo "Started"; nc -l -p 41523 >> capture.txt; done
This will listen on TCP 41523, append whatever it receives to capture.txt, and then start listening again.
If you'd like to go further and actually log out while keeping this thing running, you can simply dump this while line in a file, called honeypot.sh. Then, chmod it so that it is executable (chmod 555 honeypot.sh). Finally, invoke it as follows:
$ nohup ./honeypot.sh &
Then, logout and go watch some TV. Take a nap. Run naked through the park. Do whatever it is that you do...
Come back, and your little Netcat buddy will be running with its results stored in capture.txt. To kill it, you could simply kill the pid of the nc listener itself. Thanks to Don Smith for the nohup idea. Note that Don did NOT suggest the park idea.
So something like
while [ 1 ]; do echo "Started"; nc -l -p 999 | nc localhost 443should do the trick
(no subject)
Date: 2008-09-02 04:55 pm (UTC)The below commenter is correct though-a simple netcat should work too.
(no subject)
Date: 2008-09-02 05:16 pm (UTC)Those instructions tell an ssh client to forward local ports to the server - to, in fact, tunnel traffic through and pop it out the far side with a new address.
They also let you make your ssh server listen on ports and forward them through the tunnel to the client machine's network.
But nowhere in there does it make the server listen on a new port and forward it to a new location without an SSH connection.
Have I missed something?
(no subject)
Date: 2008-09-02 05:56 pm (UTC)http://erdelynet.com/archive/ssh-l/2007-09/3878.html
Does that make sense?
Netcat is probably faster. Setup directions are here:
http://www.governmentsecurity.org/archive/t3303.html
(no subject)
Date: 2008-09-02 06:39 pm (UTC)I see what he's trying, but it doesn't actually work.
(no subject)
Date: 2008-09-02 04:48 pm (UTC)Or the fugly hack-method: first, define tcp/999 in /etc/services as, say, fuglyhack, and make sure netcat is installed. Then add this to /etc/inetd.conf (or Ubuntu equivalent, might be xinetd): fuglyhack stream tcp nowait root /usr/bin/nc nc 192.168.223.5 443. The downside is that your application won't see the original source IP. And performance, well, will suck if you got lots of connects.
(no subject)
Date: 2008-09-02 05:03 pm (UTC)(no subject)
Date: 2008-09-02 05:24 pm (UTC)(no subject)
Date: 2008-09-02 05:32 pm (UTC)(no subject)
Date: 2008-09-02 05:42 pm (UTC)(no subject)
Date: 2008-09-02 11:18 pm (UTC)(no subject)
Date: 2008-09-02 11:25 pm (UTC)I was actually hoping that:
#1: It would be easier than this. Y'know, somebody would have the magic spell for proxying a port right on top of their heads.
#2: I'd learn something that wasn't "money fixes problems better than geeks do"
I don't think either of those is true this time, though.
(no subject)
Date: 2008-09-03 12:10 am (UTC)There have been decades of people commoditising the results of geeks fixing problems, to the point that for most common problems, and many of the more esoteric ones, there will be a product that can solve it more easily than any given geek could.
The trick is that the true geek will try and learn how to do it anyway, so that they can cope
ifwhen they don't have access to that particular toy.(no subject)
Date: 2008-09-05 01:30 pm (UTC)Yay!
(no subject)
Date: 2008-09-02 05:40 pm (UTC)Do you know where/if iptables logs traffic?
(no subject)
Date: 2008-09-02 05:51 pm (UTC)LOG Turn on kernel logging of matching packets. When this option is set for a rule, the Linux kernel will print some information on all match- ing packets (like most IP header fields) via the kernel log (where it can be read with dmesg or syslogd(8)). This is a "non-terminating tar- get", i.e. rule traversal continues at the next rule. So if you want to LOG the packets you refuse, use two separate rules with the same matching criteria, first using target LOG then DROP (or REJECT).So something likeiptables -A FORWARD -j LOG
iptables -A INPUT -j LOG
iptables -A PREROUTING -j LOG
should log what's interesting in that case.
For my example, well, it was for example only, not complete. There's probably a couple lines missing, like:
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -d 192.168.223.5 -j ACCEPT
(no subject)
Date: 2008-09-02 06:34 pm (UTC)I have a headache.
(no subject)
Date: 2008-09-02 06:37 pm (UTC)sudo ssh -L <external port>:localhost:<internal port> username@localhost
Works great. Probably slower than netcat, as I said, since it is an encrypted tunnel.
(no subject)
Date: 2008-09-02 06:41 pm (UTC)#2) Take external port traffic, encrypt it, transmit it via 22 to yourself, decrypt it, and forward it off to the internal port? That's... kinda sick. But I like it. Now, if only we could make it work without having the user logged in....
(no subject)
Date: 2008-09-02 06:42 pm (UTC)zombie zombie zombie zombie.
(no subject)
Date: 2008-09-02 06:59 pm (UTC)So I suspect there may be something wrong with the router's port forwarding, at the moment.
(no subject)
Date: 2008-09-02 07:07 pm (UTC)mknod backpipe p
nc -l -p <externaltport> 0<backpipe | tee -a inflow | nc localhost <internalport> | tee -a outflow 1>backpipe
Good luck with the router!
(no subject)
Date: 2008-09-02 07:17 pm (UTC)(external port = 999
internal port = 443
target system = 192.168.1.251
Server = 192.168.1.4
router = 192.168.1.1)
Second, can you help me parse that out so I know what it's doing?
"mknod backpipe p" creates a FIFO file called "backpipe".
"nc -l -p 999" grabs port 999 and starts listening
"0
(external port = 999
internal port = 443
target system = 192.168.1.251
Server = 192.168.1.4
router = 192.168.1.1)
Second, can you help me parse that out so I know what it's doing?
"mknod backpipe p" creates a FIFO file called "backpipe".
"nc -l -p 999" grabs port 999 and starts listening
"0<backpipe" appears to take anything in backpipe and feed it to the netcat process?
Pipe that through
"tee -a inflow" - take the input from netcat listening on port 999 and write it to a file called inflow
which is in turn piped through
"nc localhost <internalport>" - which I think should be "nc 192.168.1.251 999" because it's not localhost that needs the forwarded port.
which is, in turn piped through
"tee -a outflow 1>backpipe" - write the output to "outflow" and put that, in turn, into our FIFO backpipe file.
(no subject)
Date: 2008-09-02 07:32 pm (UTC)Other than that, you sound about peachy.
Good luck!
I'm going to go back to parsing hell now. :)
(no subject)
Date: 2008-09-02 07:40 pm (UTC)sits waiting for further input - ps aux |grep nc shows only "nc 192.168.1.251 443"
And traffic doesn't actually go.
I can telnet to localhost:999 while it's running, but it doesn't *do* anything, and hitting https://server:999/ just times out.
(no subject)
Date: 2008-09-02 08:08 pm (UTC)nc -l -p <internal port> 0<backpipe | tee -a inflow | nc <service host> <service port> | tee -a outflow 1>backpipe
It didn't work for a couple of minutes and I had to reboot since I had somehow mucked up the kernel (I'm writing a module so am quite good at mucking it up) but I'm going to ask the windows question: did you try rebooting?
Yes, I know, stupid.
(no subject)
Date: 2008-09-02 08:20 pm (UTC)I may do that tonight.
(no subject)
Date: 2008-09-02 08:22 pm (UTC)(no subject)
Date: 2008-09-05 01:42 pm (UTC)(no subject)
Date: 2008-09-05 01:56 pm (UTC)(no subject)
Date: 2008-09-02 07:30 pm (UTC)(no subject)
Date: 2008-09-02 07:41 pm (UTC)But thanks for the help. Seriously.
(no subject)
Date: 2008-09-02 07:44 pm (UTC)(no subject)
Date: 2008-09-02 07:50 pm (UTC)(no subject)
Date: 2008-09-02 07:49 pm (UTC)However, while it's happy to let me set rules to "accept" or "reject" or "log" or "drop" packets based on conditions, and I can set these rules for forwarded packets, I'm having a devil of a time trying to tell it to *do* anything with the packets - like, "if packet X arrives on port Y, send it to IP Z n port Q"
this is just frustrating, at this point.
(no subject)
Date: 2008-09-02 11:24 pm (UTC)I've used rinetd with absolutely stellar results on many occasions.
It. Just. Works.
(no subject)
Date: 2008-09-05 01:18 pm (UTC)I could kiss you.
Once again, I owe you beer. Thanks.
(no subject)
Date: 2008-09-05 02:10 pm (UTC)But, you should note that your logs on the target device won't know the actual source IPs. I didn't get the impression you'd care about that, though.
Beer, I can handle, but kisses should be proxied through your hot sister.
(no subject)
Date: 2008-09-05 02:12 pm (UTC)My hot sisters are relatively hard to get to from Toronto. Beer, however, is universal.