Buffering MySQL Remote Access - c#

I am building a .NET application that needs to access a remote MySQL database. To me that means that I will need to enable "Remote MySQL" access with a "%" qualifier so that anyone, from any IP can access the database. This is VERY insecure, right?
Is there a way to buffer the database requests/modifications through a central website so that all users of my .NET app will connect to a website and the website will access the database? Can anyone point me towards literature explaining how it would work? I am at a loss.

I think it would make more sense to expose a Web Service (WCF) and have all the data requests go through there. Your Web Service would be the only one in charge of accessing the database.
Depending on the requirements; you may choose to accept RAW SQL statements that you would then execute against the database or simply (preferably) provide an interface (Methods in your web service) for all required data modifications to the tables, etc.

It depends...
Here, the webapp user should be on the same server as the database. So you have one server.
CREATE USER 'webapp'#'localhost' IDENTIFIED BY 'abc123'
In the following example, the webserver is on 10.0.123.45, and this login can only be used from this server.
CREATE USER 'webapp'#'10.0.123.45' IDENTIFIED BY 'abc123'
Next to that, you should have a firewall that blocks access from the outside.

Related

How to securely connect to a SQL Server database in a single tier Winforms application?

Background
I'm building a single tier application Winforms application using C#. A SQL Server localdb database is attached to the application that runs when the application does. The plan was to use Windows Authentication to verify that the user is part of the MyApplication role/group and can use the application. But, in order to prevent users from accessing the database via other means, I was thinking of using an Application Role so that only the one SQL application user can edit the db tables.
Question
My understanding is that in order to use an Application Role, you need to provide a username and password in the connection string. I can encrypt this information, but obviously it will need decoded before being sent to the database. Is this a concern in a single tier application? What are other alternatives?
To use an application Role, you'll use the sp_setapprole stored procedure where you will provide the name of the application role and the password, rather than sending it in the connection string. When you use application roles, you still connect using an ordinary login, but once you successfully sp_setapprole, your connection loses its user permissions and instead gains the permissions of the application role. Having the decoded password in memory is a concern if you have reason to believe that users may decide to use a debugger to attach to your process to extract the password. Any administrator would also be able to decrypt the encrypted password on disk as well if you choose to use windows machine-level key containers. Using only a single tier for an application that uses a database is a security risk, and you have to decide based on the circumstances surrounding the application whether it is an acceptable risk to gain the reward of skipping a few weeks of design and development.
Source:
https://technet.microsoft.com/en-us/library/ms190998(v=sql.110).aspx
I highly recommend implementing a web api to manage your application's interactions with the database as well as security. This web api could use a windows "service" account to authenticate with the database, and users would authenticate with the api using their individual windows accounts. This has the added benefit of you never having to think about passwords. As far as managing API permissions, that is an issue that is up to you to design and implement as you see fit. The main issue you need to understand and deal with is uniquely identifying AD users. Take a look at this SO post for more info on that: What Active Directory field do I use to uniquely identify a user?
Your service account would have all necessary permissions on the database to do what the application needs to do, but not all api users would necessarily have permission to use all api functions. You would manage a store of uniquely identified AD users that have permission to use the application and what permissions they have. The rest is design and implementation details that are up to you.
Define user with privilege only to execute stored procedures. By this way if someone use SQL Management Studio, s/he cannot browse/edit tables and even cannot see the table names.

Desktop client working with DB - direct connection or proxy (WCF)?

First of all, I am not a programmer anymore but like to stay in touch with development. I would like to develop a simple system for my teamm embers to log their work and additional data. Basically, I need every member to use a desktop app which will basically login to DB and allow to do CRUD operations. I can think of two approaches:
Having a desktop app with harcoded (or configurable) db connection string and for every user an account would have to be created. So basically login into the system would mean logging into a database with given usesrname
Having a desktop app communicating with WCF service that would handle all the database connections, so DB root account could be used with a simple table of users.
The first point is apparently much easier as it is basically just connecting to the database but I am not sure about security issue there.
I'd recommend using Web API instead of WCF. I've found Web API much easier to develop for and consume. Plus you can secure Web API over HTTPS - easy peasy.
Regardless, Timothy Groote's point is apt: you would need to secure access to your Web service layer. There are ways to do that, certainly - starting from transmitting authentication in the HTTP headers (encrypted using SSL, of course) and going all the way to setting up an OAuth provider.
If this is a simple system, I would vote for option 1. This choice will limit your flexibility and expandability, but that may be the right tradeoff in this simple case.
If you're going to use option 1, can you use integrated security to connect to the DB? This would mean you don't have to store credentials in your connection string. If you can't use integrated security, then you could look into encrypting the connection string. However, you may not need to - if this is going to be a limited-release app, and users are generally trustworthy, then is it actually a problem if a user knows their own DB connection information?

Connect to AWS MySQL database in C# securely

I've built an application that utilizes a database hosted with Amazon. I'm trying to find a way to connect to this securely and overall, I've been assured that I have no idea what I'm doing.
My original intent was to store my database credentials in my application with encryption, but I've been led to believe that using a web service would be a better option as nothing stored local can be really secured. However, I have no idea how to use a web service for this and or why it would make a difference.
In using a web service, would I be building a Windows Server and deploying this "web service" to it to connect to, which would then connect to my database? That almost seems superfluous, though I still consider myself to be an amateur. Even then, how would I authenticate to it securely?
The end-game for me is to be able to store DB (and PayPal seller credentials) somewhere that a user cannot access in any case, but my program can. It seems much more cut and dry than people here and elsewhere have made it seem, but again, I'm a newb.
Any direction would be greatly appreciated! I'd like to deploy this, soon outside of our organization for testing and such.
Thanks, guys!
To me, it's unthinkable to give your "application" -- which I assume means an "app" deployed on people's devices or maybe some kind of a desktop application -- direct connectivity to your database, unless the thought of putting the key to your car in a paper envelope and sticking it to the window seems secure.
The application should have an extremely small number of very specific things it can do to the database, and should be leaving a trail of exactly what it does and from where and on whose behalf. The app can make requests to a service, such as a REST API, running on your application server -- the "web service" -- over https, and your application server would then mediate the requests and fetch values from, or send updates to, the database, as appropriate... only after the app has convinced the application server that it indeed represents the particular user that it claims to represent.
It's a fundamental principle that anything you don't control, you can't trust.
"Why it would make a difference" is the difference between "Ha! I hacked one user's password" and "Ha! I stole your database." The application server can authenticate any request as being legitimate for the particular end-user credentials presented... while the database is, in large measure, ill-prepared to do anything of the sort.
The end-user (not the application) would authenticate to the application server, the application server would validate those credentials against the database, providing no hints as to why authentication failed... user not found, password incorrect, we don't know, we don't care -- sorry, login failed. Give us your e-mail address and we'll send you a support email, or if that's not an e-mail we have on file, we'll pretend that we did. Hints help hackers.
store ... PayPal seller credentials
Well, you'll want to be sure that's not among the things that are prohibited from "collect, capture, use, or store" in section 10.1.2 of the Paypal Developer Agreement.
So, how do you do this? That part of the answer takes us a little bit out of scope, because there are many options, the landscape shifts, and it's largely a matter of opinion as to the "best" way but I'd assume ASP or PHP would be the most straightforward.
Think of it as building a database-enabled web site without the hassles of making it pretty, because nobody's going to see it except that back-end of your app... and there's your "web service".
I've been assured that I have no idea what I'm doing
That puts you way ahead of the terrifying number of people who don't realize that they don't know what they are doing.
If your database is mysql, then you can use: Connector/Net from mysql. Credentials for the db are usually kept in web.config of your application. Also I see no reason of using web service in your case. Web services are used when you need to transfer data from one system to another, not to provide better security for database connection.
EDIT:
As Michael kindly explained this approach is for the web application which is hosted on the server. As for the client application, keeping the database credentials in the app (encrypted or not) is very bad idea. In this case additional web application should be developed, which will provide the way to authenticate users using web service (over https), and transfer the data to users from database.

Securing sensitive data inside C# source code?

A theoretically question:
I have a C# Windows Form app, that sends back some user submitted data to an external mySQL server on the internet.
How do I prevent people from seeing the username and password for the mySQL server? (Eg. by decompiling the C# exe program).
Best regards
Short answer: you can't - anything that runs on a machine you don't control can be decompiled etc.
Possible option:
You might try an approach with a SSL-secured webservice on server-side which authenticated clients via client-cert (part of SSL standard). That webservice would be accessing the DB on behalf of the clients. you can then secure the connection between webservice and DB network-wise...
Typically you either prompt the user for a credential, or you if the account is public (eg. credentials are insecure, as in your case) you limit what the account is allowed to do.
I would pass the user submitted data to another service which does the actual talking to the database server. That way your connection details can be kept apart from the client.
If you cannot do that then you should create a restricted account for your mySQL database which is used by your client code.
Live with the fact, that everything the client software knows is known to the user.
Set up the smallest possible interface for he client software to use. Instead of exposing a full DB account, use one restricted to some stored procedures only. Or don't access DB directly, but via a (web) service. Then, make sure the service only exposes safe-to-execute methods.

Securing your Data Layer in a C# Application

I was thinking about how to secure the Data Layer in a C# Application, the layer could in this case be either a LINQ to SQL Model Diagram stored with the Application itself containg the connection string to the SQL Server Database.
Or it could be connectivity between the application and webservices.
Either you need to impement some sort of security, for instance, the Connection String in a Application can easily be reverse engineered and Webservices can easily be tracked and used for other reasons than the applications original purpose.
So my question is in a shorter way: How do you solve the security issues when handling Webservices and/or direct connection to a SQL Server From a Windows Forms Application?
In your case there are two main attack possibilities:
Steal the connection string and then access the database directly
Call methods in your C# code directly without using the UI
For the connection string you need to store it in an encrypted form in a config file. Problem is that there need to be enough information in the winforms app so that it can decrypt and use it.
For accessing the code directly you can use code access security and obfuscation.
In your case I would not give the windows app direct access to the database. Let the windows app call a WCF service, the the WCF service would access the database.
The user's user account is allowed to call the WCF service, the WCF service is running under an account that is allowed to access the database, the user's user account has no rights to the database.
Windows App with 3 Layers:
UI
Business (Security check what UI should be shown to the user)
Proxy
WCF Service with 2 Layers:
Facade / Business Layer (Security check is user allowed to call this method with this data)
Entity Framework datamodel
Common dll's to both Layers
Contracts / WCF Interfaces
Data Transfer Objects
For info on proxy, contracts and DTO's see this video:
http://www.dnrtv.com/default.aspx?showNum=103
Shiraz Bhaiji came close, but I think they missed the key step.
Yes, you want access to the database to be mediated by a middle tier, exposed through WCF, which imposes whatever business logic you require, including full access control. This service does have the connection string that you want to keep secret, but it's not accessible to the WinForm clients.
The key step is that the client uses the user's authentication to gain appropriate levels of access, and never has any ability to contact the database or even get full control of the middle tier. The middle tier grants access to methods based on the groups that the client user is a member of. This means that a user with low security can call any method they like, but they'll get access denied exceptions, or data filtering, or whatever other failure mode is appropriate. The user account, on its own, has no access to the database, so the middle tier can do whatever it likes.
The obvious way to bypass this would be for the client to use the account of someone with full access. Of course, if they could do that, they'd already have what they wanted.
Hopefully, this approach would be useful in solving your problem.
edit
This solution does not allow LINQ-to-SQL in the client, just the middle tier. If that's a dealbreaker, then this isn't for you. Then again, the moment the client can access the database directly, a gigantic security door is opened up, and it's hard to close. There's a huge amount of extra work involved in securing the database itself so that it provides the sort of user-based, row-level security that comes naturally from a three-tier solution. I would generally recommend against it, although I do recognize that there are times when it is entirely appropriate.
One way would be to use a Trusted Connection to SQL Server, that way you don't store the username / password in code.
I don't think there is any one-solution-fits-all to this problem, you will need analyze and adjust your solution to the particular problem you are having.
As far as I know there are no known ways of securely storing your connection information on the client side as your client is a "trusted" part of the communication to the server. No matter how you store the information, the client has to be able to reverse it or send it directly to the server, which also means that a potential attacker can repeat the process. Also any external communication directly to your database can potentially be intercepted/hacked.
The best way I can think of to protect your data is having a webservice (over a secure connection) as middleware controlling the communication with your database(which you need to secure) and adding logic to enforce whatever level of security you wish to attain. You can make it account based to grant different levels of access if needed. But the main thing is that it only allows safe/isolated operations.
To secure the webservice(middleware) there are two concerns, authentication and isolation.
Authentication
You can use the standard .NET authentication as Steven suggested. I normally prefer rolling my own solution though for two reasons. First off, so far I've mostly ended up handling more complex users/roles. For example using permission based roles so that you can check for permissions instead of specific roles.
And second, it gives you more control. You can avoid session based authentication and you can also use challenge-response, for example challenge with a timestamp and expect a hash of the timestamp+password(which the user has to enter at application start) or some other creative combination as answer, I am sure there are better hash combinations to respond with. This should also be done two-way, to make sure that the client verifies whatever it gets from the server.
Also here are some SO topics about WCF Authorization that might be interesting:
WCF Service authorization patterns
Authorization and Authentication using WCF
WCF Authorization - access to operations via claims
And also a book and a paper (not free)
Isolation
No matter how secure your authentication is, there is always the possibility of someone being able to access your webservice for malicious intents. As far as I know there is no one solution to this problem, but it is rather dependant on the specific application and how the data is structured and shared between users.
You will need to identify layers of isolation such that users cannot affect each other or the system in general, and also how the application is used. Will clients need to write data, or only read? If they write, is written data shared in any way and in what way can you isolate/verify that data? If they read, is the information private for the user, private for the system or shared among users?
For example a system for storing medical journals or personal task lists will have very isolated data and you can restrict access to your private information only (and possibly your doctor/boss depending on user groups). In this case you can isolate all data read/writes to the particular user, thus the attacker can only affect his own data, keeping everyone else safe.
If the data is shared between users you will need some way of verifying the input that is given from the user. Preferably you should also have some kind of trust-level for the user such as SO's reputation to prevent any one-time users to attempt a hack. This is really too specific to give any good advice on.
You also properly need to verify the input that you recieve to prevent hacks such as buffer overflow hacks and SQL injections. Allthough I don't know if buffer overflow is a problem with .NET, and SQL injections should be easily preventable with LINQ-to-SQL.
All in all there is no 100% guaranteed way of securing your data, and you should keep regular backups (separate from your database) of your data in case you get compromised and probably also transaction logs.
And as a final advice, if you are really serious about the security you should probably hire a security consultant and have a peek at how banks have set up their security infrastructure.
Also you can still use LINQ with webservices through LINQ to ADO.NET, though I haven't tried this myself.
This link might be more explaining How to move from LINQ to SQL to “LINQ to WCF”?
Where security is that important, for example when you are storing credit card information, you'll usually want the data repository and the webserver on seperate boxes, with a firewall between and both locked down by IP Security.
This way, only the webserver is exposed to the outside world. Your database server is sitting comfortably behind the firewall, and can only be accessed by the webserver through a certain port.
You might also consider SSL encryption on the web services and expose only HTTPS endpoints.
I am not completely clear here. If the winforms application calls webservice then use a appropriate model for mutually trusted authentication. This can be based on client and server certificates or SSL with client certs or even Net.Tcp if you are all .Net. Then however the webservice is exposed only trusted clients can communicate. The webservice can then stay behind a DMZ and the DB behind another DMZ. Use appropriate firewall rules and IPSec connection between webservice and SQL is an option.
For direct connection to SQL server to many winforms application the challenges are many.The connection to your DB has to authenticated and encrypted. In any case your SQL server will be exposed and I would not recommend such a model.
You don't secure it because you can't secure it. First you can't properly hide credentials, even though you figure out how to do that then an attacker can sniff (yes even if it's encrypted you can locally sniff) or do SQL Injection directly on the wire.
You need to write all of you webservice calls in a secure manner which doesn't require to transfer raw SQL Query or direct SQL Server connection.
Also it doesn't matter how much obfuscate or encrypt it if the code is not running your system it's not your code any more. By reverse engineering, debugging, modifying the code a potential attacker can change your application into something else and do whatever they want.
Also as someone else wrote your webservice will be open to direct access. Someone can make a call directly to your web service and ignore the GUI at all.
A secure method is:
Place the data layer behind a (WCF) service on an physically separate Application Server and have WinForms clients connect to the service using their Windows Credentials. The service then validates whether users can access the various methods in the service based on an Authorisation store (such as Active Directory), and database or combination thereof.
The data service can then use a single pooled identity to connect to a database.
One of the more common approaches with web services is to pass an encrypted username and password via the web method signature in order to validate that the user attempting to invoke the web method indeed has rights to do so.
In terms of the configuration file it is possible to encrypt the file itself or use integrated security as another poster mentioned.
It's difficult to provide a precise answer because I'm not sure what specific issues you are trying to solve and which is the key driver for securing the system.
However, in the past I have used WinForms -> WebService secure communication by utilising WSE
We used X509 certificates and WS-Security. This has the distinct advantage of providing End To End Security rather than relying on standard SSL transport.
However this in of itself doesn't solve issues like user authentication per se, in that case Mitch Wheat's answer seems a good solution.
However, your user authentication model will depend on whether this is a public distributed app, whether the number of users of the tool is large or small etc.
For small numbers of users, or where cost is not an issue, you could implement RSA SecurID authentication by setting up a RADIUS server or such like. This has the advantage in that each RSA key is unique and tied to that user ( though you can never stop a user giving out their credentials and PIN )
HTH
The answer is simple to protect sql strings is simple. NEVER make a direct connetion to SQL in the client side.
Only accept well formed, schema-validated xml serialized objects as the entrance of your program, after being authenticated in a hashed public private key pair (http://msdn.microsoft.com/en-us/library/6f05ezxy.aspx) , being the public key certificate shipped within your program, so someone eavesdropping wont discover the password.
Also, watch out for DDOS attacks. Measure the use of each webservice exposed for each client, and if the use rises above a given limit, block all incoming connections from the user, and from the user´s ip.
If I understand the OP correctly, the immutable design characteristics are a WinForms client connecting directly to a publicly accessible SQL Server?
Almost everyone who responded has basically said 'don't do this, use a web service instead'. This is good advice. Even if the ws is hacked, it can only do things it was designed to do. So an RPC WS can only execute methods already written whereas hacking a SQL Server connection would allow arbitrary SQL Execution. Also, I think you would find that a well designed web service would be more performant.
However, if you are going to do this then you must secure your SQL connection over SSL (see technet) as a start. As with secure web services (which also would use SSL) this will hide the contents of the traffic from the men in the middle.
You can't rely on the authentication of the connection string (but using it adds another layer for a hacker to get through), so you must have an application level authentication layer that you most likely would roll yourself.
Don't allow the WinForms application to connect to your operational database. Create another database instead and allow the connection string based auth to connect to it. Do not do dynamic SQL with this design, use stored procedures instead. Create stored procedures in your public database that would act as your "rpc web service" to hide the real SQL (which would query your operational database and return the results). This will hide the operational details of your schema and reduce the surface area of attack.
If procedures are out of the question because you must use dynamic SQL, still keep the public/operational database structure and use views to expose as little of the data as possible. Leverage user id and any multi-tenancy features you have in the database to pre-filter data in the view. If you can do that you reduce the surface area of attack to the connected user's data.
Without understanding why you must allow a direct sql connection, I can only say again that you shouldn't do it. What you are gaining by doing so in the short term is at the cost of your system's long term security.

Categories

Resources