Comments (4)
import base64
import gzip
import hashlib
import hmac
import json
import secrets
import uuid
from datetime import datetime
from io import BytesIO
from typing import Union
import httpx
from pyaes import AESModeOfOperationCBC, Encrypter, Decrypter
USER_AGENT = "AmazonWebView/GoodreadsForIOS App/4.0.1/iOS/15.4.1/iPhone"
class FrcCookieCipher:
FRC_SIG_SALT: bytes = b"HmacSHA256"
FRC_AES_SALT: bytes = b"AES/CBC/PKCS7Padding"
SIGNATURE_LENGTH: int = 8
def __init__(self, password: Union[str, bytes]) -> None:
if isinstance(password, str):
password = password.encode()
self.aes_key = self._get_key(password, self.FRC_AES_SALT)
self.sig_key = self._get_key(password, self.FRC_SIG_SALT)
def _get_key(self, password, salt: bytes) -> bytes:
return hashlib.pbkdf2_hmac("sha1", password, salt, 1000, 16)
def encrypt(self, plaintext: Union[str, bytes, dict]) -> str:
if isinstance(plaintext, dict):
plaintext = json.dumps(plaintext)
if isinstance(plaintext, str):
plaintext = plaintext.encode()
compressed = gzip.compress(plaintext)
iv = secrets.token_bytes(16)
cipher = Encrypter(AESModeOfOperationCBC(self.aes_key, iv))
ciphertext = cipher.feed(compressed) + cipher.feed()
signature = hmac.new(self.sig_key, iv + ciphertext, hashlib.sha256).digest()
return base64.b64encode(
b"\0" + signature[:self.SIGNATURE_LENGTH] + iv + ciphertext
).decode()
def decrypt(self, frc: str, verify_signature: bool = True) -> Union[dict, bytes]:
pad = (4 - len(frc) % 4) * "="
frc = BytesIO(base64.b64decode(frc + pad))
frc.seek(1) # the first byte is always 0, skip them
signature = frc.read(self.SIGNATURE_LENGTH)
iv = frc.read(16)
ciphertext = frc.read()
if verify_signature:
assert self._verify_signature(signature, iv, ciphertext) is True, "Signature missmatch"
decipher = Decrypter(AESModeOfOperationCBC(self.aes_key, iv))
plaintext = decipher.feed(ciphertext) + decipher.feed()
decompressed = gzip.decompress(plaintext)
try:
return json.loads(decompressed)
except json.JSONDecodeError:
return decompressed
def _verify_signature(self, signature, iv, ciphertext) -> bool:
new_signature = hmac.new(self.sig_key, iv + ciphertext, hashlib.sha256).digest()
return signature == new_signature[:self.SIGNATURE_LENGTH]
def register(username, password, domain):
url = f"https://api.amazon.{domain}/auth/register"
device_serial = secrets.token_hex(16).upper()
frc = {
'ApplicationVersion': '3.56.2',
'DeviceOSVersion': 'iOS/15.5',
'ScreenWidthPixels': '428',
'TimeZone': '+01:00',
'ScreenHeightPixels': '926',
'ApplicationName': 'Audible',
'DeviceJailbroken': False,
'DeviceLanguage': 'de-DE',
'DeviceFingerprintTimestamp': round(datetime.utcnow().timestamp()) * 1000,
'ThirdPartyDeviceId': str(uuid.uuid4()).upper(),
'DeviceName': 'iPhone',
'Carrier': 'Vodafone.de'
}
frc = FrcCookieCipher(device_serial).encrypt(frc)
headers = {
"x-amzn-identity-auth-domain": f"api.amazon.{domain}",
"User-Agent": USER_AGENT,
"Accept-Encoding": "gzip",
"Accept": "application/json",
"Accept-Language": "en-DE",
"Accept-Charset": "utf-8"
}
json_body = {
"requested_extensions": [
"device_info",
"customer_info"
],
"cookies": {
"website_cookies": [],
"domain": ".amazon.de"
},
"registration_data": {
"domain": "Device",
"app_version": "3.56.2",
"device_serial": device_serial,
"device_type": "A2CZJZGLK2JJVM",
"device_name": (
"%FIRST_NAME%%FIRST_NAME_POSSESSIVE_STRING%%DUPE_"
"STRATEGY_1ST%Audible for iPhone"
),
"os_version": "15.5.0",
"software_version": "35602678",
"device_model": "iPhone",
"app_name": "Audible"
},
"auth_data": {
"use_global_authentication": "false",
"user_id_password": {
"user_id": username,
"password": password
}
},
"user_context_map": {
"frc": frc
},
"requested_token_type": [
"bearer",
"mac_dms",
"website_cookies"
]
}
r = httpx.post(url, headers=headers, json=json_body)
return r
if __name__ == "__main__":
username = "" # FILL OUT
password = "" # FILL OUT
domain = "de" # FILL OUT
current_otp = "" # FILL OUT IF 2FA is activated
password += current_otp
r = register(username, password, domain)
print(r)
print(r.headers)
print(r.cookies)
print(r.json())
from audible.
I've found out myself, how to handle a challenge response from the server (like an OTPCode) when using the code above. But this step needs further user inputs equal to my implemented code. So I think I will don’t switch to this registration method.
from audible.
This issue has not been updated for a while and will be closed soon.
from audible.
This issue has automatically been closed due to no activities.
from audible.
Related Issues (20)
- 错误提示 HOT 19
- Quality is set to `Extreme` even set to `high` HOT 1
- Extract Audible Bookmark Notes HOT 3
- Issue getting activation bytes from auth server HOT 9
- Switch to poetry, implement CI+CD, remove Python version <3.8
- Lift upper version restriction on `httpx`. HOT 3
- Is there any way to get a list of books in a series? HOT 1
- Amazon authentication code not handled properly HOT 2
- I need some help with metadata HOT 2
- Audible brazil HOT 11
- Brazil Account Can not login by external_login HOT 3
- RecursionError introduced in 0.9.0 in some cases HOT 3
- API doc: Only High and Normal is allowed as quality
- encryptedPwd HOT 5
- How does the BestSellers sort_by option in the APIs work? HOT 3
- Where does the file get stored with to_file() HOT 5
- Can't filter plus catalog books from 'catalog/products' HOT 9
- state token for library requests HOT 2
- response_callback no longer works properly with the endpoint: https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/sidecar HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from audible.