-
Notifications
You must be signed in to change notification settings - Fork 18
DDoS Protection
In this guide I will try to explain how one can get an affordable and fast DDoS protection by simply knowing some Linux commands. The protection is based around a principle of using a GRE (VPN) tunnel to proxy all of the data from your main server where the game is hosted, to a VPS that has some DDoS protection. The protection on that VPS will handle all of the DDoS, leaving your main server safe and sound behind the barrier, as well as masking your server's real IP.
You still need to be careful with who you trust, since applications or scripts you put on your server have the ability to disclose the real IP of your server!
- A Linux server where the game will be hosted
- A Linux VPS that will proxy your traffic
- (preferable) Debian 9 Stretch or newer on both machines
For this, you'll need the following apt packages:
iptables
ip_gre
You need to make sure you have the following kernel modules available to use:
ip_gre
ip_tunnel
ip_tables
gre
You can do this by running:
lsmod | grep ip_and looking at the output. If these modules aren't loaded, you can load them by running:
modprobe ip_greCheck the output of
sysctl net.ipv4.ip_forwardif it's set to 0, make sure it's set to 1
sysctl -w net.ipv4.ip_forward=1First, you need to add a new GRE tunnel, you can do that by running
ip tunnel add gre1 mode gre remote PROXY_EXTERNAL_IP local HOST_EXTERNAL_IP ttl 255Make sure to replace PROXY_EXTERNAL_IP and HOST_EXTERNAL_IP with your IPs.
You can also use a name other than gre1, but for the sake of this document we'll assume the tunnel is named gre1 on both machines.
Then, you need to make sure your new tunnel is up
ip link set gre1 upAnd finally, add the IP address you wish to use. I usually use the 10.10.10.0/24 range for my use cases.
ip addr add 10.10.10.1/24 dev gre1You can, of course, change 10.10.10.1 to anything else if you want your host's internal IP to be different.
You'll need to add the interface to the routes table, to do that use a text editor or just run the following command
echo '100 GRE1' >> /etc/iproute2/rt_tablesThen you need to set up the rules for the IP, as well as default route
ip rule add from 10.10.10.0/24 table GRE1
ip route add default via 10.10.10.2 table GRE1Make sure to change 10.10.10.2 to your proxy's internal IP, as will be configured later in this document.
You may need to let iptables know that you want all connections from 10.10.10.0/24 subnet to be accepted. It will allow any connections by default, so do that only if you changed anything.
It's mostly the same deal as on the host, but reversed a bit.
You know the drill by now:
ip tunnel add gre1 mode gre remote HOST_EXTERNAL_IP local PROXY_EXTERNAL_IP ttl 255Notice that HOST_EXTERNAL_IP and PROXY_EXTERNAL_IP have changed places in this command. Beware!
Similarly, you need to set up the interface first:
ip link set gre1 upand give it an IP
ip addr add 10.10.10.2/24 dev gre1you can replace 10.10.10.2 with any desired local IP.
iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source PROXY_EXTERNAL_IPFinally, once everything is set up, in order to forward a port you simply need to run the following iptables commands:
# You can, of course, change the port to anything you desire.
# Change the protocol/port (tcp/12345) to anything you need. You need to do this for every port.
iptables -t nat -A PREROUTING -p tcp -d PROXY_EXTERNAL_IP --dport 12345 -j DNAT --to-destination 10.10.10.1:12345
iptables -A FORWARD -p tcp -d 10.10.10.1 --dport 12345 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPTMake sure you have iptables-persistent in order for your IP forwarding configuration to persist between machine restarts. Run iptables-save every time you add a new port.
And you're done!
I hope this little tutorial was useful enough to you!
General documentation
Developer documentation. To learn how to use and set up Flux, please refer to the general documentation.