How to retrieve the file that is outside of current directory using format specifier?
up vote
-2
down vote
favorite
char * read_file(char * filename) {
char * file_contents = malloc(4096 * sizeof(char));
FILE * file;
file = fopen(filename, "r");
fread(file_contents, 4096, sizeof(char), file);
fclose(file);
return file_contents;
}
char * read_flag() {
return read_file("/flag.txt"); // outside of current working directory ;)
}
int main(int argc, char* argv) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
char * flag = read_flag();
char input_filename[40];
//Current directory is /home/problem
printf("Current working directory is: ");
system("pwd");
printf("Enter a filename to print the contents of the file => ");
scanf("%39s", input_filename);
while ((directory_entry = readdir(directory)) != NULL) {
if (strcmp(input_filename, directory_entry->d_name) == 0) {
printf("File contents:n");
printf("%sn", read_file(input_filename));
return 0;
}
}
}
I need to open a file that is outside of this directory ("/flag.txt"). I have tried something like "../" in the input to get out from this directory but it is not working. I am not sure how do i enter the filename such that it can retrieve the file that is outside of the /home/problem directory. I am currently using Ubuntu to do this. I think the idea should be using something like %s%d when i enter my input. Is this possible to use any specifier or exploit this program in order to read the entire contents?
c file code-injection format-specifiers
add a comment |
up vote
-2
down vote
favorite
char * read_file(char * filename) {
char * file_contents = malloc(4096 * sizeof(char));
FILE * file;
file = fopen(filename, "r");
fread(file_contents, 4096, sizeof(char), file);
fclose(file);
return file_contents;
}
char * read_flag() {
return read_file("/flag.txt"); // outside of current working directory ;)
}
int main(int argc, char* argv) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
char * flag = read_flag();
char input_filename[40];
//Current directory is /home/problem
printf("Current working directory is: ");
system("pwd");
printf("Enter a filename to print the contents of the file => ");
scanf("%39s", input_filename);
while ((directory_entry = readdir(directory)) != NULL) {
if (strcmp(input_filename, directory_entry->d_name) == 0) {
printf("File contents:n");
printf("%sn", read_file(input_filename));
return 0;
}
}
}
I need to open a file that is outside of this directory ("/flag.txt"). I have tried something like "../" in the input to get out from this directory but it is not working. I am not sure how do i enter the filename such that it can retrieve the file that is outside of the /home/problem directory. I am currently using Ubuntu to do this. I think the idea should be using something like %s%d when i enter my input. Is this possible to use any specifier or exploit this program in order to read the entire contents?
c file code-injection format-specifiers
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
You know what./means? you know what it refers to ? BTW: in your progam fragmentdirectoryis never defined nor initialized.
– joop
Nov 19 at 12:57
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
char * read_file(char * filename) {
char * file_contents = malloc(4096 * sizeof(char));
FILE * file;
file = fopen(filename, "r");
fread(file_contents, 4096, sizeof(char), file);
fclose(file);
return file_contents;
}
char * read_flag() {
return read_file("/flag.txt"); // outside of current working directory ;)
}
int main(int argc, char* argv) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
char * flag = read_flag();
char input_filename[40];
//Current directory is /home/problem
printf("Current working directory is: ");
system("pwd");
printf("Enter a filename to print the contents of the file => ");
scanf("%39s", input_filename);
while ((directory_entry = readdir(directory)) != NULL) {
if (strcmp(input_filename, directory_entry->d_name) == 0) {
printf("File contents:n");
printf("%sn", read_file(input_filename));
return 0;
}
}
}
I need to open a file that is outside of this directory ("/flag.txt"). I have tried something like "../" in the input to get out from this directory but it is not working. I am not sure how do i enter the filename such that it can retrieve the file that is outside of the /home/problem directory. I am currently using Ubuntu to do this. I think the idea should be using something like %s%d when i enter my input. Is this possible to use any specifier or exploit this program in order to read the entire contents?
c file code-injection format-specifiers
char * read_file(char * filename) {
char * file_contents = malloc(4096 * sizeof(char));
FILE * file;
file = fopen(filename, "r");
fread(file_contents, 4096, sizeof(char), file);
fclose(file);
return file_contents;
}
char * read_flag() {
return read_file("/flag.txt"); // outside of current working directory ;)
}
int main(int argc, char* argv) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
char * flag = read_flag();
char input_filename[40];
//Current directory is /home/problem
printf("Current working directory is: ");
system("pwd");
printf("Enter a filename to print the contents of the file => ");
scanf("%39s", input_filename);
while ((directory_entry = readdir(directory)) != NULL) {
if (strcmp(input_filename, directory_entry->d_name) == 0) {
printf("File contents:n");
printf("%sn", read_file(input_filename));
return 0;
}
}
}
I need to open a file that is outside of this directory ("/flag.txt"). I have tried something like "../" in the input to get out from this directory but it is not working. I am not sure how do i enter the filename such that it can retrieve the file that is outside of the /home/problem directory. I am currently using Ubuntu to do this. I think the idea should be using something like %s%d when i enter my input. Is this possible to use any specifier or exploit this program in order to read the entire contents?
c file code-injection format-specifiers
c file code-injection format-specifiers
edited Nov 19 at 15:44
mrflash818
6361018
6361018
asked Nov 19 at 8:24
Y.M
277
277
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
You know what./means? you know what it refers to ? BTW: in your progam fragmentdirectoryis never defined nor initialized.
– joop
Nov 19 at 12:57
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31
add a comment |
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
You know what./means? you know what it refers to ? BTW: in your progam fragmentdirectoryis never defined nor initialized.
– joop
Nov 19 at 12:57
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
You know what
./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.– joop
Nov 19 at 12:57
You know what
./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.– joop
Nov 19 at 12:57
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You need to pass the full path to your file if it is outside the solution directory either with \ or one /. On a windows based system this would be for example C:\folder\file.txt. I do not use linux currently, but it should be /home/folder/file.txt.
For an absolute pathname, add a leading slash:/home/folder/file.txt.
– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part./means "in the current directory". So when you say it is outside/home/problem, and the name is./file.txt, noone can know where that file is. Find out which directoryflag.txtis in. Say e.g. it is the folder/somwhere/else/, then you just open/somwhere/else/flag.txt
– nos
Nov 19 at 13:30
add a comment |
up vote
0
down vote
The fopen function can fail, and you should handle that. Read fopen(3), open(2), path_resolution(7), errno(3) to understand the possible failure reasons. Details could be file system and computer specific (and could include hardware failures).
I recommend using perror(3) and exit(3) on failure (don't forget to include both <stdio.h> for perror and <stdlib.h> for exit):
FILE* file = fopen(filename, "r");
if (!file) {
perror(filename);
exit(EXIT_FAILURE);
}
then you'll get a meaningful error message (into stderr) on failure
My guess: your root file system (and root directory / ...) don't have a flag.txt file and you might want to retrieve what your shell understands from ~/flag.txt. Perhaps you want to retrieve it in your home directory (then build its file path, using getenv("HOME") on Linux or Unix; see this).
Read also about globbing, and glob(7).
Read also some Linux programming book, perhaps the old ALP.
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any/flag.txt(because the root directory/is not yours, and hier(7) don't document that it should have aflag.txtfile)
– Basile Starynkevitch
Nov 19 at 16:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You need to pass the full path to your file if it is outside the solution directory either with \ or one /. On a windows based system this would be for example C:\folder\file.txt. I do not use linux currently, but it should be /home/folder/file.txt.
For an absolute pathname, add a leading slash:/home/folder/file.txt.
– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part./means "in the current directory". So when you say it is outside/home/problem, and the name is./file.txt, noone can know where that file is. Find out which directoryflag.txtis in. Say e.g. it is the folder/somwhere/else/, then you just open/somwhere/else/flag.txt
– nos
Nov 19 at 13:30
add a comment |
up vote
1
down vote
You need to pass the full path to your file if it is outside the solution directory either with \ or one /. On a windows based system this would be for example C:\folder\file.txt. I do not use linux currently, but it should be /home/folder/file.txt.
For an absolute pathname, add a leading slash:/home/folder/file.txt.
– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part./means "in the current directory". So when you say it is outside/home/problem, and the name is./file.txt, noone can know where that file is. Find out which directoryflag.txtis in. Say e.g. it is the folder/somwhere/else/, then you just open/somwhere/else/flag.txt
– nos
Nov 19 at 13:30
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to pass the full path to your file if it is outside the solution directory either with \ or one /. On a windows based system this would be for example C:\folder\file.txt. I do not use linux currently, but it should be /home/folder/file.txt.
You need to pass the full path to your file if it is outside the solution directory either with \ or one /. On a windows based system this would be for example C:\folder\file.txt. I do not use linux currently, but it should be /home/folder/file.txt.
edited Nov 19 at 10:55
answered Nov 19 at 9:45
ats
694
694
For an absolute pathname, add a leading slash:/home/folder/file.txt.
– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part./means "in the current directory". So when you say it is outside/home/problem, and the name is./file.txt, noone can know where that file is. Find out which directoryflag.txtis in. Say e.g. it is the folder/somwhere/else/, then you just open/somwhere/else/flag.txt
– nos
Nov 19 at 13:30
add a comment |
For an absolute pathname, add a leading slash:/home/folder/file.txt.
– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part./means "in the current directory". So when you say it is outside/home/problem, and the name is./file.txt, noone can know where that file is. Find out which directoryflag.txtis in. Say e.g. it is the folder/somwhere/else/, then you just open/somwhere/else/flag.txt
– nos
Nov 19 at 13:30
For an absolute pathname, add a leading slash:
/home/folder/file.txt.– joop
Nov 19 at 10:53
For an absolute pathname, add a leading slash:
/home/folder/file.txt.– joop
Nov 19 at 10:53
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@joop The current directory when i execute the code is /home/problem. However the file that i want to access is outside of this directory which is ./flag.txt
– Y.M
Nov 19 at 12:48
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part
./ means "in the current directory". So when you say it is outside /home/problem, and the name is ./file.txt , noone can know where that file is. Find out which directory flag.txt is in. Say e.g. it is the folder /somwhere/else/ , then you just open /somwhere/else/flag.txt– nos
Nov 19 at 13:30
@Y.M. You can't refer to ./flag.txt without knowing the current directory, since the first part
./ means "in the current directory". So when you say it is outside /home/problem, and the name is ./file.txt , noone can know where that file is. Find out which directory flag.txt is in. Say e.g. it is the folder /somwhere/else/ , then you just open /somwhere/else/flag.txt– nos
Nov 19 at 13:30
add a comment |
up vote
0
down vote
The fopen function can fail, and you should handle that. Read fopen(3), open(2), path_resolution(7), errno(3) to understand the possible failure reasons. Details could be file system and computer specific (and could include hardware failures).
I recommend using perror(3) and exit(3) on failure (don't forget to include both <stdio.h> for perror and <stdlib.h> for exit):
FILE* file = fopen(filename, "r");
if (!file) {
perror(filename);
exit(EXIT_FAILURE);
}
then you'll get a meaningful error message (into stderr) on failure
My guess: your root file system (and root directory / ...) don't have a flag.txt file and you might want to retrieve what your shell understands from ~/flag.txt. Perhaps you want to retrieve it in your home directory (then build its file path, using getenv("HOME") on Linux or Unix; see this).
Read also about globbing, and glob(7).
Read also some Linux programming book, perhaps the old ALP.
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any/flag.txt(because the root directory/is not yours, and hier(7) don't document that it should have aflag.txtfile)
– Basile Starynkevitch
Nov 19 at 16:52
add a comment |
up vote
0
down vote
The fopen function can fail, and you should handle that. Read fopen(3), open(2), path_resolution(7), errno(3) to understand the possible failure reasons. Details could be file system and computer specific (and could include hardware failures).
I recommend using perror(3) and exit(3) on failure (don't forget to include both <stdio.h> for perror and <stdlib.h> for exit):
FILE* file = fopen(filename, "r");
if (!file) {
perror(filename);
exit(EXIT_FAILURE);
}
then you'll get a meaningful error message (into stderr) on failure
My guess: your root file system (and root directory / ...) don't have a flag.txt file and you might want to retrieve what your shell understands from ~/flag.txt. Perhaps you want to retrieve it in your home directory (then build its file path, using getenv("HOME") on Linux or Unix; see this).
Read also about globbing, and glob(7).
Read also some Linux programming book, perhaps the old ALP.
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any/flag.txt(because the root directory/is not yours, and hier(7) don't document that it should have aflag.txtfile)
– Basile Starynkevitch
Nov 19 at 16:52
add a comment |
up vote
0
down vote
up vote
0
down vote
The fopen function can fail, and you should handle that. Read fopen(3), open(2), path_resolution(7), errno(3) to understand the possible failure reasons. Details could be file system and computer specific (and could include hardware failures).
I recommend using perror(3) and exit(3) on failure (don't forget to include both <stdio.h> for perror and <stdlib.h> for exit):
FILE* file = fopen(filename, "r");
if (!file) {
perror(filename);
exit(EXIT_FAILURE);
}
then you'll get a meaningful error message (into stderr) on failure
My guess: your root file system (and root directory / ...) don't have a flag.txt file and you might want to retrieve what your shell understands from ~/flag.txt. Perhaps you want to retrieve it in your home directory (then build its file path, using getenv("HOME") on Linux or Unix; see this).
Read also about globbing, and glob(7).
Read also some Linux programming book, perhaps the old ALP.
The fopen function can fail, and you should handle that. Read fopen(3), open(2), path_resolution(7), errno(3) to understand the possible failure reasons. Details could be file system and computer specific (and could include hardware failures).
I recommend using perror(3) and exit(3) on failure (don't forget to include both <stdio.h> for perror and <stdlib.h> for exit):
FILE* file = fopen(filename, "r");
if (!file) {
perror(filename);
exit(EXIT_FAILURE);
}
then you'll get a meaningful error message (into stderr) on failure
My guess: your root file system (and root directory / ...) don't have a flag.txt file and you might want to retrieve what your shell understands from ~/flag.txt. Perhaps you want to retrieve it in your home directory (then build its file path, using getenv("HOME") on Linux or Unix; see this).
Read also about globbing, and glob(7).
Read also some Linux programming book, perhaps the old ALP.
edited Nov 19 at 16:59
answered Nov 19 at 14:32
Basile Starynkevitch
174k13164357
174k13164357
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any/flag.txt(because the root directory/is not yours, and hier(7) don't document that it should have aflag.txtfile)
– Basile Starynkevitch
Nov 19 at 16:52
add a comment |
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any/flag.txt(because the root directory/is not yours, and hier(7) don't document that it should have aflag.txtfile)
– Basile Starynkevitch
Nov 19 at 16:52
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
Why the fopen function will fail?
– Y.M
Nov 19 at 15:45
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any
/flag.txt (because the root directory / is not yours, and hier(7) don't document that it should have a flag.txt file)– Basile Starynkevitch
Nov 19 at 16:52
There are many failure reasons. Read the various links. And since the failure is not depending upon just your program, but upon the whole system state of your entire computer, you always should handle it. You cannot reasonably predict the state of the computer running your program. I'll guess that your Linux computer don't have any
/flag.txt (because the root directory / is not yours, and hier(7) don't document that it should have a flag.txt file)– Basile Starynkevitch
Nov 19 at 16:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370781%2fhow-to-retrieve-the-file-that-is-outside-of-current-directory-using-format-speci%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where does SQL come into play here?!
– Corion
Nov 19 at 8:29
You know what
./means? you know what it refers to ? BTW: in your progam fragmentdirectoryis never defined nor initialized.– joop
Nov 19 at 12:57
@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
Nov 19 at 14:31