macOS 代码运行不符合预期值修改int main int x = 42; int ptr = &x; printfpn voidptr; printfdn charptr; fflushstdout; 获取需要读取的地址 uint64_t address = uint64_tptr; 定义一个缓冲区来存储读取的数据 char buff
修改后的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <mach/mach.h> #include <arpa/inet.h>
int main() {
int x = 42;
int* ptr = &x;
printf("%p\n", (void*)ptr);
printf("%d\n", *ptr);
fflush(stdout);
// 获取需要读取的地址
uint64_t address = (uint64_t)ptr;
// 定义一个缓冲区来存储读取的数据
char buffer[sizeof(int)];
// 调用 vm_read() 函数读取数据
vm_size_t size;
kern_return_t result;
mach_port_t task;
task = mach_task_self();
result = vm_read(task, (vm_address_t)address, sizeof(int), (vm_offset_t*)buffer, &size);
if (result == KERN_SUCCESS) {
// 数据读取成功
printf("Read %zu bytes at address %llu: ", size, address);
for (int i = 0; i < sizeof(buffer); i++) {
printf("%02x ", (unsigned char)buffer[i]);
}
printf("\n");
int value = *((int*)buffer);
printf("Decoded value: %d\n", value);
if (value == 42) {
printf("Value matches\n");
} else {
printf("Value does not match\n");
}
} else {
// 数据读取失败
printf("Error: unable to read data at address %llu\n", address);
}
sleep(1);
return 0;
}
修改说明:
-
将 printf("%d\n", (char)ptr); 修改为 printf("%d\n", *ptr);,原代码中使用了错误的类型指针,导致输出不正确。
-
将 vm_map_t 修改为 mach_port_t,因为在 macOS 10.14 及以上版本中,vm_map_t 已经被弃用。
-
将 vm_read() 函数调用中的 buffer 参数的类型从 char* 修改为 vm_offset_t*,因为 vm_read() 函数的第四个参数应该是 vm_offset_t* 类型。
-
将 uint32_t 修改为 int,因为 int 类型可以存储四个字节的数据,与 uint32_t 类型相同。
-
将 ntohl() 函数去掉,因为在本地环境中不需要进行网络字节序转换。
-
修改了部分变量名和注释,使代码更加清晰易懂
原文地址: https://www.cveoy.top/t/topic/eEVm 著作权归作者所有。请勿转载和采集!