Summery

Microsoft SQL Server Compact Edition (SQL CE) is a lightweight, embedded relational database engine developed by Microsoft. It stores structured data in .sdf files and allows applications to access databases without running a separate database server. It is part of the SQL Server product family but is distinct from the full SQL Server engine. SQL CE is discontinued, but its database files may still contain sensitive application data, usernames, and credentials. SQL CE suits small, standalone applications, while MSSQL is designed for centralized databases, multiple users, and larger workloads.

Enumeration

.sdf databases are a PAIN to get into. Here are a few ways to enumerate them, automated and manual.

Automated method
You simply visit this site, supply the database file, password if needed be, and all is well.

Manual method
For manual method on Windows is to use System.Data.SqlServerCe.dll which provides classes for connecting to SQL Server Compact databases (.sdf). It should come pre-installed on a host that has this type of databse on itself.

You can then run these commands to connect to the database:

  1. Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll" - Load the SQL Server Compact .NET library
  2. $conn = New-Object System.Data.SqlServerCe.SqlCeConnection("Data Source='C:\Program Files (x86)\hMailServer\Database\hMailServer.sdf';Password='95C02068FD5D'") - Create a connection to hMailServer.sdf using the provided password
  3. $conn.Open() - Open the database connection
  4. $conn.State - Show the connection state like Open or Closed, the former one means that all is good
    Important note: It’s crucial to pick the correct .dll version in step one (x32 or x64) depending on the architecture of the software you use it for.

Then, here are the command strings to enumerate given parts of the database.
Show what tables are in a database:

  1. $cmd = $conn.CreateCommand()
  2. $cmd.CommandText = "SELECT table_name FROM information_schema.tables"
  3. $reader = $cmd.ExecuteReader()
  4. while ($reader.Read()) { $reader["table_name"] }

Show all data from a table:

  1. $cmd.CommandText = "SELECT * FROM hm_accounts"
  2. $reader = $cmd.ExecuteReader(); while ($reader.Read()) { for ($i = 0; $i -lt $reader.FieldCount; $i++) { "$($reader.GetName($i))=$($reader.GetValue($i))" }; "" }; $reader.Close()

Show what columns are there in a table:

  1. $cmd.CommandText = "SELECT column_name FROM information_schema.columns WHERE table_name = 'hm_accounts'"
  2. $reader = $cmd.ExecuteReader(); while ($reader.Read()) { $reader["column_name"] }; $reader.Close()

Show data from the specified columns:

  1. $cmd.CommandText = "SELECT accountaddress, accountpassword FROM hm_accounts"
  2. $reader = $cmd.ExecuteReader(); while ($reader.Read()) { $reader["accountaddress"], $reader["accountpassword"] -join ":" }; $reader.Close()

If you encounter the issue that the database shows the Closed status, even though it should. run it again, but change the 3rd step from $conn.Open() to try { $conn.Open() } catch { $_.Exception.Message } to see what error it produces.

So far I only encountered an error stating that the database need to be upgraded, to upgrade it, follow these steps

  1. $engine = New-Object System.Data.SqlServerCe.SqlCeEngine("Data Source=C:\Windows\Temp\hMailServer.sdf;Password=95C02068FD5D") - Create a SQL Server Compact engine object
  2. $engine.Upgrade("Data Source=C:\Windows\Temp\hMailServerUpgraded.sdf") - Upgrade the database to a newer SQL Server Compact format and save it in a new location
  3. ls C:\Windows\Temp\hMailServerUpgraded.sdf - Verify the upgraded database is there

References

Used in: hMailServer
0xdf write-up