English 中文(简体)
格式化字符串脆弱性问题
原标题:Format String Vulnerability troubles

所以我有这个功能:

void print_usage(char* arg) 
{
    char buffer[640];
sprintf(buffer, "Usage: %s [options]
"
        "Randomly generates a password, optionally writes it to /etc/shadow
"
        "
"
        "Options:
"
        "-s, --salt <salt>  Specify custom salt, default is random
"
        "-e, --seed [file]  Specify custom seed from file, default is from stdin
"
        "-t, --type <type>  Specify different encryption method
"
        "-v, --version      Show version
"
        "-h, --help     Show this usage message
"
        "
"
        "Encryption types:
"
        "   0 - DES (default)
"
        "   1 - MD5
"
        "   2 - Blowfish
"
        "   3 - SHA-256
"
        "   4 - SHA-512
", arg);
    printf(buffer);
}

我希望使用一种格式的字符串脆弱性攻击(我的任务)。

我有一个开发程序, 向缓冲器填充节点和贝壳代码( 我用这个程序来缓冲溢出相同的函数, 所以我知道它的好处 ) 。 现在, 我做了一个文件的物件倾弃, 找到了. dtors_ list 地址, 我得到了 0x0804a20c, 增加了 4 字节, 以获得 0x804a210 的结尾 。

接下来,我用 gdb 来查找我运行程序时开始的点名地址。 使用这个, 我得到了 0xffbfdb8 。

因此,直到现在,我觉得我是正确的,现在我知道我想用格式字符串 将Nop 地址复制到我的.dtors_end 地址。这是我想到的字符串(这就是我作为用户输入而提供的字符串):

“X10x2x04x08x11xx2x204x208x208x08x13x204x08x08}168u}1美元 $51u=2美元

这对我来说行不通。程序正常运行, %s 被替换为字符串 I 输入(减去前端的小内存地址,2%的符号现在因某种原因是1%的符号)。

总之,我在这里有点失落, 任何帮助都会感激不尽的。

问题回答

免责声明:我不是专家。

您重新通过 "x10xxxx2x4x04x08x12x2x204x08x12x208x13x2x404x08_18_168u1$n51u2$n228u_3$n. 64u}4$n" 的数值是 arg ? 这意味着 buffer 将包含

"Usage: x20x10xxx2x2x04x11xx204x08x12x208xx208xx208x13xx2x04x08%168u%1$%51u%2$n%2.28u%3$n%.64u%4$n[options]x0arandomly...

现在让我们进一步假设您在 x86-32 目标上返回( 如果您在 x86- 64 目标上返回, 这将不起作用 ), 而且您正在以一个优化水平进行重新编译, 该优化水平不会在 < code> print_ usage s 堆叠框中放置任何内容, 除了 640 byte < code>buffer 阵列之外 。

然后 printf(buffer) 将做下列事情,以便:

  • Push the 4-byte address &buffer.
  • Push a 4-byte return address.
  • Invoke printf...
  • Print out "Usage:x20x10xa2x04x08x11xa2x04x08x12xa2x04x08x13xa2x04x08" (a sequence of 23 bytes).
  • %.168u: Interpret the next argument to printf as an unsigned int and print it in a field of width 168. Since printf has no next argument, this is actually going to print the next thing on the stack; that is, the first four bytes of buffer; that is, "Usag" (0x67617355).
  • %1$n: Interpret the second argument to printf as a pointer to int and store 23+168 at that location. This stores 0x000000bf in location 0x67617355. So this is your main problem: You should have used %2$n instead of %1$n and added one junk byte to the front of your arg. (Incidentally, notice that GNU says "If any of the formats has a specification for the parameter position all of them in the format string shall have one. Otherwise the behavior is undefined." So you should go through and add 1$s to all your %us just to be on the safe side.)
  • %.51u: Print another 51 bytes of garbage.
  • %2$n: Interpret the third argument to printf as a pointer to int and store 0x000000f2 in that garbage location. As above, this should have been %3$n.
  • ... etc. etc. ...

因此,您在这里的主要错误是您忘记给 < code> “ Usage : 前缀记账 。

我猜你试图将四字节 0x804a210 存储到地址 0x804a210 。 可以说你做到了。 但是下一步会是什么? 您如何让程序将四字节数量 0x804a210 处理为函数指针并跳过它?

使用此代码的传统方式是利用 sprintf 中的缓冲溢出, 而不是 %n> 中的较复杂的 %n> 脆弱性。 您只需要将您的 printf 字符设置为大约 640 个字符, 并确保其中与 print_usage s 返回地址相对应的4字节包含您的 NOP 滑板的地址 。

即使 部分也是棘手的。 您也许可以想象自己遇到了与 < a href="有关的东西 http:// en.wikipedia. org/wiki/Address_space_layout_randomination" rel="nofollow" >ASLR : 您的滑雪板存在于一个运行中 < code> 0xffbfdbb8 的地址, 并不意味着它会在下一个运行中的同一地址存在 。

这有帮助吗?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...