Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
More work on #17
Browse files Browse the repository at this point in the history
  • Loading branch information
ELynx committed Sep 6, 2016
1 parent 83baed3 commit 369dff4
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 66 deletions.
11 changes: 5 additions & 6 deletions app/src/main/java/com/elynx/pogoxmitm/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
Expand Down Expand Up @@ -46,7 +45,7 @@ public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
HttpURLConnectionImplName = "libcore.net.http.HttpURLConnectionImpl";
}

XposedBridge.log("Injecting into PoGo");
Log.i("Injecting into PoGo");

// methods below are roughly in order or being called
// note that joinHeaders and readDataSteam are called from doSyncRequest
Expand Down Expand Up @@ -117,13 +116,13 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (!context.niaRequest)
return;

XposedBridge.log("[request] " + context.shortDump());
Log.d("[request] " + context.shortDump());

MitmOutputStream replacement = new MitmOutputStream((OutputStream) param.getResult(), context.requestId);
param.setResult(replacement);

if (BuildConfig.DEBUG) {
XposedBridge.log("Output stream replaced");
Log.d("Output stream replaced");
}
}
});
Expand All @@ -139,13 +138,13 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (!context.niaResponse)
return;

XposedBridge.log("[response] " + context.shortDump());
Log.d("[response] " + context.shortDump());

MitmInputStream replacement = new MitmInputStream((InputStream) param.getResult(), context.requestId);
param.setResult(replacement);

if (BuildConfig.DEBUG) {
XposedBridge.log("Input stream replaced");
Log.d("Input stream replaced");
}
}
});
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/elynx/pogoxmitm/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.elynx.pogoxmitm;

public class Log {
public static final String TAG = "PoGo MitM";

public static void d(String message) {
android.util.Log.d(TAG, message);
}

public static void i(String message) {
android.util.Log.i(TAG, message);
}

public static void e(String message) {
android.util.Log.e(TAG, message);
}

public static void e(Throwable t) {
android.util.Log.e(TAG, android.util.Log.getStackTraceString(t));
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/elynx/pogoxmitm/MitmInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Implements input stream that asks MITM provider before giving data away
* <p>
* <p/>
* Based on answer on StackOverflow
* https://stackoverflow.com/questions/4332264/wrapping-a-bytebuffer-with-an-inputstream/6603018#6603018
*/
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/com/elynx/pogoxmitm/MitmProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.nio.ByteBuffer;

import de.robv.android.xposed.XposedBridge;

/**
* Class that does actual manipulations on data
* Should be made reentrant and synchronized, since it is called from threads
Expand All @@ -22,7 +20,7 @@ public static ByteBuffer processOutboundPackage(ByteBuffer roData, int exchangeI
roData.rewind();

if (BuildConfig.DEBUG) {
XposedBridge.log("Processing outbound package of size " + Integer.toString(roData.remaining()));
Log.d("Processing outbound package of size " + Integer.toString(roData.remaining()));
}

try {
Expand All @@ -36,7 +34,7 @@ public static ByteBuffer processOutboundPackage(ByteBuffer roData, int exchangeI
// connectionOk;
}
} catch (Throwable e) {
XposedBridge.log(e);
Log.e(e);
}

return null;
Expand All @@ -53,7 +51,7 @@ public static ByteBuffer processInboundPackage(ByteBuffer roData, int exchangeId
roData.rewind();

if (BuildConfig.DEBUG) {
XposedBridge.log("Processing inbound package of size " + Integer.toString(roData.remaining()));
Log.d("Processing inbound package of size " + Integer.toString(roData.remaining()));
}

try {
Expand All @@ -67,7 +65,7 @@ public static ByteBuffer processInboundPackage(ByteBuffer roData, int exchangeId
// connectionOk
}
} catch (Throwable e) {
XposedBridge.log(e);
Log.e(e);
}

return null;
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/elynx/pogoxmitm/ScriptTestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.ByteBuffer;

public class ScriptTestActivity extends Activity implements View.OnClickListener {
protected static String dump;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,6 +29,9 @@ public void onCreate(Bundle savedInstanceState) {

Button netButton = (Button) findViewById(R.id.mitmRun);
netButton.setOnClickListener(this);

Button dumpButton = (Button) findViewById(R.id.resultDump);
dumpButton.setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -69,7 +73,9 @@ public void onClick(View v) {
}

if (v.getId() == R.id.mitmRun) {
String[] datas = {"space", "100500 CP caterpie", "mew", "mewtwo", "mewthree"};
NetworkImitation.clearResults();

String[] datas = {"space!!!", "100500 CP caterpie", "mew", "mewtwo", "mewthree"};

for (int i = 0; i < datas.length; ++i) {
byte[] bytes = datas[i].getBytes();
Expand All @@ -87,5 +93,10 @@ public void onClick(View v) {
}
}
}

if (v.getId() == R.id.resultDump) {
EditText resultEdit = (EditText) findViewById(R.id.rubyResult);
resultEdit.setText(NetworkImitation.getResults());
}
}
}
50 changes: 34 additions & 16 deletions app/src/main/java/com/elynx/pogoxmitm/test/NetworkImitation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.elynx.pogoxmitm.test;

import com.elynx.pogoxmitm.Log;
import com.elynx.pogoxmitm.MitmProvider;

import java.nio.ByteBuffer;
Expand All @@ -16,6 +17,26 @@ public class NetworkImitation {
protected static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 3000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
protected static Integer exchangeId = 0;

protected static String results = new String();

public static void clearResults() {
synchronized (results) {
results = "";
}
}

public static void appendResults(String text) {
synchronized (results) {
results += "\n" + text;
}
}

public static String getResults() {
synchronized (results) {
return new String(results);
}
}

public static void pushData(final ByteBuffer data) {
synchronized (exchangeId) {
++exchangeId;
Expand All @@ -28,17 +49,16 @@ public void run() {
}

public static void doPushData(int exchangeId, ByteBuffer data) {
ByteBuffer byteBuffer = ByteBuffer.allocate(data.capacity());
data.get(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.capacity());

boolean outConnectionOk = Math.random() <= NetworkP;

// OK to use original buffer IMO, since it is used as read-only
ByteBuffer outbound = MitmProvider.processOutboundPackage(data.asReadOnlyBuffer(), exchangeId, outConnectionOk);
ByteBuffer outbound = MitmProvider.processOutboundPackage(byteBuffer.asReadOnlyBuffer(), exchangeId, outConnectionOk);

// if Mitm returned null then "send" original data
if (outbound == null) {
outbound = ByteBuffer.allocate(data.capacity());

data.rewind();
outbound.put(data.array(), data.arrayOffset(), data.remaining());
outbound = byteBuffer;
}

//imitate server
Expand All @@ -53,7 +73,7 @@ public static void doPushData(int exchangeId, ByteBuffer data) {
}

if (b == 0x21 && i > 0) {
byte bPrev = outbound.array()[i-1];
byte bPrev = outbound.array()[i - 1];
if (bPrev == 0x21 || bPrev == 0x31) {
b = 0x31;
}
Expand All @@ -66,32 +86,30 @@ public static void doPushData(int exchangeId, ByteBuffer data) {

try {
Thread.sleep(ping);
} catch (InterruptedException e)
{
org.ruboto.Log.e(e.getMessage());
} catch (InterruptedException e) {
Log.e(e.getMessage());
}

// end of server imitation

// stop if nothing was "sent"
if (!outConnectionOk)
if (!outConnectionOk) {
appendResults("Connection lost");
return;
}

boolean inConnectionOk = Math.random() <= NetworkP;

ByteBuffer inbound = MitmProvider.processInboundPackage(outbound.asReadOnlyBuffer(), exchangeId, inConnectionOk);

// if Mitm returned null then "return" original data
if (inbound == null) {
inbound = ByteBuffer.allocate(outbound.capacity());

outbound.rewind();
inbound.put(outbound.array(), outbound.arrayOffset(), outbound.remaining());
inbound = outbound;
}

inbound.rewind();
String result = new String(inbound.array(), inbound.arrayOffset(), inbound.remaining());

org.ruboto.Log.i("Finally " + result);
appendResults(result);
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/org/ruboto/JRubyAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ruboto;

import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -12,7 +11,9 @@
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;

import com.elynx.pogoxmitm.Log;

import dalvik.system.PathClassLoader;

public class JRubyAdapter {
Expand Down
22 changes: 0 additions & 22 deletions app/src/main/java/org/ruboto/Log.java

This file was deleted.

9 changes: 1 addition & 8 deletions app/src/main/java/org/ruboto/Script.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package org.ruboto;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import com.elynx.pogoxmitm.Log;

public class Script {
private static String[] scriptsDir = new String[]{"scripts"};
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/ruboto/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;

import com.elynx.pogoxmitm.Log;

public class ScriptLoader {
/**
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_script_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:ems="10"
android:id="@+id/rubyScript"
android:layout_gravity="top|center_horizontal"
android:layout_weight="10"
android:layout_weight="3"
android:text="\@test_variable"
android:typeface="monospace" />

Expand All @@ -37,7 +37,7 @@
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/rubyResult"
android:layout_weight="4"
android:layout_weight="12"
android:typeface="monospace"
android:layout_gravity="top|center_horizontal" />

Expand All @@ -60,5 +60,14 @@
android:textAlignment="center"
android:layout_weight="1"
android:layout_gravity="bottom|center_horizontal" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/dump_results"
android:id="@+id/resultDump"
android:layout_weight="1"
android:textAlignment="center"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">Pokemon Go Xposed Mitm</string>
<string name="ruby_that">Ruby that!</string>
<string name="sim_network">Sim network</string>
<string name="dump_results">Dump results</string>
</resources>

0 comments on commit 369dff4

Please sign in to comment.