borisbabic / browser_cookie3

This is a fork of browser_cookie
GNU Lesser General Public License v3.0
860 stars 144 forks source link

Portable Opera fails #95

Open mrx23dot opened 3 years ago

mrx23dot commented 3 years ago

I've got 78.0.4093.206 opera on win7, in portable mode (non installed)

trying to manually import cookies file:

site_cookies = browser_cookie3.opera(cookie_file=r'D:\Progs\Opera_Chr\profile\data\Cookies')

File starts with: "SQLite format 3" Forwards slash doesnt make a difference.

Gives error:

Traceback (most recent call last):
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 317, in _decrypt
    return self._decrypt_windows_chromium(value, encrypted_value)
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 308, in _decrypt_windows_chromium
    _, data = crypt_unprotect_data(encrypted_value)
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 98, in crypt_unprotect_data
    raise RuntimeError('Failed to decrypt the cipher text with DPAPI')
RuntimeError: Failed to decrypt the cipher text with DPAPI

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 608, in opera
    return Opera(cookie_file, domain_name, key_file).load()
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 293, in load
    value = self._decrypt(item[5], item[6])
  File "C:\python36\lib\site-packages\browser_cookie3\__init__.py", line 321, in _decrypt
    if not self.key:
AttributeError: 'Opera' object has no attribute 'key'
mrx23dot commented 3 years ago

Manually reading installed chrome cookies work

site_cookies = browser_cookie3.chrome(cookie_file=r'C:\Users\jasmine\AppData\Local\Google\Chrome\User Data\Default\Cookies')

Opera's sqlite structure is exactly the same as Chrome's. Looks like rows are fetched but decoding of blobs fail, but only for Opera type. Fails at this:

    if not windll.crypt32.CryptUnprotectData(
        byref(blob_in), byref(desc), byref(blob_entropy),
        reserved, prompt_struct, flags, byref(blob_out)
    ):
        raise RuntimeError('Failed to decrypt the cipher text with DPAPI')
mrx23dot commented 3 years ago

This script works, it uses 'Local State' to get the keys: https://gist.github.com/GramThanos/1a9f47267b447507cf4e8538d6340e2a

rafiibrahim8 commented 3 years ago

@mrx23dot browser_cookie3 has this functionality too. You just need to pass the path of the Local State file to argument key_file.

mrx23dot commented 3 years ago

Cheers, just tried it's still complaining:

import browser_cookie3

site_cookies = browser_cookie3.opera(cookie_file=r'D:\Progs\Opera_Chr\profile\data\Cookies', key_file=r'D:\Progs\Opera_Chr\profile\data\Local State')

Failed to decrypt the cipher text with DPAPI

Is there a self test we could include just to prove that the dll is working assert dec(enc('test')) == 'test'

Weirdly I don't have 'info_cache' nor 'profile' in Local State only these: dict_keys(['ab_testing', 'adblocker', 'bookmarks', 'browser', 'browserjs', 'chars', 'component_updater', 'data_use_measurement', 'dns_over_https', 'flow_service', 'freedom', 'geolocation', 'hardware_acceleration_mode', 'hardware_acceleration_mode_previous', 'http_original_content_length', 'http_received_content_length', 'icon_version', 'intl', 'last_version', 'location', 'os_crypt', 'partner_content', 'password_manager', 'plugins', 'prefs_override', 'profile_network_context_service', 'protocol_handler', 'rich_hints', 'session_id_generator_last_value', 'shutdown', 'siteprefs', 'subresource_filter', 'sync', 'task_manager', 'ui', 'update', 'updateclientdata', 'user_experience_metrics', 'yandex'])

Are these enough to get the key?

rafiibrahim8 commented 3 years ago

The key file path should be D:\Progs\Opera_Chr\profile\Local State. Any Local State file in D:\Progs\Opera_Chr\profile? (E.g.: D:\Progs\Opera_Chr\profile\Local State)

mrx23dot commented 3 years ago

That's correct I used: cookie_file=r'D:\Progs\Opera_Chr\profile\data\Cookies', key_file=r'D:\Progs\Opera_Chr\profile\data\Local State' it exists, see it's content above.

Here is a fresh install in portable mode, you can try it yourself: both_files.zip

rafiibrahim8 commented 3 years ago

Can you confirm your key_file path is correct? It should be on D:\Progs\Opera_Chr\profile\Local State, not D:\Progs\Opera_Chr\profile\data\Local State. Notice that the Cookies and the Local State file are not in the same folder.

mrx23dot commented 3 years ago

For me they are in the same folder, even when I reinstalled them into a new folder and set usb-portable mode during install.

It can find the files the problem is that the content is different in portable mode. See file attached above.

2021-10-10_181601