android調用C語言實現內存的讀取與修改的方法示例
寫之前需要準備以下內容
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: 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;//查找游戲進程pidint 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, int size, long *addr) { if (fd <= 0 || buff == NULL || size <= 0 || addr == NULL) return; pwrite64(fd, buff, size, (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, 4, addr); return pid;}
C代碼中需要自行修改的地方
char *game = “com.tencent.tmgp.sgame”; //包名long *addr = (long *) 0x12C0085C;//內存地址
效果圖鏈接:yuanma/men_jb51.rar
以上是簡單的內存地址修改方法,到此這篇關于android調用C語言實現內存的讀取與修改的方法示例的文章就介紹到這了,更多相關android調用C語言實現內存讀取修改內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
