LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 01-09-2015, 02:57 PM   #1
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Protect host (and LAN) from virtualbox guest using NAT


I am running a Debian guest on a Debian host in virtualbox. The guest is running a webserver, and ports 80 and 443 of my home router are forwarded to non-standard ports on the host. The host in turn forwards these ports to the guest ports 80 and 443 using virtual box port forwarding with a NAT type networking mode.

In my security concept I am considering the guest as "untrusted". So in the event where it gets compromised through a security hole in apache or my own website code I want to make sure that the guest is completely isolated for the host (and other clients in the host's network).

I understand that if anything, the guest sees the host on the default gateway ip 10.0.2.2. After reading the virtualbox documentation's NAT section I decided to run virtualbox with a dedicated usergroup called vdebian and to set up the following iptables rules on the host to protect it from the guest. (Simplified for clarity)

Code:
#!/bin/sh

# Iptables
FW="/sbin/iptables"
$FW -F
$FW -X

$FW -P INPUT   ACCEPT
$FW -P FORWARD ACCEPT
$FW -P OUTPUT  ACCEPT

############################################### Chain for Handling of virtual debian traffic #####################################################
$FW -N VDEBIAN_OUT
$FW -A VDEBIAN_OUT -d 127.0.0.1 -j DROP -m comment --comment "Disallow traffic from guest to host" 
$FW -A VDEBIAN_OUT -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow guest to answer all inbound traffic"
$FW -A VDEBIAN_OUT -p tcp -m multiport --dports 80,443 -j ACCEPT -m comment --comment "Allow guest outbound http and https"
$FW -A VDEBIAN_OUT -p udp -m multiport --dports 53,123 -j ACCEPT -m comment --comment "Allow guest outbound dns and ntp"
$FW -A VDEBIAN_OUT -j DROP

################################################################ INPUT ###########################################################################
$FW -A INPUT -i lo -j ACCEPT 
$FW -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$FW -A INPUT -p tcp -m multiport --dports 20022,20080,20443 -m state --state NEW -j ACCEPT -m comment --comment "Forwarded Ports to guest"
$FW -A INPUT -p tcp -m multiport --dports 80,443 -m state --state NEW -j ACCEPT -m comment --comment "Inbound http (only for testing purposes)"
$FW -A INPUT -j DROP

################################################################ FORWARD #########################################################################

$FW -A FORWARD -j DROP

################################################################ OUTPUT ##########################################################################

# Jump to virtual debian chains
$FW -A OUTPUT -m owner --gid-owner vdebian -j VDEBIAN_OUT

... Other stuff
I tested this and it seems to behave as intended. The guest cannot open a web page on the host, but any other LAN client can. Nor can the guest access anything else on the host.

Now my question is: Can I reasonably assume that the host (and the rest of the LAN) is protected from the guest machine in case it gets compromised?
 
Old 01-09-2015, 06:58 PM   #2
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
I am not sure if I have the necessary knowledge to help you, but I do have one question: why do you think that disallowing traffic to 127.0.0.1 means disallowing traffic from host to guest? Doesn't 127.0.0.1 have a local meaning of loop (virtual network interface)? I don't understand the connection.

There are some services dependent on the loopback, such as rndc (the software that controls the dns daemon), but it still has a local significance. So I don't understand how restricting access to loop affects the connection itself between the host and the guest.
 
Old 01-09-2015, 07:05 PM   #3
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Original Poster
Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by vincix View Post
I am not sure if I have the necessary knowledge to help you, but I do have one question: why do you think that disallowing traffic to 127.0.0.1 means disallowing traffic from host to guest? Doesn't 127.0.0.1 have a local meaning of loop (virtual network interface)? I don't understand the connection.

There are some services dependent on the loopback, such as rndc (the software that controls the dns daemon), but it still has a local significance. So I don't understand how restricting access to loop affects the connection itself between the host and the guest.
Yeah, it took me a while to figure that out. If you take a look at the virtualbox documentation (I linked to the relevant section in my previous post) you'll find this statement:

Quote:
To an application on the host, or to another computer on the same network as the host, it looks like the data was sent by the VirtualBox application on the host, using an IP address belonging to the host.
So I created a logging rule in iptables for all traffic and loaded a webpage on the host from within the guest. Both the source and the destination addresses were shown as 127.0.0.1.

So I am using the gid rule to differentiate the virtualbox traffic from the other traffic to lo that you mentioned. Seems to work, but I am wondering if I missed anything...
 
Old 01-10-2015, 02:42 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
Another question: why didn't you not impose the same states in OUTPUT (VDEBIAN_OUT) as you did in INPUT? I'm referring to 80, 443 and 50, 123 respectively and to the NEW state. But, in my opinion, it probably doesn't make too much of a difference, unless you want to be a little bit more strict.

As you can see, I'm probably going to learn more from you than you'll learn from this thread

Last edited by vincix; 01-10-2015 at 02:52 AM.
 
Old 01-10-2015, 05:36 AM   #5
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Original Poster
Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by vincix View Post
Another question: why didn't you not impose the same states in OUTPUT (VDEBIAN_OUT) as you did in INPUT? I'm referring to 80, 443 and 50, 123 respectively and to the NEW state. But, in my opinion, it probably doesn't make too much of a difference, unless you want to be a little bit more strict.

As you can see, I'm probably going to learn more from you than you'll learn from this thread
Not something I did deliberately for a good reason. My real rules are a bit longer and I simplified them for clarity. That involved some copy pasting....
 
Old 01-10-2015, 08:53 AM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
Ok, I have a more significant question now:
I see you have the special ports permitted in INPUT. But how does the host actually know how to forward those ports (so basically change them) to 80/443 if you don't use dnat? (PREROUTING)

Maybe I didn't understand it well - are those special ports open on the guest too? Meaning are ports 80/443 changed to those special ports (in apache, if that's what you're using). From what you're saying, they are not, because you say that the host forwards http(s) to 80/443. And if that is so, my main question stands.

Last edited by vincix; 01-10-2015 at 09:15 AM.
 
Old 01-10-2015, 09:21 AM   #7
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
I think I would do this differently and set VB to use a bridged adaptor so that the guest gets a separate IP address from the router. Then the ports will just be forwarded to the guest. If your router has a DMZ you could even put the guest in there though I've never figured out exactly what the benefit of that set-up is.
That way you could set up any firewall rules on the guest to disallow communication with the host IP if you wish or vice versa without having to use 127.0.0.1 and the like which I would consider to be confusing.
 
1 members found this post helpful.
Old 01-11-2015, 09:26 AM   #8
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Original Poster
Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
That was actually what I tried first, but I was struggling to set it up in the router. And then I also thought that theoretically, if the vm got compromised, it could just connect to the trusted vlan. I obviously cannot physically separate it since it uses the same network interface as the host.


Does that make sense or am I missing something?
 
Old 01-11-2015, 11:18 AM   #9
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
I am not sure it really matters which way you do it I just find having the VM have it's own IP address from the router the easiest to comprehend.
 
Old 01-11-2015, 11:44 AM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Original Poster
Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Agreed. But getting the VLAN stuff to run on my router seems to be a pain in the butt. I tried for a day or two and gave up...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
guest to guest mount using nat and virtualbox pan64 Linux - Networking 1 08-28-2014 03:15 PM
Host Fedora14, Windows 7 is running as Virtualbox guest- Mic is not working on Guest mwaheed Linux - Virtualization and Cloud 1 10-20-2011 12:51 PM
[SOLVED] VirtualBox NAT to access Linux server as guest dashesy Linux - Virtualization and Cloud 2 09-25-2011 01:23 PM
VirtualBox: Slackware64 host and guest; NFS mount from host fails catkin Linux - Virtualization and Cloud 0 11-15-2010 06:54 AM
VirtualBox Networking (NAT) not working on XP host with Mandriva guest FreeRadical2600 Linux - Newbie 9 10-29-2008 08:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration