forward all mysql traffic to a ssh tunnel

Infrid

I have a local application that needs to connect to a remote mysql server at 40.40.40.40:3306

The main firewall blocks all connections but ssh, I can set up a ssh tunnel and connect to the server without problems

ssh [email protected] -L 3306:127.0.0.1:3306 -N  

(in another terminal)

$ mysql -udb_user -h127.0.0.1 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
[...]

My intent is to configure iptables to forwards the connection destinated to 40.40.40.40:3306 to my tunnel at 127.0.0.1:3306

# iptables -t nat -A PREROUTING -d 40.40.40.40 -p tcp --dport 3306 -j DNAT --to-destination 127.0.0.1:3306

# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  anywhere             40.40.40.40       tcp dpt:mysql to:127.0.0.1:3306

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination 

#cat /proc/sys/net/ipv4/ip_forward
1

With this setup my applications still can't connect to the database, if I change the connection settings to 127.0.0.1 I have no problems, so I assume that the application works perfectly.

Tim

You need to use the OUTPUT chain to redirect an outbound connection to a local port.
This rule will work as you need:

iptables -t nat -A OUTPUT -p tcp -d 40.40.40.40 --dport 3306 -j REDIRECT --to-port 3306

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Is it possible to redirect ssh traffic (not forward), How?

분류에서Dev

Expose a mysql server over an ssh tunnel

분류에서Dev

Tunnel all remote ports through SSH to local hostname

분류에서Dev

How to redirect all traffic except SSH to one local port with iptables

분류에서Dev

SSH tunnel with different ssh port

분류에서Dev

Route Client Traffic Through VPN Tunnel

분류에서Dev

Route Client Traffic Through VPN Tunnel

분류에서Dev

SSH reverse tunnel into Docker container

분류에서Dev

Notepad++ NppFTP SSH Tunnel

분류에서Dev

How to forward traffic between Linux network namespaces?

분류에서Dev

How to make an SSH tunnel publicly accessible?

분류에서Dev

Destroy ssh tunnel if exists and create new one

분류에서Dev

Routing ALL traffic through VPS

분류에서Dev

Sniffing all network traffic in C

분류에서Dev

mysql forward engineering error

분류에서Dev

Do ports placed in a blocking state by STP still forward normal traffic?

분류에서Dev

How do I route my internet through a SSH tunnel?

분류에서Dev

SSH fails to connect on private interface over IPSec tunnel

분류에서Dev

RDP over SSH Tunnel through intermediate Linux server

분류에서Dev

Force SSH server to always and only tunnel to telnet server

분류에서Dev

Forward SSH port - Connect from other place

분류에서Dev

Using iptables to redirect all traffic to local webserver

분류에서Dev

How to release ports on the SSH server when a reverse ssh tunnel disconnects abruptly/uncleanly?

분류에서Dev

Why would SSH freeze for minutes at a time when other traffic is unaffected?

분류에서Dev

Forward traffic to virtual interface based on source IP address dynamically using iptables

분류에서Dev

ssh tunnel-bind : 이미 사용중인 주소

분류에서Dev

연결 후 tunnel-ssh에서 오류가 발생 함

분류에서Dev

How to create SSH tunnel with PuTTY to connect Neo4j database

분류에서Dev

How do to bind x11vnc to localhost only and tunnel through SSH?

Related 관련 기사

  1. 1

    Is it possible to redirect ssh traffic (not forward), How?

  2. 2

    Expose a mysql server over an ssh tunnel

  3. 3

    Tunnel all remote ports through SSH to local hostname

  4. 4

    How to redirect all traffic except SSH to one local port with iptables

  5. 5

    SSH tunnel with different ssh port

  6. 6

    Route Client Traffic Through VPN Tunnel

  7. 7

    Route Client Traffic Through VPN Tunnel

  8. 8

    SSH reverse tunnel into Docker container

  9. 9

    Notepad++ NppFTP SSH Tunnel

  10. 10

    How to forward traffic between Linux network namespaces?

  11. 11

    How to make an SSH tunnel publicly accessible?

  12. 12

    Destroy ssh tunnel if exists and create new one

  13. 13

    Routing ALL traffic through VPS

  14. 14

    Sniffing all network traffic in C

  15. 15

    mysql forward engineering error

  16. 16

    Do ports placed in a blocking state by STP still forward normal traffic?

  17. 17

    How do I route my internet through a SSH tunnel?

  18. 18

    SSH fails to connect on private interface over IPSec tunnel

  19. 19

    RDP over SSH Tunnel through intermediate Linux server

  20. 20

    Force SSH server to always and only tunnel to telnet server

  21. 21

    Forward SSH port - Connect from other place

  22. 22

    Using iptables to redirect all traffic to local webserver

  23. 23

    How to release ports on the SSH server when a reverse ssh tunnel disconnects abruptly/uncleanly?

  24. 24

    Why would SSH freeze for minutes at a time when other traffic is unaffected?

  25. 25

    Forward traffic to virtual interface based on source IP address dynamically using iptables

  26. 26

    ssh tunnel-bind : 이미 사용중인 주소

  27. 27

    연결 후 tunnel-ssh에서 오류가 발생 함

  28. 28

    How to create SSH tunnel with PuTTY to connect Neo4j database

  29. 29

    How do to bind x11vnc to localhost only and tunnel through SSH?

뜨겁다태그

보관