Skip to content

Commit

Permalink
Reworking Mitm
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Diaz committed Sep 29, 2016
1 parent af237a3 commit 090e444
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 28 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@

package com.icecream.snorlax.module;

import java.util.List;

import javax.inject.Singleton;

import android.app.Application;
import android.util.LongSparseArray;

import com.icecream.snorlax.module.feature.mitm.MitmRelay;

import dagger.Module;
import dagger.Provides;
import de.robv.android.xposed.XSharedPreferences;

import static POGOProtos.Networking.Requests.RequestOuterClass.Request;

@Module
final class SnorlaxModule {

Expand Down Expand Up @@ -59,4 +64,10 @@ Application provideAppliction() {
MitmRelay provideMitmRelay() {
return MitmRelay.getInstance();
}

@Provides
@Singleton
LongSparseArray<List<Request>> provideLongSparseArray() {
return new LongSparseArray<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,38 @@
public final class Mitm implements Feature {

private final ClassLoader mClassLoader;
private final MitmInputStreamFactory mMitmInputStreamFactory;
private final MitmOutputStreamFactory mMitmOutputStreamFactory;

private XC_MethodHook.Unhook mUnhookInputStream;
private XC_MethodHook.Unhook mUnhookOutputStream;

@Inject
Mitm(ClassLoader classLoader) {
Mitm(ClassLoader classLoader, MitmInputStreamFactory mitmInputStreamFactory, MitmOutputStreamFactory mitmOutputStreamFactory) {
mClassLoader = classLoader;
mMitmInputStreamFactory = mitmInputStreamFactory;
mMitmOutputStreamFactory = mitmOutputStreamFactory;
}

@Override
public void subscribe() throws Exception {
final Class<?> http = XposedHelpers.findClass(getHttpUrlConnection(), mClassLoader);
if (http == null) {
Log.e("Cannot find HttpsURLConnection class");
Log.e("Cannot find HttpURLConnection implementation class");
return;
}

mUnhookInputStream = XposedHelpers.findAndHookMethod(http, "getInputStream", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
// TODO factory
param.setResult(new MitmInputStream((InputStream) param.getResult()));
param.setResult(mMitmInputStreamFactory.create((InputStream) param.getResult()));
}
});

mUnhookOutputStream = XposedHelpers.findAndHookMethod(http, "getOutputStream", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
// TODO factory
param.setResult(new MitmOutputStream((OutputStream) param.getResult()));
param.setResult(mMitmOutputStreamFactory.create((OutputStream) param.getResult()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
final class MitmInputStream extends InputStream {

private static final int sAverageSize = 4096;

private final MitmProvider mMitmProvider;
private boolean mMitmDone;
private ByteBuffer mByteBuffer;

MitmInputStream(InputStream inputStream) {
MitmInputStream(InputStream inputStream, MitmProvider mitmProvider) {
mMitmProvider = mitmProvider;
mMitmDone = false;

if (inputStream == null) {
Expand Down Expand Up @@ -95,7 +96,7 @@ private void mitmStream() {
if (mMitmDone)
return;

ByteBuffer fromMitm = MitmProvider.processInboundPackage(
ByteBuffer fromMitm = mMitmProvider.processInboundPackage(
mByteBuffer.asReadOnlyBuffer(),
mByteBuffer.hasRemaining()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.icecream.snorlax.module.feature.mitm;

import java.io.InputStream;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
final class MitmInputStreamFactory {

private final MitmProvider mMitmProvider;

@Inject
MitmInputStreamFactory(MitmProvider mitmProvider) {
mMitmProvider = mitmProvider;
}

MitmInputStream create(InputStream inputStream) {
return new MitmInputStream(inputStream, mMitmProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

final class MitmOutputStream extends ByteArrayOutputStream {

private final OutputStream mOutputStream;
private final MitmProvider mMitmProvider;
private OutputStream mOutputStream;

MitmOutputStream(OutputStream outputStream) {
MitmOutputStream(OutputStream outputStream, MitmProvider mitmProvider) {
super(2048);
mOutputStream = outputStream;
mMitmProvider = mitmProvider;
}

@Override
Expand All @@ -43,7 +45,7 @@ public void close() throws IOException {

@SuppressWarnings("unused")
private void mitmStream() {
ByteBuffer fromMitm = MitmProvider.processOutboundPackage(
ByteBuffer fromMitm = mMitmProvider.processOutboundPackage(
ByteBuffer.wrap(buf, 0, count).asReadOnlyBuffer(),
mOutputStream != null
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.icecream.snorlax.module.feature.mitm;

import java.io.OutputStream;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
final class MitmOutputStreamFactory {

private final MitmProvider mMitmProvider;

@Inject
MitmOutputStreamFactory(MitmProvider mitmProvider) {
mMitmProvider = mitmProvider;
}

MitmOutputStream create(OutputStream outputStream) {
return new MitmOutputStream(outputStream, mMitmProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.nio.ByteBuffer;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import android.util.LongSparseArray;

import com.google.protobuf.ByteString;
Expand All @@ -38,15 +41,17 @@
import static POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse;
import static android.R.attr.id;

@Singleton
final class MitmProvider {

static {
sRequests = new LongSparseArray<>();
}
private final LongSparseArray<List<Request>> mRequests;

private static LongSparseArray<List<Request>> sRequests;
@Inject
MitmProvider(LongSparseArray<List<Request>> requests) {
mRequests = requests;
}

static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk) {
ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk) {
if (!connectionOk)
return null;

Expand All @@ -72,14 +77,14 @@ static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk
return null;
}

private static void processOutBuffer(RequestEnvelope envelope) {
sRequests.put(
private void processOutBuffer(RequestEnvelope envelope) {
mRequests.put(
envelope.getRequestId(),
envelope.getRequestsList()
);
}

static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk) {
ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk) {
if (!connectionOk)
return null;

Expand Down Expand Up @@ -109,8 +114,8 @@ static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk)
return null;
}

private static ByteBuffer processInBuffer(ResponseEnvelope envelope) throws InvalidProtocolBufferException {
List<Request> requests = sRequests.get(envelope.getRequestId());
private ByteBuffer processInBuffer(ResponseEnvelope envelope) throws InvalidProtocolBufferException {
List<Request> requests = mRequests.get(envelope.getRequestId());

if (requests == null) {
return null;
Expand All @@ -129,7 +134,7 @@ private static ByteBuffer processInBuffer(ResponseEnvelope envelope) throws Inva
}
}
}
sRequests.remove(id);
mRequests.remove(id);

if (!isDone) {
return null;
Expand All @@ -138,7 +143,7 @@ private static ByteBuffer processInBuffer(ResponseEnvelope envelope) throws Inva
return ByteBuffer.wrap(envelope.toByteArray());
}

private static ByteString processInventoryResponse(GetInventoryResponse response) {
private ByteString processInventoryResponse(GetInventoryResponse response) {
if (!response.getSuccess() || !response.hasInventoryDelta()) {
return null;
}
Expand Down Expand Up @@ -177,8 +182,4 @@ private static ByteString processInventoryResponse(GetInventoryResponse response

return null;
}

private MitmProvider() {
throw new AssertionError("No instances");
}
}

0 comments on commit 090e444

Please sign in to comment.