C-获取存储在进程虚拟内存中某个地址的值



我得到一个地址(十六进制数(,表示进程虚拟内存中的内存地址。

我已经验证了地址是否存在于堆中。但现在我想访问位于该地址的字节的值。

这就是我目前所拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/*
- Takes a single arg: required_address
- if the address is in virtual memory:
-   print to stdout the value of the single byte of memory located at address
-   return with exit code 1
- else:
-   print nothing
-   return with exit code 0
- 00405000-00426000 [heap]
*/
int main(int argc, char const *argv[]) {
unsigned long ret_adr = strtoul(argv[1], NULL, 16);
int pid = getpid();
char find[10] = "heap";
char high[32], low[32];
// Read maps file
char maps_file_addr[20];
sprintf(maps_file_addr, "/proc/%d/maps", pid);
puts(maps_file_addr);
FILE* maps_file = fopen(maps_file_addr, "r");
char line[256];
if (maps_file == NULL){
printf("Error! opening maps filen");
// Program exits if the file pointer returns NULL.
exit(1);
}
// Get range of heap
while (fgets(line, sizeof(line), maps_file) != NULL) {
if(strstr(line, find)){
char * token = strtok(line, " ");
strcpy(low, strtok(token, "-"));
strcpy(high, strtok(NULL, "-"));
}
}
unsigned long low_hex = strtoul(low, NULL, 16);
unsigned long high_hex = strtoul(high, NULL, 16);
printf("Address: %lun", ret_adr);
printf("Low Hex: %lun", low_hex);
printf("High Hex: %lun", high_hex);
// Check if address is in heap range
if (low_hex < ret_adr < high_hex) {
char *p = (char *)ret_adr;
printf("%cn", *p);
} else {
exit(1);
}
fclose(maps_file);
return 0;
}

在行中:

if (low_hex < ret_adr < high_hex) {
char *p = (char *)ret_adr;
printf("%cn", *p);
}

我尝试访问存储在ret_adr位置的虚拟内存中的值。但是什么都没有打印出来。如何访问存储在该位置的值?

参考终端:

[task1]$ setarch x86_64 -R ./task1 400000
/proc/24603/maps
Address: 4194304
Low Hex: 4214784
High Hex: 4349952

if (low_hex < ret_adr < high_hex) {

此行不正确,应为:

if (low_hex <= ret_adr && ret_addr < high_hex) {

最新更新