| Home Register Memberlist Help Search Quick Links No Replies |
|
||||||
| Basic How-tos Help Us Noobs by Sharing Your Wisdom |
![]() |
![]() |
|
Thread Tools | ![]() |
|
#1
|
||||
|
||||
|
From the prior guide at this location, we are first going to review a few points.
pf Basic Commands Review Configuration file location: Code:
/etc/pf.conf Code:
pfctl -vnf /etc/pf.conf Code:
pfctl -F all -f /etc/pf.conf Code:
pfctl -s all Good Comments To ensure our firewall policy is easily understood, we are going to use comments for all entries in the /etc/pf.conf file. Comments start with # such as the following: Code:
### This is a comment. Frontend Interface Last time, we discussed setting the interface macro for the frontend IP, so our firewall will start with this interface as the first setting: Code:
### fxp0 is our frontend IP interface based on ifconfig -a ext_if = "fxp0" Since pf will traverse the ruleset until it reaches the last rule, then handle any filtering based on the last (not the first) rule unless quick is added to the rule, the default rules usually start with a block policy: Code:
### block all incoming and outgoing packets from all source ### addresses and all source ports to all destination addresses ### and destination ports block all Code:
block in all block out all In order to ensure we do not block packets on local addresses within the network, we want to skip localhost: Code:
### skip localhost to prevent it from being blocked set skip on lo0 Code:
server# ifconfig -a | grep -i loopback lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384 Antispoof prevents incoming packets from pretending to be stemming from the network itself, so we will add the following rule next: Code:
### antispoof drop packets using the source IP or pretending ### to stem from the network for the frontend IP antispoof for $ext_if inet Code:
antispoof for $ext_if inet6 Block Reserved Internal IPs Now, let’s block those reserved IP addresses we mentioned in the last guide, but we will define a macro at the top of the file after the $ext_if defined one: Code:
### define a list of reserved IPs that shouldn't be used for
### incoming packets
reserved = "{ 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 255.255.255.255/32 }"
Code:
### Block any incoming packets with logging and force this rule ### on the frontend IP from the reserved IP blocks to any ### destination address or any destination port block in log quick on $ext_if from $reserved to any Last guide, we defined this macro: Code:
### define a list of common protocols
mainproto = "{ tcp, udp, icmp }"
Code:
### Allow outgoing packets and force the ruleset on the frontend ### IP for tcp, udp and icmp traffic from any source address or ### source port to any destination address or destination port pass out quick on $ext_if proto $mainproto from any to any We want to allow in packets that can handshake with our machine and cut down on denial of service attempts. This will be handled with the next two rules: Code:
### Allow incoming packets on the frontend IP via tcp from any ### source address or source port to any destination address ### on the destination port SSH with flag on SYN and using SYN ### with ACK only if the packets can return a synproxy handshake pass in on $ext_if proto tcp from any to any port ssh flags S/SA synproxy state ### allow incoming packets on the frontend IP via tcp from any ### source address or source port to any destination address ### on the destination port www with flag on SYN and using SYN ### with ACK only if the packets can return a synproxy handshake pass in on $ext_if proto tcp from any to any port www flags S/SA synproxy state Stop SSH and IMAP Brute Force Attempts The next rule is even more complex, but important to define as it will be the basis you can use for other brute force attempt rules: Code:
### Set up a rule table called <ssh_abuse> for connections ### that try to brute force the SSH service with repeated ### requests. Block incoming packets in that table and force ### the ruleset. Pass in on the frontend IP for tcp to any ### destination address on port SSH using SYN on flag with ### SYN and ACK in keep state where the max source connections ### are 10 OR the max source connection rate is 3 attempts in ### 5 seconds, putting those packets into the ssh_abuse table table <ssh_abuse> persist block in quick from <ssh_abuse> pass in on $ext_if proto tcp to any port ssh flags S/SA keep state (max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush) Code:
table <imap_abuse> persist block in quick from <imap_abuse> pass in on $ext_if proto tcp to any port imap flags S/SA keep state (max-src-conn 10, max-src-conn-rate 3/5, overload <imap_abuse> flush) action (in | out) [log] [quick] on interface [af] [proto protocol] [src_addr | dst_addr] [src_port | dst_port] [tcp_flags] [state] [ ] around an option means it is optional We covered all of the prior options in the last guide besides state. For state, the delimiters are the following: no state - works with TCP, UDP, and ICMP. PF will not track this connection statefully. For TCP connections, flags any is usually also required keep state works with TCP, UDP, and ICMP. This option is the default for all filter rules modulate state works only with TCP. PF will generate strong Initial Sequence Numbers (ISNs) for packets matching this rule synproxy state - proxies incoming TCP connections to help protect servers from spoofed TCP SYN floods. This option includes the functionality of keep state and modulate state When state is mentioned in a rule, then additional options for connections can then be added in ( ) after the state such as what we just listed above: Code:
keep state (max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush) max-src-conn-rate # / interval - limits the rate of new connections to a certain amount per time second interval overload <table_name> - puts an offending source host IP address into the table <table_name> flush [global] - kills any other states that match this rule and that were created by the source host IP. If global is added after flush, then all states are killed that match this source host IP no matter which rule created the state For the full SSH rule again: Code:
table <ssh_abuse> persist block in quick from <ssh_abuse> pass in on $ext_if proto tcp to any port ssh flags S/SA keep state (max-src-conn 10, max-src-conn-rate 3/5, overload <ssh_abuse> flush) After an IP hits that table, future connections from that IP are checked in the persistent table <ssh_abuse> and blocked for incoming connections, forcing that ruleset. Block Set IPs Please note that if you want to block a series of IPs on the machine, you could do the following: Code:
### Load the table <blockedips> from the file ### /etc/pf.blockedips.conf. Block incoming packets, log, ### forcing the ruleset, on the frontend IP for IPs in the ### <blockedips> table to any destination address or ### destination port. table <blockedips> persist file "/etc/pf.blockedips.conf" block in log quick on $ext_if from <blockedips> to any You will now need to create the file: Code:
touch /etc/pf.blockedips.conf Code:
echo 74.74.74.74 >> /etc/pf.blockedips.conf To dynamically add an IP as blocked (temporary add won't stick on server reboot or firewall reload): Code:
pfctl -t blockedips -T add 74.74.74.74 Code:
pfctl -t blockedips -T delete 74.74.74.74 Code:
pfctl -t blockedips -T show Code:
pfctl -t blockedips -T show -v Finally, we might want to create a similar table of allowed IPs to prevent our IPs from being blocked: Code:
### Load the table <allowed> from the file ### /etc/pf.allowedips.conf. Allow incoming packets, log, ### forcing the ruleset, on the frontend IP for IPs in the ### <allowedips> table to any destination address or ### destination port. table <allowedips> persist file "/etc/pf.allowedips.conf" pass in log quick on $ext_if from <allowedips> to any Code:
touch /etc/pf.allowedips.conf Code:
pfctl -t allowedips -T add 75.75.75.75 Code:
pfctl -t allowedips -T delete 75.75.75.75 Pass in All Other Packets Since you need to allow any other traffic that hasn't been blocked already using quick as otherwise you might be blocking services you need to have open, you need to end the configuration on the machine with the following: Code:
### allow all incoming packets from all source addresses and ### all source ports to all destinations addresses and ### destination ports pass in all Code:
### allow outgoing packets and force the ruleset on the frontend ### IP for tcp, udp and icmp traffice from any source address or ### source port to any destination address or detination port pass out quick on $ext_if proto $mainproto from any to any You can view the final firewall configuration at this location. Viewing Logs If you’ve set pf to log in /etc/rc.conf with this option: Code:
pflog_enable="YES" Code:
/etc/rc.d/pflog start Code:
tcpdump -n -e -ttt -r /var/log/pflog tcpdump -n -e -ttt -r /var/log/pflog port 80 tcpdump -n -e -ttt -r /var/log/pflog host 74.74.74.74 Code:
tcpdump -n -e -ttt -i pflog0 tcpdump -n -e -ttt -i pflog0 port 80 tcpdump -n -e -ttt -i pflog0 host 74.74.74.74
__________________
Miraenda ~ Ex uno disce omnes ~ |
![]() |
| Bookmarks |
| Thread Tools | |
|
|
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. |
||