获取微信小程序源码

一、Android ROOT手机

一、ROOT的Androis手机可以在/data/data/com.tencent.mm/MicroMsg/<长串字符>/appbrand/pkg, 虽然看不到小程序名称或者appkey,但是可以根据文件改动时间大概猜测是哪个wxapkg文件。wxapkg的解压下面会介绍

二、抓包获取

通过抓包获取downloadURL, 直接下载即可。下载完成后是一个<长串字符串>文件,没有后缀。其实这个就是wxapkg, 可以直接改成xxxx.wxapkg就可以了。

解包 wxapkg

请下载unpack-wxapkg, 我是通过PHP解包的,据说还有python、JS版本, 请前往Github自寻。unpack-wxapkg.php 使用方法如下:

php unpack-wxapkg.php  xxxx.wxapkg  

解包后生成xxx.wxapkg.unpacked 文件夹,里面就包含各种经过处理的源码了。基本上所有的逻辑代码都在 app-service.js 里面。

附上unpack-wxapkg.php源码:

<?php


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);