无法访问特定的内存地址。在C程序上

时间:2018-11-10 00:46:35

标签: c linux gcc memory gdb

我正在研究“黑客技术的利用”一书,练习Convert2.c,第61页。

这是我的代码。以下是我的问题。

#include <stdio.h>

void usage(char *program_name) {
    printf("Usage: %s <message> <# of times to repeat>\n", program_name);
    exit(1);
}

int main(int argc, char *argv[]) {
    int i, count;

   // if(argc < 3) //if fewer than 3 arguments is used 
     //   usage(argv[0]); // display usage message and exit

    count = atoi(argv[2]); //convert the second arg into an  interger
    printf("Repeating %d times\n", count);

    for(i=0; i < count; i++) 
        printf("%3d - %s\n", i, argv[1]); // print the first arg 

}

GDB输出...

    ➜ git:(master) ✗  gdb -q a.out
Reading symbols from a.out...done.
(gdb) run test
Starting program: /home/fruitdealer/clones/C_zombie/hacking/a.out test

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.
(gdb) break main
Breakpoint 1 at 0x555555554707: file convert.c, line 14.
(gdb) where
#0  __GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
#1  0x00007ffff7a29122 in __strtol (nptr=<optimized out>, endptr=endptr@entry=0x0, base=base@entry=10)
    at ../stdlib/strtol.c:106
#2  0x00007ffff7a24690 in atoi (nptr=<optimized out>) at atoi.c:27
#3  0x000055555555471f in main (argc=2, argv=0x7fffffffdeb8) at convert.c:14
(gdb) run test
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/fruitdealer/clones/C_zombie/hacking/a.out test

Breakpoint 1, main (argc=2, argv=0x7fffffffdeb8) at convert.c:14
14          count = atoi(argv[2]); //convert the second arg into an  interger
(gdb) cont
Continuing.

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, base=base@entry=10, group=group@entry=0,
    loc=0x7ffff7dd0560 <_nl_global_locale>) at ../stdlib/strtol_l.c:292
292     ../stdlib/strtol_l.c: No such file or directory.
(gdb) x/3xw 0x7fffffffdeb8
0x7fffffffdeb8: 0xffffe220      0x00007fff      0xffffe250
(gdb) x/s 0xffffe220
0xffffe220:     <error: Cannot access memory at address 0xffffe220>
(gdb) x/s 0xffffe250
0xffffe250:     <error: Cannot access memory at address 0xffffe250>
(gdb) x/sw 0xffffe250
0xffffe250:     <error: Cannot access memory at address 0xffffe250>
(gdb)

我发布了所有gdb输出,因为我不确定您需要多少输出。当我在gdb上运行“ x / s”并收到<error: Cannot access memory at address 0xffffe250>错误时,我的问题就出在GDB输出的底部。

在书中,乔恩·埃里克森(Jon Erickson)能够访问0xffffe2200x00007fff,然后他在0xffffe250的这部分内存中出错。

我想念什么?

为什么我不能访问0x7fffffffdeb8中的三个地址中的任何一个?

1 个答案:

答案 0 :(得分:3)

地址的前半部分被切断。如果您注意到,因为您使用的是64位计算机而不是32位计算机,因此需要8个字节来存储地址。您正在尝试访问截断的地址。

您正在查看的不是一个grep -l 'service:' dev/* | xargs sed -i "" 's/\service:/app:/g'的三个地址。尝试访问以0x7fffffffdeb8 ...

开头的字节