繁体   English   中英

将字节作为命令行参数传递给程序

[英]Handing bytes as command line argument to program

我正在尝试利用缓冲区溢出漏洞。 为此,我想将先前确定的地址(例如\x00\x00\x41\xab\x00\xab )传递给程序,以便将其作为字节写入 memory,而不是字节表示形式输入字符串。 考虑这个例子:

Address I want to write as hex string: \x00\x00\x41\xab\x00\xab 

Memory before overflow: ab ab 00 00 aa 00  (these values could be arbitrary since its stack memory) 
Memory after overflow : 00 00 41 ab 00 ab

我试图写入的缓冲区是通过以下方式访问的:

void overflow(char input[], end){
    for(int a = 0; a < end; ++a){
        char buffer[32];
        buffer[a] = input[a];
}

end的值由input的长度决定。 溢出与我的问题无关。 我要问的是:

如何将任何表示形式(十六进制、字节串等)的地址作为命令行参数(通过argv[] )传递给程序,以便将其写入 memory,如上所示?

特别是,如何处理\x00字符?

我发现了这个问题,不幸的是没有提供解决方案。 请注意,我没有尝试使用 pipe 参数,因为程序不从标准输入读取。

我会将输入作为文本字符串,如“000041ab00ab”,然后编写一些简单的代码来进行转换。

就像是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char conv_char(char c)
{
        if (c >= '0' && c <= '9')
        {
            return c - '0';
        }
        
        if (c >= 'a' && c <= 'f')
        {
            return c - 'a' + 10;
        }
        
        exit(1);
}

void get_values(const char* str, unsigned char* values)
{
    if (strlen(str) != 12) exit(1);
    for (int i=0; i < 6; ++i)
    {
        values[i] = conv_char(str[2*i]);
        values[i] = values[i] << 4;
        values[i] = values[i] | conv_char(str[2*i + 1]);
    }
}

int main(int argc, char* argv[]) 
{
    if (argc != 2) exit(1);
    unsigned char values[6];
    get_values(argv[1], values);
    
    for (int i=0; i < 6; ++i)
    {
        printf("0x%02x ", values[i]);
    }    
    puts("");
    
    return 0;
}

prog 000041ab00ab运行它会得到 output:

0x00 0x00 0x41 0xab 0x00 0xab 

暂无
暂无

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

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