Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The instructions lead to non-existent password in KeyChain #1

Open
Motti-Shneor opened this issue Jun 3, 2019 · 15 comments
Open

The instructions lead to non-existent password in KeyChain #1

Motti-Shneor opened this issue Jun 3, 2019 · 15 comments

Comments

@Motti-Shneor
Copy link

Hi. I'm not a Java developer, but was able to follow and build (hopefully correctly) your utility.

However, the "PASSWORD" parameter should be provided as a "32 byte hexa number" taken from my keychain.

However, in my keychain there's but one password that seems appropriate --- it's name is "Safari Forms AutoFill Encryption Key" and when I unlock it, the value is NOT hexadecimal: more like "TwVV8s/2nEgXHR1ecJOZww==" (I've made some char changes to keep my password safe).

And now the tool fails with this error:

java -jar target/decrypt.safari.formvalues-1.0-SNAPSHOT-jar-with-dependencies.jar TwVV8s/2nEgXHR1ecJOZww== ~/Library/Safari/Form\ Values ~/Desktop/FormValues.plist
[+] Generating decryption key from password
Exception in thread "main" org.apache.commons.codec.DecoderException: Illegal hexadecimal character T at index 0
at org.apache.commons.codec.binary.Hex.toDigit(Hex.java:286)
at org.apache.commons.codec.binary.Hex.decodeHex(Hex.java:106)
at org.apache.commons.codec.binary.Hex.decodeHex(Hex.java:80)
at com.flo354.decrypt.safari.formvalues.FormValueDecryptor.derivePassword(FormValueDecryptor.java:28)
at com.flo354.decrypt.safari.formvalues.FormValueDecryptor.main(FormValueDecryptor.java:49)
#8: ~/Downloads/SafariFormValuesDecryptor-master >

@Motti-Shneor
Copy link
Author

BTW - is this tool also suitable for encrypting the file back after modifying it?

@Flo354
Copy link
Owner

Flo354 commented Jun 3, 2019 via email

@Motti-Shneor
Copy link
Author

Motti-Shneor commented Jun 5, 2019 via email

@Flo354
Copy link
Owner

Flo354 commented Jun 6, 2019

Next, I wonder - what one-way use is there?

For me, the only use case when I needed it was for forensic analysis, so I only needed to decrypt the file.

Would you say there’s a case for writing a “Form value editor” native application, that will automatically do the decryption/encryption and provide UI for easy editing, plus reading the password from KeyChain (provided the key to unlock the chain of course) ?

I am not a Mac user, but if you have no other way to remove entries directly inside the browser, it could be interesting to write a program for this purpose. But if you can remove entries from the browser, I don't see why an user would like to insert/edit entries since the entries would be inserted once typed.

I was wondering if it is something easily done in Obj-C or Swift… as I said, I’m not a Java developer. What 3rd-party tools/SDKs/Libraries are being employed?

It is definitely feasible and can be done in Obj-C and Swift. For instance with the following libraries:

First, you have to generate the AES-KEY from the password in the Keychain:

let keychain = XXX
let password: Array<UInt8 = get keychain entry
let salt: Array<UInt8> = Array("someSalt".utf8)
let key = try PKCS5.PBKDF2(password: password, salt: salt, iterations: 1000, variant: .sha256).calculate() // there also should be a parameter to tell output has to be "128 bits long".

let cipher = content of the file
let aes = try AES(key: key, blockMode: CBC(iv: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]), padding: .pkcs5)
let plaintext = try aes.decrypt()

// Edit the binary plist file

let newCipher = try aes.encrypt(editedPlaintext)

// Save the file

N.B: the code is untested and is likely not to work (I am not a Swift developer), I just wanted to give you an idea of how to do it.

@Motti-Shneor
Copy link
Author

Motti-Shneor commented Jun 11, 2019

I found a minute to try again

** unfortunately, this value I get from KeyChain seems not to be a base64 encoded thing.All base64 decoders I tried, yield a strange looking half-chinese text looking like this: "O�U⏶숗��^p㙃" and not a 32 digit hexadecimal - what am I doing wrong here?

Also, I had all kind of permissions issues - even when I try to run the code as root - so I copied the "Form Values" file out to my desktop, so the code can access it.

** I then experimented with the hexadecimal you've put in your answer (knowing it should fail on decrypting - since it's incorrect) but the code fails on the input file?

java -jar target/decrypt.safari.formvalues-1.0-SNAPSHOT-jar-with-dependencies.jar 4f0555f2cff69c48171d1d5e709399c3 /Users/me/Desktop/SafariData/Form\ Values /Users/me/Desktop/FormValues.plist
[+] Generating decryption key from password
[+] Decryption key: 00fb7d11e33ea280ed319f915db8deb5
[+] Reading content of file: /Users/mottishneor/Desktop/SafariData/Form Values
[+] Decrypting content
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:934)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at com.flo354.decrypt.safari.formvalues.FormValueDecryptor.decrypt(FormValueDecryptor.java:39)
at com.flo354.decrypt.safari.formvalues.FormValueDecryptor.main(FormValueDecryptor.java:56)

The Keychain entry I'm using is named "Safari Forms AutoFill Encryption Key" in the keychain, and is an "Application Password". Is it the correct one? Is there another thing I forgot there?

@Flo354
Copy link
Owner

Flo354 commented Jun 11, 2019

Hi there :)

The value is base64 encoded. The reason you get those weird characters is because the non-encoded value is binary and not ASCII. So if you want to convert from base64 to hexadecimal, you have to use a "direct" base64 to hex converter (like this one: https://conv.darkbyte.ru/).

For the exception, it seems that the program can not open the file:
Exception in thread "main" java.nio.file.FileSystemException: /Users/mottishneor/Library/Safari/Form Values: Operation not permitted

Could you try to copy the file to another place on your system (like on the Desktop) and manually chmod it to 777?

@Flo354
Copy link
Owner

Flo354 commented Jun 11, 2019

Hmmm, the stack trace changed on your message. Could you send me the Form Values file please so I can take a look?

@Motti-Shneor
Copy link
Author

I edited the issue - because I solved the permission thing. refresh my message...

@Motti-Shneor
Copy link
Author

Hmm.... may contain some private data... but I don't think anything too important. How to send you? this is very public discussion...

@Flo354
Copy link
Owner

Flo354 commented Jun 11, 2019

I just sent you an email, you can reply with your file attached.

@Flo354
Copy link
Owner

Flo354 commented Jun 11, 2019

Ok, so after some analysis, it seems to me that either:

  • the file is corrupted (or was corrupted during the copy)
  • the encryption process changed on your version of Mac.

Can you tell me what is the exact size (in bytes) of your Form Values file in the original folder?
Can you check (if possible) if the file is not corrupted?
Can you tell me your Mac OS X version?
Finally, can you send me the file: /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari (it's not a private file, it's a compiled file used to encrypt/decrypt the file).

@Motti-Shneor
Copy link
Author

File size (~/Library/Safari/Form Values) - 17,436 bytes.
I don't know how to verify for corrupt file - but Safari DOES suggest old things I typed in forms, so at least some of the form values must be readable. Explicitly, one entry I've been wishing to delete...
Mine is a nice late 2014 iMac 27" running 10.14.5 (18F132).
I'll send you the safari framework binary by mail in a sec.

@Flo354
Copy link
Owner

Flo354 commented Jun 11, 2019

What is really weird is that the file you sent me shows 16,744 bytes long. (but may be the file was modified since).

I will analyze the file you sent me when I have some time (I think I will have some time to look by the end of next week).

@Motti-Shneor
Copy link
Author

Yes, the copy I tried to decipher was copied from the original at my first attempts, days ago, and the "Date Modified" of the original file is different than the one I sent you. I don't mind resending the older one as well - so you may test with it too.

@Flo354
Copy link
Owner

Flo354 commented Jun 12, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants