本次分析的是ios 7.2版本的,感谢大佬分享的老版本shield代码,原文地址:看雪-安全社区|安全招聘|kanxue.com[原创]一个朋友的真实咕视-顺便开源下xhshttps://bbs.kanxue.com/thread-267748.htm大佬分享的版本生成的shield是32位加密字符串,而7.2版本的shield位数是134位;
134位的shield由:ios_flag + ios_app_hex + ios_appversion_hex + device_hex + 老版本生成的32位魔改md5组成,影响因素有 path、params、xy_common_params、xy_plantform、data、xy_direction
核心代码
private static byte[] sub_B2A73E2C(byte[] s, String uuid) {
List<Integer> r3 = sub_B2A732D0(uuid);
byte[] result = sub_B2A73954(s, r3);
byte[] bytes = {0x31, 0x01, 0x32, 0x34, 0x04, 0x02, 0x08, 0x61, 0x66, 0x7A, 0x66, 0x66, 0x07, 0x17, 0x66, 0x39};
for (int i = 0; i < s.length; i++) {
if (i != 0 && i % 16 == 0) {
result = sub_B2A73954(Arrays.copyOfRange(s, i, i + 16), r3);
}
int temp = s[i] & 0xFF;
s[i] = (byte) (result[i % bytes.length] ^ bytes[i % bytes.length]);
bytes[i % bytes.length] = (byte) temp;
}
return Arrays.copyOfRange(s, 16, s.length - (s[s.length - 1] & 0xFF));
}
private static List<Integer> sub_B2A732D0(String uuid) {
List<Integer> v15 = sub_B29EEF14(uuid);
for (int i = 0; i < 10 / 2; i++) {
int r = 4 * (10 - i);
int p = 4 * i;
int temp = v15.get(p);
v15.set(p, v15.get(r));
v15.set(r, temp);
temp = v15.get(p + 1);
v15.set(p + 1, v15.get(r + 1));
v15.set(r + 1, temp);
temp = v15.get(p + 2);
v15.set(p + 2, v15.get(r + 2));
v15.set(r + 2, temp);
temp = v15.get(p + 3);
v15.set(p + 3, v15.get(r + 3));
v15.set(r + 3, temp);
}
for (int i = 1; i < 10; i++) {
int p = i * 4;
v15.set(p, sub_B2A732D0_sub(v15.get(p)));
v15.set(p + 1, sub_B2A732D0_sub(v15.get(p + 1)));
v15.set(p + 2, sub_B2A732D0_sub(v15.get(p + 2)));
v15.set(p + 3, sub_B2A732D0_sub(v15.get(p + 3)));
}
return v15;
}
private static List<Integer> sub_B29EEF14(String uuid) {
byte[] key = uuid.substring(0, 16).getBytes();
List<Integer> v9 = new LinkedList<>(Arrays.asList(0xF1892131, 0xFF001123, 0xF1001356, 0xF1234890));
for (int i = 0; i < 4; i++) {
v9.set(i, v9.get(i) ^ read(key, i * 4));
}
for (int i = 0; i < 10; i++) {
int v3 = v9.get(v9.size() - 1);
v9.add(v9.get(i * 4) ^ dword_B2A48060[(v3 >> 16) & 0xFF] & 0xFF000000 ^ dword_B2A48460[v3 >> 8 & 0xFF] & 0xFF0000
^ dword_B2A48860[v3 & 0xFF] & 0xFF00 ^ byte_B2A48C60[4 * (v3 >> 24 & 0xFF)] ^ dword_B2A49060[i]);
v9.add(v9.get(v9.size() - 4) ^ v9.get(v9.size() - 1));
v9.add(v9.get(v9.size() - 4) ^ v9.get(v9.size() - 1));
v9.add(v9.get(v9.size() - 4) ^ v9.get(v9.size() - 1));
}
return v9;
}
public static byte[] xyHmacMd5Bytes(byte[] key, byte[] data) {
int length = 64;
byte[] keyArr = new byte[length];
System.arraycopy(key, 0, keyArr, 0, key.length);
byte[] kIpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kIpadXorResult[i] = (byte) (keyArr[i] ^ 0x36);
}
xyMD5Util md5_kIpadXor = new xyMD5Util();
md5_kIpadXor.md5Update(kIpadXorResult, kIpadXorResult.length);
md5_kIpadXor.md5Update(data, data.length);
byte[] bytes1 = md5_kIpadXor.md5Final();
byte[] kOpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kOpadXorResult[i] = (byte) (keyArr[i] ^ 0x5C);
}
xyMD5Util md5_kOpadXor = new xyMD5Util();
md5_kOpadXor.md5Update(kOpadXorResult, kOpadXorResult.length);
return md5_kOpadXor.md5Final(bytes1);
}
xyMD5Util类
public class xyMD5Util {
static final byte[] PADDING = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0};
private final long[] state = new long[4];
private final long[] count = new long[2];
private final byte[] buffer = new byte[64];
private final byte[] digest = new byte[16];
public xyMD5Util() {
md5Init();
}
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
state[0] = 0x10325476L;
state[1] = 0x98badcfeL;
state[2] = 0xefcdab89L;
state[3] = 0x67452301L;
}
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
private static long ROR(long value, long offset) {
value &= 0xFFFFFFFFL;
long temp = value >> offset;
long mas = 0;
for (int i = 0; i < offset; i++) {
mas += Math.pow(2, i);
}
return temp | (value & mas) << 32 - offset;
}
private static long BIC(long value, long mas) {
value &= 0xFFFFFFFFL;
return value & ~mas;
}
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ROR(a, s);
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ROR(a, s);
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ROR(a, s);
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ROR(a, s);
a += b;
return a;
}
public void md5Update(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
if (inputLen >= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
}
index = 0;
} else {
i = 0;
}
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
}
public byte[] md5Final() {
return md5Final(null);
}
public byte[] md5Final(byte[] data) {
byte[] bits = new byte[8];
int index, padLen;
Encode(bits, count, 8);
if (data != null) {
byte[] temp = new byte[64];
System.arraycopy(data, 0, temp, 0, data.length);
temp[data.length] = (byte) 0x80;
temp[56] = (byte) 0x80;
temp[57] = 2;
md5Update(temp, 64);
} else {
/* Pad out to 56 mod 64.*/
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
md5Update(PADDING, padLen);
}
/* Append length (before padding) */
md5Update(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
return digest;
}
private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos, int len) {
System.arraycopy(input, inpos, output, outpos, len);
}
private void md5Transform(byte[] block) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], 26, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], 19, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], 15, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], 11, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], 25, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], 20, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], 15, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], 12, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], 25, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], 20, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], 16, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], 10, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], 25, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], 19, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], 15, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], 10, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], 27, BIC(0xf61e2562L, 0xFF00FF)); /* 17 */
d = GG(d, a, b, c, x[6], 23, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], 18, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], 12, 0xe9b6c7aaL & 0xFF0011FFL); /* 20 */
a = GG(a, b, c, d, x[5], 27, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], 23, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], 18, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], 12, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], 27, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], 23, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], 18, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], 12, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], 27, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], 23, 0xfcefa3f8L & 0xFF110011L); /* 30 */
c = GG(c, d, a, b, x[7], 18, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], 12, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], 28, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], 21, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], 16, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], 9, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], 28, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], 21, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60L); /* 39 */
a = HH(a, b, c, d, x[13], 28, 0x289b7ec6L); /* 41 */
b = HH(b, c, d, a, x[10], 9, 0xbebfbc70L); /* 40 */
c = HH(c, d, a, b, x[3], 16, 0xd4ef3085L); /* 43 */
d = HH(d, a, b, c, x[0], 21, 0xeaa127faL); /* 42 */
b = HH(b, c, d, a, x[6], 9, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], 28, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], 21, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], 9, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], 26, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], 22, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], 17, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], 11, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], 26, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], 22, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], 17, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], 11, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], 26, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], 22, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], 17, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], 11, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], 26, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], 22, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], 17, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], 11, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
}
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}
}
设备参数
byte[] a = Base64.getDecoder().decode("服务端下发的xy_ter_str");
xy_ter_str长度为128位;
iOS只有在全新状态下抓包才能获取到这个参数值;安卓存储到s.xml文件中 具体可以搜索下”main_hmac“
String key = "设备device_id";
设备did可以通过抓包获取
参数分析
String path = "/api/sns/v6/homefeed";
String params = "oid=homefeed_recommendapp_id=ECFAAF02&build=8513104&channel=AppStore&deviceId=6F18771D-D60F-436E-B092-9985E5F9EB04&device_fingerprint=20231109091433862817566a1621c685e9d1e88dbec56701fc5e009a96c60f&device_model=phone&fid=1724942160-0-0-9d02ebfce5a1782bdf8f776dae4f9af2&gid=7c760978137c55e1bedee7d31ba81cf66e6749f84735963b77d887f5&identifier_flag=0&is_mac=0&launch_id=746635677&overseas_channel=0&platform=iOS&project_id=ECFAAF&sid=session.1725189048259779765670&t=1725440755&teenager=0&tz=Asia/Shanghai&uis=dark&version=8.51.333platform=iOS&version=8.51.3&build=8513104&deviceId=6F18771D-D60F-436E-B092-9985E5F9EB04&bundle=com.xingin.discover"";
String b = path + params;// 去掉?进行拼接
String appid_hex_str = "ecfaaf02";// 安卓为 ecfaaf01
String flag = "02"; // 安卓为 01
String appversion_hex_str = strToHexstr("8513104"); // app版本号 -> 16进制
String deviceid_hex_str = strToHexstr(key);// key -> 16进制
String old_sign = hexDigest(xyHmacMd5Bytes(bytes, b.getBytes()));
String calc_str = flag + appid_hex_str + appversion_hex_str + deviceid_hex_str + old_sign;
byte[] output1 = initBase64Table(fromHex(calc_str));
byte[] output2 = new byte[]{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 83, 0, 0, 0, 83};
// 使用 ByteBuffer 拼接字节数组
ByteBuffer buffer = ByteBuffer.allocate(output2.length + output1.length);
buffer.put(output2);
buffer.put(output1);
byte[] b64EncodeStr = buffer.array();
// 打印结果
String encodedStr = Base64.getEncoder().encodeToString(b64EncodeStr);
System.out.println("shieild: XY" + encodedStr);
结果验证
python调用
params = path + params + xy_common_params + device['xy-direction'] + xy_platform_info
# 调用 jar包
result = subprocess.run(['java', '-jar', 'shield.jar',device['device_id'],device['main_hmac'],params,device['platform'],device['build']], capture_output = True, text = True)
headers['shield'] = result.stdout.strip()
以上就是IOS7.2版本的shield算法的全部。