3

I'm new to SQL server and I have a question

I created a database on a SQL server local DB instance and then I copied the .mdf file of the database to a USB flash drive and I was able to open the database from a different user account' So I want to understand why is it so? Does it mean that anyone can copy my database file and open it on their own server? And how can I secure that?

Thanks

Lerner
  • 1,111
  • 13
  • 19
  • 1
    depends on the server platform. Some db platforms allow data to be saved encrypted. yours might allow this if we knew which one you were using. – Hogan Nov 10 '14 at 21:08

2 Answers2

5

Does it mean that anyone can copy my database file and open it on their own server?

Yes, as long as it's the same version of SQL Server. This is why it's critical to maintain control of a server running SQL Server and prevent unauthorized access to the file system or backups.

So I want to understand why is it so?

The same software uses the same format. Much like how an Excel document can be saved on one computer and opened on another, so, too, can a database. By default, SQL Server stores data in the easiest and most straightforward manner it can for best possible performance. Security of the data files is often a secondary concern as most SQL Servers physically reside behind locked doors and OS access is restricted to computer administrators with domain authentication.

If you're running SQL Server 2008+ Enterprise, you have access to Transparent Data Encryption which will encrypt the data files on disk. That doesn't prevent moving the database if you have full access to the original host system, but it does mean there are additional steps and it will be difficult to access the data by directly reading the data from the disk. Furthermore, you can also encrypt your backups (I believe SQL Server Standard and above supports this, I'm not sure about Express).

As far as Express LocalDB... your options are pretty limited. You can use NTFS level encryption, but that will impact performance as the DB engine is no longer aware that encryption is going on. Furthermore, anybody who can access the DB unencrypted would be able to access the DB files to copy, so you're not actually protected against inside attacks. You could also use BitLocker full disk encryption, but again, that will come with some I/O cost and, again, doesn't protect against inside attacks.

Finally, no matter what you do anybody with sa or dbo level access to the server will be able to read your data. You can choose to store only encrypted data in the DB, but that pretty much eliminates any advantage of using SQL, and your application would still need to store the means to decrypt it somewhere, which means someone could find your decryption key and work backwards from there.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
3

If the server is a real production server and it's properly secured, people should be able to connect to the database (using the server name and the database name and some kind of authentication), but beyond that, no one except administrators should have any other kind of access to the server.

Specifically, no one but the administrator(s) should:

  • be able to see the .mdf file or the folder that contains it
  • have physical access to the server so that they are even able to plug in an USB flash drive

That's how it should be on a "real" server.

SQL Server LocalDB is a local-machine-only version of SQL Server Express for development.
So I guess that the machine you're talking about is a development machine.

But it doesn't matter if it's a production server or development machine - the following always applies:

  1. Whenever someone has access to the actual physical machine, they will be able to copy the database files.

  2. When someone has a copy of the database file, they will be able to restore it on another server and open it with an admin account, no matter what permissions you had set. An admin account will always be able to open it.

There's no way to prevent this, other than to make sure that evil people with USB flash drives will never have any kind of access to that computer, beyond remotely connecting to the database.


EDIT:

What about data encryption and special key?

I personally didn't know about it before, but there's already some information about it in the other answer - go read it.

TL/DR:

Community
  • 1
  • 1
Christian Specht
  • 35,843
  • 15
  • 128
  • 182