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

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

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

    3168 引用 • 8207 回帖
  • Fiddler
    4 引用 • 26 回帖
  • 微信

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

    129 引用 • 793 回帖 • 1 关注
  • 游戏

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

    169 引用 • 799 回帖

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • SEO

    发布对别人有帮助的原创内容是最好的 SEO 方式。

    35 引用 • 200 回帖 • 24 关注
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 133 关注
  • 导航

    各种网址链接、内容导航。

    37 引用 • 168 回帖
  • Redis

    Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。从 2010 年 3 月 15 日起,Redis 的开发工作由 VMware 主持。从 2013 年 5 月开始,Redis 的开发由 Pivotal 赞助。

    284 引用 • 247 回帖 • 175 关注
  • 新人

    让我们欢迎这对新人。哦,不好意思说错了,让我们欢迎这位新人!
    新手上路,请谨慎驾驶!

    51 引用 • 226 回帖
  • GitBook

    GitBook 使您的团队可以轻松编写和维护高质量的文档。 分享知识,提高团队的工作效率,让用户满意。

    3 引用 • 8 回帖
  • 负能量

    上帝为你关上了一扇门,然后就去睡觉了....努力不一定能成功,但不努力一定很轻松 (° ー °〃)

    85 引用 • 1201 回帖 • 450 关注
  • 资讯

    资讯是用户因为及时地获得它并利用它而能够在相对短的时间内给自己带来价值的信息,资讯有时效性和地域性。

    53 引用 • 85 回帖
  • JRebel

    JRebel 是一款 Java 虚拟机插件,它使得 Java 程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响。

    26 引用 • 78 回帖 • 623 关注
  • Lute

    Lute 是一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。

    25 引用 • 191 回帖 • 21 关注
  • jQuery

    jQuery 是一套跨浏览器的 JavaScript 库,强化 HTML 与 JavaScript 之间的操作。由 John Resig 在 2006 年 1 月的 BarCamp NYC 上释出第一个版本。全球约有 28% 的网站使用 jQuery,是非常受欢迎的 JavaScript 库。

    63 引用 • 134 回帖 • 740 关注
  • Gzip

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

    9 引用 • 12 回帖 • 111 关注
  • Vue.js

    Vue.js(读音 /vju ː/,类似于 view)是一个构建数据驱动的 Web 界面库。Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。

    261 引用 • 662 回帖
  • Mobi.css

    Mobi.css is a lightweight, flexible CSS framework that focus on mobile.

    1 引用 • 6 回帖 • 697 关注
  • WordPress

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

    45 引用 • 113 回帖 • 314 关注
  • wolai

    我来 wolai:不仅仅是未来的云端笔记!

    1 引用 • 11 回帖 • 2 关注
  • 黑曜石

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

    A second brain, for you, forever.

    10 引用 • 85 回帖
  • OkHttp

    OkHttp 是一款 HTTP & HTTP/2 客户端库,专为 Android 和 Java 应用打造。

    16 引用 • 6 回帖 • 53 关注
  • 爬虫

    网络爬虫(Spider、Crawler),是一种按照一定的规则,自动地抓取万维网信息的程序。

    106 引用 • 275 回帖
  • 微软

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

    8 引用 • 44 回帖
  • SpaceVim

    SpaceVim 是一个社区驱动的模块化 vim/neovim 配置集合,以模块的方式组织管理插件以
    及相关配置,为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全,
    语法检查、格式化、调试、REPL 等特性。用户仅需载入相关语言的模块即可得到一个开箱
    即用的 Vim-IDE。

    3 引用 • 31 回帖 • 71 关注
  • 正则表达式

    正则表达式(Regular Expression)使用单个字符串来描述、匹配一系列遵循某个句法规则的字符串。

    31 引用 • 94 回帖
  • PWL

    组织简介

    用爱发电 (Programming With Love) 是一个以开源精神为核心的民间开源爱好者技术组织,“用爱发电”象征开源与贡献精神,加入组织,代表你将遵守组织的“个人开源爱好者”的各项条款。申请加入:用爱发电组织邀请帖
    用爱发电组织官网:https://programmingwithlove.stackoverflow.wiki/

    用爱发电组织的核心驱动力:

    • 遵守开源守则,体现开源&贡献精神:以分享为目的,拒绝非法牟利。
    • 自我保护:使用适当的 License 保护自己的原创作品。
    • 尊重他人:不以各种理由、各种漏洞进行未经允许的抄袭、散播、洩露;以礼相待,尊重所有对社区做出贡献的开发者;通过他人的分享习得知识,要留下足迹,表示感谢。
    • 热爱编程、热爱学习:加入组织,热爱编程是首当其要的。我们欢迎热爱讨论、分享、提问的朋友,也同样欢迎默默成就的朋友。
    • 倾听:正确并恳切对待、处理问题与建议,及时修复开源项目的 Bug ,及时与反馈者沟通。不抬杠、不无视、不辱骂。
    • 平视:不诋毁、轻视、嘲讽其他开发者,主动提出建议、施以帮助,以和谐为本。只要他人肯努力,你也可能会被昔日小看的人所超越,所以请保持谦虚。
    • 乐观且活跃:你的努力决定了你的高度。不要放弃,多年后回头俯瞰,才会发现自己已经成就往日所仰望的水平。积极地将项目开源,帮助他人学习、改进,自己也会获得相应的提升、成就与成就感。
    1 引用 • 487 回帖 • 7 关注
  • NetBeans

    NetBeans 是一个始于 1997 年的 Xelfi 计划,本身是捷克布拉格查理大学的数学及物理学院的学生计划。此计划延伸而成立了一家公司进而发展这个商用版本的 NetBeans IDE,直到 1999 年 Sun 买下此公司。Sun 于次年(2000 年)六月将 NetBeans IDE 开源,直到现在 NetBeans 的社群依然持续增长。

    78 引用 • 102 回帖 • 642 关注
  • iOS

    iOS 是由苹果公司开发的移动操作系统,最早于 2007 年 1 月 9 日的 Macworld 大会上公布这个系统,最初是设计给 iPhone 使用的,后来陆续套用到 iPod touch、iPad 以及 Apple TV 等产品上。iOS 与苹果的 Mac OS X 操作系统一样,属于类 Unix 的商业操作系统。

    84 引用 • 139 回帖
  • webpack

    webpack 是一个用于前端开发的模块加载器和打包工具,它能把各种资源,例如 JS、CSS(less/sass)、图片等都作为模块来使用和处理。

    41 引用 • 130 回帖 • 294 关注
  • 深度学习

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

    40 引用 • 40 回帖