PHP微信小程序解包过程实例详解

 5078

这篇文章主要介绍了php微信小程序解包过程实例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

这个解包只能看个大概

1、找到小程序压缩包

1.1、手机root或安装模拟器(我用的是夜神)

1.2、在模拟器上安装微信(用android5系统的模拟器,低版本小程序容易打不开)

1.3、打开登陆微信后,打开小程序

1.4、打开模拟器自带的文件管理器来到目录:/data/data/com.tencent.mm/MicroMsg/{{一串32位的16进制字符串文件夹}}/appbrand/pkg/

1.5、里面有很多wxapkg文件,找到最新修改日期的文件比如 -357038350_91.wxapkg,前面打勾选中

1.6、文件管理器回到/mnt/shared/Other目录,粘贴即可,打开安卓模拟器上我的电脑 =〉打开电脑文件夹找到粘贴的文件-357038350_91.wxapkg 夜神教程链接:跳转查看

2、对压缩包解包

详细参考:https://codechina.csdn.net/mirrors/leo9960/wechat-app-unpack?utm_source=csdn_github_accelerator

我用的php类:

使用方法:cmd =>cd php文件目录 =〉php wx_unpak.php 357038350_91.wxapkg

我主要是想用其中的一些图片,很多图片都被base64了放到js(app-service.js)和样式(app-wxss.js)文件中了;需要我们匹配组装一下

  1. <?php
  2. $str = file_get_contents('_-357038350_97.wxapkg.unpacked/app-service.js');
  3. $preg = '/(data:image.*?)\"/';
  4. $len = strlen('data:image/png;base64,');
  5. if(preg_match_all($preg, $str, $arr)){
  6.     foreach($arr[1] as $k => $img){
  7.         file_put_contents('./images/'.$k.'.png',base64_decode(substr($img,$len)));
  8.         //echo substr($img,$len);exit;
  9.     }
  10. } else {
  11.     echo 'no';
  12. }
  13. $str = file_get_contents('_-357038350_97.wxapkg.unpacked/app-wxss.js');
  14. $preg = '/\((data:image.*?)\)/';
  15. $len = strlen('data:image/png;base64,');
  16. if(preg_match_all($preg, $str, $arr)){
  17.     foreach($arr[1] as $k => $img){
  18.         file_put_contents('./images/a2_'.$k.'.png',base64_decode(substr($img,$len)));
  19.         //echo substr($img,$len);exit;
  20.     }
  21. } else {
  22.     echo 'no';
  23. }

wx_unpak.php

  1. <?php
  2. /**
  3.  * 源文件目录
  4.  * /data/data/com.tencent.mm/MicroMsg/{{一串32位的16进制字符串文件夹}}/appbrand/pkg/
  5.  * /data/data/com.eg.android.AlipayGphone, 在files/nebulaInstallApps/目录下存储了所有加载过的小程序
  6.  * [php] /path/to/unpack-wxapkg.php <xxx.wxapkg>
  7.  * php unpak.php _1123949441_351.wxapkg
  8.  **/
  9. function unpack_wxapkg($file, $targetDir)
  10. {
  11.     if (!is_dir($targetDir)){
  12.         mkdir($targetDir);
  13.     }
  14.     echo "Reading file.\n";
  15.     $file = file_get_contents($file);
  16.     $ptr = 18;
  17.     $headerStruct = new StructDef([
  18.         'mask1' => 'ushort',
  19.         'info1' => 'ulong',
  20.         'indexInfoLength' => 'ulong',
  21.         'bodyInfoLength' => 'ushort',
  22.         'mask2' => 'ushort',
  23.         'fileCount' => 'ulong',
  24.     ]);
  25.     echo "Parsing file header...\n";
  26.     $header = $headerStruct->unpack($file);
  27.     // print_r(['header' => $header]);
  28.     $unpackULong = function () use (&$file, &$ptr) {
  29.         $ret = unpack_ulong(substr($file, $ptr, 4));
  30.         $ptr += 4;
  31.         return $ret;
  32.     };
  33.     $unpackUShort = function () use (&$file, &$ptr) {
  34.         $ret = unpack_ushort(substr($file, $ptr, 2));
  35.         $ptr += 2;
  36.         return $ret;
  37.     };
  38.     $unpackStr = function ($len) use (&$file, &$ptr) {
  39.         $ret = substr($file, $ptr, $len);
  40.         $ptr += $len;
  41.         return $ret;
  42.     };
  43.     $fileCount = $header['fileCount'];
  44.     echo "Got $fileCount files.\n";
  45.     $unpackedFiles = [];
  46.     for ($i = 0; $i < $fileCount; $i++) {
  47.         $nameLength = $unpackULong();
  48.         $f = [
  49.             'nameLength' => $nameLength,
  50.             'name' => $unpackStr($nameLength),
  51.             'offset' => $unpackULong(),
  52.             'size' => $unpackULong(),
  53.         ];
  54.         echo "Unpacking file {$f['name']} ({$f['size']}bytes)...\n";
  55.         $f['content'] = substr($file, $f['offset'], $f['size']);
  56.         $unpackedFiles[] = $f;
  57.         $destFile = $targetDir . $f['name'];
  58.         $destDir = dirname($destFile);
  59.         if (!is_dir($destDir)){
  60.             mkdir($destDir, 0777, true);
  61.         }
  62.         file_put_contents($targetDir . $f['name'], $f['content']);
  63.     }
  64.     // print_r(['unpackedFiles' => $unpackedFiles]);
  65.     echo "All done.\n";
  66. }
  67. function unpack_ulong($str)
  68. {
  69.     $x = unpack('N', $str);
  70.     return $x[1];
  71. }
  72. function unpack_ushort($str)
  73. {
  74.     $x = unpack('n', $str);
  75.     return $x[1];
  76. }
  77. class StructDef
  78. {
  79.     protected $def;
  80.     protected $unpackFormat;
  81.     public function __construct($def)
  82.     {
  83.         $this->def = $def;
  84.         $this->unpackFormat = self::convertStructDefToUnpackFormat($def);
  85.     }
  86.     public function unpack($data)
  87.     {
  88.         return unpack($this->unpackFormat, $data);
  89.     }
  90.     protected static function convertStructDefToUnpackFormat($def)
  91.     {
  92.         $defTypeToUnpackType = [
  93.             'byte' => 'C',
  94.             'uchar' => 'C',
  95.             'u8' => 'C',
  96.             'ushort' => 'n',
  97.             'u16' => 'n',
  98.             'ulong' => 'N',
  99.             'u32' => 'N',
  100.         ];
  101.         $ret = [];
  102.         foreach ($def as $key => $type) {
  103.             $ret[] = $defTypeToUnpackType[$type] . $key;
  104.         }
  105.         return implode('/', $ret);
  106.     }
  107. }
  108. $packageFile = $argv[1];
  109. //支持目录下文件批量解压
  110. if (is_dir($packageFile)){
  111.     $handle = opendir($packageFile);
  112.     if($handle){
  113.         while(($fl = readdir($handle)) !== false){
  114.             $temp = $packageFile.DIRECTORY_SEPARATOR.$fl;
  115.             //如果不加 $fl!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来
  116.             if(is_file($temp)){
  117.                 if($fl!='.' && $fl != '..'){
  118.                     $targetDir = $temp . '.unpacked';
  119.                     unpack_wxapkg($temp, $targetDir);
  120.                 }
  121.             }
  122.         }
  123.     } 
  124. }else if (is_file($packageFile)){
  125.     $targetDir = $packageFile . '.unpacked';
  126.     unpack_wxapkg($packageFile, $targetDir);
  127. }else{
  128.     echo <<<HELP
  129. Usage:
  130.   [php] {$argv[0]} <xxx.wxapkg>
  131.   - Unpack the `xxx.wxapkg` to `xxx.wxapkg.unpacked` directory.
  132. HELP;
  133.     exit(1);
  134. }
  135. exit(0);


TAG标签:
本文网址:https://www.zztuku.com/detail-8767.html
站长图库 - PHP微信小程序解包过程实例详解
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐