The code inside main.c
(main.c中的代码)
#include <stdio.h>
#include <unistd.h>
int main() {
int c_variable = 0; // the target
for(int x = 0; x < 100; x++) {
c_variable += 5; // increase by 5 to change the value of the int
printf("%i
", c_variable); // print current value
sleep(8); // sleep so I have time to scan memory
}
return 0;
}
What I am trying to achieve is to read the integer c_variable
and then to modify it inside another .c program.
(我想要实现的是读取整数c_variable
,然后在另一个.c程序中对其进行修改。)
I am on linux so I did ps -A | grep main
(我在linux上,所以我做了ps -A | grep main
)
ps -A | grep main
and got the PID of the running program. (ps -A | grep main
并获取正在运行的程序的PID。)
I then did sudo scanmem PID
and entered the current value of c_variable
a few times. (然后,我做了sudo scanmem PID
并输入了c_variable
的当前值几次。)
I was left with three memory addresses and executing the command set 500
changed the value the program printed, effectively changing the memory address' value to 500 instead of 35 or whatever the program was currently at. (我只有三个内存地址,执行命令set 500
更改程序打印的值,有效地将内存地址的值更改为500,而不是35或程序当前所在的位置。)
I then executed the following code (然后我执行了以下代码)
#include <stdio.h>
int main() {
const long unsigned addr = 0x772d85fa1008; // one of the three addresses from scanmem
printf("%lu
", addr);
return 0;
}
but I got some random long string of numbers, not the current number.
(但是我得到了一些随机的长字符串,而不是当前数字。)
The tutorials and answers I have read on how to read and write memory on linux does not have to use long unsigned
but can use char* or just int* instead. (我已经阅读了有关如何在linux上读写内存的教程和答案,不必使用long unsigned
而可以使用char *或int *。)
My memory address seems to be a bit long, I have not see memory addresses that long before. (我的内存地址似乎有点长,不久之前我还没有看到内存地址。)
Anyhow, how do I read and write the memory address of the integer c_variable
? (无论如何,我如何读写整数c_variable
的内存地址?)
Edit: the output of scanmem looks something like this
(编辑:scanmem的输出看起来像这样)
info: we currently have 3 matches.
3> list
[ 0] 7771ff64b090, 6 + 1e090, stack, 20, [I64 I32 I16 I8 ]
[ 1] 7771ff64b5d8, 6 + 1e5d8, stack, 20, [I64 I32 I16 I8 ]
[ 2] 7771ff64b698, 6 + 1e698, stack, 20, [I32 I16 I8 ]
3> set 50
info: setting *0x7771ff64b090 to 0x32...
info: setting *0x7771ff64b5d8 to 0x32...
info: setting *0x7771ff64b698 to 0x32...
output
(输出)
...
150
155
160
165
170
175
55
60
65
...
ask by RealAnwersOnly translate from so