Skip to content

Commit

Permalink
add get phone(system) info
Browse files Browse the repository at this point in the history
add test hook return+
  • Loading branch information
littleWhiteDuck committed Apr 16, 2023
1 parent 2ad137e commit 8f1a5d4
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId "me.duck.hooktest"
minSdk 21
targetSdk 33
versionCode 6
versionName "1.6"
versionCode 7
versionName "1.7"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
private static int scores;
private static boolean isGood;
private me.duck.hooktest.bean.UseBean useBean;
private me.duck.hooktest.bean.TestGson testReturn2();
}
-keep class me.duck.hooktest.bean.UseBean

Expand Down
64 changes: 63 additions & 1 deletion app/src/main/java/me/duck/hooktest/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.util.DisplayMetrics;
Expand Down Expand Up @@ -38,21 +39,48 @@
import java.lang.reflect.Field;
import java.util.UUID;

import me.duck.hooktest.bean.TestGson;
import me.duck.hooktest.bean.UseBean;
import me.duck.hooktest.databinding.ActivityMainBinding;
import me.duck.hooktest.utils.ShellUtils;
import me.duck.hooktest.utils.WindowPreferencesManager;
import me.duck.hooktest.view.PopupView;


@SuppressWarnings("ResultOfMethodCallIgnored")
public class MainActivity extends AppCompatActivity {

private static final String config = "{\"packageName\":\"me.duck.hooktest\",\"appName\":\"HookTest\",\"versionName\":\"1.4\",\"description\":\"\",\"configs\":\"[{\\\"mode\\\":1,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"alterParams\\\",\\\"params\\\":\\\"I,Z,java.lang.String\\\",\\\"resultValues\\\":\\\"10,,参数\\\"},{\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"getReturnValue\\\",\\\"resultValues\\\":\\\"测更更更试了返t回\\\"},{\\\"mode\\\":2,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"breakPerform\\\"},{\\\"mode\\\":1,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"getPrimitiveString\\\",\\\"params\\\":\\\"B,C,S,I,J,F,D,Z\\\",\\\"resultValues\\\":\\\"2b,2c,2short,2,2L,2f,2d,false\\\"},{\\\"mode\\\":3,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"changeStaticFields\\\",\\\"fieldName\\\":\\\"isGood\\\",\\\"fieldClassName\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"resultValues\\\":\\\"true\\\"},{\\\"mode\\\":4,\\\"className\\\":\\\"me.duck.hooktest.bean.UseBean\\\",\\\"methodName\\\":\\\"<init>\\\",\\\"params\\\":\\\"Z,I\\\",\\\"fieldName\\\":\\\"isHook\\\",\\\"resultValues\\\":\\\"true\\\"},{\\\"mode\\\":4,\\\"className\\\":\\\"me.duck.hooktest.bean.UseBean\\\",\\\"methodName\\\":\\\"<init>\\\",\\\"params\\\":\\\"Z,I\\\",\\\"fieldName\\\":\\\"level\\\",\\\"resultValues\\\":\\\"188888\\\"},{\\\"mode\\\":3,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"changeStaticFields\\\",\\\"fieldName\\\":\\\"scores\\\",\\\"fieldClassName\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"resultValues\\\":\\\"10000\\\"}]\",\"id\":170}\n";
private static final String config = "{\"packageName\":\"me.duck.hooktest\",\"appName\":\"HookTest\",\"versionName\":\"1.4\",\"description\":\"\",\"configs\":\"[{\\\"mode\\\":1,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"alterParams\\\",\\\"params\\\":\\\"I,Z,java.lang.String\\\",\\\"resultValues\\\":\\\"10,,参数\\\"},{\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"getReturnValue\\\",\\\"resultValues\\\":\\\"测更更更试了返t回\\\"},{\\\"mode\\\":2,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"breakPerform\\\"},{\\\"mode\\\":1,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"getPrimitiveString\\\",\\\"params\\\":\\\"B,C,S,I,J,F,D,Z\\\",\\\"resultValues\\\":\\\"2b,2c,2short,2,2L,2f,2d,false\\\"},{\\\"mode\\\":3,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"changeStaticFields\\\",\\\"fieldName\\\":\\\"isGood\\\",\\\"fieldClassName\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"resultValues\\\":\\\"true\\\"},{\\\"mode\\\":4,\\\"className\\\":\\\"me.duck.hooktest.bean.UseBean\\\",\\\"methodName\\\":\\\"<init>\\\",\\\"params\\\":\\\"Z,I\\\",\\\"fieldName\\\":\\\"isHook\\\",\\\"resultValues\\\":\\\"true\\\"},{\\\"mode\\\":4,\\\"className\\\":\\\"me.duck.hooktest.bean.UseBean\\\",\\\"methodName\\\":\\\"<init>\\\",\\\"params\\\":\\\"Z,I\\\",\\\"fieldName\\\":\\\"level\\\",\\\"resultValues\\\":\\\"188888\\\"},{\\\"mode\\\":3,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"changeStaticFields\\\",\\\"fieldName\\\":\\\"scores\\\",\\\"fieldClassName\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"resultValues\\\":\\\"10000\\\"},{\\\"mode\\\":10,\\\"className\\\":\\\"me.duck.hooktest.MainActivity\\\",\\\"methodName\\\":\\\"testReturn2\\\",\\\"resultValues\\\":\\\"{\\\\\\\"testBoolean\\\\\\\":false,\\\\\\\"testInt\\\\\\\":666,\\\\\\\"testString\\\\\\\":\\\\\\\"test hook return+ success\\\\\\\"}\\\",\\\"hookPoint\\\":\\\"after\\\",\\\"returnClassName\\\":\\\"me.duck.hooktest.bean.TestGson\\\"}]\",\"id\":0}\n";
private static int scores = 0;
private static boolean isGood = false;
private UseBean useBean;
private String cachePath;

private ActivityMainBinding binding;

private static final String KEY_NEW_LINE_CHAR = "\n";

private static final String[] props = new String[]{
"getprop ro.build.id",
"getprop ro.build.display.id",
"getprop ro.product.name",
"getprop ro.product.device",
"getprop ro.product.board",
"getprop ro.product.manufacturer",
"getprop ro.product.brand",
"getprop ro.product.model"
};
private static final String[] propNames = new String[]{
"ID",
"DISPLAY",
"PRODUCT",
"DEVICE",
"BOARD",
"MANUFACTURER",
"BRAND",
"MODEL",
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -88,6 +116,10 @@ private void changeStaticFields() {
isGood = false;
}

private TestGson testReturn2() {
return new TestGson(999, true, "test hook return+ by gson");
}


@SuppressLint("SetTextI18n")
private void initView() {
Expand Down Expand Up @@ -115,8 +147,38 @@ private void initView() {
binding.btWriteFile.setOnClickListener(v -> writeText());
binding.btReadFile.setOnClickListener(v -> readText());
binding.btExit.setOnClickListener(v -> exitApp());
binding.btGetSystemInfo.setOnClickListener(v -> showGetSystemInfoDialog());
TestGson testGson = testReturn2();
binding.tvTestReturn2.setText("testInt: " + testGson.getTestInt() + "\ntestBoolean: " + testGson.isTestBoolean() + "\ntestString: " + testGson.getTestString());
}


private void showGetSystemInfoDialog() {
StringBuilder stringBuilder = new StringBuilder();
String propValue;
for (String propName : propNames) {
try {
Field field = Build.class.getField(propName);
propValue = (String) field.get(null);
} catch (Exception ignored) {
propValue = "unknown";
}
stringBuilder.append(propName).append(": ").append(propValue).append(KEY_NEW_LINE_CHAR);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
stringBuilder.append("SOC_MANUFACTURER: ").append(Build.SOC_MANUFACTURER).append(KEY_NEW_LINE_CHAR);
stringBuilder.append("SOC_MODEL: ").append(Build.SOC_MODEL).append(KEY_NEW_LINE_CHAR);
}
stringBuilder.append("===============").append(KEY_NEW_LINE_CHAR);
String[] results = ShellUtils.execCmd(props).split(KEY_NEW_LINE_CHAR);
// maybe error
for (int i = 0; i < results.length; i++) {
stringBuilder.append(propNames[i]).append(": ").append(results[i]).append(KEY_NEW_LINE_CHAR);
}
showMessageDialog(getString(R.string.main_get_system_info), stringBuilder.toString());
}


private void exitApp() {
CharSequence[] items = getResources().getStringArray(R.array.exit_app_items);
new MaterialAlertDialogBuilder(this)
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/me/duck/hooktest/bean/TestGson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package me.duck.hooktest.bean;

import androidx.annotation.Keep;

@Keep
public class TestGson {

private int testInt;
private boolean testBoolean;
private String testString;

public TestGson(int testInt, boolean testBoolean, String testString) {

this.testInt = testInt;
this.testBoolean = testBoolean;
this.testString = testString;
}

public String getTestString() {
return testString;
}

public void setTestString(String testString) {
this.testString = testString;
}

public boolean isTestBoolean() {
return testBoolean;
}

public void setTestBoolean(boolean testBoolean) {
this.testBoolean = testBoolean;
}

public int getTestInt() {
return testInt;
}

public void setTestInt(int testInt) {
this.testInt = testInt;
}
}
64 changes: 64 additions & 0 deletions app/src/main/java/me/duck/hooktest/utils/ShellUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.duck.hooktest.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellUtils {
private static final String KEY_COMMAND_SHELL = "sh";
private static final String KEY_LINE_END_CHAR = "\n";
private static final String KEY_EXIT = "exit\n";

public static String execCmd(String command) {
return execCmd(new String[]{command}).trim();
}

public static String execCmd(String[] commands) {
Process process = null;
BufferedReader reader = null;
InputStreamReader inputStream = null;
DataOutputStream outputStream = null;
try {
process = Runtime.getRuntime().exec(KEY_COMMAND_SHELL);
inputStream = new InputStreamReader(process.getInputStream());
reader = new BufferedReader(inputStream);
outputStream = new DataOutputStream(process.getOutputStream());
StringBuilder stringBuilder = new StringBuilder();
String line;
for (String command : commands) {
outputStream.writeBytes(command + KEY_LINE_END_CHAR);
}
outputStream.writeBytes(KEY_EXIT);
outputStream.flush();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append(KEY_LINE_END_CHAR);
}
process.waitFor();
return stringBuilder.toString();
} catch (Exception ignored) {
return "null";
} finally {
try {
if (outputStream != null) {
outputStream.close();
}

if (reader != null) {
reader.close();
}

if (inputStream != null) {
inputStream.close();
}

if (process != null) {
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.duck.hooktest;
package me.duck.hooktest.utils;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@
android:layout_marginTop="10dp"
android:text="@string/main_test_boxed_primitive_type" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_test_return2"
style="?attr/textAppearanceListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -94,7 +102,7 @@
<androidx.constraintlayout.helper.widget.Flow
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:constraint_referenced_ids="bt_showToast,bt_showDialog,bt_showSuperDialog,bt_readAssets,bt_createFile,bt_deleteFile,bt_writeFile,bt_readFile,bt_read_clipboard,bt_write_clipboard,bt_popup1,bt_popup2,bt_exit"
app:constraint_referenced_ids="bt_showToast,bt_showDialog,bt_showSuperDialog,bt_readAssets,bt_createFile,bt_deleteFile,bt_writeFile,bt_readFile,bt_read_clipboard,bt_write_clipboard,bt_popup1,bt_popup2,bt_exit,bt_get_system_info"
app:flow_horizontalBias="0"
app:flow_horizontalGap="5dp"
app:flow_horizontalStyle="packed"
Expand Down Expand Up @@ -180,6 +188,12 @@
android:layout_height="wrap_content"
android:text="@string/main_exit_app" />

<com.google.android.material.button.MaterialButton
android:id="@+id/bt_get_system_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_get_system_info" />


</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
<string name="main_exit_app">退出应用</string>
<string name="main_test_crash">测试闪退</string>
<string name="main_read_assets_file">读取Assets文件</string>
<string name="main_get_system_info">获取系统信息</string>
</resources>
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 @@ -43,6 +43,7 @@
<string name="main_exit_app">Exit app</string>
<string name="main_test_crash">Test Crash</string>
<string name="main_read_assets_file">Read Assets file</string>
<string name="main_get_system_info">Get system info</string>
<array name="exit_app_items">
<item>Finish()</item>
<item>System.exit()</item>
Expand Down

0 comments on commit 8f1a5d4

Please sign in to comment.