202f7 [Solved] multiple rules fail - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Server & Networking > Firewalls

Firewalls IPFW, PF, IPF (but not limited) related discussion

Reply
 
Thread Tools Display Modes
  #1  
Old May 11th, 2012, 09:54
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default multiple rules fail

Hello,

I want to route my jails internal with pf like descriped in: http://forums.freebsd.org/showthread.php?t=30063

pf.conf
Code:
ext_if="em0"
#jail_if="lo111" #because no use in pf.conf

IP_PUB="192.168.178.10"

NET_JAIL="10.0.0.0/24"

IP_JAIL_WWW="10.0.0.1"
IP_JAIL_BUILD="10.0.0.2"

PORT_WWW="{80,443}"
PORT_BUILD=""

scrub in all

nat pass on $ext_if from $NET_JAIL to any -> $IP_PUB
                                        
rdr pass on $ext_if proto tcp from any to $IP_PUB port $PORT_WWW -> $IP_JAIL_WWW
rdr pass on $ext_if proto tcp from any to $IP_PUB port $PORT_BUILD -> $IP_JAIL_BUILD

pass out
pass in
fails with:
Code:
# /etc/rc.d/pf restart
Disabling pf.
Enabling pf/etc/pf.conf:19: syntax error
pfctl: Syntax error in config file: pf rules not loaded
.
When I remove the second rdr pass rule it works.

What am I doing wrong/what is missing?

Regards

Last edited by bsus; May 11th, 2012 at 10:07. Reason: fixed small typo
Reply With Quote
  #2  
Old May 11th, 2012, 10:09
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,725
Thanks: 47
Thanked 2,023 Times in 1,862 Posts
Default

Quote:
Originally Posted by bsus View Post
What am I doing wrong/what is missing?
This:
Code:
PORT_BUILD=""
Will make the rule look like this:
Code:
rdr pass on em0 proto tcp from any to 192.168.178.10 port -> 10.0.0.2
Which is a syntax error.

This isn't going to work either:
Code:
IP_JAIL_BUILD=10.0.0.2"
There's a quote missing.

And I suggest changing this:
Code:
nat pass on $ext_if from $NET_JAIL to any -> $IP_PUB
To this:
Code:
nat pass on $ext_if from $NET_JAIL to any -> ($ext_if)
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
The Following User Says Thank You to SirDice For This Useful Post:
bsus (May 11th, 2012)
  #3  
Old May 11th, 2012, 10:14
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Code:
ext_if="em0"

IP_PUB="192.168.178.10"

NET_JAIL="{ 10.0.0.0/24 }"

IP_JAIL_WWW="10.0.0.1"
IP_JAIL_BUILD="10.0.0.2"

PORT_WWW="{ 80,443 }"
PORT_BUILD="" #when no port don't mention in rule with port keyword

scrub in all

nat pass on $ext_if from $NET_JAIL to any -> ($ext_if)

rdr pass on $ext_if proto tcp from any to $IP_PUB port $PORT_WWW -> $IP_JAIL_WWW
rdr pass on $ext_if proto tcp from any to $IP_PUB -> $IP_JAIL_BUILD

pass out
pass in
You're right.

Have updated the config. How can I open ports for the host system? For example when enabling pf I can't access the server through ssh anymore.

Regards!

Last edited by DutchDaemon; May 11th, 2012 at 17:37.
Reply With Quote
  #4  
Old May 11th, 2012, 10:21
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,725
Thanks: 47
Thanked 2,023 Times in 1,862 Posts
Default

Quote:
Originally Posted by bsus View Post
For example when enabling pf I can't access the server through ssh anymore.
Remove this line:
Code:
rdr pass on $ext_if proto tcp from any to $IP_PUB -> $IP_JAIL_BUILD
It redirects all ports to your jail.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #5  
Old May 11th, 2012, 13:17
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

So, I now handled the basic firewall for the system. I only fail with the nat for the jails.

I used the OpenBSD PF FAQ for configuring it. I am just confused about the difference to the tutorial.

Any one an idea how I:
1. Map the internal network for the jails (10.0.0.0/24)
2. Open Ports for ftp (downloading files) and http(s) (webserver)
3. Blocking all other ports for the jails?

How do I do this?

All fail when adding:
Code:
nat pass on $HOST_IF from $JAIL_NET to any -> ($HOST_IF)
rdr pass on $HOST_IF proto tcp from any to $HOST_IP port $JAIL_PORTS_WWW -> $JAIL_IP_WWW
rdr pass on $HOST_IF proto tcp from any to $HOST_IP -> $JAIL_IP_BUILD
over the pass/block rulesets with an syntax error:
Code:
Disabling pf.
Enabling pf/etc/pf.conf:19: Rules must be in order: options, normalization, queueing, translation, filtering
/etc/pf.conf:22: syntax error
pfctl: Syntax error in config file: pf rules not loaded
Following man pf.conf the order should be right (translation above filtering)
I also can't find any rdr nat prefixed rules in the OpenBSD pf FAQ

Here is the config:
Code:
# general
EXT_IF="{ em0 }"
INT_IF="{ lo111 }"
HOST_IP="192.168.178.10"
HOST_PORTS="{ 50000 }"

# jails
JAIL_NET="{ 10.0.0.0/24 }"
JAIL_IP_BUILD="10.0.0.1"
JAIL_IP_WWW="10.0.0.2"
JAIL_PORTS_WWW="{ 80,443 }"

# tables
table <intranet> { 192.168.178.0/24, !192.168.178.1 }
table <bruteforce> persist

# options
set skip on lo0
scrub in all
antispoof for $EXT_IF inet

# nat
#match out on $EXT_IF from $INT_IF:network nat-to ($EXT_IF)
#pass out on $EXT_IF proto tcp from $JAIL_NET port $JAIL_PORTS_WWW to $HOST_IP

# rulesets block
block
block in quick from { urpf-failed, <bruteforce> }

# rulesets pass
pass in on $EXT_IF proto tcp from <intranet> modulate state
pass in on $EXT_IF proto tcp from any to any port $HOST_PORTS flags S/SA keep state (max-src-conn 100, max-src-conn-rate 15/5, overload <bruteforce> flush global)
pass out on $EXT_IF all keep state
I googled aroung a bit more and found out that there where significant changes in the nat syntax.
nat and rdr are depreacted now there is match, nat-to (and pass)

it should look a bit like this:
Code:
#match out on $EXT_IF from $INT_IF:network nat-to ($EXT_IF)
#pass out on $EXT_IF proto tcp from $JAIL_NET port $JAIL_PORTS_WWW to $HOST_IP
Code:
# service pf restart
Disabling pf.
Enabling pf/etc/pf.conf:35: syntax error
pfctl: Syntax error in config file: pf rules not loaded
...

Last edited by bsus; May 11th, 2012 at 14:19.
Reply With Quote
  #6  
Old May 11th, 2012, 14:20
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Any suggestions?
Reply With Quote
  #7  
Old May 11th, 2012, 14:31
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

For one, post whole configuration again, and use :set number in vi so it's easier to see where the error is.
Reply With Quote
  #8  
Old May 11th, 2012, 14:46
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Code:
      1 # general
      2 EXT_IF="{ em0 }"
      3 INT_IF="{ lo111 }"
      4 HOST_IP="192.168.178.10"
      5 HOST_PORTS="{ 50000 }"
      6
      7 # jails
      8 JAIL_NET="{ 10.0.0.0/24 }"
      9 JAIL_IP_BUILD="10.0.0.1"
     10 JAIL_IP_WWW="10.0.0.2"
     11 JAIL_PORTS_WWW="{ 80,443 }"
     12
     13 # tables
     14 table <intranet> { 192.168.178.0/24, !192.168.178.1 }
     15 table <bruteforce> persist
     16
     17 # options
     18 set skip on lo0
     19 scrub in all
     20 antispoof for $EXT_IF inet
     21
     22 # nat
     23 match out on $EXT_IF from $INT_IF:network nat-to ($EXT_IF)
     24 pass out on $EXT_IF proto tcp from $JAIL_NET port $JAIL_PORTS_WWW to $HOST_IP
     25
     26 # rulesets block
     27 block
     28 block in quick from { urpf-failed, <bruteforce> }
     29
     30 # rulesets pass
     31 pass in on $EXT_IF proto tcp from <intranet> modulate state
     32 pass in on $EXT_IF proto tcp from any to any port $HOST_PORTS flags S/SA keep state (max-src-conn 100, max-src-conn-rate 15/5, overload <bruteforce> flush global)
     33 pass out on $EXT_IF all keep state
~
Code:
# service pf restart
Disabling pf.
Enabling pf/etc/pf.conf:23: syntax error
pfctl: Syntax error in config file: pf rules not loaded
.
Marked the bad lines red
Reply With Quote
  #9  
Old May 11th, 2012, 15:38
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

Code:
 23 match out on $EXT_IF from $INT_IF:network nat-to ($EXT_IF)
 24 pass out on $EXT_IF proto tcp from $JAIL_NET port $JAIL_PORTS_WWW to $HOST_IP
Line 23 doesn't work since that's pf 5.0 syntax. Use this:
Code:
nat on $ext_if from $int_if:network to any -> ($ext_if)
You use $int_if for loopback which makes it harder to follow (usually it's internal LAN interface).

Line 24 doesn't make sense. What is it you are trying to do exactly?

Remember there are two two sets of rules which you need to implement to make sure jail with loopback IP can communicate. One is rdr on which redirects packets from incoming interface to loopback. This works for both LAN and external interface. Second is nat on which translates packets from loopback to LAN/external interface as loopback is not known outside firewall itself. Again, for both LAN and external interface. Both these rules are NAT, its just that pf uses "redirection" to specify destination address translation, whereas uses "nat" for source address translation.

Aside from these translations rules you still need to let firewall know what packets to even let through to be translated in the first place. For example if you want port 80 and 443 to be initiated from internet, you need to let pass in on on external interface. Only then these rules are permitted to go to loopback interface.

Finally, loopback itself needs permission to let packets in. You could have set skip on lo0 here, for instance.

Keep in mind firewall inspects packets on each individual interface.

hth
Reply With Quote
The Following User Says Thank You to bbzz For This Useful Post:
bsus (May 11th, 2012)
  #10  
Old May 11th, 2012, 16:13
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi,

Thank you for the rescuing answer!

Quote:
You use $int_if for loopback which makes it harder to follow (usually it's internal LAN interface).
Which macro name do you suggest?

Quote:
Line 24 doesn't make sense. What is it you are trying to do exactly?
This should forward all webserver ports from the jails to the Internet or at least to the ext_if.

Quote:
Remember there are two two sets of rules which you need to implement to make sure jail with loopback IP can communicate. One is rdr on which redirects packets from incoming interface to loopback. This works for both LAN and external interface. Second is nat on which translates packets from loopback to LAN/external interface as loopback is not known outside firewall itself. Again, for both LAN and external interface. Both these rules are NAT, its just that pf uses "redirection" to specify destination address translation, whereas uses "nat" for source address translation.
So rdr redirects from em0 to lo111 (jail) and nat redirects from lo111 (jail) to em0. This makes sense. But how would a rdr look now? I always get a syntax error when uncommenting the rdr line.

Quote:
Aside from these translations rules you still need to let firewall know what packets to even let through to be translated in the first place. For example if you want port 80 and 443 to be initiated from internet, you need to let pass in on on external interface. Only then these rules are permitted to go to loopback interface.
This would be:

Code:
pass in on $ext_if proto tcp from any to any port $JAIL_PORTS_WWW modulate state
?

Merged together:

Code:
# general
ext_if="{ em0 }"
int_if="{ lo111 }"
HOST_IP="192.168.178.10"
HOST_PORTS="{ 50000 }"

# jails
JAIL_NET="{ 10.0.0.0/24 }"
JAIL_IP_BUILD="10.0.0.1"
JAIL_IP_WWW="10.0.0.2"
JAIL_PORTS_WWW="{ 80,443 }"

# tables
table <intranet> { 192.168.178.0/24, !192.168.178.1 }
table <bruteforce> persist

# options
set skip on lo0
scrub in all
antispoof for $ext_if inet

# nat
nat on $ext_if from $int_if:network to any -> ($ext_if)
rdr pass on $ext_if proto tcp from any to $HOST_IP -> ($int_if)

# rulesets block
block
block in quick from { urpf-failed, <bruteforce> }

# rulesets pass
pass in on $ext_if proto tcp from <intranet> modulate state
pass in on $ext_if proto tcp from any to any port $HOST_PORTS flags S/SA keep state (max-src-conn 100, \
max-src-conn-rate 15/5, overload <bruteforce> flush global)
pass in on $ext_if proto tcp from any to any port $JAIL_PORTS_WWW modulate state
pass out on $ext_if all keep state
*) added lines
*) error causing lines

Code:
# service pf restart
Disabling pf.
Enabling pf/etc/pf.conf:23: syntax error
/etc/pf.conf:24: syntax error
pfctl: Syntax error in config file: pf rules not loaded
.

Last edited by DutchDaemon; May 11th, 2012 at 17:41. Reason: Mind your writing style: http://forums.freebsd.org/showthread.php?t=18043
Reply With Quote
  #11  
Old May 11th, 2012, 17:51
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

Try this:

Code:
ext_if="em0"
int_if="em1"
jail_if="lo111"

jbuild="10.0.0.1"
jwww="10.0.0.2"
www_ports="{ 80 443 }"

table <martians> const { 127.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12, \
                        10.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24, \
                        0.0.0.0/8, 240.0.0.0/4 }
table <ssh_abuse> persist
table <firewall> const { self }

# NORMALIZATION #
scrub on $ext_if all random-id min-ttl 254 max-mss 1452 reassemble tcp fragment reassemble

# TRANSLATION #
nat on $int_if from $jail_if to any -> ($int_if:0)
nat on $ext_if from $jail_if to any -> ($ext_if:0)
nat on $ext_if from $int_if:network to any -> ($ext_if:0)
rdr on $int_if proto tcp to $int_if port 80-> $jwww port 80
rdr on $int_if proto tcp to $int_if port 443-> $jwww port 443
rdr on $ext_if proto tcp to $ext_if port 80 -> $jwww port 80
rdr on $ext_if proto tcp to $ext_if port 443 -> $jwww port 443
no rdr

# OPTIONS #
set skip on lo0
set skip on lo111
antispoof log quick for $ext_if inet

# TRAFFIC RULES #
block in quick from <ssh_abuse>
block in quick on $ext_if from any to 255.255.255.255
block in quick on $ext_if from <martians>
block log all

#internal interface
pass  in  quick on $int_if from $int_if:network proto tcp to $int_if port $www_ports modulate state
pass  in  log quick on $int_if from $int_if:network proto tcp to $int_if port ssh modulate state \
			(max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush global)
block in  quick on $int_if to <firewall>
pass  in  quick on $int_if from $int_if:network

#external interface
pass  in  quick on $ext_if proto tcp to $ext_if port $www_ports modulate state
pass  in  log quick on $ext_if proto tcp to $ext_if port ssh modulate state \
			(max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush global)
pass  out quick on $ext_if
note 1: change internal interface to whatever you have
note 2: you can use this as template since I'm still not sure what setup exactly are you running
note 3: this lets any host on internal LAN communicate both ssh and www services on firewall, but blocks all other.
note 4: same for external interface
note 5: I haven't parsed this in pf, there might be errors so try it out first if there's errors.

Last edited by bbzz; May 11th, 2012 at 18:00.
Reply With Quote
The Following User Says Thank You to bbzz For This Useful Post:
bsus (May 11th, 2012)
  #12  
Old May 11th, 2012, 18:44
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi, thank you for the template! This helps me a lot and also others who look after a current example.

Code:
ext_if="em0"
#int_if="em1" # because server is no router, only a normal server :)
jail_if="lo111"

pub_ip="192.168.178.10"
pub_ports="50000" #custom ssh port

jbuild="10.0.0.1"
jwww="10.0.0.2"
jftp="10.0.0.3"
www_ports="{ 80 443 }"
ftp_ports="{ 21 22 }"

table <intranet> const { 127.0.0.0/8, 192.168.178.0/16, 10.0.0.0/8, 0.0.0.0/8 }
table <ssh_abuse> persist
table <firewall> const { self }

# NORMALIZATION #
scrub on $ext_if all random-id min-ttl 254 max-mss 1452 reassemble tcp fragment reassemble

# TRANSLATION #
nat on $ext_if from $jail_if to any -> ($ext_if:0)
rdr on $ext_if proto tcp from any to $pub_ip port $www_ports -> $jwww port $www_ports
rdr on $ext_if proto tcp from any to $pub_ip port $ftp_ports -> $jftp port $ftp_ports
no rdr

# OPTIONS #
set skip on lo0
set skip on lo111
antispoof log quick for $ext_if inet

# TRAFFIC RULES #
block in quick from <ssh_abuse>
block in quick on $ext_if from any to 255.255.255.0 # what does the netmask do?
block in quick on $ext_if from <intranet>
block log all

#external interface
pass  in  quick on $ext_if proto tcp to $ext_if port $www_ports modulate state
pass  in  log quick on $ext_if proto tcp to $ext_if port $pub_ports modulate state \
                        (max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush global)
pass  out quick on $ext_if
There are only two things to do:
  1. SSH (on port 50000) should be allowed but I don't have access when enabling pf.
  2. The second rdr line fails. I think that it is only valid to have one rdr line. But how do I open additional ports?

Last edited by DutchDaemon; May 11th, 2012 at 23:58. Reason: more formatting
Reply With Quote
  #13  
Old May 11th, 2012, 22:36
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

  1. Is the service listening on port 50000? The firewall controls the traffic, but you need to have the service listen on that port.
  2. You can have as many redirection rules as you need, as long as you are careful which IP/port translates to which IP/port. That is, use individual rules, don't group them. pf expands these rules when you load them anyway (check pfctl -sa).
  3. That is not a mask, 255.255.255.255 is broadcast address. If you are on cable service you'll have a lot of this noise.

Last edited by DutchDaemon; May 13th, 2012 at 00:49.
Reply With Quote
  #14  
Old May 12th, 2012, 06:10
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Quote:
Is the service listening on port 50000? The firewall controls the traffic, but you need to have the service listen on that port.
Yes, when I disable pf I can log in

Quote:
You can have as many redirection rules as you need, as long as you are careful which IP/port translates to which IP/port. That is, use individual rules, don't group them. pf expands these rules when you load them anyway (check pfctl -sa).
But why do I get a syntax error when adding another rpf rule? There different ports (80,443 vs 21,22)

Quote:
That is not a mask, 255.255.255.255 is broadcast address. If you are on cable service you'll have a lot of this noise.
So something like a wildcard?

Regards

Last edited by DutchDaemon; May 13th, 2012 at 00:49.
Reply With Quote
  #15  
Old May 12th, 2012, 14:43
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

Quote:
Originally Posted by bsus View Post
Yes, when I disable pf I can log in
The rule is ok. You are missing something else.

Quote:
But why do I get a syntax error when adding another rpf rule? There different ports (80,443 vs 21,22)
Individual ports.

Code:
rdr on $ext_if proto tcp from any to $ext_if port 80 -> $jwww port 80
rdr on $ext_if proto tcp from any to $ext_if port 443 -> $jwww port 443
Quote:
So something like a wildcard?
Regards
Something like it.

Also, FTP might require additional configuration.
Reply With Quote
  #16  
Old May 13th, 2012, 07:39
bsus bsus is offline
Member
 
Join Date: Feb 2011
Posts: 296
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Quote:
The rule is ok. You are missing something else.
Question is what is missing

Quote:
Individual ports.
I thought that this happens while parsing the config because the values in the lists are getting executed like a foreach loop?

Quote:
Also, FTP might require additional configuration.
Ah ok, the ftp port was mainly a placeholder but I will remind me if I have to fallback from http to ftp.

Thank you for your help and patience, I think I will get the last issues at my own
Reply With Quote
  #17  
Old May 13th, 2012, 13:05
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

You are most welcome.
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[IPF] Regarding IPF rules shesjustaglitch Firewalls 1 April 2nd, 2012 06:38
my ipf rules rill Firewalls 2 October 18th, 2010 13:50
buildworld fail SIFE Installing & Upgrading 4 June 15th, 2010 18:47
Are there any way to add new rules to pf without write the rules to the pf.conf tanakorn Firewalls 2 February 8th, 2010 09:09
[Solved] My first pf rules locutus Firewalls 6 October 13th, 2009 10:04


All times are GMT +1. The time now is 20:47.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0