Git Product home page Git Product logo

howto-decrypt-signal.sqlite-for-ios's Introduction

By looking into the sourcecode of Signal on github i found the file "GRDBDatabaseStorageAdapter" to be of interest: https://github.com/signalapp/Signal-iOS/blob/e76583ac7d521e32f5de8ba4001f1847dea19c6e/SignalServiceKit/src/Storage/Database/GRDBDatabaseStorageAdapter.swift

Signal is storing the DB-key in the keyvalue "GRDBDatabaseCipherKeySpec". The value is randomly generated when initialising the Database for the first time. The Key and value are then stored in the keychain.

 private static let keyServiceName: String = "GRDBKeyChainService" 
 private static let keyName: String = "GRDBDatabaseCipherKeySpec" 

I also found out how Signal prepares and opens the DB:

 configuration.prepareDatabase = { (db: Database) in 
      let keyspec = try keyspec.fetchString() 
      try db.execute(sql: "PRAGMA key = \"\(keyspec)\"") 
      try db.execute(sql: "PRAGMA cipher_plaintext_header_size = 32") 

Using a decrypted keychain.plist i located the key "GRDBDatabaseCipherKeySpec" by searching for the keyname encoded in base64:

 user@computer:/Work/Signal$ echo -n GRDBDatabaseCipherKeySpec | base64 
 R1JEQkRhdGFiYXNlQ2lwaGVyS2V5U3BlYw== 

The value found under "v_data" is the value we are looking for:

 <key>v_Data</key> 
 <data> 
      VaVsbZDrjMKuVf3H+TUGCWcZMXn5zLGBxd9dnb/DYpAok+Q8Yw/zExRxMpGDfBee 
 </data> 

The value is the decoded using base64:

 user@computer:/Work/Signal$ echo -n VaVsbZDrjMKuVf3H+TUGCWcZMXn5zLGBxd9dnb/DYpAok+Q8Yw/zExRxMpGDfBee | base64 -d | xxd -p -c 48 
 55a56c6d90eb8cc2ae55fdc7f935060967193179f9ccb181c5df5d9dbfc362902893e43c630ff31314713291837c179e 

To open and decrypt the database i use sqlcipher. The version supplied via ubuntu failed in decrypting the db so i had to build sqlcipher from source.

  user@computer:/Work/Signal/sqlcipher/bld$ ./sqlcipher signal.sqlite 
  SQLCipher version 3.28.0 2019-04-16 19:49:53 
  Enter ".help" for usage hints. 
  sqlite> PRAGMA key="x'55a56c6d90eb8cc2ae55fdc7f935060967193179f9ccb181c5df5d9dbfc362902893e43c630ff31314713291837c179e'"; 
  sqlite> PRAGMA cipher_plaintext_header_size = 32; 
  sqlite> .tables 
  grdb_migrations                            
  keyvalue                                   
  media_gallery_items                      
  model_ExperienceUpgrade                    
  model_InstalledSticker                     
  model_KnownStickerPack                     
  model_OWSContactQuery                      
  model_OWSDevice            
  .... 
  sqlite> ATTACH DATABASE 'signal_decrypted.sqlite' AS signal_decrypted KEY ''; 
  sqlite> SELECT sqlcipher_export('signal_decrypted'); 
  sqlite> DETACH DATABASE signal_decrypted; 

The decrypted database is now found under signal_decrypted.sqlite.

A similar procedure can be used for a previous version of Signal:

Signal is storing the DB-key in the keyvalue "OWSDatabaseCipherKeySpec" (Base64: T1dTRGF0YWJhc2VDaXBoZXJLZXlTcGVj).

The version of sqlcipher used is apparently slightly older, so the version parameters must be set accordingly. See https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283

  ./sqlcipher /tmp/Signal.sqlite
  SQLCipher version 3.30.1 2019-10-10 20:19:45
  Enter ".help" for usage hints.
  sqlite> PRAGMA key="x'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'";
  ok
  sqlite> PRAGMA cipher_plaintext_header_size=32;
  sqlite> PRAGMA cipher_page_size = 1024;
  sqlite> PRAGMA cipher_hmac_algorithm = HMAC_SHA1;
  sqlite> PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;
  sqlite> .tables
  database2                                                                    
  fts_FullTextSearchFinderExtension                                            
  fts_FullTextSearchFinderExtension_config                                     
  fts_FullTextSearchFinderExtension_content                                    
  fts_FullTextSearchFinderExtension_data                                       
  .....

howto-decrypt-signal.sqlite-for-ios's People

Contributors

magpol avatar schramp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ilqp jelbrek

howto-decrypt-signal.sqlite-for-ios's Issues

Correct private key format

I managed to extract the private key using keychain_dumper:

Generic Password
----------------
Service: GRDBKeyChainService
Account: GRDBDatabaseCipherKeySpec
Entitlement Group: U68MSDN6DR.org.whispersystems.signal
Label: (null)
Accessible Attribute: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, protection level 4
Description: (null)
Comment: (null)
Synchronizable: 0
Generic Field: (null)
Keychain Data (Hex): 0xfbbe15a2bc59d7cc7fbb21ee74a13a6e2f097d38fc6ee3b9b879b82325ae4542d46c339ae3c092e3e00aa2a74ec5cdcb

Then ran sqlcipher.

$ sqlcipher signal.sqlite
SQLCipher version 3.15.2 2016-11-28 19:13:37
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key="x'fbbe15a2bc59d7cc7fbb21ee74a13a6e2f097d38fc6ee3b9b879b82325ae4542d46c339ae3c092e3e00aa2a74ec5cdcb'";
sqlite> PRAGMA cipher_plaintext_header_size = 32;
sqlite> .tables
Error: file is encrypted or is not a database
sqlite> PRAGMA cipher_plaintext_header_size=32;
sqlite> PRAGMA cipher_page_size = 1024;
sqlite> PRAGMA cipher_hmac_algorithm = HMAC_SHA1;
sqlite> PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;
sqlite> .tables
Error: file is encrypted or is not a database

However, this doesn't work. Note that I removed the 0x from the beginning of the key โ€“ I also tried leaving it in. I also attempted opening the file with DB Browser for SQLite for Mac, with which I was able to decrypt a Signal for Mac backup previously, but that did't work either.

I wonder if this is an issue with the sqlcipher version or perhaps the key format. Any help would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.