android呼叫C語言實現記憶體的讀取與修改的方法示例
阿新 • • 發佈:2020-03-06
寫之前需要準備以下內容
android studio 已ROOT安卓裝置 GG修改器
開啟android studio,建立Native C++ Project
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btn" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="~" /> </LinearLayout>
MainActivity.java
package com.gs.jc; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; private JNI jni; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); jni = new JNI(); textView = (TextView) findViewById(R.id.tv); } public void btn(View view) { textView.setText(String.valueOf(jni.searchMem())); } }
新建一個java類,以實現java呼叫對應C程式碼
package com.gs.jc; public class JNI { static { System.loadLibrary("native-lib"); } /* *定義native方法 *呼叫C程式碼對應的方法 */ public native int searchMem(); }
O_RDONLY只讀開啟
O_WRONLY只寫開啟
O_RDWR可讀可寫開啟
O_SYNC以同步的方式開啟檔案
C++核心程式碼
#include <jni.h> #include <string> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <dirent.h> #include <unistd.h> static int fd = 0; //查詢遊戲程序pid int getPID(const char *pack_name) { int id = -1,pid = -1; DIR *dir = 0; FILE *file = 0; char filename[32] = {0}; char cmdline[256] = {0}; struct dirent *entry = 0; if (pack_name == NULL) { return -1; } dir = opendir("/proc"); if (dir == NULL) { return -1; } while ((entry = readdir(dir)) != NULL) { id = atoi(entry->d_name); if (id > 0) { sprintf(filename,"/proc/%d/cmdline",id); file = fopen(filename,"r"); if (file) { fgets(cmdline,sizeof(cmdline),file); fclose(file); if (strcmp(pack_name,cmdline) == 0) { pid = id; break; } } } } closedir(dir); return pid; } //開啟檔案控制代碼 int open_proc_mem(int pid) { if (pid <= 0) return -1; char mempath[64] = {0}; int handle = -1; sprintf(mempath,"/proc/%d/mem",pid); handle = open(mempath,O_RDWR,O_SYNC); return handle; } //讀記憶體 void pread64_mem(int fd,void *buff,int size,long *addr) { if (fd <= 0 || buff == NULL || size <= 0 || addr == NULL) return; pread64(fd,buff,size,(unsigned long) addr); } //寫記憶體 void pwrite64_mem(int fd,const void *buff,long *addr) { if (fd <= 0 || buff == NULL || size <= 0 || addr == NULL) return; pwrite64(fd,(unsigned long) addr); } extern "C" jint Java_com_gs_jc_JNI_searchMem(JNIEnv *env,jobject thiz) { char *game = "com.tencent.tmgp.sgame"; //包名 int pid = getPID(game); //獲取程序PID fd = open_proc_mem(pid); //開啟程序記憶體 //long base = 0; long buf[1] = {666}; //需要修改記憶體的值 long *addr = (long *) 0x12C0085C; //記憶體地址:0x12C0085C pwrite64_mem(fd,&buf[0],4,addr); //寫入記憶體資料 //pread64_mem(fd,&base,addr); return pid; }
C程式碼中需要自行修改的地方
char *game = “com.tencent.tmgp.sgame”; //包名 long *addr = (long *) 0x12C0085C;//記憶體地址
效果圖連結:yuanma/men_jb51.rar
以上是簡單的記憶體地址修改方法,到此這篇關於android呼叫C語言實現記憶體的讀取與修改的方法示例的文章就介紹到這了,更多相關android呼叫C語言實現記憶體讀取修改內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!