This article is dedicated to the task of securing MySQL client-server connection using functionality provided by the Secure Shell (SSH) protocol. To be exact, the SSH tunneling concept is utilized. We will review the steps needed to build secure MySQL client applications and implement a sample one ourselves.
MySQL traffic is not the only kind of data that can be tunneled by the Secure Shell. SSH can be used to secure any application-layer TCP-based protocol, such as HTTP, SMTP and POP3. If your application needs to secure such a protocol by tunneling it through a protected SSH connection, this article will be useful to you.
Let's imagine that we are developing an enterprise application that needs to send requests to a number of SQL servers all over the world and get responses from them (let's imagine that it's a super-powerful bank system that stores information about millions of accounts).
Let's take a look at what we have:
SSH (Secure Shell) is a protocol that may help in solving this problem. One of its outstanding features is its ability to tunnel different types of connections through a single, confident and integrity-protected connection.
It works in the following way:
Please note, that the SSH client acts as a TCP server for the connections it accepts, and the SSH server acts as a TCP client for the connections it establishes to the remote host.
A single SSH connection can tunnel as many application layer connections as needed. This means that you can defend your server by moving all the listening ports (e.g., database and application server ports) to a local network, leaving only the SSH port open. It is much easier to take care of a single port, rather than a dozen different listening ports.
Let's develop a small application that illustrates the use of SSH forwarding capabilities. We will consider an important task of securing a connection between a MySQL client application and a MySQL server. Imagine that we need to get information from the database server, which is located a thousand miles away from us, in a secure way.
The following picture explains the scheme we will utilize:




Forwarding.Address = tbSSHAddress.Text;
Forwarding.Port = Convert.ToInt32(tbSSHPort.Text);
// Specifying network interface and port number to be opened locally
Forwarding.ForwardedHost = "";
Forwarding.ForwardedPort = Convert.ToInt32(tbFwdPort.Text);
// Please note, that the destination should be specified according to
// SSH servers localhost, not SSH clients one.
Forwarding.DestHost = tbDBAddress.Text;
Forwarding.DestPort = Convert.ToInt32(tbDBPort.Text);
SFTP NET Compnents developer that has proven experience in .net coding.
// forming connection string
string connString = "database=" + tbDBName.Text + ";Connect Timeout=30;user id=" + tbDBUsername.Text + "; pwd=" + tbDBPassword.Text + ";";
if (cbUseTunnelling.Checked)
{
local // specifying real MySQL server location if forwarding is not used
connString = connString + "server=" + tbDBAddress.Text + "; port=" + tbDBPort.Text;
}
MySQLConnection.ConnectionString = connString;
try
{
MySQLConnection.Open();
Log("Connection to MySQL server established. Version: " + MySQLConnection.ServerVersion + ".");
// closing both MySQL and SSH connections
Log("Closing MySQL connection");
reader.Close();
MySQLConnection.Close();
Forwarding.Close();
}
}
catch (Exception ex)
{
Log("MySQL connection failed (" + ex.Message + ")");
}
}
And, that's all!
But there is one more thing I need to draw your attention to. As both SSH and MySQL protocols run in separate threads and access GUI controls from those threads, we need to handle the GUI access in a special way to prevent a cross-thread problems. I will illustrate this with the example of the Log() method:
Now click the Start button and wait for the query results. If all the parameters have been specified correctly, we should get something like this:
Report this article
Source: http://www.a1articles.com/ssh-tunneling-in-your-application-480449.html
