以下代碼基于inotify進行記憶體監控實作
JNIEXPORT jboolean JNICALL Java_com_TeSo_testinotify (JNIEnv *, jobject)
{
LOGD("start work===============");
int ret, len, i;
int pid = getpid();
const int MAXLEN = 2048;
char buf[1024];
char readbuf[MAXLEN] ={0};
char result[256] ={0};
int fd, wd;
fd_set readfds;//定義檔案描述符字的集合
char *cur_event_filename = NULL;
fd = inotify_init();//用于創建一個 inotify 實體的系統呼叫,并回傳一個指向該實體的檔案描述符
sprintf(buf, "/proc/%d/maps", pid);
LOGD("PId======%s",buf);
wd = inotify_add_watch(fd, buf, IN_ALL_EVENTS);
//增加對檔案或者目錄的監控,并指定需要監控哪些事件,標志用于控制是否將事件添加到已有的監控中
if (wd >= 0) {
while (1) {
i = 0;
FD_ZERO(&readfds); // 對檔案描述符集清空
FD_SET(fd, &readfds); // 將fd加入readfds集合
ret = select(fd + 1, &readfds, 0, 0, 0);//檢查套接字是否可讀
if (ret == -1) {
break;
}
if (ret) {
len = read(fd, readbuf, MAXLEN);//讀取包含一個或者多個事件資訊的快取
while (i < len) {
struct inotify_event *event = (struct inotify_event *) &readbuf[i];
//這里會出現自身掃描的時候訪問記憶體也有資料情況,
if ((event->mask & IN_ACCESS) || (event->mask & IN_OPEN)) {
//int ret = kill(pid, SIGKILL);
LOGD("打開并訪問了記憶體 ======%s",event->name);
// kill(pid, SIGKILL); //強制關閉自身行程
}
//修改器重點修改記憶體在這塊
if( (event->mask & IN_MODIFY))
{
// kill(pid, SIGKILL);
LOGD("修改了了了記憶體 =======%s",event->name);
}
i += sizeof(struct inotify_event) + event->len;
}
}
}
}
inotify_rm_watch(fd, wd);//從監控串列中移出監控專案
close(fd);
return true;
}
更多安全技術文章,請關注公眾號 “游戲安全攻防”
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263037.html
標籤:其他
