Eight checks to secure your Metin2 server
Eight things we check on every Metin2 server before players get on. Commands for FreeBSD and Linux.
Take a backup of the databases first, and keep a second SSH session open while you work on SSH or the firewall. If something goes wrong, you're still logged in.
Database on localhost only
This is the classic, and the first thing we look at on any server. MySQL or MariaDB listens on every address, port 3306 is open to the internet, and scanners try logins around the clock. To see what it listens on, run this on FreeBSD:
On Linux:
If you see *:3306 or 0.0.0.0:3306, it's open. Put this in the [mysqld] section of my.cnf:
Depending on the version, my.cnf is /usr/local/etc/mysql/my.cnf on FreeBSD, or /etc/mysql/my.cnf or a file in /etc/mysql/mariadb.conf.d/ on Linux. Restart the service afterwards. The cores sit on the same machine and keep connecting through localhost. And 127.0.0.1 means "this server itself", not your PC at home, so it works the same on a rented server.
For Navicat from home, use an SSH tunnel through the "SSH" tab in its connection settings instead of an open port. Lots of older install guides set up exactly the access you don't want for Navicat: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'. The next section gets rid of it.
Clean up database users
Many files ship with users that can log in from anywhere, or anonymous users with no name at all. List them:
Host % and empty names are what you're looking for. Normally one user for the cores with host localhost is all you need. Drop the rest:
First check which user the CONFIG files of your cores and your website use. We always give the website its own user, with rights only on the tables it really needs. Never root.
Only the ports you need
From outside, a Metin2 server needs the auth port, the channel ports and SSH. The P2P ports between the cores and the db core port stay off the internet, the cores talk over the local interface anyway. With the db core port open, anyone can hook their own core up to your db core and change accounts, items and GM rights. Open P2P ports let tools send your cores packets that only other cores should send.
On FreeBSD we use pf. It's part of the base system, so no custom kernel like the old IPFW tutorials. A basic /etc/pf.conf, with the ports adjusted to your CONFIG files:
Test it with pfctl -nf /etc/pf.conf before you load it, then enable pf in rc.conf:
On the first start we always leave a way back in. pf turns itself off again after two minutes, in case your rules block SSH:
Now open a new SSH session. If you get in, stop the switch-off with pkill -f 'sleep 120; pfctl -d'. If not, wait the two minutes. Worst case, there's still the console or rescue system of your host.
If a channel or the database runs on a second machine, allow exactly that machine's IP to the internal ports:
The ports are examples. The real ones are in CONFIG under P2P_PORT, and for the db core in conf.txt under BIND_PORT. On Linux, same idea with ufw or nftables.
Something a lot of people miss: the cores also accept admin commands on their port, like a notice to everyone, disconnecting all players or a shutdown. On older cores, two lines in every core's CONFIG decide who may send them:
Without ADMINPAGE_IP the core only checks the password, which in shared files is often a well known one. Some files also shipped misspelled entries like adminpage_ip0 or admin_page_password that the core never reads, so check the spelling in every CONFIG file.
Newer sources have ENABLE_PORT_SECURITY in common/CommonDefines.h. With it set, the core blocks admin commands from other addresses, accepts P2P only from its own server, and the db core only takes connections from the local network. If your cores run on several servers, you have to adjust those checks in the source or they won't connect.
SSH with keys only
Root login with a password is the other big one. Set up an SSH key first and test that you can log in with it. Then in /etc/ssh/sshd_config:
Restart the service (service sshd restart on FreeBSD, systemctl restart ssh on Debian and Ubuntu) and check from your second session that the key login still works.
PuTTY and WinSCP use their own .ppk format. Create the key in PuTTYgen, or load your existing OpenSSH key there and save it as .ppk. Only the public part goes on the server, the line at the top of the PuTTYgen window, on its own line in /root/.ssh/authorized_keys. Create the folder if it's missing. If the permissions are too open, SSH ignores the file:
In PuTTY the private key goes under Connection, SSH, Auth (Auth, Credentials in newer versions), in WinSCP under Advanced, SSH, Authentication. Moving SSH to another port only keeps automated scans out of your log, it doesn't replace a key. If you change it, open the new port in the firewall first.
Updates
Plenty of Metin2 servers run FreeBSD versions that no longer get security updates. freebsd-version tells you which one you have. To update:
If your version is out of support, plan the move to a current release together with a full backup. On Linux, apt update && apt upgrade is usually all it takes.
Backups
For us a backup only counts if it runs regularly, isn't on the same disk and can actually be restored. A daily dump of the important databases is a good start:
Many Metin2 files use MyISAM tables, so the dump locks them for a moment to get a consistent state. Run it at a quiet hour. Then copy the file to another machine or to object storage, and once a month restore a backup on a test system. The backup guide has the details.
GM and test accounts
Shared files often come with test and GM accounts that have well known passwords. Go through the gmlist table in the common database and the accounts in account, delete everything you didn't create yourself, and give the real GM accounts long passwords of their own.
Logs and core dumps
Channel syslogs and core dumps from crashes grow over time, and once the disk is full the server stops. Check df -h now and then and let newsyslog (FreeBSD) or logrotate (Linux) clean up old logs. How to read the dumps is in the syserr guide.
Have it checked automatically
The server check from Servergilde looks at these eight points every day, for free. Every finding comes with instructions for your system.
Frequently asked questions
Which ports does a Metin2 server need open?
How do I check if my database is open to the internet?
sockstat -4l | grep 3306 on FreeBSD or ss -tlnp | grep 3306 on Linux. If it shows *:3306 or 0.0.0.0:3306, it's open. Add bind-address = 127.0.0.1 to the [mysqld] section of my.cnf and restart the service.How do I connect with Navicat if port 3306 is closed?
%.