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
    2 days ago










  • You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
    – joop
    2 days ago










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    2 days ago















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
    2 days ago










  • You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
    – joop
    2 days ago










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    2 days ago













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 2 days ago









mrflash818

6301018




6301018










asked 2 days ago









Y.M

277




277












  • Where does SQL come into play here?!
    – Corion
    2 days ago










  • You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
    – joop
    2 days ago










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    2 days ago


















  • Where does SQL come into play here?!
    – Corion
    2 days ago










  • You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
    – joop
    2 days ago










  • @joop sorry i typo. Supposed to be /flag.txt without the "."
    – Y.M
    2 days ago
















Where does SQL come into play here?!
– Corion
2 days ago




Where does SQL come into play here?!
– Corion
2 days ago












You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
– joop
2 days ago




You know what ./ means? you know what it refers to ? BTW: in your progam fragment directory is never defined nor initialized.
– joop
2 days ago












@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
2 days ago




@joop sorry i typo. Supposed to be /flag.txt without the "."
– Y.M
2 days ago












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
    2 days ago










  • @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
    2 days ago










  • @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
    2 days ago




















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
    2 days ago










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













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
    2 days ago










  • @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
    2 days ago










  • @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
    2 days ago

















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
    2 days ago










  • @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
    2 days ago










  • @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
    2 days ago















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 2 days ago

























answered 2 days ago









ats

594




594












  • For an absolute pathname, add a leading slash: /home/folder/file.txt.
    – joop
    2 days ago










  • @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
    2 days ago










  • @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
    2 days ago




















  • For an absolute pathname, add a leading slash: /home/folder/file.txt.
    – joop
    2 days ago










  • @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
    2 days ago










  • @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
    2 days ago


















For an absolute pathname, add a leading slash: /home/folder/file.txt.
– joop
2 days ago




For an absolute pathname, add a leading slash: /home/folder/file.txt.
– joop
2 days ago












@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
2 days ago




@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
2 days ago












@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
2 days ago






@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
2 days ago














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
    2 days ago










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

















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
    2 days ago










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















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 yesterday

























answered 2 days ago









Basile Starynkevitch

174k13163357




174k13163357












  • Why the fopen function will fail?
    – Y.M
    2 days ago










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




















  • Why the fopen function will fail?
    – Y.M
    2 days ago










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


















Why the fopen function will fail?
– Y.M
2 days ago




Why the fopen function will fail?
– Y.M
2 days ago












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
yesterday






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
yesterday




















 

draft saved


draft discarded



















































 


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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith