Initially an important point, in iptables the rules of PREROUTING
that do port redirects perform before filter rules that define which ports can be accessed and by which Ips, as can be seen in the diagram below:
So when an IP tries to access the port 3306
it will first be redirected to the port 54235
and only then are executed the filtering rules from which ports it can access. This means that any IP that has access to the port 54235
will also be able to access the port 3306
even if you don’t have access to it since as soon as it accesses the door 3306
it will already be redirected and only then check if it can access (but now it is already in the port 54235
).
Knowing this, if you want to continue then configure iptables as follows to allow access only to certain ip on port 3306 (this rule will not be useful because as stated above it will not be checked just put to make clear the rules of iptables):
sudo iptables -A INPUT -p tcp -s IP_AQUI --dport 3306 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 3306 -m conntrack --ctstate ESTABLISHED -j ACCEPT
The first rule you must repeat for the two Ips allowed, this will cause only the ips defined in this rule can initiate a connection on this port, the second step is to redirect the ports, the command is this:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3306 -j REDIRECT --to-port 54235
Finally not to allow other connections on the port 3306
(again this rule also will not be executed)
iptables -A INPUT -p tcp --dport 3306 -j DROP
To accept all connections on port 54235
iptables -A INPUT -p tcp --dport 54235 -j ACCEPT
And to modify the default policy for DROP
iptables -P INPUT DROP
It is part of programming, because the database used in the system needs the specific release to be able to work on the server.
– Mylon