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

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

最近很流行的一款微信小游戏《海盗来了》,用来打发时间还不错,就是建岛和转盘太慢了,于是用 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 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 2 关注
  • Fiddler
    4 引用 • 26 回帖
  • 微信

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

    129 引用 • 793 回帖
  • 游戏

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

    169 引用 • 799 回帖

相关帖子

欢迎来到这里!

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

注册 关于
请输入回帖内容 ...
  • Hilite

    "errcode":40003,"errmsg":"参数错误",请求地址改了?还是传参改了?

  • 其他回帖
  • shaxiaozhu

    不是很懂,只会用 fiddler 截取发送!

  • guandaxia

    secret 随机生成可以吗?不得登录之后获取吗?

  • relyn
    作者

    不好意思各位,源码包的链接弄错了,现在好了,在打赏区

  • 查看全部回帖
relyn
永远相信美好的事情即将发生 泉州

推荐标签 标签

  • GitHub

    GitHub 于 2008 年上线,目前,除了 Git 代码仓库托管及基本的 Web 管理界面以外,还提供了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱(报表)、代码片段分享(Gist)等功能。正因为这些功能所提供的便利,又经过长期的积累,GitHub 的用户活跃度很高,在开源世界里享有深远的声望,并形成了社交化编程文化(Social Coding)。

    207 引用 • 2031 回帖
  • 数据库

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

    330 引用 • 614 回帖
  • golang

    Go 语言是 Google 推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 Go,是因为过去 10 多年间软件开发的难度令人沮丧。Go 是谷歌 2009 发布的第二款编程语言。

    491 引用 • 1383 回帖 • 374 关注
  • OpenStack

    OpenStack 是一个云操作系统,通过数据中心可控制大型的计算、存储、网络等资源池。所有的管理通过前端界面管理员就可以完成,同样也可以通过 Web 接口让最终用户部署资源。

    10 引用 • 10 关注
  • 尊园地产

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

    1 引用 • 22 回帖 • 682 关注
  • danl
    61 关注
  • SOHO

    为成为自由职业者在家办公而努力吧!

    7 引用 • 55 回帖 • 97 关注
  • JVM

    JVM(Java Virtual Machine)Java 虚拟机是一个微型操作系统,有自己的硬件构架体系,还有相应的指令系统。能够识别 Java 独特的 .class 文件(字节码),能够将这些文件中的信息读取出来,使得 Java 程序只需要生成 Java 虚拟机上的字节码后就能在不同操作系统平台上进行运行。

    180 引用 • 120 回帖 • 1 关注
  • Java

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

    3167 引用 • 8207 回帖 • 2 关注
  • 智能合约

    智能合约(Smart contract)是一种旨在以信息化方式传播、验证或执行合同的计算机协议。智能合约允许在没有第三方的情况下进行可信交易,这些交易可追踪且不可逆转。智能合约概念于 1994 年由 Nick Szabo 首次提出。

    1 引用 • 11 回帖 • 9 关注
  • Electron

    Electron 基于 Chromium 和 Node.js,让你可以使用 HTML、CSS 和 JavaScript 构建应用。它是一个由 GitHub 及众多贡献者组成的活跃社区共同维护的开源项目,兼容 Mac、Windows 和 Linux,它构建的应用可在这三个操作系统上面运行。

    15 引用 • 136 回帖 • 9 关注
  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    116 引用 • 99 回帖 • 266 关注
  • 分享

    有什么新发现就分享给大家吧!

    242 引用 • 1746 回帖 • 1 关注
  • 安装

    你若安好,便是晴天。

    128 引用 • 1184 回帖 • 1 关注
  • 小薇

    小薇是一个用 Java 写的 QQ 聊天机器人 Web 服务,可以用于社群互动。

    由于 Smart QQ 从 2019 年 1 月 1 日起停止服务,所以该项目也已经停止维护了!

    34 引用 • 467 回帖 • 692 关注
  • CAP

    CAP 指的是在一个分布式系统中, Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得。

    11 引用 • 5 回帖 • 563 关注
  • SMTP

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。

    4 引用 • 18 回帖 • 589 关注
  • 深度学习

    深度学习(Deep Learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。

    40 引用 • 40 回帖 • 1 关注
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    18595 引用 • 69207 回帖
  • CodeMirror
    1 引用 • 2 回帖 • 115 关注
  • etcd

    etcd 是一个分布式、高可用的 key-value 数据存储,专门用于在分布式系统中保存关键数据。

    5 引用 • 26 回帖 • 492 关注
  • 互联网

    互联网(Internet),又称网际网络,或音译因特网、英特网。互联网始于 1969 年美国的阿帕网,是网络与网络之间所串连成的庞大网络,这些网络以一组通用的协议相连,形成逻辑上的单一巨大国际网络。

    96 引用 • 330 回帖
  • 小说

    小说是以刻画人物形象为中心,通过完整的故事情节和环境描写来反映社会生活的文学体裁。

    28 引用 • 108 回帖 • 1 关注
  • BAE

    百度应用引擎(Baidu App Engine)提供了 PHP、Java、Python 的执行环境,以及云存储、消息服务、云数据库等全面的云服务。它可以让开发者实现自动地部署和管理应用,并且提供动态扩容和负载均衡的运行环境,让开发者不用考虑高成本的运维工作,只需专注于业务逻辑,大大降低了开发者学习和迁移的成本。

    19 引用 • 75 回帖 • 620 关注
  • 微软

    微软是一家美国跨国科技公司,也是世界 PC 软件开发的先导,由比尔·盖茨与保罗·艾伦创办于 1975 年,公司总部设立在华盛顿州的雷德蒙德(Redmond,邻近西雅图)。以研发、制造、授权和提供广泛的电脑软件服务业务为主。

    8 引用 • 44 回帖
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    35 引用 • 35 回帖
  • Node.js

    Node.js 是一个基于 Chrome JavaScript 运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。Node.js 使用事件驱动, 非阻塞 I/O 模型而得以轻量和高效。

    138 引用 • 268 回帖 • 200 关注