frankyxu 发表于 2020-7-7 11:38

python 逆向某咖啡小程序接口

# 文章来由
## 心血来潮突然想逆向一下某小程序,于是便有了这篇文章

# 逆向工具
## wxappUnpacker
用来解压和欢迎小程序
## pycharm
编写python代码

# 原理
用android版本登录后打开小程序,然后通过手机把源码传到电脑,导入到开发者工具,然后对源码进行调试即可,一般来说,小程序端的加密会比其他客户端简单很多

# 关键代码
## 加密代码
```
var e = require("../@babel/runtime/helpers/interopRequireDefault"), i = e(require("../@babel/runtime/helpers/typeof")), o = e(require("../service/baseService/login.js")), t = e(require("./../store/store.js")), n = require("./promise/es6-promise.min.js"), a = require("./crypto.js"), r = require("../config/config.js"), s = require("./storage"), d = function(e) {
    return new n(function(o, n) {
      if (!e) return "";
      e.data || (e.data = {});
      var d = r.api.code + "" + r.api.version, u = r.api.key, l = r.api.replaceSpecial, p = "object" === (0,
      i.default)(e.data) ? JSON.stringify(e.data) : e.data, c = a.aes.en(p, u, l), f = [ "cid=".concat(d), "q=".concat(c) ], g = t.default.data.configure.uid || s.getStore("uid");
      g && g.length > 0 && f.push("uid=".concat(g)), o({
            cid: d,
            q: c,
            sign: a.md5(f.sort().join(";") + u),
            uid: g
      });
    });
};
```
## 解密代码
```
success: function(n) {
                        e.options && !1 === e.options.loading || wx.hideLoading();
                        var d = null;
                        if (n.data && "string" == typeof n.data && (d = a.aes.de(n.data, r.api.key, r.api.replaceSpecial),
                        d = JSON.parse(d)), r.api.debug && console.log("请求参数:", e, "返回结果", d), d && (t.default.data.configure.uid = d.uid,
                        s.setStore("uid", d.uid)), 5 !== d.code) if ("BASE101" !== d.BASE101) if (7 !== d.code) 1 === d.code ? i && i(d) : wx.showToast({
                            title: d.msg,
                            icon: "none",
                            duration: 2e3
                        }); else if (e.options && !0 === e.options.needOriginResult) i(d); else {
                            var u = d.msg || "业务处理错误";
                            wx.showToast({
                              title: u,
                              icon: "none",
                              duration: 2e3
                            });
                        } else wx.navigateTo({
                            url: "/pages/member/supplement/supplement"
                        }); else {
                            if (o.default.setLoginStatus(!1), e.options && !1 === e.options.needLogin) return;
                            o.default.loginRouter();
                        }
                  },
```

# 核心源码
```
var r = require("./cryptojs/cryptojs.js").Crypto, e = 128, t = function(r) {
    for (var t = function(r) {
      for (var e, t, n = [], a = 0; a < r.length; a++) {
            e = r.charCodeAt(a), t = [];
            do {
                t.push(255 & e), e >>= 8;
            } while (e);
            n = n.concat(t.reverse());
      }
      return n;
    }(r), n = new Array(), a = e / 8, s = 0; s < a; s++) t.length > s ? n.push(t) : n.push(0);
    return n;
}, n = function(r, e) {
    return (255 & r) << 24 | (255 & r) << 16 | (255 & r) << 8 | 255 & r;
}, a = {
    en: function(e, n, a) {
      var s = new r.mode.ECB(r.pad.pkcs7), o = r.charenc.UTF8.stringToBytes(e), c = (r.charenc.UTF8.stringToBytes(n),
      r.AES.encrypt(o, t(n), {
            iv: "",
            mode: s,
            asBpytes: !0
      }));
      return a && (c = c.replace(/\+/g, "-").replace(/\//g, "_")), c;
    },
    de: function(e, n, a) {
      a && (e = e.replace(/-/g, "+").replace(/_/g, "/"));
      var s = new r.mode.ECB(r.pad.pkcs7), o = r.util.base64ToBytes(e);
      r.charenc.UTF8.stringToBytes(n);
      return r.AES.decrypt(o, t(n), {
            asBpytes: !0,
            mode: s,
            iv: ""
      });
    },
    md5: function(e) {
      var t = r.MD5(e, {
            asBytes: !0
      });
      if (16 !== t.length) throw new Error("MD5加密结果字节数组错误");
      var a = Math.abs(n(t, 0)), s = Math.abs(n(t, 4)), o = Math.abs(n(t, 8)), c = Math.abs(n(t, 12));
      return a.toString() + s.toString() + o.toString() + c.toString();
    }
};


module.exports = {
    aes: a,
    md5: function(e) {
      var t = r.MD5(e, {
            asBytes: !0
      });
      if (16 !== t.length) throw new Error("MD5加密结果字节数组错误");
      var a = Math.abs(n(t, 0)), s = Math.abs(n(t, 4)), o = Math.abs(n(t, 8)), c = Math.abs(n(t, 12));
      return a.toString() + s.toString() + o.toString() + c.toString();
    }
};


var d = "230101",
   u = "GBEHvhyjW7ReK5Uw8LzS",
   l = true,
   p = "{\"Width\":1125,\"Height\":2436,\"source\":2,\"displayLocation\":0,\"miniversion\":\"3820\"}"
   c = a.en(p, u, l);
   f = [ "cid=".concat(d), "q=".concat(c), "uid=".concat("fa5ee657-658b-4337-8fc8-aaff6b83ef681594088412273") ],
   // g = t.default.data.configure.uid || s.getStore("uid"),
    sign = a.md5(f.sort().join(";") + u);
console.log(c)
console.log(sign)

data = 'znq6Ob4SNy9KAkGnKq34SRVAHT1Yn126aLkgMzrwwMW1DypsOMixRkMbYQI__LNNVHok4dK84qGXX1zFEG7PD6llecC8WmkPUJo4AmBsdiTUcqivS8BRxy4BuPWKSvq9L3gKR0XXYfWe0MPt572Amrkcd3tirK47xY_0djMKU8OGO7HTHeZFkEqx8swU2LyM-R8pstmpVxXIbyi-nrTzpZvqsqBh51I136EJPbuApRSDiJ4sU69Oxh-FQuZ1Zhcit4w9qq5wpP_M73GRVtbUOHJWmLs9g-NxGqwwW-RumvcI1UCy4FTi5cEYOi8K7cnbfYuHK3t4SfCe4QnwYrkZAgurb_UtDGRSbvGs489WVfj72sYRJMahv7GpBIn0cHe7su0j4D5DAPAG7RHGTvMY5Up0LfHOp3HEfACEubj59BxJbkhzWxd49qjCBWUczPQhDbcObMIzdiQTCeuGwO9cKFmTlJUmpLnoDyZqbLPDT9rymZFTTdcLlCf9NjDF1KOmiBEf7dvLq4OLFsg-vSMMC7oCNA56kFNNmc1G7B3g3uwdP3tr8CLyk7gDXH0c9ysJnOODXdKbuO7b7jrKOKKZ9W0AgS1-UIDpAKhjERk5fJmWGZ1AnkcS1gdCZA0Ddg9PSUCxYRoqiM5HvKf2zVBs7CaxgzPKPu2J3_yAEbBc0V54RidIFx6MoWJjfx8A4L6rqSa9NolPLhKSg9Tz9B0ZWN2GH-_d0EllJHZHqvXzqk5huQ40k4N3ZbSTYaTngSP6Sf8kHji7vgStDq0I64TG23dNodUKHT-Vc_sBZusX7T8TK9TwrIL6o90rj1ijaX07S_d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E7g38LAwj76CH8YAbcSLNFha9AFlVwIxmiYkSK9-9-EdKXtlZlUdItyhXSvzd-dtmt-ljFKXr6nSBgk1J33TXvbL8Qf8JvMZ013pPBbhxCauHQFE5gzDF6Gfz0UTZqYG00lODDSwwW3H3I-bpSEO2nIjt2rNM7orIAcEH3IcNMdVJvpKG6Ty5Eb8frTOaeP816tff301GGCIkaD4uYF_49g_Gb4yoCCgQde9mROa-KQqOgxHioA=='

var result = a.de(data,u,l);
console.log(result)
```

# python代码(非完整代码,仅供学习使用)
```
import requests

headers = {
    'Pragma': 'no-cache',
    'Sec-Fetch-Site': 'cross-site',
    'Origin': 'http://127.0.0.1:64433',
    'Accept-Encoding': 'gzip, deflate, br',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 wechatdevtools/1.02.1910120 MicroMessenger/7.0.4 Language/zh_CN webview/',
    'content-type': 'application/x-www-form-urlencoded',
    'Accept': '*/*',
    'Cache-Control': 'no-cache',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://servicewechat.com/wx320c13777fb3443d/devtools/page-frame.html',
    'Sec-Fetch-User': '?F',
    'Connection': 'keep-alive',
}

data = {
'cid': '230101',
'q': 'sY9NqpIBOPCmlF8Xc7Xd5oYDqzSQ3aUezMpgSp4RuYRaAHJ_Nu67z-UFU7M4Q891QooDqZx-s_CGYrW0ITN4o5TU2yMyW_MXwykUqD3iNgg=',
'sign': '111847979720550523108734027961242945837',
'uid': 'xxx'
}

response = requests.post('https://capi.xxx.com/resource/core/v1/product/list', headers=headers, data=data)

```

# 感谢阅读

cnfengjie 发表于 2020-7-7 11:47

咖啡小程序是什么东西?

表骗我2z 发表于 2020-7-7 11:48

虽然看不懂但是学习了

帅哥很淡定 发表于 2020-7-7 11:49

不错, 学习了,模仿模仿

戒心 发表于 2020-7-7 11:51

学习一下 学习一下

heidiansama 发表于 2020-7-7 11:56

某个民族之光吗

cici300 发表于 2020-7-7 11:58

学习{:1_893:}学习{:1_893:}

zdnyp 发表于 2020-7-7 11:58

这有啥好阅读的,开始-结束,编剧都脑补不出来画面

Capitalwell 发表于 2020-7-7 13:12

手机怎么获得小程序的源码导入到电脑上的呢,楼主好像没提

jerry828 发表于 2020-7-7 13:50

可以免费喝咖啡?
页: [1] 2
查看完整版本: python 逆向某咖啡小程序接口