【原创干货】微信小游戏海盗来了源码分析 (JAVA 辅助)

本贴最后更新于 2602 天前,其中的信息可能已经时移俗易

最近很流行的一款微信小游戏《海盗来了》,用来打发时间还不错,就是建岛和转盘太慢了,于是用 Fiddler 抓了下包,分析了下请求报文,发现所有的请求都需要 sign 签名,尝试了几次都得不到签名值,于是搞了个安卓模拟器,把小游戏的源码拷出来分析了下。

《海盗来了》小游戏的 game.js 源码

《海盗来了》game.js 格式化后

《海盗来了》未格式化的 wxapkg 源码

分析源码发现,请求的签名算法就是普通的字典排序,拼成 URL 后把&符号去掉,再进行 MD5 签名!
例如:sign=md5(uid=666t=666secret=666)

试了下,还是可以用的,但请求的频率不宜过高,我的一个号就是这样被封了,所以。。。

运行结果:

param >> bet=1&isWxGame=true&secret=2ca123ee4a764293af00b139781c291e&t=1525228492778&uid=199471083

sign >> 9d1506d57913290ac439dc134990bde4

转盘 >> {"data":{"money":1337034,"maxEnergy":50,"energy":0,"recoverEnergy":6,"timeToRecover":3135,"shields":2,"wantedCount":0,"ShipwreckCount":0,"cookieCount":0,"potionCount":0,"hatchCount":0,"hornCount":0,"miniShieldCount":0,"monthCardExpired":0,"gotNewbieGift":false,"gotOccasionalGift":true,"gotDailyShop":true,"allInOnePiece":0,"killTitanCannonBall":30,"summonStone":1,"puffer":0,"lolly":0,"guildMedal":0,"doubleMoneyCard":0,"stealIslands":null,"attackTarget":null,"revengeList":null,"rollerItem":{"index":1,"type":0,"value":48000},"betCount":1,"shareCoinFactor":0},"errcode":0,"errmsg":""}
length >> 586

Talk is cheap, show you the code :

解释一下,这里用到了一个 RelynSpider 类,是我自己的爬虫类,其实就是 POST 请求,用 HttpClient 就可以实现,POST 的时候,记得用微信的 User-Agent,相关的方法我都贴在最后面了。

package com.relyn; import java.util.HashMap; import java.util.Map; public class RelynPirate { private static RelynSpider relynSpider = new RelynSpider(); public static void start(String userId, String island) { String url = "https://pirate-api.hortor002.com/game/entry/wxgame"; Map<String, Object> map = new HashMap<String, Object>(); Map<String, String> dataMap = new HashMap<String, String>(); String t = String.valueOf(System.currentTimeMillis()); String resp = ""; String sign = ""; String param = ""; String uid = ""; String secret = java.util.UUID.randomUUID().toString().replace("-", ""); System.out.println("secret >> " + secret); // 基础登录 url = "https://pirate-api.hortor002.com/game/basic/login"; dataMap = new HashMap<String, String>(); dataMap.put("userId", userId); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("userId", userId); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("基础登录 >> " + resp); resp = resp.substring(resp.indexOf("uid\":") + 5); uid = resp.substring(0, resp.indexOf(",")); System.out.println("UID >> " + uid); // 领取并赠送能量 url = "https://pirate-api.hortor002.com/game/friend/donate"; t = String.valueOf(System.currentTimeMillis()); dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("fid", "0"); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("fid", "0"); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("领取并赠送能量 >> " + resp); // 金矿 url = "https://pirate-api.hortor002.com/game/island/collect"; t = String.valueOf(System.currentTimeMillis()); dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("金矿 >> " + resp); // 转盘 int length = 100; while (length > 34) { t = String.valueOf(System.currentTimeMillis()); url = "https://pirate-api.hortor002.com/game/roller/roll"; dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("bet", "1"); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("bet", "1"); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("转盘 >> " + resp); length = resp.length(); System.out.println("length >> " + length); if (length > 600) { if (resp.indexOf("stealIslands\":null") != -1) { resp = resp.substring(resp.indexOf("uid\":") + 5); String puid = resp.substring(0, resp.indexOf(",")); // 攻击 t = String.valueOf(System.currentTimeMillis()); System.out.println("[*] 攻击"); url = "https://pirate-api.hortor002.com/game/pvp/attack"; dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("puid", puid); dataMap.put("building", "3"); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("puid", puid); map.put("building", "3"); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("攻击 >> " + resp); } else { // 盗窃 t = String.valueOf(System.currentTimeMillis()); System.out.println("[*] 盗窃"); url = "https://pirate-api.hortor002.com/game/pvp/steal"; dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("idx", "1"); dataMap.put("isWxGame", "true"); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("idx", "1"); map.put("isWxGame", "true"); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("盗窃 >> " + resp); } } } // 许愿瓶 // url = "https://pirate-api.hortor002.com/game/annual/open-lucky-box"; // dataMap = new HashMap<String, String>(); // dataMap.put("uid", uid); // dataMap.put("useFree", "false"); // dataMap.put("isWxGame", "true"); // dataMap.put("t", t); // dataMap.put("secret", secret); // param = formatUrlMap(dataMap, false, false); // System.out.println("param >> " + param); // param = param.replaceAll("&", ""); //把&去掉 // sign = md5(param); // System.out.println("sign >> " + sign); // map = new HashMap<String, Object>(); // map.put("uid", uid); // map.put("useFree", "false"); // map.put("isWxGame", "true"); // map.put("t", t); // map.put("secret", secret); // map.put("sign", sign); // resp = relynSpider.postWechat(url, map); // System.out.println("HTML >> " + resp); // 建造 for (int building = 0; building < 5; building++) { for (int level = 1; level < 6; level++) { t = String.valueOf(System.currentTimeMillis()); url = "https://pirate-api.hortor002.com/game/island/build"; dataMap = new HashMap<String, String>(); dataMap.put("uid", uid); dataMap.put("island", island); dataMap.put("building", String.valueOf(building)); dataMap.put("level", String.valueOf(level)); dataMap.put("t", t); dataMap.put("secret", secret); param = RelynSpider.formatUrlMap(dataMap, false, false); System.out.println("param >> " + param); param = param.replaceAll("&", ""); // 把&去掉 sign = RelynSpider.md5(param); System.out.println("sign >> " + sign); map = new HashMap<String, Object>(); map.put("uid", uid); map.put("island", island); map.put("building", building); map.put("level", level); map.put("t", t); map.put("secret", secret); map.put("sign", sign); resp = relynSpider.postWechat(url, map); System.out.println("建造 >> " + resp); } } } public static void main(String[] args) { // TODO Auto-generated method stub userId = ""; island = ""; // 当前建造第几座岛屿,第一座是0,依次类推 start(userId, island); } }

RelynSpider 类中的 postWechat 方法:

/** * 微信POST请求 * @param url * @return */ public String postWechat(String url, Map<String, Object> map) { String respString = ""; try { HCB hcb = HCB.custom() // .proxy(proxyArray[0], proxyPort) //代理 .timeout(30000) // 超时 .pool(100, 10) // 启用连接池,每个路由最大创建10个链接,总连接数限制为100个 .sslpv(SSLProtocolVersion.SSLv3) // 设置ssl版本号,默认SSLv3,也可以调用sslpv("TLSv1.2") .ssl() // https,支持自定义ssl证书路径和密码,ssl(String keyStorePath, // String keyStorepass) .retry(5); // 重试5次 HttpConfig config = HttpConfig.custom() //.headers(headers) //设置headers,不需要时则无需设置 .url(url) //设置请求的url .map(map) //设置请求参数,没有则无需设置 //.encoding("utf-8")//设置请求和返回编码,默认就是Charset.defaultCharset() .client(hcb.setUserAgent("MicroMessenger/6.6.6.1300(0x26060636) NetType/WIFI Language/zh_CN").build()) //如果只是简单使用,无需设置,会自动获取默认的一个client对象 //.inenc("utf-8") //设置请求编码,如果请求返回一直,不需要再单独设置 //.inenc("utf-8") //设置返回编码,如果请求返回一直,不需要再单独设置 //.json("json字符串") //json方式请求的话,就不用设置map方法,当然二者可以共用。 //.context(httpCookies.getContext()) //设置cookie,用于完成携带cookie的操作 //.out(new FileOutputStream("保存地址")) //下载的话,设置这个方法,否则不要设置 //.files(new String[]{"d:/1.txt","d:/2.txt"}) //上传的话,传递文件路径,一般还需map配置,设置服务器保存路径 ; respString = HttpClientUtil.post(config); } catch (HttpProcessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return respString; }

RelynSpider 类中的 formatUrlMap 方法:

/** * * 方法用途: 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序),并且生成url参数串<br> * 实现步骤: <br> * * @param paraMap * 要排序的Map对象 * @param urlEncode * 是否需要URLENCODE * @param keyToLower * 是否需要将Key转换为全小写 true:key 转化成小写,false:不转化 * @return */ public static String formatUrlMap(Map<String, String> paraMap, boolean urlEncode, boolean keyToLower) { String buff = ""; try { List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(paraMap.entrySet()); // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序) Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return (o1.getKey()).toString().compareTo(o2.getKey()); } }); // 构造URL 键值对的格式 StringBuilder buf = new StringBuilder(); for (Map.Entry<String, String> item : infoIds) { if (StringUtils.isNotBlank(item.getKey())) { String key = item.getKey(); String val = item.getValue(); if (urlEncode) { val = URLEncoder.encode(val, "utf-8"); } if (keyToLower) { buf.append(key.toLowerCase() + "=" + val); } else { buf.append(key + "=" + val); } buf.append("&"); } } buff = buf.toString(); if (buff.isEmpty() == false) { buff = buff.substring(0, buff.length() - 1); } } catch (Exception e) { return null; } return buff; }

RelynSpider 类中的 md5 方法:

/** * md5加密方法 * @param text * @return */ public static String md5(String text) { String result=""; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(text.getBytes("UTF-8")); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } result = buf.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }
打赏 1 积分后可见
1 积分 • 52 打赏
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3201 引用 • 8216 回帖
  • Fiddler
    5 引用 • 26 回帖
  • 微信

    腾讯公司 2011 年 1 月 21 日推出的一款手机通讯软件。用户可以通过摇一摇、搜索号码、扫描二维码等添加好友和关注公众平台,同时可以将自己看到的精彩内容分享到微信朋友圈。

    133 引用 • 796 回帖
  • 游戏

    沉迷游戏伤身,强撸灰飞烟灭。

    185 引用 • 825 回帖 • 2 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
请输入回帖内容 ...
relyn
永远相信美好的事情即将发生 泉州

推荐标签 标签

  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    10 引用 • 15 回帖 • 9 关注
  • Shell

    Shell 脚本与 Windows/Dos 下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比 Windows 下的批处理更强大,比用其他编程程序编辑的程序效率更高,因为它使用了 Linux/Unix 下的命令。

    125 引用 • 74 回帖 • 3 关注
  • 尊园地产

    昆明尊园房地产经纪有限公司,即:Kunming Zunyuan Property Agency Company Limited(简称“尊园地产”)于 2007 年 6 月开始筹备,2007 年 8 月 18 日正式成立,注册资本 200 万元,公司性质为股份经纪有限公司,主营业务为:代租、代售、代办产权过户、办理银行按揭、担保、抵押、评估等。

    1 引用 • 22 回帖 • 796 关注
  • Office

    Office 现已更名为 Microsoft 365. Microsoft 365 将高级 Office 应用(如 Word、Excel 和 PowerPoint)与 1 TB 的 OneDrive 云存储空间、高级安全性等结合在一起,可帮助你在任何设备上完成操作。

    5 引用 • 34 回帖 • 1 关注
  • HBase

    HBase 是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的 Google 论文 “Bigtable:一个结构化数据的分布式存储系统”。就像 Bigtable 利用了 Google 文件系统所提供的分布式数据存储一样,HBase 在 Hadoop 之上提供了类似于 Bigtable 的能力。

    17 引用 • 6 回帖 • 65 关注
  • 黑曜石

    黑曜石是一款强大的知识库工具,支持本地 Markdown 文件编辑,支持双向链接和关系图。

    A second brain, for you, forever.

    24 引用 • 242 回帖 • 1 关注
  • Gzip

    gzip (GNU zip)是 GNU 自由软件的文件压缩程序。我们在 Linux 中经常会用到后缀为 .gz 的文件,它们就是 Gzip 格式的。现今已经成为互联网上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

    9 引用 • 12 回帖 • 175 关注
  • React

    React 是 Facebook 开源的一个用于构建 UI 的 JavaScript 库。

    192 引用 • 291 回帖 • 371 关注
  • flomo

    flomo 是新一代 「卡片笔记」 ,专注在碎片化时代,促进你的记录,帮你积累更多知识资产。

    6 引用 • 143 回帖 • 1 关注
  • HTML

    HTML5 是 HTML 下一个的主要修订版本,现在仍处于发展阶段。广义论及 HTML5 时,实际指的是包括 HTML、CSS 和 JavaScript 在内的一套技术组合。

    108 引用 • 295 回帖
  • VirtualBox

    VirtualBox 是一款开源虚拟机软件,最早由德国 Innotek 公司开发,由 Sun Microsystems 公司出品的软件,使用 Qt 编写,在 Sun 被 Oracle 收购后正式更名成 Oracle VM VirtualBox。

    10 引用 • 2 回帖 • 17 关注
  • 安全

    安全永远都不是一个小问题。

    199 引用 • 818 回帖
  • 服务

    提供一个服务绝不仅仅是简单的把硬件和软件累加在一起,它包括了服务的可靠性、服务的标准化、以及对服务的监控、维护、技术支持等。

    41 引用 • 24 回帖 • 1 关注
  • Love2D

    Love2D 是一个开源的, 跨平台的 2D 游戏引擎。使用纯 Lua 脚本来进行游戏开发。目前支持的平台有 Windows, Mac OS X, Linux, Android 和 iOS。

    14 引用 • 53 回帖 • 558 关注
  • AngularJS

    AngularJS 诞生于 2009 年,由 Misko Hevery 等人创建,后为 Google 所收购。是一款优秀的前端 JS 框架,已经被用于 Google 的多款产品当中。AngularJS 有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入等。2.0 版本后已经改名为 Angular。

    12 引用 • 50 回帖 • 512 关注
  • WordPress

    WordPress 是一个使用 PHP 语言开发的博客平台,用户可以在支持 PHP 和 MySQL 数据库的服务器上架设自己的博客。也可以把 WordPress 当作一个内容管理系统(CMS)来使用。WordPress 是一个免费的开源项目,在 GNU 通用公共许可证(GPLv2)下授权发布。

    45 引用 • 114 回帖 • 172 关注
  • 友情链接

    确认过眼神后的灵魂连接,站在链在!

    24 引用 • 373 回帖
  • Notion

    Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.

    10 引用 • 77 回帖
  • 创业

    你比 99% 的人都优秀么?

    82 引用 • 1395 回帖 • 1 关注
  • CongSec

    本标签主要用于分享网络空间安全专业的学习笔记

    1 引用 • 1 回帖 • 37 关注
  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    693 引用 • 537 回帖 • 1 关注
  • 酷鸟浏览器

    安全 · 稳定 · 快速
    为跨境从业人员提供专业的跨境浏览器

    3 引用 • 59 回帖 • 50 关注
  • JetBrains

    JetBrains 是一家捷克的软件开发公司,该公司位于捷克的布拉格,并在俄国的圣彼得堡及美国麻州波士顿都设有办公室,该公司最为人所熟知的产品是 Java 编程语言开发撰写时所用的集成开发环境:IntelliJ IDEA

    18 引用 • 54 回帖 • 1 关注
  • LaTeX

    LaTeX(音译“拉泰赫”)是一种基于 ΤΕΧ 的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在 20 世纪 80 年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由 TeX 所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。

    12 引用 • 59 回帖 • 5 关注
  • DevOps

    DevOps(Development 和 Operations 的组合词)是一组过程、方法与系统的统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通、协作与整合。

    59 引用 • 25 回帖 • 1 关注
  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    345 引用 • 753 回帖 • 2 关注
  • Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的操作系统上。容器完全使用沙箱机制,几乎没有性能开销,可以很容易地在机器和数据中心中运行。

    496 引用 • 934 回帖 • 1 关注