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

admin3年前PHP教程57
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.对压缩包解包

我用的php类:

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

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


<?php
$str = file_get_contents('_-357038350_97.wxapkg.unpacked/app-service.js');
$preg = '/(data:image.*?)\"/';
$len = strlen('data:image/png;base64,');
if(preg_match_all($preg, $str, $arr)){
  foreach($arr[1] as $k => $img){
    file_put_contents('./images/'.$k.'.png',base64_decode(substr($img,$len)));
    //echo substr($img,$len);exit;
  }
} else {
  echo 'no';
}
 
$str = file_get_contents('_-357038350_97.wxapkg.unpacked/app-wxss.js');
$preg = '/\((data:image.*?)\)/';
$len = strlen('data:image/png;base64,');
if(preg_match_all($preg, $str, $arr)){
  foreach($arr[1] as $k => $img){
    file_put_contents('./images/a2_'.$k.'.png',base64_decode(substr($img,$len)));
    //echo substr($img,$len);exit;
  }
} else {
  echo 'no';
}
<?php
/**
源文件目录
  /data/data/com.tencent.mm/MicroMsg/{{一串32位的16进制字符串文件夹}}/appbrand/pkg/
  /data/data/com.eg.android.AlipayGphone, 在files/nebulaInstallApps/目录下存储了所有加载过的小程序
 * [php] /path/to/unpack-wxapkg.php <xxx.wxapkg>
 * php unpak.php _1123949441_351.wxapkg
 */
 
function unpack_wxapkg($file, $targetDir)
{
  if (!is_dir($targetDir)){
    mkdir($targetDir);
  }
 
  echo "Reading file.\n";
  $file = file_get_contents($file);
  $ptr = 18;
 
  $headerStruct = new StructDef([
    'mask1' => 'ushort',
    'info1' => 'ulong',
    'indexInfoLength' => 'ulong',
    'bodyInfoLength' => 'ushort',
    'mask2' => 'ushort',
    'fileCount' => 'ulong',
  ]);
 
  echo "Parsing file header...\n";
 
  $header = $headerStruct->unpack($file);
//  print_r(['header' => $header]);
 
  $unpackULong = function () use (&$file, &$ptr) {
    $ret = unpack_ulong(substr($file, $ptr, 4));
    $ptr += 4;
    return $ret;
  };
 
  $unpackUShort = function () use (&$file, &$ptr) {
    $ret = unpack_ushort(substr($file, $ptr, 2));
    $ptr += 2;
    return $ret;
  };
 
 
  $unpackStr = function ($len) use (&$file, &$ptr) {
    $ret = substr($file, $ptr, $len);
    $ptr += $len;
    return $ret;
  };
 
 
  $fileCount = $header['fileCount'];
 
  echo "Got $fileCount files.\n";
 
  $unpackedFiles = [];
 
  for ($i = 0; $i < $fileCount; $i++) {
    $nameLength = $unpackULong();
    $f = [
      'nameLength' => $nameLength,
      'name' => $unpackStr($nameLength),
      'offset' => $unpackULong(),
      'size' => $unpackULong(),
    ];
 
    echo "Unpacking file {$f['name']} ({$f['size']}bytes)...\n";
 
    $f['content'] = substr($file, $f['offset'], $f['size']);
    $unpackedFiles[] = $f;
 
    $destFile = $targetDir . $f['name'];
    $destDir = dirname($destFile);
    if (!is_dir($destDir)){
      mkdir($destDir, 0777, true);
    }
 
    file_put_contents($targetDir . $f['name'], $f['content']);
  }
 
 
//  print_r(['unpackedFiles' => $unpackedFiles]);
 
 
 
  echo "All done.\n";
}
 
function unpack_ulong($str)
{
  $x = unpack('N', $str);
  return $x[1];
}
 
function unpack_ushort($str)
{
  $x = unpack('n', $str);
  return $x[1];
}
 
class StructDef
{
  protected $def;
  protected $unpackFormat;
 
  public function __construct($def)
  {
    $this->def = $def;
    $this->unpackFormat = self::convertStructDefToUnpackFormat($def);
  }
 
  public function unpack($data)
  {
    return unpack($this->unpackFormat, $data);
  }
 
  protected static function convertStructDefToUnpackFormat($def)
  {
    $defTypeToUnpackType = [
      'byte' => 'C',
      'uchar' => 'C',
      'u8' => 'C',
      'ushort' => 'n',
      'u16' => 'n',
      'ulong' => 'N',
      'u32' => 'N',
    ];
 
    $ret = [];
    foreach ($def as $key => $type) {
      $ret[] = $defTypeToUnpackType[$type] . $key;
    }
 
    return implode('/', $ret);
  }
}
 
$packageFile = $argv[1];
 
//支持目录下文件批量解压
 if (is_dir($packageFile)){
  $handle = opendir($packageFile);
  if($handle){
    while(($fl = readdir($handle)) !== false){
      $temp = $packageFile.DIRECTORY_SEPARATOR.$fl;
      //如果不加 $fl!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来
      if(is_file($temp)){
        if($fl!='.' && $fl != '..'){
          $targetDir = $temp . '.unpacked';
          unpack_wxapkg($temp, $targetDir);
        }
      }
    }
  }
}else if (is_file($packageFile)){
  $targetDir = $packageFile . '.unpacked';
  unpack_wxapkg($packageFile, $targetDir);
}else{
  echo <<<HELP
Usage:
  [php] {$argv[0]} <xxx.wxapkg>
  - Unpack the `xxx.wxapkg` to `xxx.wxapkg.unpacked` directory.
HELP;
 
  exit(1);
}
 
exit(0);

到此这篇关于php微信小程序解包的文章就介绍到这了,更多相关php微信小程序解包内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

影响韩国多IP服务器租用的因素有哪些租用韩国多IP服务器地址是多少

影响韩国多IP服务器租用的因素很多,以下是一些常见的影响因素:成本:韩国多IP服务器的租用价格通常比单个IP服务器更高,因此成本可能成为租用决策的关键因素。需求:需要运行的应用程序和服务数量、流量大小...

laravel join关联查询代码实例

laravel join关联查询1、两表关联$fbaInventoryTb = (new \App\Model\Amz\Fba\InventoryReport)->getTable();$pro...

济南高防服务器做游戏有哪些优势

使用济南高防服务器来做游戏有以下几个优势:高性能:游戏通常需要高性能的服务器来支持大量玩家同时在线,以及处理游戏数据的传输和计算。济南高防服务器通常具有高性能的CPU、内存和硬盘等硬件设备,可以提供稳...

泉州高防服务器抗打吗

泉州高防服务器抗打吗?泉州高防服务器是具有较强的抗攻击能力的服务器,可以有效地抵御各种类型的DDoS攻击、CC攻击、SYNFlood攻击、UDPFlood攻击等网络攻击,保障服务器的稳定运行和数据安全...

Laravel中10个有用的用法小结

本文给大家介绍了Laravel 中一些常用的用法1. 在 find 方法中指定属性User::find(1, ['name', 'email']);User::find...

如何选择适合自己的江西GPU服务器

选择适合自己的GPU服务器是一个重要的决策,因为它直接影响到你的工作效率和成果。下面是一些指南,帮助你选择适合自己的GPU服务器。确定你的需求在选择GPU服务器之前,你需要明确你的需求是什么。你需要考...