当前位置:   article > 正文

m3u8加密文件原理及下载脚本_#ext-x-key:method=aes-128

#ext-x-key:method=aes-128

一、加密ts文件解密

  1. #EXTM3U
  2. #EXT-X-VERSION:3
  3. #EXT-X-MEDIA-SEQUENCE:0
  4. #EXT-X-ALLOW-CACHE:YES
  5. #EXT-X-TARGETDURATION:13
  6. #EXT-X-KEY:METHOD=AES-128,URI="https://j-island.net/movie/hls_key/s/857401e309d8a032c3bb18f4b09b8db2/?f=jj_20190401_hihijets_004",IV=0xaa3dcf6a7acb92ff4fb08d9b3b3d6f51
  7. #EXTINF:12.078733,
  8. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400000.ts
  9. #EXTINF:9.009000,
  10. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400001.ts
  11. #EXTINF:9.009000,
  12. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400002.ts
  13. #EXTINF:12.012000,
  14. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400003.ts
  15. #EXTINF:9.009000,
  16. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400004.ts
  17. #EXTINF:2.002000,
  18. https://j-island.stream2.johnnys-net.jp/j-island/jj_20190401_hihijets_004/jj_20190401_hihijets_00400005.ts
  19. #EXT-X-ENDLIST

m3u8视频另一种下载方式 如上图中用ts链接直接下载下来的ts视频文件是加密的(为什么要下加密的?虽然加密但是下载速度快啊)。

#EXT-X-KEY 字段已经写明了加密方式是AES-128,key通过URI获取,IV也有。

  1. #EXT-X-KEY:
  2. METHOD=AES-128,
  3. URI="https://j-island.net/movie/hls_key/s/857401e309d8a032c3bb18f4b09b8db2/?f=jj_20190401_hihijets_004",
  4. IV=0xaa3dcf6a7acb92ff4fb08d9b3b3d6f51
 

那怎么解密呢?

1.复制URI地址到网页,你将秒速自动下载得到一个16字节文件 2.打开终端,打开该文件查看16进制:

  • 打开二进制key

vi -b /路径/key文件
  • 输入“ :%!xxd”再把二进制文件切换到16进制

00000000: 283d cc2d 3747 e965 0a81 ead3 1e04 fa8a  (=.-7G.e........

获取到key就是 283dcc2d3747e9650a81ead31e04fa8a 3.打开终端,转化

openssl aes-128-cbc -d -in /路径/media_0.ts -out /路径/media_decryptd_0.ts -nosalt -iv $iv -K $strkey

*media_0.ts* : 加密ts文件(无法播放) *media_decryptd_0.ts* : 解密后ts文件(可以直接播放) *$iv* : aa3dcf6a7acb92ff4fb08d9b3b3d6f51(#EXT-X-KEY字段中获取,注意去掉0x) *$strkey* : 283dcc2d3747e9650a81ead31e04fa8a(上一步获得)

例子:

openssl aes-128-cbc -d -in /Users/用户名/Downloads/001.ts -out /Users/用户名/Downloads/001_dec.ts -nosalt -iv aa3dcf6a7acb92ff4fb08d9b3b3d6f51 -K 283dcc2d3747e9650a81ead31e04fa8a

命令执行完秒速转化(也可能我文件特别短,都切成ts了,一般都不长吧…… 4.到对应路径下去,就能看到转化成功到ts视频了

二、案例

1、m3u8文件

通过连接:

https://XXXXXXX/video/20210713/48466f15b9094154a3dc405efd5b54fd/cloudv-transfer/555555552773r8qq5556n265646r92r4_e8f05058b8c14e679738b91d96fa6d60_0_3.m3u8?t=1652097774&k=a062691ead867c707b07b5c6caf87385&vid=9e74e191017a1000dd3e822700000000&r=a8aede38018010004b91ff8f00000000
下载文件内容如下
​
​

  1. #EXTM3U
  2. #EXT-X-VERSION:3
  3. #EXT-X-MEDIA-SEQUENCE:0
  4. #EXT-X-ALLOW-CACHE:YES
  5. #EXT-X-TARGETDURATION:11
  6. #EXT-X-KEY:METHOD=AES-128,URI="https://api.cloudv.haplat.net/vod/videoManage/getHlsKeyByVideoId?cid=882006&t=1652097774&k=a062691ead867c707b07b5c6caf87385&vid=9e74e191017a1000dd3e822700000000&r=a8aede38018010004b91ff8f00000000",IV=0x993181d25c49657b7b6bfeff30f13d8c
  7. #EXTINF:1.200000,
  8. a08a0c0364dc4000b46560e905564520_0000000.ts
  9. #EXTINF:1.000000,
  10. a08a0c0364dc4000b46560e905564520_0000001.ts

2、key值

https://api.cloudv.haplat.net/vod/videoManage/getHlsKeyByVideoId?cid=882006&t=1652097774&k=a062691ead867c707b07b5c6caf87385&vid=9e74e191017a1000dd3e822700000000&r=a8aede38018010004b91ff8f00000000
找到请求的返回值


​将字符串进行16进制编码

 

3 、下载ts文件

通过分析,ts下载网址为

https://xxxxxxx/video/20210713/48466f15b9094154a3dc405efd5b54fd/cloudv-transfer/a08a0c0364dc4000b46560e905564520_0000000.ts
​
构造ts的下载网址为https://xxxxxxx/video/20210713/48466f15b9094154a3dc405efd5b54fd/cloudv-transfer/ + m3u8文件中的数据

4、解码

下载的ts为加密后的,需要进行解码,采用openssl解码

openssl aes-128-cbc -d -in 0.ts -out 0_1.ts -nosalt -iv 993181d25c49657b7b6bfeff30f13d8c -K f370739f5a517a4724c677a73e156126
​5脚本:
  1. #!C:\Python3.7
  2. # -*- coding:utf-8 -*-
  3. # coding=utf-8
  4. import requests
  5. import os
  6. headers = {
  7. "User - Agent": "Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 84.0.4147.105 Safari / 537.36"
  8. }
  9. def download(baseurl,file_m3u8,path_in):
  10. url_list=[]
  11. with open(file_m3u8,"r") as f:
  12. for r in f.readlines():
  13. if ".ts" in r :
  14. url_list.append(r.replace("\n","").strip())
  15. # print(url_list)
  16. for r in url_list:
  17. url = baseurl+r
  18. filename=os.path.join(path_in,r.split("_")[1])
  19. # print(url,filename)
  20. try:
  21. re = requests.get(url=url,headers=headers)
  22. except Exception as e:
  23. print(e)
  24. pass
  25. with open(filename,"ab") as f:
  26. f.write(re.content)
  27. print("[*]download:"+filename)
  28. def mergeFileToMP4(pathname):
  29. os.chdir(pathname)
  30. cmd = "copy /b *.ts new.tmp"
  31. os.system(cmd)
  32. os.system('del /Q *.ts')
  33. os.system('del /Q *.mp4')
  34. os.rename("new.tmp", "new.mp4")
  35. os.chdir('..')
  36. print("merge file is :",str(os.path.join(pathname,"new.mp4")))
  37. def key_test(key):
  38. print(len(key))
  39. key_hex=""
  40. for c in key:
  41. tem=hex(ord(c))[2:]
  42. key_hex=key_hex+tem
  43. print(key_hex)
  44. return key_hex
  45. def decode_openssl(key,iv,path_in,path_out):
  46. for file in os.listdir(path_in):
  47. file_in=os.path.join(path_in,file)
  48. file_out = os.path.join(path_out,file)
  49. # print(file_in,file_out)
  50. cmd=f"openssl aes-128-cbc -d -in {file_in} -out {file_out} -nosalt -iv {iv} -K {key}"
  51. print(cmd)
  52. os.system(cmd)
  53. def clear_ts(path):
  54. os.chdir(path)
  55. os.system("del /Q *.ts")
  56. def main(baseurl,key,iv,file_m3u8):
  57. # print("main")
  58. basepath = os.path.split(file_m3u8)[0]
  59. basepath_in = os.path.join(basepath, "in")
  60. basepath_out = os.path.join(basepath, "out")
  61. if not os.path.exists(basepath_in):
  62. os.mkdir(basepath_in)
  63. if not os.path.exists(basepath_out):
  64. os.mkdir(basepath_out)
  65. download(baseurl=baseurl, file_m3u8=file_m3u8,path_in=basepath_in)
  66. key = key_test(key)
  67. decode_openssl(key,iv,basepath_in,basepath_out)
  68. mergeFileToMP4(basepath_out)
  69. clear_ts(basepath_in)
  70. if __name__ == '__main__':
  71. # https://xxxxxxx/video/20210713/ea044c31965940559162b816ec8e152a/cloudv-transfer/a08a0bff11c84000b465638605564520_0000001.ts
  72. baseurl="https://xxxxxxxxx/video/20210713/ea044c31965940559162b816ec8e152a/cloudv-transfer/"
  73. key="±úx£Ú:êh'ÿs{câ"
  74. iv="131f71ed838a8019b731be5b7f2a1703"
  75. file_m3u8="./jixian/3.m3u8"
  76. file_m3u8 = os.path.join(os.getcwd(),file_m3u8)
  77. if(os.path.exists(file_m3u8)):
  78. main(baseurl,key,iv,file_m3u8)
  79. else:
  80. print("[-]"+file_m3u8+" not found:\n")
  81. # download(baseurl)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/154699
推荐阅读
相关标签
  

闽ICP备14008679号