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?










share|improve this question
























  • 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










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    Nov 19 at 14:31















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?










share|improve this question
























  • 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










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    Nov 19 at 14:31













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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










  • @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












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.






share|improve this answer























  • 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 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




















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.






share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • 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 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

















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.






share|improve this answer























  • 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 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















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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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




















  • 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 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


















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














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.






share|improve this answer























  • 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

















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.






share|improve this answer























  • 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















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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 a flag.txt file)
    – Basile Starynkevitch
    Nov 19 at 16:52




















  • 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


















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno