Skip to content

Commit 1d3586d

Browse files
committed
[aes] Add unpad_pkcs7
1 parent c533c89 commit 1d3586d

File tree

8 files changed

+45
-53
lines changed

8 files changed

+45
-53
lines changed

yt_dlp/aes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
from math import ceil
44

5-
from .compat import compat_b64decode, compat_pycrypto_AES
6-
from .utils import bytes_to_intlist, intlist_to_bytes
5+
from .compat import (
6+
compat_b64decode,
7+
compat_ord,
8+
compat_pycrypto_AES,
9+
)
10+
from .utils import (
11+
bytes_to_intlist,
12+
intlist_to_bytes,
13+
)
714

815

916
if compat_pycrypto_AES:
@@ -25,6 +32,10 @@ def aes_gcm_decrypt_and_verify_bytes(data, key, tag, nonce):
2532
return intlist_to_bytes(aes_gcm_decrypt_and_verify(*map(bytes_to_intlist, (data, key, tag, nonce))))
2633

2734

35+
def unpad_pkcs7(data):
36+
return data[:-compat_ord(data[-1])]
37+
38+
2839
BLOCK_SIZE_BYTES = 16
2940

3041

@@ -506,5 +517,6 @@ def ghash(subkey, data):
506517
'aes_encrypt',
507518
'aes_gcm_decrypt_and_verify',
508519
'aes_gcm_decrypt_and_verify_bytes',
509-
'key_expansion'
520+
'key_expansion',
521+
'unpad_pkcs7',
510522
]

yt_dlp/cookies.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from enum import Enum, auto
1212
from hashlib import pbkdf2_hmac
1313

14-
from .aes import aes_cbc_decrypt_bytes, aes_gcm_decrypt_and_verify_bytes
14+
from .aes import (
15+
aes_cbc_decrypt_bytes,
16+
aes_gcm_decrypt_and_verify_bytes,
17+
unpad_pkcs7,
18+
)
1519
from .compat import (
1620
compat_b64decode,
1721
compat_cookiejar_Cookie,
@@ -846,10 +850,9 @@ def pbkdf2_sha1(password, salt, iterations, key_length):
846850

847851

848852
def _decrypt_aes_cbc(ciphertext, key, logger, initialization_vector=b' ' * 16):
849-
plaintext = aes_cbc_decrypt_bytes(ciphertext, key, initialization_vector)
850-
padding_length = plaintext[-1]
853+
plaintext = unpad_pkcs7(aes_cbc_decrypt_bytes(ciphertext, key, initialization_vector))
851854
try:
852-
return plaintext[:-padding_length].decode('utf-8')
855+
return plaintext.decode('utf-8')
853856
except UnicodeDecodeError:
854857
logger.warning('failed to decrypt cookie (AES-CBC) because UTF-8 decoding failed. Possibly the key is wrong?', only_once=True)
855858
return None

yt_dlp/downloader/fragment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from .common import FileDownloader
1616
from .http import HttpFD
17-
from ..aes import aes_cbc_decrypt_bytes
17+
from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
1818
from ..compat import (
1919
compat_os_name,
2020
compat_urllib_error,
@@ -366,8 +366,7 @@ def decrypt_fragment(fragment, frag_content):
366366
# not what it decrypts to.
367367
if self.params.get('test', False):
368368
return frag_content
369-
decrypted_data = aes_cbc_decrypt_bytes(frag_content, decrypt_info['KEY'], iv)
370-
return decrypted_data[:-decrypted_data[-1]]
369+
return unpad_pkcs7(aes_cbc_decrypt_bytes(frag_content, decrypt_info['KEY'], iv))
371370

372371
return decrypt_fragment
373372

yt_dlp/extractor/adn.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import random
99

1010
from .common import InfoExtractor
11-
from ..aes import aes_cbc_decrypt
11+
from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
1212
from ..compat import (
1313
compat_HTTPError,
1414
compat_b64decode,
15-
compat_ord,
1615
)
1716
from ..utils import (
1817
ass_subtitles_timecode,
@@ -84,14 +83,11 @@ def _get_subtitles(self, sub_url, video_id):
8483
return None
8584

8685
# http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
87-
dec_subtitles = intlist_to_bytes(aes_cbc_decrypt(
88-
bytes_to_intlist(compat_b64decode(enc_subtitles[24:])),
89-
bytes_to_intlist(binascii.unhexlify(self._K + 'ab9f52f5baae7c72')),
90-
bytes_to_intlist(compat_b64decode(enc_subtitles[:24]))
91-
))
92-
subtitles_json = self._parse_json(
93-
dec_subtitles[:-compat_ord(dec_subtitles[-1])].decode(),
94-
None, fatal=False)
86+
dec_subtitles = unpad_pkcs7(aes_cbc_decrypt_bytes(
87+
compat_b64decode(enc_subtitles[24:]),
88+
binascii.unhexlify(self._K + 'ab9f52f5baae7c72'),
89+
compat_b64decode(enc_subtitles[:24])))
90+
subtitles_json = self._parse_json(dec_subtitles.decode(), None, fatal=False)
9591
if not subtitles_json:
9692
return None
9793

yt_dlp/extractor/drtv.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88

99
from .common import InfoExtractor
10-
from ..aes import aes_cbc_decrypt
10+
from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
1111
from ..compat import compat_urllib_parse_unquote
1212
from ..utils import (
13-
bytes_to_intlist,
1413
ExtractorError,
1514
int_or_none,
16-
intlist_to_bytes,
1715
float_or_none,
1816
mimetype2ext,
1917
str_or_none,
@@ -191,13 +189,11 @@ def hex_to_bytes(hex):
191189
def decrypt_uri(e):
192190
n = int(e[2:10], 16)
193191
a = e[10 + n:]
194-
data = bytes_to_intlist(hex_to_bytes(e[10:10 + n]))
195-
key = bytes_to_intlist(hashlib.sha256(
196-
('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest())
197-
iv = bytes_to_intlist(hex_to_bytes(a))
198-
decrypted = aes_cbc_decrypt(data, key, iv)
199-
return intlist_to_bytes(
200-
decrypted[:-decrypted[-1]]).decode('utf-8').split('?')[0]
192+
data = hex_to_bytes(e[10:10 + n])
193+
key = hashlib.sha256(('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest()
194+
iv = hex_to_bytes(a)
195+
decrypted = unpad_pkcs7(aes_cbc_decrypt_bytes(data, key, iv))
196+
return decrypted.decode('utf-8').split('?')[0]
201197

202198
for asset in assets:
203199
kind = asset.get('Kind')

yt_dlp/extractor/newstube.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
import hashlib
66

77
from .common import InfoExtractor
8-
from ..aes import aes_cbc_decrypt
8+
from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
99
from ..utils import (
10-
bytes_to_intlist,
1110
int_or_none,
12-
intlist_to_bytes,
1311
parse_codecs,
1412
parse_duration,
1513
)
@@ -47,10 +45,8 @@ def _real_extract(self, url):
4745
}))
4846
key = hashlib.pbkdf2_hmac(
4947
'sha1', video_guid.replace('-', '').encode(), enc_data[:16], 1)[:16]
50-
dec_data = aes_cbc_decrypt(
51-
bytes_to_intlist(enc_data[32:]), bytes_to_intlist(key),
52-
bytes_to_intlist(enc_data[16:32]))
53-
sources = self._parse_json(intlist_to_bytes(dec_data[:-dec_data[-1]]), video_guid)
48+
dec_data = unpad_pkcs7(aes_cbc_decrypt_bytes(enc_data[32:], key, enc_data[16:32]))
49+
sources = self._parse_json(dec_data, video_guid)
5450

5551
formats = []
5652
for source in sources:

yt_dlp/extractor/rtl2.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
import re
55

66
from .common import InfoExtractor
7-
from ..aes import aes_cbc_decrypt
7+
from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
88
from ..compat import (
99
compat_b64decode,
10-
compat_ord,
1110
compat_str,
1211
)
1312
from ..utils import (
14-
bytes_to_intlist,
1513
ExtractorError,
16-
intlist_to_bytes,
1714
int_or_none,
1815
strip_or_none,
1916
)
@@ -142,17 +139,12 @@ def _real_extract(self, url):
142139
self._BACKWERK_BASE_URL + 'stream/video/' + video_id, video_id)
143140

144141
data, iv = compat_b64decode(stream_data['streamUrl']).decode().split(':')
145-
stream_url = intlist_to_bytes(aes_cbc_decrypt(
146-
bytes_to_intlist(compat_b64decode(data)),
147-
bytes_to_intlist(self._AES_KEY),
148-
bytes_to_intlist(compat_b64decode(iv))
149-
))
142+
stream_url = unpad_pkcs7(aes_cbc_decrypt_bytes(
143+
compat_b64decode(data), self._AES_KEY, compat_b64decode(iv)))
150144
if b'rtl2_you_video_not_found' in stream_url:
151145
raise ExtractorError('video not found', expected=True)
152146

153-
formats = self._extract_m3u8_formats(
154-
stream_url[:-compat_ord(stream_url[-1])].decode(),
155-
video_id, 'mp4', 'm3u8_native')
147+
formats = self._extract_m3u8_formats(stream_url.decode(), video_id, 'mp4', 'm3u8_native')
156148
self._sort_formats(formats)
157149

158150
video_data = self._download_json(

yt_dlp/extractor/shemaroome.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from __future__ import unicode_literals
33

44
from .common import InfoExtractor
5-
from ..aes import aes_cbc_decrypt
5+
from ..aes import aes_cbc_decrypt, unpad_pkcs7
66
from ..compat import (
77
compat_b64decode,
8-
compat_ord,
98
)
109
from ..utils import (
1110
bytes_to_intlist,
@@ -76,8 +75,7 @@ def _real_extract(self, url):
7675
url_data = bytes_to_intlist(compat_b64decode(data_json['new_play_url']))
7776
key = bytes_to_intlist(compat_b64decode(data_json['key']))
7877
iv = [0] * 16
79-
m3u8_url = intlist_to_bytes(aes_cbc_decrypt(url_data, key, iv))
80-
m3u8_url = m3u8_url[:-compat_ord((m3u8_url[-1]))].decode('ascii')
78+
m3u8_url = unpad_pkcs7(intlist_to_bytes(aes_cbc_decrypt(url_data, key, iv))).decode('ascii')
8179
formats, m3u8_subs = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, fatal=False, headers={'stream_key': data_json['stream_key']})
8280
self._sort_formats(formats)
8381

0 commit comments

Comments
 (0)