我有一个任务,在那里,我需要登录到一个简单的程序使用缓冲区溢出。该程序将带有get()和strcpy()的两个arg包含在v.username和v.password中。
struct {
int32_t goodcanary;
char password[25];
int32_t canary;
char good_username[25];
char good_password[25];
char username[25];
} v;金丝雀是我所知道的一个10位数。由于一个int32_t只有4个字节,所以我将这个数字转换为ascii,并希望溢出密码,即input2,而且由于金丝雀在内存中的密码之后,我想我可以在它溢出后将它放在input2的末尾。我不能让它工作,我知道结构有填充,但我不知道它到底是如何工作的,填充在哪里。以下是结构的每个成员的内存位置:

我怎样才能看到垫子在哪里,我如何知道把金丝雀的价值放在哪里?
谢谢。
发布于 2022-06-08 06:25:11
这些规则基本上如下:
在具有对齐需求的系统上,必须始终从偶数地址开始分配
可以使用offsetof (stddef.h)打印任何结构成员的字节位置。示例:
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
typedef struct
{
int32_t goodcanary;
char password[25];
int32_t canary;
char good_username[25];
char good_password[25];
char username[25];
} v;
#define print_offset(x) printf("Offset %s: %zu\n", #x, offsetof(v,x))
int main (void)
{
printf("Size of struct: %zu\n", sizeof(v));
print_offset(goodcanary);
print_offset(password);
print_offset(canary);
print_offset(good_username);
print_offset(good_password);
print_offset(username);
return 0;
}输出(x86_64计算机):
Size of struct: 112
Offset goodcanary: 0
Offset password: 4
Offset canary: 32
Offset good_username: 36
Offset good_password: 61
Offset username: 86由于canary从32开始,必须有32-25-4=3个填充字节插入.而且它们肯定是在password之后插入的,因为goodcanary是4字节对齐的,没有后缀填充的必要。
或者如果你愿意:
offsetof(v, canary) - sizeof((v){0}.password) - offsetof(v,password)https://stackoverflow.com/questions/72540798
复制相似问题