Today I want to share with you some useful tips to protect your server from Denial of Service attacks. This tutorial assumes that you are running Metin2 server under FreeBSD 9.x (might work on earlier versions too) on a dedicated server. I only worked with dedicated servers so I can't help you much with Hamachi or local servers.
Enabling the pf firewall
Add the following line to /etc/rc.conf
Code:
pf_enable="YES"
Code:
ee /etc/pf.conf
The "code" lines in this section are intended to be entered in /etc/pf.conf. This file may exist or not in your system, if it's already present, just delete everything on it and add the following lines, making the necessary changes for your setup as shown under each part of the file.
Code:
ext_if="em0"

Code:
service_ports="{ 22 }"
game_ports="{ 11000, 13000, 13010, 13020, 13030, 13099 }"
Code:
table <trusted_hosts> const { 8.8.8.8, 8.8.8.4 }
table <abusive_hosts> persist
If your own IP gets blocked out you can always use SSH from your other server(s) to unblock it (more on this later), or the rescue mode provided by some hosting companies.
Code:
# options
set block-policy drop
set loginterface $ext_if
set skip on lo
scrub on $ext_if reassemble tcp no-df random-id
antispoof quick for { lo0 $ext_if }
Code:
block in
pass out all keep state
pass out on $ext_if all modulate state
pass in quick from <trusted_hosts>
block in quick from <abusive_hosts>
pass in inet proto icmp all icmp-type echoreq
pass in on $ext_if proto tcp to any port $service_ports flags S/SA keep state \
(max-src-conn 40, max-src-conn-rate 20/5, overload <abusive_hosts> flush)
pass in on $ext_if proto {tcp,udp} to any port $game_ports flags S/SA keep state \
(max-src-conn 40, max-src-conn-rate 20/5, overload <abusive_hosts> flush)
This is all for pf.conf, you can find the whole text file here:

If you are using official login code and need UDP use this instead:

Troubleshooting
A legitimate player could get blocked if he logs in a lot of accounts, or you can get blocked yourself out of the system if you make many connections for whatever reason. In this case run this command to remove the IP from the abusive_hosts table:
Code:
pfctl -t abusive_hosts -T delete 8.8.8.8
Code:
pfctl -t abusive_hosts -T add 8.8.8.8
Finally, once you are happy with the setup, you can enable the firewall with the following command:
Code:
service pf start
Code:
kldload pf.ko
Safe settings on sysctl
Disclaimer: I'm by no means a FreeBSD expert, I collected this stuff from some sources and put it together, and so far it's working for me.
Just copy and paste this on your /etc/sysctl.conf, unless you have some custom stuff in there already.
These system settings limit the chances of some attacks to succeed.
Code:
security.bsd.see_other_uids=0 tcp.path_mtu_discovery=0 sysctl net.inet.tcp.syncookies=1 net.inet.ip.forwarding=1 net.inet.ip.fastforwarding=1 net.inet.tcp.nolocaltimewait=1 net.inet.tcp.syncache.rexmtlimit=1 net.inet.ip.check_interface=1 net.inet.ip.portrange.randomized=1 net.inet.ip.process_options=0 net.inet.ip.random_id=1 net.inet.ip.redirect=0 net.inet.ip.accept_sourceroute=0 net.inet.ip.sourceroute=0 net.inet.icmp.bmcastecho=0 net.inet.icmp.maskfake=0 net.inet.icmp.maskrepl=0 net.inet.icmp.log_redirect=0 net.inet.icmp.drop_redirect=1 net.inet.tcp.drop_synfin=1 net.inet.tcp.ecn.enable=1 net.inet.tcp.fast_finwait2_recycle=1 net.inet.tcp.icmp_may_rst=0 net.inet.tcp.maxtcptw=15000 net.inet.tcp.msl=5000 net.inet.tcp.path_mtu_discovery=0 net.inet.tcp.rfc3042=0 net.inet.udp.blackhole=1 net.inet.tcp.blackhole=2 net.inet.ip.rtexpire=60 net.inet.ip.rtminexpire=2 net.inet.ip.rtmaxcache=1024
Thanks to Tim for the original pf.conf file and tips.






