1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| package com.nb.demo;
import com.github.unidbg.AndroidEmulator; import com.github.unidbg.Module; import com.github.unidbg.linux.android.AndroidEmulatorBuilder; import com.github.unidbg.linux.android.AndroidResolver; import com.github.unidbg.linux.android.dvm.*; import com.github.unidbg.linux.android.dvm.array.ByteArray; import com.github.unidbg.linux.android.dvm.jni.ProxyDvmObject; import com.github.unidbg.memory.Memory; import okhttp3.*; import okio.Buffer; import okio.BufferedSink; import org.apache.commons.codec.binary.Base64;
import java.io.File; import java.io.IOException; import java.nio.charset.Charset;
public class XHS extends AbstractJni { public static AndroidEmulator emulator; public static Memory memory; public static VM vm; public static Module module; private static Request request;
public XHS() { emulator = AndroidEmulatorBuilder.for32Bit().setProcessName("com.xhs").build();
memory = emulator.getMemory();
memory.setLibraryResolver(new AndroidResolver(23));
vm = emulator.createDalvikVM(new File("apks/xhs/v6.73.0.apk")); vm.setJni(this);
DalvikModule dm = vm.loadLibrary(new File("apks/xhs/libshield.so"), false); dm.callJNI_OnLoad(emulator);
module = dm.getModule();
}
public void initializeNative() { DvmClass cls = vm.resolveClass("com/xingin/shield/http/XhsHttpInterceptor"); String method = "initializeNative()V"; cls.callStaticJniMethodObject(emulator, method); }
public long initialize(String str) { DvmClass cls = vm.resolveClass("com/xingin/shield/http/XhsHttpInterceptor"); String method = "initialize(Ljava/lang/String;)J"; long cPtr = cls.callStaticJniMethodLong(emulator, method, new StringObject(vm, str));
return cPtr; }
public void intercept(long cPtr) { DvmClass cls = vm.resolveClass("com/xingin/shield/http/XhsHttpInterceptor"); String method = "intercept(Lokhttp3/Interceptor$Chain;J)Lokhttp3/Response;"; cls.callStaticJniMethodLong( emulator, method, vm.resolveClass("okhttp3/Interceptor$Chain").newObject(null), cPtr ); }
public static void main(String[] args) { String method = args[0]; String url = args[1]; String commonParams = args[2]; String content = args[3];
XHS xhs = new XHS(); xhs.initializeNative(); long l = xhs.initialize("main"); if (method.equalsIgnoreCase("post")) { MediaType TEXT = MediaType.parse("text/plan;charset=utf-8"); RequestBody body = RequestBody.create(TEXT, content); request = new Request.Builder() .url(url) .addHeader("xy-common-params", commonParams) .addHeader("content-type", "application/x-www-form-urlencoded") .method("post", body) .build(); } else { request = new Request.Builder() .url(url) .addHeader("xy-common-params", commonParams) .build(); }
xhs.intercept(l); }
@Override public DvmObject<?> callStaticObjectMethodV(BaseVM vm, DvmClass dvmClass, String signature, VaList vaList) { if (signature.equals("java/nio/charset/Charset->defaultCharset()Ljava/nio/charset/Charset;")) { Charset charset = Charset.defaultCharset(); return ProxyDvmObject.createObject(vm, charset); }
if (signature.equals("com/xingin/shield/http/Base64Helper->decode(Ljava/lang/String;)[B")) { String input = (String) vaList.getObjectArg(0).getValue(); byte[] result = Base64.decodeBase64(input); return new ByteArray(vm, result); } return super.callStaticObjectMethodV(vm, dvmClass, signature, vaList); }
@Override public DvmObject<?> getStaticObjectField(BaseVM vm, DvmClass dvmClass, String signature) { if (signature.equals("com/xingin/shield/http/ContextHolder->sDeviceId:Ljava/lang/String;")) { return ProxyDvmObject.createObject(vm, "c90832a6-bfb9-3e46-a3dd-f5a939a4bdd2");
}
return super.getStaticObjectField(vm, dvmClass, signature); }
@Override public int getStaticIntField(BaseVM vm, DvmClass dvmClass, String signature) { if (signature.equals("com/xingin/shield/http/ContextHolder->sAppId:I")) { return -319115519; } return super.getStaticIntField(vm, dvmClass, signature); }
@Override public boolean getStaticBooleanField(BaseVM vm, DvmClass dvmClass, String signature) { if (signature.equals("com/xingin/shield/http/ContextHolder->sExperiment:Z")) { return true; } return super.getStaticBooleanField(vm, dvmClass, signature); }
@Override public DvmObject<?> callObjectMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) { if (signature.equals("android/content/Context->getSharedPreferences(Ljava/lang/String;I)Landroid/content/SharedPreferences;")) { String xmlName = (String) vaList.getObjectArg(0).getValue();
return vm.resolveClass("android/content/SharedPreferences").newObject(null); }
if (signature.equals("android/content/SharedPreferences->getString(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")) { String key = (String) vaList.getObjectArg(0).getValue(); String defaultValue = (String) vaList.getObjectArg(1).getValue();
if (key.equals("main")) { return new StringObject(vm, defaultValue); } if (key.equals("main_hmac")) { return new StringObject(vm, "aUIfjHpYObXfXBYfXvjCQwwUbuORShG8BR/LCeecL8fwui6ZXoqVZFLMNx9nO2x/fn8Izev0xOINhYGMMMAvCVw5bkgPk7sqavtlPJYDW9O3IEmqHktC4JUzOjRgnjSs"); } }
if (signature.equals("okhttp3/Interceptor$Chain->request()Lokhttp3/Request;")) { return vm.resolveClass("okhttp3/Request").newObject(request); } if (signature.equals("okhttp3/Request->url()Lokhttp3/HttpUrl;")) { Request request = (Request) dvmObject.getValue(); return vm.resolveClass("okhttp3/HttpUrl").newObject(request.url()); } if (signature.equals("okhttp3/HttpUrl->encodedPath()Ljava/lang/String;")) { HttpUrl httpUrl = (HttpUrl) dvmObject.getValue(); return new StringObject(vm, httpUrl.encodedPath()); }
if (signature.equals("okhttp3/HttpUrl->encodedQuery()Ljava/lang/String;")) { HttpUrl httpUrl = (HttpUrl) dvmObject.getValue(); if (httpUrl.encodedQuery() != null) { return new StringObject(vm, httpUrl.encodedQuery()); } return new StringObject(vm, ""); }
if (signature.equals("okhttp3/Request->body()Lokhttp3/RequestBody;")) { Request request = (Request) dvmObject.getValue(); return vm.resolveClass("okhttp3/RequestBody").newObject(request.body()); } if (signature.equals("okhttp3/Request->headers()Lokhttp3/Headers;")) { Request request = (Request) dvmObject.getValue(); return vm.resolveClass("okhttp3/Headers").newObject(request.headers()); } if (signature.equals("okio/Buffer->writeString(Ljava/lang/String;Ljava/nio/charset/Charset;)Lokio/Buffer;")) { Buffer buffer = (Buffer) dvmObject.getValue(); buffer.writeString(vaList.getObjectArg(0).getValue().toString(), (Charset) vaList.getObjectArg(1).getValue()); return dvmObject; }
if (signature.equals("okhttp3/Headers->name(I)Ljava/lang/String;")) { Headers headers = (Headers) dvmObject.getValue(); return new StringObject(vm, headers.name(vaList.getIntArg(0))); } if (signature.equals("okhttp3/Headers->value(I)Ljava/lang/String;")) { Headers headers = (Headers) dvmObject.getValue(); return new StringObject(vm, headers.value(vaList.getIntArg(0))); }
if (signature.equals("okio/Buffer->clone()Lokio/Buffer;")) { Buffer buffer = (Buffer) dvmObject.getValue(); return vm.resolveClass("okio/Buffer").newObject(buffer.clone()); }
if (signature.equals("okhttp3/Request->newBuilder()Lokhttp3/Request$Builder;")) { Request request = (Request) dvmObject.getValue(); return vm.resolveClass("okhttp3/Request$Builder").newObject(request.newBuilder()); } if (signature.equals("okhttp3/Request$Builder->header(Ljava/lang/String;Ljava/lang/String;)Lokhttp3/Request$Builder;")) { Request.Builder builder = (Request.Builder) dvmObject.getValue(); builder.header(vaList.getObjectArg(0).getValue().toString(), vaList.getObjectArg(1).getValue().toString()); if ("shield".equals(vaList.getObjectArg(0).getValue().toString())) { String shield = vaList.getObjectArg(1).getValue().toString(); System.out.println("shield=" + shield); } return dvmObject; } if (signature.equals("okhttp3/Request$Builder->build()Lokhttp3/Request;")) { Request.Builder builder = (Request.Builder) dvmObject.getValue(); Request request = builder.build(); return vm.resolveClass("okhttp3/Request").newObject(request); } if (signature.equals("okhttp3/Interceptor$Chain->proceed(Lokhttp3/Request;)Lokhttp3/Response;")) { return vm.resolveClass("okhttp3/Response").newObject(null); } return super.callObjectMethodV(vm, dvmObject, signature, vaList); }
@Override public DvmObject<?> newObjectV(BaseVM vm, DvmClass dvmClass, String signature, VaList vaList) { if (signature.equals("okio/Buffer-><init>()V")) { return dvmClass.newObject(new Buffer()); } return super.newObjectV(vm, dvmClass, signature, vaList); }
@Override public int callIntMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) { if (signature.equals("okhttp3/Headers->size()I")) { Headers headers = (Headers) dvmObject.getValue(); return headers.size(); } if (signature.equals("okio/Buffer->read([B)I")) { Buffer buffer = (Buffer) dvmObject.getValue(); return buffer.read((byte[]) vaList.getObjectArg(0).getValue()); } if (signature.equals("okhttp3/Response->code()I")) { return 200; } return super.callIntMethodV(vm, dvmObject, signature, vaList); }
@Override public void callVoidMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) { if (signature.equals("okhttp3/RequestBody->writeTo(Lokio/BufferedSink;)V")) { BufferedSink bufferedSink = (BufferedSink) vaList.getObjectArg(0).getValue(); RequestBody requestBody = (RequestBody) dvmObject.getValue(); if (requestBody != null) { try { requestBody.writeTo(bufferedSink); } catch (IOException e) {
} } return; }
super.callVoidMethodV(vm, dvmObject, signature, vaList); } }
|