简体   繁体   English

如何使用 GDB 在 memory 中查找字符串的地址?

[英]How to find the address of a string in memory using GDB?

I want to find the address of a string in memory.我想在 memory 中找到一个字符串的地址。 In this case, I'm looking for "/bin/sh".在这种情况下,我正在寻找“/bin/sh”。 Its an initialized variable, so its in the.data section and after compilation, it has a fixed address.它是一个初始化的变量,所以它在.data段中,编译后有一个固定的地址。 So what do I do in GDB to find out its memory address?那么在 GDB 中该怎么做才能找出它的 memory 地址呢? And I do not know the name of the variable its stored in.而且我不知道它存储的变量的名称。

Using info proc map sounds like a better approach to me.使用info proc map对我来说听起来是一种更好的方法。

(gdb) info proc map
process 930
Mapped address spaces:

      Start Addr           End Addr       Size     Offset objfile
        0x400000           0x401000     0x1000        0x0 /myapp
        0x600000           0x601000     0x1000        0x0 /myapp
        0x601000           0x602000     0x1000     0x1000 /myapp
  0x7ffff7a1c000     0x7ffff7bd2000   0x1b6000        0x0 /usr/lib64/libc-2.17.so
  0x7ffff7bd2000     0x7ffff7dd2000   0x200000   0x1b6000 /usr/lib64/libc-2.17.so
  0x7ffff7dd2000     0x7ffff7dd6000     0x4000   0x1b6000 /usr/lib64/libc-2.17.so
  0x7ffff7dd6000     0x7ffff7dd8000     0x2000   0x1ba000 /usr/lib64/libc-2.17.so

(gdb) find 0x7ffff7a1c000,0x7ffff7bd2000,"/bin/sh"
0x7ffff7b98489
1 pattern found.
(gdb) x /s 0x7ffff7b98489
0x7ffff7b98489: "/bin/sh"
(gdb) x /xg 0x7ffff7b98489
0x7ffff7b98489: 0x0068732f6e69622f

If you want to search in the whole address space of the process, you need to get the memory mapping for your process and use the start address the end address with the find command in gdb.如果要在进程的整个地址空间中搜索,则需要为您的进程获取 memory 映射,并使用 gdb 中的查找命令使用起始地址结束地址。

for instance, if cat /proc/$PID/maps shows that your process's virtual memory ranges from 0x08048000 to 0xc0000000 you can search as follows:例如,如果cat /proc/$PID/maps显示您的进程的虚拟 memory 范围从 0x08048000 到 0xc0000000 您可以搜索如下:

(gdb) find 0x80048000, 0xc0000000, "/bin/sh"

Another way to get the memory mapping of your process is using the gdb's embedded command:获取进程的 memory 映射的另一种方法是使用 gdb 的嵌入式命令:

(gdb) info proc map

Use the find command.使用查找命令。

find [/sn] start_addr, +len, val1 [, val2, …]
find [/sn] start_addr, end_addr, val1 [, val2, …]

Search memory for the sequence of bytes specified by val1, val2, etc. The search begins at address start_addr and continues for either len bytes or through to end_addr inclusive.在 memory 中搜索由 val1、val2 等指定的字节序列。搜索从地址 start_addr 开始,继续查找 len 个字节或一直到 end_addr(含)。 s and n are optional parameters. s 和 n 是可选参数。 They may be specified in either order, apart or together.它们可以按任意顺序、分开或一起指定。

s, search query size The size of each search query value. s, search query size 每个搜索查询值的大小。

b bytes b 字节

h halfwords (two bytes) h 半字(两个字节)

w words (four bytes) w 字(四个字节)

g giant words (eight bytes) g 巨字(八字节)

All values are interpreted in the current language.所有值都以当前语言解释。 This means, for example, that if the current source language is C/C++ then searching for the string “hello” includes the trailing '\0'.这意味着,例如,如果当前源语言是 C/C++,那么搜索字符串“hello”包括结尾的 '\0'。

If the value size is not specified, it is taken from the value's type in the current language.如果未指定值大小,则取自当前语言中的值类型。 This is useful when one wants to specify the search pattern as a mixture of types.当想要将搜索模式指定为混合类型时,这很有用。 Note that this means, for example, that in the case of C-like languages a search for an untyped 0x42 will search for '(int) 0x42' which is typically four bytes.请注意,这意味着,例如,在类 C 语言的情况下,搜索无类型的 0x42 将搜索通常为四个字节的 '(int) 0x42'。

n, maximum number of finds The maximum number of matches to print. n, maximum number of finds 要打印的最大匹配数。 The default is to print all finds.默认是打印所有发现。

You can use strings as search values.您可以使用字符串作为搜索值。 Quote them with double-quotes ("). The string value is copied into the search pattern byte by byte, regardless of the endianness of the target and the size specification.用双引号 (") 引用它们。字符串值被逐字节复制到搜索模式中,而不管目标的字节顺序和大小规范。

The address of each match found is printed as well as a count of the number of matches found.打印找到的每个匹配的地址以及找到的匹配数的计数。

The address of the last value found is stored in convenience variable '$_'.找到的最后一个值的地址存储在便利变量“$_”中。 A count of the number of matches is stored in '$numfound'.匹配数的计数存储在“$numfound”中。

Take this example:举个例子:
1.find the string "Can not open script" . 1.找到字符串"Can not open script"
2. gdb>info proc map 2. gdb>info proc map

process 8636
Mapped address spaces:

    Start Addr   End Addr       Size     Offset objfile
     0x8048000  0x8898000   0x850000        0x0 /home/lela/ask/mLinux32
     0x8898000  0x8902000    0x6a000   0x850000 /home/lela/ask/mLinux32
     0x8902000  0x8d4c000   0x44a000        0x0 [heap]
    0xf6800000 0xf6821000    0x21000        0x0 
    0xf6821000 0xf6900000    0xdf000        0x0 
    0xf6a00000 0xf6a21000    0x21000        0x0 
    0xf6a21000 0xf6b00000    0xdf000        0x0 
    0xf6b00000 0xf6b21000    0x21000        0x0 
    0xf6b21000 0xf6c00000    0xdf000        0x0 
    0xf6cbf000 0xf6cc0000     0x1000        0x0 
    0xf6cc0000 0xf6d00000    0x40000        0x0 
    0xf6d00000 0xf6d21000    0x21000        0x0 
    0xf6d21000 0xf6e00000    0xdf000        0x0 
    0xf6e06000 0xf6e07000     0x1000        0x0 
    0xf6e07000 0xf6e47000    0x40000        0x0 
    0xf6e47000 0xf6e48000     0x1000        0x0 
    0xf6e48000 0xf6e88000    0x40000        0x0 
    0xf6e88000 0xf6e89000     0x1000        0x0 
    0xf6e89000 0xf794f000   0xac6000        0x0 
    0xf794f000 0xf7a4f000   0x100000        0x0 
    0xf7a4f000 0xf7c15000   0x1c6000        0x0 
    0xf7c15000 0xf7c17000     0x2000        0x0 /usr/lib/i386-linux-gnu/libgcc_s.so.1
    0xf7c17000 0xf7c2d000    0x16000     0x2000 /usr/lib/i386-linux-gnu/libgcc_s.so.1
    0xf7c2d000 0xf7c32000     0x5000    0x18000 /usr/lib/i386-linux-gnu/libgcc_s.so.1
    0xf7c32000 0xf7c33000     0x1000    0x1c000 /usr/lib/i386-linux-gnu/libgcc_s.so.1

like this result.喜欢这个结果。

3.use find command with the start address and end address,and string with string type length ex: {char[19]} ,likes below. 3.使用find命令与开始地址和结束地址,以及字符串类型长度的字符串ex: {char[19]} ,如下所示。

gdb➤  find  0x8048000, 0x8902000, {char[19]}"Can not open script"
0x8611234
1 pattern found.
gdb➤  x/s 0x8611234
0x8611234:  "Can not open script file \"%s\" to execute.\n"

4.finish. 4.完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM