当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-20 12:03:36 浏览: 评论:0 

这篇文章主要介绍了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.  
  14. $str = file_get_contents('_-357038350_97.wxapkg.unpacked/app-wxss.js'); 
  15. $preg = '/\((data:image.*?)\)/'; 
  16. $len = strlen('data:image/png;base64,'); 
  17. if(preg_match_all($preg, $str, $arr)){ 
  18.   foreach($arr[1] as $k => $img){ 
  19.     file_put_contents('./images/a2_'.$k.'.png',base64_decode(substr($img,$len))); 
  20.     //echo substr($img,$len);exit; 
  21.   } 
  22. } else { 
  23.   echo 'no'; 
  24. } 

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.  
  10. function unpack_wxapkg($file, $targetDir) 
  11. { 
  12.   if (!is_dir($targetDir)){ 
  13.     mkdir($targetDir); 
  14.   } 
  15.  
  16.   echo "Reading file.\n"; 
  17.   $file = file_get_contents($file); 
  18.   $ptr = 18; 
  19.  
  20.   $headerStruct = new StructDef([ 
  21.     'mask1' => 'ushort', 
  22.     'info1' => 'ulong', 
  23.     'indexInfoLength' => 'ulong', 
  24.     'bodyInfoLength' => 'ushort', 
  25.     'mask2' => 'ushort', 
  26.     'fileCount' => 'ulong', 
  27.   ]); 
  28.  
  29.   echo "Parsing file header...\n"; 
  30.  
  31.   $header = $headerStruct->unpack($file); 
  32. //  print_r(['header' => $header]); 
  33.  
  34.   $unpackULong = function () use (&$file, &$ptr) { 
  35.     $ret = unpack_ulong(substr($file, $ptr, 4)); 
  36.     $ptr += 4; 
  37.     return $ret; 
  38.   }; 
  39.  
  40.   $unpackUShort = function () use (&$file, &$ptr) { 
  41.     $ret = unpack_ushort(substr($file, $ptr, 2)); 
  42.     $ptr += 2; 
  43.     return $ret; 
  44.   }; 
  45.  
  46.  
  47.   $unpackStr = function ($len) use (&$file, &$ptr) { 
  48.     $ret = substr($file, $ptr, $len); 
  49.     $ptr += $len; 
  50.     return $ret; 
  51.   }; 
  52.  
  53.  
  54.   $fileCount = $header['fileCount']; 
  55.  
  56.   echo "Got $fileCount files.\n"; 
  57.  
  58.   $unpackedFiles = []; 
  59.  
  60.   for ($i = 0; $i < $fileCount; $i++) { 
  61.     $nameLength = $unpackULong(); 
  62.     $f = [ 
  63.       'nameLength' => $nameLength, 
  64.       'name' => $unpackStr($nameLength), 
  65.       'offset' => $unpackULong(), 
  66.       'size' => $unpackULong(), 
  67.     ]; 
  68.  
  69.     echo "Unpacking file {$f['name']} ({$f['size']}bytes)...\n"; 
  70.  
  71.     $f['content'] = substr($file, $f['offset'], $f['size']); 
  72.     $unpackedFiles[] = $f; 
  73.  
  74.     $destFile = $targetDir . $f['name']; 
  75.     $destDir = dirname($destFile); 
  76.     if (!is_dir($destDir)){ 
  77.       mkdir($destDir, 0777, true); 
  78.     } 
  79.  
  80.     file_put_contents($targetDir . $f['name'], $f['content']); 
  81.   } 
  82.  
  83.  
  84. //  print_r(['unpackedFiles' => $unpackedFiles]); 
  85.  
  86.  
  87.  
  88.   echo "All done.\n"; 
  89. } 
  90.  
  91. function unpack_ulong($str) 
  92. { 
  93.   $x = unpack('N', $str); 
  94.   return $x[1]; 
  95. } 
  96.  
  97. function unpack_ushort($str) 
  98. { 
  99.   $x = unpack('n', $str); 
  100.   return $x[1]; 
  101. } 
  102.  
  103. class StructDef 
  104. { 
  105.   protected $def; 
  106.   protected $unpackFormat; 
  107.  
  108.   public function __construct($def) 
  109.   { 
  110.     $this->def = $def; 
  111.     $this->unpackFormat = self::convertStructDefToUnpackFormat($def); 
  112.   } 
  113.  
  114.   public function unpack($data) 
  115.   { 
  116.     return unpack($this->unpackFormat, $data); 
  117.   } 
  118.  
  119.   protected static function convertStructDefToUnpackFormat($def) 
  120.   { 
  121.     $defTypeToUnpackType = [ 
  122.       'byte' => 'C', 
  123.       'uchar' => 'C', 
  124.       'u8' => 'C', 
  125.       'ushort' => 'n', 
  126.       'u16' => 'n', 
  127.       'ulong' => 'N', 
  128.       'u32' => 'N', 
  129.     ]; 
  130.  
  131.     $ret = []; 
  132.     foreach ($def as $key => $type) { 
  133.       $ret[] = $defTypeToUnpackType[$type] . $key; 
  134.     } 
  135.  
  136.     return implode('/', $ret); 
  137.   } 
  138. } 
  139.  
  140. $packageFile = $argv[1]; 
  141.  
  142. //支持目录下文件批量解压 
  143.  if (is_dir($packageFile)){ 
  144.   $handle = opendir($packageFile); 
  145.   if($handle){ 
  146.     while(($fl = readdir($handle)) !== false){ 
  147.       $temp = $packageFile.DIRECTORY_SEPARATOR.$fl; 
  148.       //如果不加 $fl!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来 
  149.       if(is_file($temp)){ 
  150.         if($fl!='.' && $fl != '..'){ 
  151.           $targetDir = $temp . '.unpacked'; 
  152.           unpack_wxapkg($temp, $targetDir); 
  153.         } 
  154.       } 
  155.     } 
  156.   }  
  157. }else if (is_file($packageFile)){ 
  158.   $targetDir = $packageFile . '.unpacked'; 
  159.   unpack_wxapkg($packageFile, $targetDir); 
  160. }else{ 
  161.   echo <<<HELP 
  162. Usage: 
  163.   [php] {$argv[0]} <xxx.wxapkg> 
  164.   - Unpack the `xxx.wxapkg` to `xxx.wxapkg.unpacked` directory. 
  165. HELP; 
  166.  
  167.   exit(1); 
  168. } 
  169.  
  170. exit(0);

Tags: php微信小程序解包过程

分享到: