forked from abcz316/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps_helper.cpp
More file actions
74 lines (60 loc) · 2.35 KB
/
Copy pathmaps_helper.cpp
File metadata and controls
74 lines (60 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "maps_helper.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//显然,这里面核心的就是get_module_base函数:
/*
此函数的功能就是通过遍历/proc/pid/maps文件,来找到目的module_name的内存映射起始地址。
由于内存地址的表达方式是startAddrxxxxxxx-endAddrxxxxxxx的,所以会在后面使用strtok(line,"-")来分割字符串
如果pid = -1,表示获取本地进程的某个模块的地址,
否则就是pid进程的某个模块的地址。
*/
void* get_module_base(pid_t pid, const char* module_name)
{
FILE *fp;
long addr = 0;
char *pch;
char filename[32];
char line[1024];
if (pid < 0) {
/* self process */
snprintf(filename, sizeof(filename), "/proc/self/maps", pid);
}
else {
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
}
fp = fopen(filename, "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, module_name)) {
//分解字符串为一组字符串。line为要分解的字符串,"-"为分隔符字符串。
pch = strtok(line, "-");
//将参数pch字符串根据参数base(表示进制)来转换成无符号的长整型数
addr = strtoull(pch, NULL, 16);
if (addr == 0x8000)
addr = 0;
break;
}
}
fclose(fp);
}
return (void *)addr;
}
/*
该函数为一个封装函数,通过调用get_module_base函数来获取目的进程的某个模块的起始地址,然后通过公式计算出指定函数在目的进程的起始地址。
*/
void* get_remote_addr(pid_t target_pid, const char* module_name, void* local_addr)
{
void* local_handle, *remote_handle;
//获取本地某个模块的起始地址
local_handle = get_module_base(-1, module_name);
if (local_handle == NULL) {
return local_handle;
}
//获取远程pid的某个模块的起始地址
remote_handle = get_module_base(target_pid, module_name);
TRACE("[+] get_remote_addr: local[%p], remote[%p]\n", local_handle, remote_handle);
/*这需要我们好好理解:local_addr - local_handle的值为指定函数(如mmap)在该模块中的偏移量,然后再加上rempte_handle,结果就为指定函数在目的进程的虚拟地址*/
void * ret_addr = (void *)((uintptr_t)local_addr + (uintptr_t)remote_handle - (uintptr_t)local_handle);
return ret_addr;
}