在知道内存偏移量和字符串长度的文件中查找十六进制字符串

发布于 2024-12-01 16:24:26 字数 1230 浏览 0 评论 0原文

早上好,堆栈溢出!

我正在尝试从二进制文件中提取十六进制字符串。我无法直接找到这个字符串,但我知道该字符串的长度为 0x30,并且在已知的十六进制字符串之后从 0x10 开始。

这是我的代码

 FILE *f = NULL;
    unsigned char *buffer = NULL;
    unsigned long fileLen;

    unsigned char known_string[] = {0x45, 0x45, 0x45, 0x45};


printf("Opening file...\n");  
    f = fopen(argv[1], "rb");

    if (!f) {
        fprintf(stderr, "Unable to open %s\n", argv[1]);
        return -1;
    }

// Get the length
    fseek(f, 0, SEEK_END);
    fileLen=ftell(f);

    fseek(f, 0, SEEK_SET);

// Allocate memory
    buffer=malloc(fileLen);

    if (!buffer)
    {
        fprintf(stderr, "Memory error!\n");
        fclose(f);
        return -1;
    }

// File to buffer

fread(buffer, fileLen, 1, f);
    fclose(f);

printf("Buffer starts: %p\n", &buffer[0]);
printf("Buffer ends: %p\n", &buffer[fileLen]);

// Determines offset of known_string

char *p = memmem(buffer, fileLen, bytes, 4);
if (!p) {
    return -1;
} else {

  printf(" General offset: %x\n", p);    
}

  free(buffer);

所以我得到了已知字符串的一般偏移量,但我需要获取相对于文件的偏移量。我在这一步有点卡住了。我想我必须做类似 p - &buffer[0] 的事情,但是 p 和 &buffer[0] 不是同一类型,而且 p 甚至不是真正的偏移量(例如 678987 而不是 10678987)。那么在我得到相对偏移量的情况下,我怎样才能找到未知的字符串呢?

Good morning stackoverflow !

I'm trying to extract from a binary file a hexadecimal string. I can't find this string directly, but I know that the string is 0x30 long and begins 0x10 after a known hex string.

So here is my code

 FILE *f = NULL;
    unsigned char *buffer = NULL;
    unsigned long fileLen;

    unsigned char known_string[] = {0x45, 0x45, 0x45, 0x45};


printf("Opening file...\n");  
    f = fopen(argv[1], "rb");

    if (!f) {
        fprintf(stderr, "Unable to open %s\n", argv[1]);
        return -1;
    }

// Get the length
    fseek(f, 0, SEEK_END);
    fileLen=ftell(f);

    fseek(f, 0, SEEK_SET);

// Allocate memory
    buffer=malloc(fileLen);

    if (!buffer)
    {
        fprintf(stderr, "Memory error!\n");
        fclose(f);
        return -1;
    }

// File to buffer

fread(buffer, fileLen, 1, f);
    fclose(f);

printf("Buffer starts: %p\n", &buffer[0]);
printf("Buffer ends: %p\n", &buffer[fileLen]);

// Determines offset of known_string

char *p = memmem(buffer, fileLen, bytes, 4);
if (!p) {
    return -1;
} else {

  printf(" General offset: %x\n", p);    
}

  free(buffer);

So I get the general offset of the known string but I need to get the one relative to the file. I'm a bit stuck at this step. I think I must do something like p - &buffer[0], but p and &buffer[0] are not of the same type, and p is not even the real offset (678987 instead of 10678987 e.g). Then in the case I got the relative offset, how could I find the unknown string ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-12-08 16:24:26

由于您只对指针地址感兴趣,而不是它指向的数据,因此您也可以使用 void *,并且不必执行 &buffer[0],它不是数组。

unsigned long off_to_string = 0x10 + 4 + ((void *)p) - ((void *)buffer);

我添加 0x10 + 4 以跳过找到的十六进制字节序列和搜索字符串之前的字节。

As you're only interested in the pointer address, not the data it points to, you may just as well use void *, and you don't have to do &buffer[0], it's not an array.

unsigned long off_to_string = 0x10 + 4 + ((void *)p) - ((void *)buffer);

I add 0x10 + 4 to skip the found hex byte sequence and the bytes up to the searched for string.

冰雪之触 2024-12-08 16:24:26

您需要做的就是使用 unsigned char * 作为 p 的类型 - 然后您只需减去:

unsigned char *p = memmem(buffer, fileLen, bytes, 4);

if (!p) {
    return -1;
} else {
  printf(" General offset: %p\n", p);    
  printf(" Offset within file: %llx\n", (unsigned long long)(p - buffer));
}

All you need to do is use unsigned char * for the type of p - then you can just subtract:

unsigned char *p = memmem(buffer, fileLen, bytes, 4);

if (!p) {
    return -1;
} else {
  printf(" General offset: %p\n", p);    
  printf(" Offset within file: %llx\n", (unsigned long long)(p - buffer));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文